Flowgrind
Advanced TCP traffic generator
fg_barrier.c
Go to the documentation of this file.
1 
6 /*
7  * Copyright (C) 2014 Alexander Zimmermann <alexander.zimmermann@netapp.com>
8  * Copyright (C) 2010 Brent Priddy <brent.priddy@priddysoftware.com>
9  *
10  * This file is part of Flowgrind.
11  *
12  * Flowgrind is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation, either version 3 of the License, or
15  * (at your option) any later version.
16  *
17  * Flowgrind is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with Flowgrind. If not, see <http://www.gnu.org/licenses/>.
24  *
25  */
26 
27 #ifdef HAVE_CONFIG_H
28 #include <config.h>
29 #endif /* HAVE_CONFIG_H */
30 
31 #include <pthread.h>
32 #include <errno.h>
33 
34 #include "fg_definitions.h"
35 #include "fg_barrier.h"
36 
38  const pthread_barrierattr_t *attr, unsigned count)
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 }
63 
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 }
79 
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_mutex_t mutex
Protect shared data structures from concurrent modifications.
Definition: fg_barrier.h:42
#define unlikely(x)
These macros gain us a few percent of speed.
int pthread_barrierattr_t
Barrier attribute object.
Definition: fg_barrier.h:37
int pthread_barrier_destroy(pthread_barrier_t *barrier)
Destroys the barrier referenced by barrier.
Definition: fg_barrier.c:64
int tripCount
Current number of threads waiting at the barrier.
Definition: fg_barrier.h:48
int count
Required number of threads have to wait at the barrier.
Definition: fg_barrier.h:46
Missing pthread barrier implemenation for OS X.
pthread_cond_t cond
Thread to suspend its execution until the condition is satisfied.
Definition: fg_barrier.h:44
Common definitions used by the Flowgrind daemon, controller, and libs.
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.
Definition: fg_barrier.c:37
Object for barrier synchronization.
Definition: fg_barrier.h:40
int pthread_barrier_wait(pthread_barrier_t *barrier)
Synchronizes participating threads at the barrier referenced by barrier.
Definition: fg_barrier.c:80