docs: cmake build
[m6w6/libmemcached] / libtest / thread.hpp
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Data Differential YATL (i.e. libtest) library
4 *
5 * Copyright (C) 2012 Data Differential, http://datadifferential.com/
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following disclaimer
16 * in the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 */
36
37 #pragma once
38
39 #include <pthread.h>
40
41 #if __cplusplus < 201103L
42 # define noexcept(a)
43 #endif
44
45 namespace libtest
46 {
47 namespace thread
48 {
49
50 class Mutex
51 {
52 public:
53 Mutex() :
54 _err(0)
55 {
56 _err= pthread_mutex_init(&_mutex, NULL);
57 }
58
59 ~Mutex() noexcept(false)
60 {
61 if ((_err= pthread_mutex_destroy(&_mutex)))
62 {
63 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_cond_destroy: %s", strerror(_err));
64 }
65 }
66
67 pthread_mutex_t* handle()
68 {
69 if (_err != 0)
70 {
71 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_mutex_init: %s", strerror(_err));
72 }
73
74 return &_mutex;
75 }
76
77 private:
78 int _err;
79 pthread_mutex_t _mutex;
80 };
81
82 class ScopedLock
83 {
84 public:
85 ScopedLock(Mutex& mutex_) :
86 _mutex(mutex_)
87 {
88 init();
89 }
90
91 ~ScopedLock() noexcept(false)
92 {
93 int err;
94 if ((err= pthread_mutex_unlock(_mutex.handle())))
95 {
96 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_mutex_unlock: %s", strerror(err));
97 }
98 }
99
100 Mutex* handle()
101 {
102 return &_mutex;
103 }
104
105 private:
106 void init()
107 {
108 int err;
109 if ((err= pthread_mutex_lock(_mutex.handle())))
110 {
111 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_mutex_lock: %s", strerror(err));
112 }
113 }
114
115 private:
116 Mutex& _mutex;
117 };
118
119 class Condition
120 {
121 public:
122 Condition()
123 {
124 int err;
125 if ((err= pthread_cond_init(&_cond, NULL)))
126 {
127 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_mutex_init: %s", strerror(err));
128 }
129 }
130
131 ~Condition() noexcept(false)
132 {
133 int err;
134 if ((err= pthread_cond_destroy(&_cond)))
135 {
136 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_cond_destroy: %s", strerror(err));
137 }
138 }
139
140 void broadcast()
141 {
142 int err;
143 if ((err= pthread_cond_broadcast(&_cond)))
144 {
145 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_cond_broadcast: %s", strerror(err));
146 }
147 }
148
149 void signal()
150 {
151 int err;
152 if ((err= pthread_cond_signal(&_cond)))
153 {
154 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_cond_broadcast: %s", strerror(err));
155 }
156 }
157
158 void wait(ScopedLock& lock_)
159 {
160 int err;
161 if ((err= pthread_cond_wait(&_cond, lock_.handle()->handle())))
162 {
163 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_cond_wait: %s", strerror(err));
164 }
165 }
166
167 private:
168 pthread_cond_t _cond;
169 };
170
171 class Barrier
172 {
173 public:
174 explicit Barrier(uint32_t count):
175 _threshold(count),
176 _count(count),
177 _generation(0)
178 {
179 if (_count == 0)
180 {
181 fatal_assert("Zero is an invalid value");
182 }
183 }
184
185 ~Barrier()
186 {
187 }
188
189 bool wait()
190 {
191 ScopedLock l(_mutex);
192 uint32_t gen = _generation;
193
194 if (--_count == 0)
195 {
196 _generation++;
197 _count = _threshold;
198 _cond.broadcast();
199
200 return true;
201 }
202
203 while (gen == _generation)
204 {
205 _cond.wait(l);
206 }
207
208 return false;
209 }
210
211 private:
212 Mutex _mutex;
213 Condition _cond;
214 uint32_t _threshold;
215 uint32_t _count;
216 uint32_t _generation;
217 };
218
219 class Thread
220 {
221 private:
222 typedef void *(*start_routine_fn) (void *);
223
224 public:
225 template <class Function,class Arg1>
226 Thread(Function func, Arg1 arg):
227 _joined(false),
228 _func((start_routine_fn)func),
229 _context(arg)
230 {
231 int err;
232 if ((err= pthread_create(&_thread, NULL, entry_func, (void*)this)))
233 {
234 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_create: %s", strerror(err));
235 }
236 _owner= pthread_self();
237 }
238
239 bool running() const
240 {
241 return (pthread_kill(_thread, 0) == 0);
242 }
243
244 bool detached()
245 {
246 if (EDEADLK == pthread_join(_thread, NULL))
247 {
248 return true;
249 }
250
251 /* Result of pthread_join was EINVAL == detached thread */
252 return false;
253 }
254
255 bool join()
256 {
257 if (_thread == pthread_self())
258 {
259 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "Thread cannot join on itself");
260 }
261
262 if (_owner != pthread_self())
263 {
264 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "Attempt made by a non-owner thead to join on thread");
265 }
266
267 bool ret= false;
268 {
269 ScopedLock l(_join_mutex);
270 if (_joined == false)
271 {
272 int err;
273 if ((err= pthread_join(_thread, NULL)))
274 {
275 switch(err)
276 {
277 case EINVAL:
278 break;
279
280 case ESRCH:
281 ret= true;
282 break;
283
284 case EDEADLK:
285 default:
286 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_join: %s", strerror(err));
287 }
288 }
289 else
290 {
291 ret= true;
292 }
293
294 _joined= true;
295 }
296 }
297
298 return ret;
299 }
300
301 ~Thread()
302 {
303 join();
304 }
305
306 protected:
307 void run()
308 {
309 _func(_context);
310 }
311
312 private:
313 static void * entry_func(void* This)
314 {
315 ((Thread *)This)->run();
316 return NULL;
317 }
318
319 private:
320 bool _joined;
321 pthread_t _thread;
322 pthread_t _owner;
323 start_routine_fn _func;
324 void* _context;
325 Mutex _join_mutex;
326 };
327
328 } // namespace thread
329 } // namespace libtest