Debian Codegraph @

src/common/compat_pthread_barrier.c

raw symbols SCIP
1 /*
2 * Copyright 2015 Red Hat Inc., Durham, North Carolina.
3 * All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors:
20 * Martin Preisler <[email protected]>
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include "compat_pthread_barrier.h"
28
29 #ifdef OS_APPLE
30
31 #include <errno.h>
32
33 int pthread_barrier_init(pthread_barrier_t *barrier, const pthread_barrierattr_t *attr, unsigned int count)
34 {
35 if (count == 0)
36 {
37 errno = EINVAL;
38 return -1;
39 }
40
41 if (pthread_mutex_init(&barrier->mutex, 0) != 0)
42 return -1;
43
44 if (pthread_cond_init(&barrier->cond, 0) != 0)
45 {
46 pthread_mutex_destroy(&barrier->mutex);
47 return -1;
48 }
49
50 barrier->trip_count = count;
51 barrier->count = 0;
52
53 return 0;
54 }
55
56 int pthread_barrier_destroy(pthread_barrier_t *barrier)
57 {
58 pthread_cond_destroy(&barrier->cond);
59 pthread_mutex_destroy(&barrier->mutex);
60
61 return 0;
62 }
63
64 int pthread_barrier_wait(pthread_barrier_t *barrier)
65 {
66 pthread_mutex_lock(&barrier->mutex);
67
68 barrier->count += 1;
69
70 if (barrier->count >= barrier->trip_count)
71 {
72 barrier->count = 0;
73 pthread_cond_broadcast(&barrier->cond);
74 pthread_mutex_unlock(&barrier->mutex);
75
76 return PTHREAD_BARRIER_SERIAL_THREAD;
77 }
78 else
79 {
80 // The mutex is unlocked at this point by cond_wait, other threads
81 // can then lock it by entering this function.
82 pthread_cond_wait(&barrier->cond, &barrier->mutex);
83 pthread_mutex_unlock(&barrier->mutex);
84
85 return 0;
86 }
87 }
88
89 #endif