Mercurial > hg > freeDiameter
comparison tests/testfifo.c @ 878:901526bc034c
Added simple implementation of barriers for Mac OS X
author | Sebastien Decugis <sdecugis@freediameter.net> |
---|---|
date | Sat, 03 Nov 2012 17:18:48 +0100 |
parents | a5a82d50c25e |
children | 2b14ccdd0b92 |
comparison
equal
deleted
inserted
replaced
877:e815a8cfca4b | 878:901526bc034c |
---|---|
35 | 35 |
36 #include "tests.h" | 36 #include "tests.h" |
37 #include <unistd.h> | 37 #include <unistd.h> |
38 #include <limits.h> | 38 #include <limits.h> |
39 | 39 |
40 /* Wrapper for pthread_barrier stuff on Mac OS X */ | |
41 #ifndef HAVE_PTHREAD_BAR | |
42 | |
43 #define PTHREAD_BARRIER_SERIAL_THREAD 1 | |
44 typedef struct { | |
45 int count; | |
46 int entered; | |
47 int serial; | |
48 pthread_mutex_t mutex; | |
49 pthread_cond_t cond; | |
50 } pthread_barrier_t; | |
51 | |
52 int pthread_barrier_init(pthread_barrier_t * barrier, int * barrier_attr, int count) | |
53 { | |
54 memset(barrier, 0, sizeof(pthread_barrier_t)); | |
55 barrier->count = count; | |
56 pthread_mutex_init(&barrier->mutex, NULL); | |
57 pthread_cond_init(&barrier->cond, NULL); | |
58 return 0; | |
59 } | |
60 | |
61 int pthread_barrier_destroy(pthread_barrier_t * barrier) | |
62 { | |
63 pthread_mutex_destroy(&barrier->mutex); | |
64 pthread_cond_destroy(&barrier->cond); | |
65 return 0; | |
66 } | |
67 | |
68 int pthread_barrier_wait(pthread_barrier_t * barrier) | |
69 { | |
70 int ret = 0; | |
71 int serial; | |
72 pthread_mutex_lock(&barrier->mutex); | |
73 serial = barrier->serial; | |
74 | |
75 /* first thread gets the special value */ | |
76 if (barrier->entered++ == 0) | |
77 ret = PTHREAD_BARRIER_SERIAL_THREAD; | |
78 | |
79 /* Count was achieved? */ | |
80 if (barrier->entered == barrier->count) { | |
81 /* Ok, increase serial, reset number of threads, and signal everyone */ | |
82 barrier->entered = 0; | |
83 barrier->serial++; | |
84 pthread_cond_broadcast(&barrier->cond); | |
85 } else { | |
86 do { | |
87 pthread_cond_wait(&barrier->cond, &barrier->mutex); | |
88 } while (barrier->serial == serial); | |
89 /* this protects against spurious wakes */ | |
90 } | |
91 pthread_mutex_unlock(&barrier->mutex); | |
92 return 0; | |
93 } | |
94 | |
95 #endif /* HAVE_PTHREAD_BAR */ | |
96 | |
40 /* Structure for testing threshold function */ | 97 /* Structure for testing threshold function */ |
41 static struct thrh_test { | 98 static struct thrh_test { |
42 struct fifo * queue; /* pointer to the queue */ | 99 struct fifo * queue; /* pointer to the queue */ |
43 int h_calls; /* number of calls of h_cb */ | 100 int h_calls; /* number of calls of h_cb */ |
44 int l_calls; /* number of calls of l_cb */ | 101 int l_calls; /* number of calls of l_cb */ |