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.
52 if ((err= pthread_mutex_init(&_mutex, NULL)))
54 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_mutex_init: %s", strerror(err));
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()
73 pthread_mutex_t _mutex;
79 ScopedLock(Mutex& mutex_) :
88 if ((err= pthread_mutex_unlock(_mutex.handle())))
90 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_mutex_unlock: %s", strerror(err));
103 if ((err= pthread_mutex_lock(_mutex.handle())))
105 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_mutex_lock: %s", strerror(err));
119 if ((err= pthread_cond_init(&_cond, NULL)))
121 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_mutex_init: %s", strerror(err));
128 if ((err= pthread_cond_destroy(&_cond)))
130 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_cond_destroy: %s", strerror(err));
137 if ((err= pthread_cond_broadcast(&_cond)))
139 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_cond_broadcast: %s", strerror(err));
146 if ((err= pthread_cond_signal(&_cond)))
148 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_cond_broadcast: %s", strerror(err));
152 void wait(ScopedLock& lock_)
155 if ((err= pthread_cond_wait(&_cond, lock_.handle()->handle())))
157 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_cond_wait: %s", strerror(err));
162 pthread_cond_t _cond;
168 explicit Barrier(uint32_t count):
175 fatal_assert("Zero is an invalid value");
185 ScopedLock l(_mutex);
186 uint32_t gen = _generation;
197 while (gen == _generation)
210 uint32_t _generation;
216 typedef void *(*start_routine_fn) (void *);
219 template <class Function,class Arg1>
220 Thread(Function func, Arg1 arg):
222 _func((start_routine_fn)func),
226 if ((err= pthread_create(&_thread, NULL, entry_func, (void*)this)))
228 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_create: %s", strerror(err));
230 _owner= pthread_self();
235 return (pthread_kill(_thread, 0) == 0);
240 if (EDEADLK == pthread_join(_thread, NULL))
245 /* Result of pthread_join was EINVAL == detached thread */
251 if (_thread == pthread_self())
253 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "Thread cannot join on itself");
256 if (_owner != pthread_self())
258 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "Attempt made by a non-owner thead to join on thread");
263 ScopedLock l(_join_mutex);
264 if (_joined == false)
267 if ((err= pthread_join(_thread, NULL)))
280 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_join: %s", strerror(err));
307 static void * entry_func(void* This)
309 ((Thread *)This)->run();
317 start_routine_fn _func;
322 } // namespace thread
323 } // namespace libtest