Flowgrind
Advanced TCP traffic generator
fg_barrier.h File Reference

Missing pthread barrier implemenation for OS X. More...

#include <config.h>
#include <pthread.h>

Go to the source code of this file.

Data Structures

struct  pthread_barrier_t
 Object for barrier synchronization. More...
 

Typedefs

typedef int pthread_barrierattr_t
 Barrier attribute object. More...
 

Functions

int pthread_barrier_destroy (pthread_barrier_t *barrier)
 Destroys the barrier referenced by barrier. More...
 
int pthread_barrier_init (pthread_barrier_t *barrier, const pthread_barrierattr_t *attr, unsigned count)
 Allocates resources required to use the barrier referenced by barrier. More...
 
int pthread_barrier_wait (pthread_barrier_t *barrier)
 Synchronizes participating threads at the barrier referenced by barrier. More...
 

Detailed Description

Missing pthread barrier implemenation for OS X.

Definition in file fg_barrier.h.

Typedef Documentation

◆ pthread_barrierattr_t

typedef int pthread_barrierattr_t

Barrier attribute object.

Definition at line 37 of file fg_barrier.h.

Function Documentation

◆ pthread_barrier_destroy()

int pthread_barrier_destroy ( pthread_barrier_t barrier)

Destroys the barrier referenced by barrier.

It releases any resources used by the barrier. Subsequent use of the barrier is undefined until the barrier is reinitialized by pthread_barrier_init().

Parameters
[in]barrierbarrier that should be initialized
Returns
return 0 for success, or -1 for failure (in which case errno is set appropriately)

Definition at line 64 of file fg_barrier.c.

65 {
66  if (unlikely(!barrier)) {
67  errno = EINVAL;
68  return -1;
69  }
70 
71  if (pthread_cond_destroy(&barrier->cond) != 0)
72  return -1;
73 
74  if (pthread_mutex_destroy(&barrier->mutex) != 0)
75  return -1;
76 
77  return 0;
78 }

◆ pthread_barrier_init()

int pthread_barrier_init ( pthread_barrier_t barrier,
const pthread_barrierattr_t attr,
unsigned  count 
)

Allocates resources required to use the barrier referenced by barrier.

It initializes the barrier with attributes referenced by attr. If attr is NULL, the default barrier attributes are used.

Parameters
[in]barrierbarrier that should be initialized
[in]attrNULL, or the attributes attr that barrier should use
[in]countnumber of threads that must call pthread_barrier_wait() before any of them successfully returns. Value must be greater than zero
Returns
return 0 for success, or -1 for failure (in which case errno is set appropriately)

Definition at line 37 of file fg_barrier.c.

39 {
40  if (unlikely(!barrier) || unlikely(count == 0)) {
41  errno = EINVAL;
42  return -1;
43  }
44 
45  if (attr && (*attr != PTHREAD_PROCESS_PRIVATE)) {
46  errno = EINVAL;
47  return -1;
48  }
49 
50  if (pthread_mutex_init(&barrier->mutex, NULL) != 0)
51  return -1;
52 
53  if (pthread_cond_init(&barrier->cond, NULL) != 0) {
54  pthread_mutex_destroy(&barrier->mutex);
55  return -1;
56  }
57 
58  barrier->tripCount = count;
59  barrier->count = 0;
60 
61  return 0;
62 }

◆ pthread_barrier_wait()

int pthread_barrier_wait ( pthread_barrier_t barrier)

Synchronizes participating threads at the barrier referenced by barrier.

The calling thread blocks until the required number of threads have called pthread_barrier_wait(), specifying the barrier.

Parameters
[in]barrierbarrier that should be initialized
Returns
return 0 for success, or -1 for failure (in which case errno is set appropriately)

Definition at line 80 of file fg_barrier.c.

81 {
82  if (unlikely(!barrier)) {
83  errno = EINVAL;
84  return -1;
85  }
86 
87  if (pthread_mutex_lock(&barrier->mutex) !=0)
88  return -1;
89  ++(barrier->count);
90 
91  /* No remaining pthreads, broadcast to wake all waiting threads */
92  if (barrier->count >= barrier->tripCount) {
93  barrier->count = 0;
94  pthread_cond_broadcast(&barrier->cond);
95  } else {
96  pthread_cond_wait(&barrier->cond, &(barrier->mutex));
97  }
98 
99  pthread_mutex_unlock(&barrier->mutex);
100 
101  return 0;
102 }
pthread_barrier_t::tripCount
int tripCount
Current number of threads waiting at the barrier.
Definition: fg_barrier.h:48
pthread_barrier_t::cond
pthread_cond_t cond
Thread to suspend its execution until the condition is satisfied.
Definition: fg_barrier.h:44
unlikely
#define unlikely(x)
These macros gain us a few percent of speed.
Definition: fg_definitions.h:35
pthread_barrier_t::mutex
pthread_mutex_t mutex
Protect shared data structures from concurrent modifications.
Definition: fg_barrier.h:42
pthread_barrier_t::count
int count
Required number of threads have to wait at the barrier.
Definition: fg_barrier.h:46