1 // Copyright 2012 Google LLC
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are
5 // met:
6 //
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above
10 // copyright notice, this list of conditions and the following disclaimer
11 // in the documentation and/or other materials provided with the
12 // distribution.
13 // * Neither the name of Google LLC nor the names of its
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 // This contains Pthread-related functions not provided by the Android NDK
30 // but required by the Breakpad unit test. The functions are inlined here
31 // in a C++ anonymous namespace in order to keep the build files simples.
32
33 #ifndef GOOGLE_BREAKPAD_COMMON_ANDROID_TESTING_PTHREAD_FIXES_H
34 #define GOOGLE_BREAKPAD_COMMON_ANDROID_TESTING_PTHREAD_FIXES_H
35
36 #include <pthread.h>
37
38 namespace {
39
40 // Android doesn't provide pthread_barrier_t for now.
41 #ifndef PTHREAD_BARRIER_SERIAL_THREAD
42
43 // Anything except 0 will do here.
44 #define PTHREAD_BARRIER_SERIAL_THREAD 0x12345
45
46 typedef struct {
47 pthread_mutex_t mutex;
48 pthread_cond_t cond;
49 unsigned count;
50 } pthread_barrier_t;
51
pthread_barrier_init(pthread_barrier_t * barrier,const void *,unsigned count)52 int pthread_barrier_init(pthread_barrier_t* barrier,
53 const void* /* barrier_attr */,
54 unsigned count) {
55 barrier->count = count;
56 pthread_mutex_init(&barrier->mutex, NULL);
57 pthread_cond_init(&barrier->cond, NULL);
58 return 0;
59 }
60
pthread_barrier_wait(pthread_barrier_t * barrier)61 int pthread_barrier_wait(pthread_barrier_t* barrier) {
62 // Lock the mutex
63 pthread_mutex_lock(&barrier->mutex);
64 // Decrement the count. If this is the first thread to reach 0, wake up
65 // waiters, unlock the mutex, then return PTHREAD_BARRIER_SERIAL_THREAD.
66 if (--barrier->count == 0) {
67 // First thread to reach the barrier
68 pthread_cond_broadcast(&barrier->cond);
69 pthread_mutex_unlock(&barrier->mutex);
70 return PTHREAD_BARRIER_SERIAL_THREAD;
71 }
72 // Otherwise, wait for other threads until the count reaches 0, then
73 // return 0 to indicate this is not the first thread.
74 do {
75 pthread_cond_wait(&barrier->cond, &barrier->mutex);
76 } while (barrier->count > 0);
77
78 pthread_mutex_unlock(&barrier->mutex);
79 return 0;
80 }
81
pthread_barrier_destroy(pthread_barrier_t * barrier)82 int pthread_barrier_destroy(pthread_barrier_t* barrier) {
83 barrier->count = 0;
84 pthread_cond_destroy(&barrier->cond);
85 pthread_mutex_destroy(&barrier->mutex);
86 return 0;
87 }
88
89 #endif // defined(PTHREAD_BARRIER_SERIAL_THREAD)
90
91 } // namespace
92
93 #endif // GOOGLE_BREAKPAD_COMMON_ANDROID_TESTING_PTHREAD_FIXES_H
94