forked from shaygalon/memcache-perf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbarrier.cc
More file actions
33 lines (29 loc) · 728 Bytes
/
barrier.cc
File metadata and controls
33 lines (29 loc) · 728 Bytes
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
#include <pthread.h>
#include "barrier.h"
int barrier_init(barrier_t *barrier,int needed)
{
barrier->needed = needed;
barrier->called = 0;
pthread_mutex_init(&barrier->mutex,NULL);
pthread_cond_init(&barrier->cond,NULL);
return 0;
}
int barrier_destroy(barrier_t *barrier)
{
pthread_mutex_destroy(&barrier->mutex);
pthread_cond_destroy(&barrier->cond);
return 0;
}
int barrier_wait(barrier_t *barrier)
{
pthread_mutex_lock(&barrier->mutex);
barrier->called++;
if (barrier->called == barrier->needed) {
barrier->called = 0;
pthread_cond_broadcast(&barrier->cond);
} else {
pthread_cond_wait(&barrier->cond,&barrier->mutex);
}
pthread_mutex_unlock(&barrier->mutex);
return 0;
}