vfplot  1.0.15
About: vfplot is a program for plotting two-dimensional vector fields (supporting Gerris) fluid flows simulation files).
  Fossies Dox: vfplot-1.0.15.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

pthreadextra.c
Go to the documentation of this file.
1 /*
2  This is taken from Aleksey Demakov [1] (with just cosmetic
3  changes) implementing the pthread barrier functionality
4  missing from OSX. The licence is BSD 2-Clause "Simplified"
5 
6  [1] https://github.com/ademakov/DarwinPthreadBarrier
7 */
8 
9 #include "pthreadextra.h"
10 
11 #include <errno.h>
12 
13 #ifdef __GNUC__
14 #define __unused __attribute__((unused))
15 #else
16 #define __unused
17 #endif
18 
20 {
21  return 0;
22 }
23 
25 {
26  return 0;
27 }
28 
30  int *restrict pshared)
31 {
32  *pshared = PTHREAD_PROCESS_PRIVATE;
33  return 0;
34 }
35 
37 {
38  if (pshared != PTHREAD_PROCESS_PRIVATE)
39  {
40  errno = EINVAL;
41  return -1;
42  }
43  return 0;
44 }
45 
47  const pthread_barrierattr_t *restrict attr __unused,
48  unsigned count)
49 {
50  if (count == 0)
51  {
52  errno = EINVAL;
53  return -1;
54  }
55 
56  if (pthread_mutex_init(&barrier->mutex, 0) < 0)
57  {
58  return -1;
59  }
60 
61  if (pthread_cond_init(&barrier->cond, 0) < 0)
62  {
63  int errno_save = errno;
64  pthread_mutex_destroy(&barrier->mutex);
65  errno = errno_save;
66  return -1;
67  }
68 
69  barrier->limit = count;
70  barrier->count = 0;
71  barrier->phase = 0;
72 
73  return 0;
74 }
75 
77 {
78  pthread_mutex_destroy(&barrier->mutex);
79  pthread_cond_destroy(&barrier->cond);
80  return 0;
81 }
82 
84 {
85  pthread_mutex_lock(&barrier->mutex);
86  barrier->count++;
87  if (barrier->count >= barrier->limit)
88  {
89  barrier->phase++;
90  barrier->count = 0;
91  pthread_cond_broadcast(&barrier->cond);
92  pthread_mutex_unlock(&barrier->mutex);
94  }
95  else
96  {
97  unsigned phase = barrier->phase;
98  do
99  pthread_cond_wait(&barrier->cond, &barrier->mutex);
100  while (phase == barrier->phase);
101  pthread_mutex_unlock(&barrier->mutex);
102  return 0;
103  }
104 }
PTHREAD_PROCESS_PRIVATE
#define PTHREAD_PROCESS_PRIVATE
Definition: pthreadextra.h:21
pthread_barrierattr_t
Definition: pthreadextra.h:28
pthread_barrierattr_destroy
int pthread_barrierattr_destroy(pthread_barrierattr_t *attr)
Definition: pthreadextra.c:24
pthread_barrier_init
int pthread_barrier_init(pthread_barrier_t *restrict barrier, const pthread_barrierattr_t *restrict attr, unsigned count)
Definition: pthreadextra.c:46
__unused
#define __unused
Definition: pthreadextra.c:16
pthread_barrierattr_setpshared
int pthread_barrierattr_setpshared(pthread_barrierattr_t *attr, int pshared)
Definition: pthreadextra.c:36
PTHREAD_BARRIER_SERIAL_THREAD
#define PTHREAD_BARRIER_SERIAL_THREAD
Definition: pthreadextra.h:17
pthread_barrier_t::count
unsigned int count
Definition: pthreadextra.h:36
pthread_barrier_wait
int pthread_barrier_wait(pthread_barrier_t *barrier)
Definition: pthreadextra.c:83
pthread_barrier_t::cond
pthread_cond_t cond
Definition: pthreadextra.h:34
pthread_barrier_t::limit
unsigned int limit
Definition: pthreadextra.h:35
pthread_barrier_destroy
int pthread_barrier_destroy(pthread_barrier_t *barrier)
Definition: pthreadextra.c:76
pthread_barrier_t::phase
unsigned int phase
Definition: pthreadextra.h:37
pthread_barrier_t::mutex
pthread_mutex_t mutex
Definition: pthreadextra.h:33
pthread_barrierattr_init
int pthread_barrierattr_init(pthread_barrierattr_t *attr)
Definition: pthreadextra.c:19
pthread_barrier_t
Definition: pthreadextra.h:32
pthreadextra.h
pthread_barrierattr_getpshared
int pthread_barrierattr_getpshared(const pthread_barrierattr_t *restrict attr, int *restrict pshared)
Definition: pthreadextra.c:29