-
Notifications
You must be signed in to change notification settings - Fork 0
/
pthread_barrier.c
131 lines (104 loc) · 2.52 KB
/
pthread_barrier.c
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <stdio.h>
#include <errno.h>
#include <pthread.h>
#include "pthread_barrier.h"
int
pthread_barrier_destroy (pthread_barrier_t* bar)
{
int err, err2;
if (!bar) return EINVAL;
err = pthread_mutex_trylock (&bar->mtx);
if (err) return err;
if (bar->n_threads_left != bar->n_threads_required) {
err = EBUSY;
goto err_done;
}
if (!bar->valid) {
err = EINVAL;
goto err_done;
}
bar->valid = 0; /* It's officially dead. */
pthread_mutex_unlock (&bar->mtx);
err = pthread_mutex_destroy (&bar->mtx);
err2 = pthread_cond_destroy (&bar->cv);
if (err) return err;
return err2;
err_done:
pthread_mutex_unlock (&bar->mtx);
return err;
}
int
pthread_barrier_init (pthread_barrier_t* bar,
const pthread_barrierattr_t* attr,
unsigned nthr)
{
int err;
if (!bar) return EINVAL;
if (0 == nthr) return EINVAL;
err = pthread_mutex_init (&bar->mtx, NULL);
if (err) return err;
err = pthread_cond_init (&bar->cv, NULL);
if (err) {
pthread_mutex_destroy (&bar->mtx);
return err;
}
bar->n_threads_required = nthr;
bar->n_threads_left = nthr;
bar->cycle = 0;
bar->valid = 1;
return 0;
}
int
pthread_barrier_wait (pthread_barrier_t* bar_)
{
volatile pthread_barrier_t* bar = bar_;
int tmp;
int err = 0;
int cycle;
if (!bar || !bar->valid) return EINVAL;
err = pthread_mutex_lock (&bar_->mtx);
if (err) return err;
cycle = bar->cycle;
tmp = --bar->n_threads_left;
if (0 == tmp) {
bar->n_threads_left = bar->n_threads_required;
bar->cycle ^= 0x01;
err = pthread_cond_broadcast (&bar_->cv);
if (!err) err = PTHREAD_BARRIER_SERIAL_THREAD;
}
else {
while (cycle == bar->cycle) {
err = pthread_cond_wait (&bar_->cv, &bar_->mtx);
}
}
pthread_mutex_unlock (&bar_->mtx);
return err;
}
int
pthread_barrierattr_destroy (pthread_barrierattr_t* attr /*NOTUSED*/)
{
if (!attr) return EINVAL;
return 0;
}
int
pthread_barrierattr_getpshared (const pthread_barrierattr_t* attr /*NOTUSED*/,
int* pshared)
{
if (!attr) return EINVAL;
*pshared = PTHREAD_PROCESS_PRIVATE;
return 0;
}
int
pthread_barrierattr_init (pthread_barrierattr_t* attr)
{
if (!attr) return EINVAL;
return 1;
}
int
pthread_barrierattr_setpshared (pthread_barrierattr_t* attr, int pshared)
{
if (!attr) return EINVAL;
if (pshared != PTHREAD_PROCESS_PRIVATE)
return ENOTSUP; /* XXX: Non-standard. */
return 0;
}