-
Notifications
You must be signed in to change notification settings - Fork 0
/
barrier.h
77 lines (69 loc) · 2.02 KB
/
barrier.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
* File: barrier.h
* Author: Vasileios Trigonakis <[email protected]>,
* Tudor David <[email protected]>
* Description:
* barrier.h is part of ASCYLIB
*
* Copyright (c) 2014 Vasileios Trigonakis <[email protected]>,
* Tudor David <[email protected]>
* Distributed Programming Lab (LPD), EPFL
*
* ASCYLIB is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, version 2
* of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#ifndef _BARRIER_H_
#define _BARRIER_H_
/* ################################################################### *
* BARRIER
* ################################################################### */
typedef struct barrier
{
pthread_cond_t complete;
pthread_mutex_t mutex;
int count;
int crossing;
} barrier_t;
static inline void
barrier_init(barrier_t *b, int n)
{
pthread_cond_init(&b->complete, NULL);
pthread_mutex_init(&b->mutex, NULL);
b->count = n;
b->crossing = 0;
}
static inline void
barrier_cross(barrier_t *b)
{
pthread_mutex_lock(&b->mutex);
/* One more thread through */
b->crossing++;
/* If not all here, wait */
if (b->crossing < b->count) {
pthread_cond_wait(&b->complete, &b->mutex);
} else {
pthread_cond_broadcast(&b->complete);
/* Reset for next time */
b->crossing = 0;
}
pthread_mutex_unlock(&b->mutex);
}
#define EXEC_IN_DEC_ID_ORDER(id, nthr) \
{ int __i; \
for (__i = nthr - 1; __i >= 0; __i--) \
{ \
if ((int) id == __i) \
{
#define EXEC_IN_DEC_ID_ORDER_END(barrier) \
} \
barrier_cross(barrier); \
}}
#endif /* _BARRIER_H_ */