forked from teamhimeh/simutrans
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimthread.cc
56 lines (47 loc) · 1.25 KB
/
simthread.cc
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
#include "simthread.h"
#if defined(_USE_POSIX_BARRIERS) || !defined(MULTI_THREAD)
// use native pthread barriers
// (and do not try to compile this file for non-multithread builds)
#else
// implement barriers using other pthread primitives
#include <errno.h>
int simthread_barrier_init(simthread_barrier_t *barrier, const simthread_barrierattr_t */*attr*/, unsigned int count)
{
if( count == 0 ) {
errno = EINVAL;
return -1;
}
if( pthread_mutex_init( &barrier->mutex, 0 ) < 0 ) {
return -1;
}
if( pthread_cond_init( &barrier->cond, 0 ) < 0 ) {
pthread_mutex_destroy( &barrier->mutex );
return -1;
}
barrier->tripCount = count;
barrier->count = 0;
return 0;
}
int simthread_barrier_destroy(simthread_barrier_t *barrier)
{
pthread_cond_destroy( &barrier->cond );
pthread_mutex_destroy( &barrier->mutex );
return 0;
}
int simthread_barrier_wait(simthread_barrier_t *barrier)
{
pthread_mutex_lock( &barrier->mutex );
barrier->count++;
if( barrier->count >= barrier->tripCount ) {
barrier->count = 0;
pthread_cond_broadcast( &barrier->cond );
pthread_mutex_unlock( &barrier->mutex );
return 1;
}
else {
pthread_cond_wait( &barrier->cond, &barrier->mutex );
pthread_mutex_unlock( &barrier->mutex );
return 0;
}
}
#endif