1/*- 2 * Copyright (c) 2009-2010 The FreeBSD Foundation 3 * All rights reserved. 4 * 5 * This software was developed by Pawel Jakub Dawidek under sponsorship from 6 * the FreeBSD Foundation. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * $P4: //depot/projects/trustedbsd/openbsm/bin/auditdistd/synch.h#3 $ 30 */ 31 32#ifndef _SYNCH_H_ 33#define _SYNCH_H_ 34 35#include <errno.h> 36#include <pthread.h> 37#ifdef HAVE_PTHREAD_NP_H 38#include <pthread_np.h> 39#endif 40#include <stdbool.h> 41#include <time.h> 42 43#include "pjdlog.h" 44 45#ifndef PJDLOG_ASSERT 46#include <assert.h> 47#define PJDLOG_ASSERT(...) assert(__VA_ARGS__) 48#endif 49 50static __inline void 51mtx_init(pthread_mutex_t *lock) 52{ 53 int error; 54 55 error = pthread_mutex_init(lock, NULL); 56 PJDLOG_ASSERT(error == 0); 57} 58static __inline void 59mtx_destroy(pthread_mutex_t *lock) 60{ 61 int error; 62 63 error = pthread_mutex_destroy(lock); 64 PJDLOG_ASSERT(error == 0); 65} 66static __inline void 67mtx_lock(pthread_mutex_t *lock) 68{ 69 int error; 70 71 error = pthread_mutex_lock(lock); 72 PJDLOG_ASSERT(error == 0); 73} 74static __inline bool 75mtx_trylock(pthread_mutex_t *lock) 76{ 77 int error; 78 79 error = pthread_mutex_trylock(lock); 80 PJDLOG_ASSERT(error == 0 || error == EBUSY); 81 return (error == 0); 82} 83static __inline void 84mtx_unlock(pthread_mutex_t *lock) 85{ 86 int error; 87 88 error = pthread_mutex_unlock(lock); 89 PJDLOG_ASSERT(error == 0); 90} 91static __inline bool 92mtx_owned(pthread_mutex_t *lock) 93{ 94 95 return (pthread_mutex_isowned_np(lock) != 0); 96} 97 98static __inline void 99rw_init(pthread_rwlock_t *lock) 100{ 101 int error; 102 103 error = pthread_rwlock_init(lock, NULL); 104 PJDLOG_ASSERT(error == 0); 105} 106static __inline void 107rw_destroy(pthread_rwlock_t *lock) 108{ 109 int error; 110 111 error = pthread_rwlock_destroy(lock); 112 PJDLOG_ASSERT(error == 0); 113} 114static __inline void 115rw_rlock(pthread_rwlock_t *lock) 116{ 117 int error; 118 119 error = pthread_rwlock_rdlock(lock); 120 PJDLOG_ASSERT(error == 0); 121} 122static __inline void 123rw_wlock(pthread_rwlock_t *lock) 124{ 125 int error; 126 127 error = pthread_rwlock_wrlock(lock); 128 PJDLOG_ASSERT(error == 0); 129} 130static __inline void 131rw_unlock(pthread_rwlock_t *lock) 132{ 133 int error; 134 135 error = pthread_rwlock_unlock(lock); 136 PJDLOG_ASSERT(error == 0); 137} 138 139static __inline void 140cv_init(pthread_cond_t *cv) 141{ 142 pthread_condattr_t attr; 143 int error; 144 145 error = pthread_condattr_init(&attr); 146 PJDLOG_ASSERT(error == 0); 147#ifdef HAVE_PTHREAD_CONDATTR_SETCLOCK 148 error = pthread_condattr_setclock(&attr, CLOCK_MONOTONIC); 149 PJDLOG_ASSERT(error == 0); 150#endif 151 error = pthread_cond_init(cv, &attr); 152 PJDLOG_ASSERT(error == 0); 153 error = pthread_condattr_destroy(&attr); 154 PJDLOG_ASSERT(error == 0); 155} 156static __inline void 157cv_wait(pthread_cond_t *cv, pthread_mutex_t *lock) 158{ 159 int error; 160 161 error = pthread_cond_wait(cv, lock); 162 PJDLOG_ASSERT(error == 0); 163} 164static __inline bool 165cv_timedwait(pthread_cond_t *cv, pthread_mutex_t *lock, int timeout) 166{ 167 struct timespec ts; 168 int error; 169 170 if (timeout == 0) { 171 cv_wait(cv, lock); 172 return (false); 173 } 174 175#ifdef HAVE_PTHREAD_CONDATTR_SETCLOCK 176 error = clock_gettime(CLOCK_MONOTONIC, &ts); 177 PJDLOG_ASSERT(error == 0); 178 ts.tv_sec += timeout; 179 error = pthread_cond_timedwait(cv, lock, &ts); 180#elif HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE_NP 181 ts.tv_sec = timeout; 182 ts.tv_nsec = 0; 183 error = pthread_cond_timedwait_relative_np(cv, lock, &ts); 184#else 185#error Neither pthread_condattr_setclock nor pthread_cond_timedwait_relative_np is available. 186#endif 187 PJDLOG_ASSERT(error == 0 || error == ETIMEDOUT); 188 return (error == ETIMEDOUT); 189} 190static __inline void 191cv_signal(pthread_cond_t *cv) 192{ 193 int error; 194 195 error = pthread_cond_signal(cv); 196 PJDLOG_ASSERT(error == 0); 197} 198static __inline void 199cv_broadcast(pthread_cond_t *cv) 200{ 201 int error; 202 203 error = pthread_cond_broadcast(cv); 204 PJDLOG_ASSERT(error == 0); 205} 206#endif /* !_SYNCH_H_ */ 207