1 #ifndef BOOST_THREAD_PTHREAD_PTHREAD_HELPERS_HPP
2 #define BOOST_THREAD_PTHREAD_PTHREAD_HELPERS_HPP
3 // Copyright (C) 2017
4 // Vicente J. Botet Escriba
5 //
6 //  Distributed under the Boost Software License, Version 1.0. (See
7 //  accompanying file LICENSE_1_0.txt or copy at
8 //  http://www.boost.org/LICENSE_1_0.txt)
9 
10 #include <boost/thread/detail/config.hpp>
11 #include <boost/throw_exception.hpp>
12 #include <pthread.h>
13 #include <errno.h>
14 
15 #include <boost/config/abi_prefix.hpp>
16 
17 #ifndef BOOST_THREAD_HAS_NO_EINTR_BUG
18 #define BOOST_THREAD_HAS_EINTR_BUG
19 #endif
20 
21 namespace boost
22 {
23   namespace posix
24   {
25     BOOST_FORCEINLINE BOOST_THREAD_DISABLE_THREAD_SAFETY_ANALYSIS
pthread_mutex_init(pthread_mutex_t * m,const pthread_mutexattr_t * attr=NULL)26     int pthread_mutex_init(pthread_mutex_t* m, const pthread_mutexattr_t* attr = NULL)
27     {
28       return ::pthread_mutex_init(m, attr);
29     }
30 
31     BOOST_FORCEINLINE BOOST_THREAD_DISABLE_THREAD_SAFETY_ANALYSIS
pthread_cond_init(pthread_cond_t * c)32     int pthread_cond_init(pthread_cond_t* c)
33     {
34 #ifdef BOOST_THREAD_INTERNAL_CLOCK_IS_MONO
35       pthread_condattr_t attr;
36       int res = pthread_condattr_init(&attr);
37       if (res)
38       {
39         return res;
40       }
41       BOOST_VERIFY(!pthread_condattr_setclock(&attr, CLOCK_MONOTONIC));
42       res = ::pthread_cond_init(c, &attr);
43       BOOST_VERIFY(!pthread_condattr_destroy(&attr));
44       return res;
45 #else
46       return ::pthread_cond_init(c, NULL);
47 #endif
48     }
49 
50 #ifdef BOOST_THREAD_HAS_EINTR_BUG
51     BOOST_FORCEINLINE BOOST_THREAD_DISABLE_THREAD_SAFETY_ANALYSIS
pthread_mutex_destroy(pthread_mutex_t * m)52     int pthread_mutex_destroy(pthread_mutex_t* m)
53     {
54       int ret;
55       do
56       {
57           ret = ::pthread_mutex_destroy(m);
58       } while (ret == EINTR);
59       return ret;
60     }
61 
62     BOOST_FORCEINLINE BOOST_THREAD_DISABLE_THREAD_SAFETY_ANALYSIS
pthread_cond_destroy(pthread_cond_t * c)63     int pthread_cond_destroy(pthread_cond_t* c)
64     {
65       int ret;
66       do
67       {
68           ret = ::pthread_cond_destroy(c);
69       } while (ret == EINTR);
70       return ret;
71     }
72 
73     BOOST_FORCEINLINE BOOST_THREAD_DISABLE_THREAD_SAFETY_ANALYSIS
pthread_mutex_lock(pthread_mutex_t * m)74     int pthread_mutex_lock(pthread_mutex_t* m)
75     {
76       int ret;
77       do
78       {
79           ret = ::pthread_mutex_lock(m);
80       } while (ret == EINTR);
81       return ret;
82     }
83 
84     BOOST_FORCEINLINE BOOST_THREAD_DISABLE_THREAD_SAFETY_ANALYSIS
pthread_mutex_trylock(pthread_mutex_t * m)85     int pthread_mutex_trylock(pthread_mutex_t* m)
86     {
87       int ret;
88       do
89       {
90           ret = ::pthread_mutex_trylock(m);
91       } while (ret == EINTR);
92       return ret;
93     }
94 
95     BOOST_FORCEINLINE BOOST_THREAD_DISABLE_THREAD_SAFETY_ANALYSIS
pthread_mutex_unlock(pthread_mutex_t * m)96     int pthread_mutex_unlock(pthread_mutex_t* m)
97     {
98       int ret;
99       do
100       {
101           ret = ::pthread_mutex_unlock(m);
102       } while (ret == EINTR);
103       return ret;
104     }
105 
106     BOOST_FORCEINLINE BOOST_THREAD_DISABLE_THREAD_SAFETY_ANALYSIS
pthread_cond_wait(pthread_cond_t * c,pthread_mutex_t * m)107     int pthread_cond_wait(pthread_cond_t* c, pthread_mutex_t* m)
108     {
109       int ret;
110       do
111       {
112           ret = ::pthread_cond_wait(c, m);
113       } while (ret == EINTR);
114       return ret;
115     }
116 
117     BOOST_FORCEINLINE BOOST_THREAD_DISABLE_THREAD_SAFETY_ANALYSIS
pthread_cond_timedwait(pthread_cond_t * c,pthread_mutex_t * m,const struct timespec * t)118     int pthread_cond_timedwait(pthread_cond_t* c, pthread_mutex_t* m, const struct timespec* t)
119     {
120       int ret;
121       do
122       {
123           ret = ::pthread_cond_timedwait(c, m, t);
124       } while (ret == EINTR);
125       return ret;
126     }
127 #else
128     BOOST_FORCEINLINE BOOST_THREAD_DISABLE_THREAD_SAFETY_ANALYSIS
pthread_mutex_destroy(pthread_mutex_t * m)129     int pthread_mutex_destroy(pthread_mutex_t* m)
130     {
131       return ::pthread_mutex_destroy(m);
132     }
133 
134     BOOST_FORCEINLINE BOOST_THREAD_DISABLE_THREAD_SAFETY_ANALYSIS
pthread_cond_destroy(pthread_cond_t * c)135     int pthread_cond_destroy(pthread_cond_t* c)
136     {
137       return ::pthread_cond_destroy(c);
138     }
139 
140     BOOST_FORCEINLINE BOOST_THREAD_DISABLE_THREAD_SAFETY_ANALYSIS
pthread_mutex_lock(pthread_mutex_t * m)141     int pthread_mutex_lock(pthread_mutex_t* m)
142     {
143       return ::pthread_mutex_lock(m);
144     }
145 
146     BOOST_FORCEINLINE BOOST_THREAD_DISABLE_THREAD_SAFETY_ANALYSIS
pthread_mutex_trylock(pthread_mutex_t * m)147     int pthread_mutex_trylock(pthread_mutex_t* m)
148     {
149       return ::pthread_mutex_trylock(m);
150     }
151 
152     BOOST_FORCEINLINE BOOST_THREAD_DISABLE_THREAD_SAFETY_ANALYSIS
pthread_mutex_unlock(pthread_mutex_t * m)153     int pthread_mutex_unlock(pthread_mutex_t* m)
154     {
155       return ::pthread_mutex_unlock(m);
156     }
157 
158     BOOST_FORCEINLINE BOOST_THREAD_DISABLE_THREAD_SAFETY_ANALYSIS
pthread_cond_wait(pthread_cond_t * c,pthread_mutex_t * m)159     int pthread_cond_wait(pthread_cond_t* c, pthread_mutex_t* m)
160     {
161       return ::pthread_cond_wait(c, m);
162     }
163 
164     BOOST_FORCEINLINE BOOST_THREAD_DISABLE_THREAD_SAFETY_ANALYSIS
pthread_cond_timedwait(pthread_cond_t * c,pthread_mutex_t * m,const struct timespec * t)165     int pthread_cond_timedwait(pthread_cond_t* c, pthread_mutex_t* m, const struct timespec* t)
166     {
167       return ::pthread_cond_timedwait(c, m, t);
168     }
169 #endif
170 
171     BOOST_FORCEINLINE BOOST_THREAD_DISABLE_THREAD_SAFETY_ANALYSIS
pthread_cond_signal(pthread_cond_t * c)172     int pthread_cond_signal(pthread_cond_t* c)
173     {
174       return ::pthread_cond_signal(c);
175     }
176 
177     BOOST_FORCEINLINE BOOST_THREAD_DISABLE_THREAD_SAFETY_ANALYSIS
pthread_cond_broadcast(pthread_cond_t * c)178     int pthread_cond_broadcast(pthread_cond_t* c)
179     {
180       return ::pthread_cond_broadcast(c);
181     }
182   }
183 }
184 
185 #include <boost/config/abi_suffix.hpp>
186 
187 #endif
188