-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.h
52 lines (43 loc) · 1.23 KB
/
utils.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
#include <pthread.h>
#include <stdint.h>
#define PTHREAD_BARRIER_SERIAL_THREAD 1
typedef struct pthread_barrier {
pthread_mutex_t mutex;
pthread_cond_t cond;
volatile uint32_t flag;
size_t count;
size_t num;
} pthread_barrier_t;
int pthread_barrier_init(pthread_barrier_t *bar, int num)
{
int ret = 0;
if ((ret = pthread_mutex_init(&(bar->mutex), 0))) return ret;
if ((ret = pthread_cond_init(&(bar->cond), 0))) return ret;
bar->flag = 0;
bar->count = 0;
bar->num = num;
return 0;
}
int pthread_barrier_wait(pthread_barrier_t *bar)
{
int ret = 0;
uint32_t flag = 0;
if ((ret = pthread_mutex_lock(&(bar->mutex)))) return ret;
flag = bar->flag;
bar->count++;
if (bar->count == bar->num) {
bar->count = 0;
bar->flag = 1 - bar->flag;
if ((ret = pthread_cond_broadcast(&(bar->cond)))) return ret;
if ((ret = pthread_mutex_unlock(&(bar->mutex)))) return ret;
return PTHREAD_BARRIER_SERIAL_THREAD;
}
while (1) {
if (bar->flag == flag) {
ret = pthread_cond_wait(&(bar->cond), &(bar->mutex));
if (ret) return ret;
} else { break; }
}
if ((ret = pthread_mutex_unlock(&(bar->mutex)))) return ret;
return 0;
}