Open SCAP Library
compat_pthread_barrier.h
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Authors:
20  * Martin Preisler <mpreisle@redhat.com>
21  */
22 
23 #ifndef OSCAP_COMPAT_PTHREAD_BARRIER_H_
24 #define OSCAP_COMPAT_PTHREAD_BARRIER_H_
25 
26 /*
27  * This file implements a very slow and basic pthread_barrier_t
28  * on systems where this implementation isn't available.
29  *
30  * One such commonly used system is Apple MacOS X.
31  */
32 
33 #include <pthread.h>
34 
35 // TODO: Maybe there is a better macro to check here
36 #ifdef __APPLE__
37 
38 // returned to the last thread that hits the trip count
39 #define PTHREAD_BARRIER_SERIAL_THREAD -1
40 // 0 is returned to all the other threads that have been waiting
41 
42 typedef int pthread_barrierattr_t;
43 
44 typedef struct
45 {
46  pthread_mutex_t mutex;
47  pthread_cond_t cond;
48  int count;
49  int trip_count;
50 } pthread_barrier_t;
51 
52 int pthread_barrier_init(pthread_barrier_t *barrier, const pthread_barrierattr_t *attr, unsigned int count);
53 int pthread_barrier_destroy(pthread_barrier_t *barrier);
54 int pthread_barrier_wait(pthread_barrier_t *barrier);
55 
56 #endif
57 
58 #endif