Style cleanup.
[awesomized/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 namespace libtest
42 {
43 namespace thread
44 {
45
46 class Mutex
47 {
48 public:
49 Mutex()
50 {
51 int err;
52 if ((err= pthread_mutex_init(&_mutex, NULL)))
53 {
54 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_mutex_init: %s", strerror(err));
55 }
56 }
57
58 ~Mutex()
59 {
60 int err;
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 return &_mutex;
70 }
71
72 private:
73 pthread_mutex_t _mutex;
74 };
75
76 class ScopedLock
77 {
78 public:
79 ScopedLock(Mutex& mutex_) :
80 _mutex(mutex_)
81 {
82 init();
83 }
84
85 ~ScopedLock()
86 {
87 int err;
88 if ((err= pthread_mutex_unlock(_mutex.handle())))
89 {
90 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_mutex_unlock: %s", strerror(err));
91 }
92 }
93
94 Mutex* handle()
95 {
96 return &_mutex;
97 }
98
99 private:
100 void init()
101 {
102 int err;
103 if ((err= pthread_mutex_lock(_mutex.handle())))
104 {
105 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_mutex_lock: %s", strerror(err));
106 }
107 }
108
109 private:
110 Mutex& _mutex;
111 };
112
113 class Condition
114 {
115 public:
116 Condition()
117 {
118 int err;
119 if ((err= pthread_cond_init(&_cond, NULL)))
120 {
121 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_mutex_init: %s", strerror(err));
122 }
123 }
124
125 ~Condition()
126 {
127 int err;
128 if ((err= pthread_cond_destroy(&_cond)))
129 {
130 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_cond_destroy: %s", strerror(err));
131 }
132 }
133
134 void broadcast()
135 {
136 int err;
137 if ((err= pthread_cond_broadcast(&_cond)))
138 {
139 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_cond_broadcast: %s", strerror(err));
140 }
141 }
142
143 void signal()
144 {
145 int err;
146 if ((err= pthread_cond_signal(&_cond)))
147 {
148 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_cond_broadcast: %s", strerror(err));
149 }
150 }
151
152 void wait(ScopedLock& lock_)
153 {
154 int err;
155 if ((err= pthread_cond_wait(&_cond, lock_.handle()->handle())))
156 {
157 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_cond_wait: %s", strerror(err));
158 }
159 }
160
161 private:
162 pthread_cond_t _cond;
163 };
164
165 class Barrier
166 {
167 public:
168 Barrier(uint32_t count):
169 _threshold(count),
170 _count(count),
171 _generation(0)
172 {
173 if (_count == 0)
174 {
175 fatal_assert("Zero is an invalid value");
176 }
177 }
178
179 ~Barrier()
180 {
181 }
182
183 bool wait()
184 {
185 ScopedLock l(_mutex);
186 uint32_t gen = _generation;
187
188 if (--_count == 0)
189 {
190 _generation++;
191 _count = _threshold;
192 _cond.broadcast();
193
194 return true;
195 }
196
197 while (gen == _generation)
198 {
199 _cond.wait(l);
200 }
201
202 return false;
203 }
204
205 private:
206 Mutex _mutex;
207 Condition _cond;
208 uint32_t _threshold;
209 uint32_t _count;
210 uint32_t _generation;
211 };
212
213 class Thread
214 {
215 private:
216 typedef void *(*start_routine_fn) (void *);
217
218 public:
219 template <class Function,class Arg1>
220 Thread(Function func, Arg1 arg):
221 _joined(false),
222 _func((start_routine_fn)func),
223 _context(arg)
224 {
225 int err;
226 if ((err= pthread_create(&_thread, NULL, entry_func, (void*)this)))
227 {
228 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_create: %s", strerror(err));
229 }
230 _owner= pthread_self();
231 }
232
233 bool running() const
234 {
235 return (pthread_kill(_thread, 0) == 0);
236 }
237
238 bool detached()
239 {
240 if (EDEADLK == pthread_join(_thread, NULL))
241 {
242 return true;
243 }
244
245 /* Result of pthread_join was EINVAL == detached thread */
246 return false;
247 }
248
249 bool join()
250 {
251 if (_thread == pthread_self())
252 {
253 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "Thread cannot join on itself");
254 }
255
256 if (_owner != pthread_self())
257 {
258 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "Attempt made by a non-owner thead to join on thread");
259 }
260
261 bool ret= false;
262 {
263 ScopedLock l(_join_mutex);
264 if (_joined == false)
265 {
266 int err;
267 if ((err= pthread_join(_thread, NULL)))
268 {
269 switch(err)
270 {
271 case EINVAL:
272 break;
273
274 case ESRCH:
275 ret= true;
276 break;
277
278 case EDEADLK:
279 default:
280 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_join: %s", strerror(err));
281 }
282 }
283 else
284 {
285 ret= true;
286 }
287
288 _joined= true;
289 }
290 }
291
292 return ret;
293 }
294
295 ~Thread()
296 {
297 join();
298 }
299
300 protected:
301 void run()
302 {
303 _func(_context);
304 }
305
306 private:
307 static void * entry_func(void* This)
308 {
309 ((Thread *)This)->run();
310 return NULL;
311 }
312
313 private:
314 bool _joined;
315 pthread_t _thread;
316 pthread_t _owner;
317 start_routine_fn _func;
318 void* _context;
319 Mutex _join_mutex;
320 };
321
322 } // namespace thread
323 } // namespace libtest