1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 * Data Differential YATL (i.e. libtest) library
5 * Copyright (C) 2012 Data Differential, http://datadifferential.com/
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
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
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
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.
41 #if __cplusplus < 201103L
56 _err= pthread_mutex_init(&_mutex, NULL);
59 ~Mutex() noexcept(false)
61 if ((_err= pthread_mutex_destroy(&_mutex)))
63 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_cond_destroy: %s", strerror(_err));
67 pthread_mutex_t* handle()
71 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_mutex_init: %s", strerror(_err));
79 pthread_mutex_t _mutex;
85 ScopedLock(Mutex& mutex_) :
91 ~ScopedLock() noexcept(false)
94 if ((err= pthread_mutex_unlock(_mutex.handle())))
96 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_mutex_unlock: %s", strerror(err));
109 if ((err= pthread_mutex_lock(_mutex.handle())))
111 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_mutex_lock: %s", strerror(err));
125 if ((err= pthread_cond_init(&_cond, NULL)))
127 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_mutex_init: %s", strerror(err));
131 ~Condition() noexcept(false)
134 if ((err= pthread_cond_destroy(&_cond)))
136 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_cond_destroy: %s", strerror(err));
143 if ((err= pthread_cond_broadcast(&_cond)))
145 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_cond_broadcast: %s", strerror(err));
152 if ((err= pthread_cond_signal(&_cond)))
154 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_cond_broadcast: %s", strerror(err));
158 void wait(ScopedLock& lock_)
161 if ((err= pthread_cond_wait(&_cond, lock_.handle()->handle())))
163 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_cond_wait: %s", strerror(err));
168 pthread_cond_t _cond;
174 explicit Barrier(uint32_t count):
181 fatal_assert("Zero is an invalid value");
191 ScopedLock l(_mutex);
192 uint32_t gen = _generation;
203 while (gen == _generation)
216 uint32_t _generation;
222 typedef void *(*start_routine_fn) (void *);
225 template <class Function,class Arg1>
226 Thread(Function func, Arg1 arg):
228 _func((start_routine_fn)func),
232 if ((err= pthread_create(&_thread, NULL, entry_func, (void*)this)))
234 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_create: %s", strerror(err));
236 _owner= pthread_self();
241 return (pthread_kill(_thread, 0) == 0);
246 if (EDEADLK == pthread_join(_thread, NULL))
251 /* Result of pthread_join was EINVAL == detached thread */
257 if (_thread == pthread_self())
259 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "Thread cannot join on itself");
262 if (_owner != pthread_self())
264 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "Attempt made by a non-owner thead to join on thread");
269 ScopedLock l(_join_mutex);
270 if (_joined == false)
273 if ((err= pthread_join(_thread, NULL)))
286 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_join: %s", strerror(err));
313 static void * entry_func(void* This)
315 ((Thread *)This)->run();
323 start_routine_fn _func;
328 } // namespace thread
329 } // namespace libtest