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 _err= pthread_mutex_init(&_mutex, NULL);
57 if ((_err= pthread_mutex_destroy(&_mutex)))
59 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_cond_destroy: %s", strerror(_err));
63 pthread_mutex_t* handle()
67 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_mutex_init: %s", strerror(_err));
75 pthread_mutex_t _mutex;
81 ScopedLock(Mutex& mutex_) :
90 if ((err= pthread_mutex_unlock(_mutex.handle())))
92 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_mutex_unlock: %s", strerror(err));
105 if ((err= pthread_mutex_lock(_mutex.handle())))
107 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_mutex_lock: %s", strerror(err));
121 if ((err= pthread_cond_init(&_cond, NULL)))
123 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_mutex_init: %s", strerror(err));
130 if ((err= pthread_cond_destroy(&_cond)))
132 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_cond_destroy: %s", strerror(err));
139 if ((err= pthread_cond_broadcast(&_cond)))
141 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_cond_broadcast: %s", strerror(err));
148 if ((err= pthread_cond_signal(&_cond)))
150 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_cond_broadcast: %s", strerror(err));
154 void wait(ScopedLock& lock_)
157 if ((err= pthread_cond_wait(&_cond, lock_.handle()->handle())))
159 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_cond_wait: %s", strerror(err));
164 pthread_cond_t _cond;
170 explicit Barrier(uint32_t count):
177 fatal_assert("Zero is an invalid value");
187 ScopedLock l(_mutex);
188 uint32_t gen = _generation;
199 while (gen == _generation)
212 uint32_t _generation;
218 typedef void *(*start_routine_fn) (void *);
221 template <class Function,class Arg1>
222 Thread(Function func, Arg1 arg):
224 _func((start_routine_fn)func),
228 if ((err= pthread_create(&_thread, NULL, entry_func, (void*)this)))
230 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_create: %s", strerror(err));
232 _owner= pthread_self();
237 return (pthread_kill(_thread, 0) == 0);
242 if (EDEADLK == pthread_join(_thread, NULL))
247 /* Result of pthread_join was EINVAL == detached thread */
253 if (_thread == pthread_self())
255 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "Thread cannot join on itself");
258 if (_owner != pthread_self())
260 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "Attempt made by a non-owner thead to join on thread");
265 ScopedLock l(_join_mutex);
266 if (_joined == false)
269 if ((err= pthread_join(_thread, NULL)))
282 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_join: %s", strerror(err));
309 static void * entry_func(void* This)
311 ((Thread *)This)->run();
319 start_routine_fn _func;
324 } // namespace thread
325 } // namespace libtest