synch.h revision 243730
1243730Srwatson/*-
2243730Srwatson * Copyright (c) 2009-2010 The FreeBSD Foundation
3243730Srwatson * All rights reserved.
4243730Srwatson *
5243730Srwatson * This software was developed by Pawel Jakub Dawidek under sponsorship from
6243730Srwatson * the FreeBSD Foundation.
7243730Srwatson *
8243730Srwatson * Redistribution and use in source and binary forms, with or without
9243730Srwatson * modification, are permitted provided that the following conditions
10243730Srwatson * are met:
11243730Srwatson * 1. Redistributions of source code must retain the above copyright
12243730Srwatson *    notice, this list of conditions and the following disclaimer.
13243730Srwatson * 2. Redistributions in binary form must reproduce the above copyright
14243730Srwatson *    notice, this list of conditions and the following disclaimer in the
15243730Srwatson *    documentation and/or other materials provided with the distribution.
16243730Srwatson *
17243730Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
18243730Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19243730Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20243730Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
21243730Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22243730Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23243730Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24243730Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25243730Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26243730Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27243730Srwatson * SUCH DAMAGE.
28243730Srwatson *
29243730Srwatson * $P4: //depot/projects/trustedbsd/openbsm/bin/auditdistd/synch.h#1 $
30243730Srwatson */
31243730Srwatson
32243730Srwatson#ifndef	_SYNCH_H_
33243730Srwatson#define	_SYNCH_H_
34243730Srwatson
35243730Srwatson#ifdef HAVE_CONFIG_H
36243730Srwatson#include "config.h"
37243730Srwatson#endif
38243730Srwatson
39243730Srwatson#include <errno.h>
40243730Srwatson#include <pthread.h>
41243730Srwatson#ifdef HAVE_PTHREAD_NP_H
42243730Srwatson#include <pthread_np.h>
43243730Srwatson#endif
44243730Srwatson#include <stdbool.h>
45243730Srwatson#include <time.h>
46243730Srwatson
47243730Srwatson#include <pjdlog.h>
48243730Srwatson
49243730Srwatson#ifndef	PJDLOG_ASSERT
50243730Srwatson#include <assert.h>
51243730Srwatson#define	PJDLOG_ASSERT(...)	assert(__VA_ARGS__)
52243730Srwatson#endif
53243730Srwatson
54243730Srwatsonstatic __inline void
55243730Srwatsonmtx_init(pthread_mutex_t *lock)
56243730Srwatson{
57243730Srwatson	int error;
58243730Srwatson
59243730Srwatson	error = pthread_mutex_init(lock, NULL);
60243730Srwatson	PJDLOG_ASSERT(error == 0);
61243730Srwatson}
62243730Srwatsonstatic __inline void
63243730Srwatsonmtx_destroy(pthread_mutex_t *lock)
64243730Srwatson{
65243730Srwatson	int error;
66243730Srwatson
67243730Srwatson	error = pthread_mutex_destroy(lock);
68243730Srwatson	PJDLOG_ASSERT(error == 0);
69243730Srwatson}
70243730Srwatsonstatic __inline void
71243730Srwatsonmtx_lock(pthread_mutex_t *lock)
72243730Srwatson{
73243730Srwatson	int error;
74243730Srwatson
75243730Srwatson	error = pthread_mutex_lock(lock);
76243730Srwatson	PJDLOG_ASSERT(error == 0);
77243730Srwatson}
78243730Srwatsonstatic __inline bool
79243730Srwatsonmtx_trylock(pthread_mutex_t *lock)
80243730Srwatson{
81243730Srwatson	int error;
82243730Srwatson
83243730Srwatson	error = pthread_mutex_trylock(lock);
84243730Srwatson	PJDLOG_ASSERT(error == 0 || error == EBUSY);
85243730Srwatson	return (error == 0);
86243730Srwatson}
87243730Srwatsonstatic __inline void
88243730Srwatsonmtx_unlock(pthread_mutex_t *lock)
89243730Srwatson{
90243730Srwatson	int error;
91243730Srwatson
92243730Srwatson	error = pthread_mutex_unlock(lock);
93243730Srwatson	PJDLOG_ASSERT(error == 0);
94243730Srwatson}
95243730Srwatsonstatic __inline bool
96243730Srwatsonmtx_owned(pthread_mutex_t *lock)
97243730Srwatson{
98243730Srwatson
99243730Srwatson	return (pthread_mutex_isowned_np(lock) != 0);
100243730Srwatson}
101243730Srwatson
102243730Srwatsonstatic __inline void
103243730Srwatsonrw_init(pthread_rwlock_t *lock)
104243730Srwatson{
105243730Srwatson	int error;
106243730Srwatson
107243730Srwatson	error = pthread_rwlock_init(lock, NULL);
108243730Srwatson	PJDLOG_ASSERT(error == 0);
109243730Srwatson}
110243730Srwatsonstatic __inline void
111243730Srwatsonrw_destroy(pthread_rwlock_t *lock)
112243730Srwatson{
113243730Srwatson	int error;
114243730Srwatson
115243730Srwatson	error = pthread_rwlock_destroy(lock);
116243730Srwatson	PJDLOG_ASSERT(error == 0);
117243730Srwatson}
118243730Srwatsonstatic __inline void
119243730Srwatsonrw_rlock(pthread_rwlock_t *lock)
120243730Srwatson{
121243730Srwatson	int error;
122243730Srwatson
123243730Srwatson	error = pthread_rwlock_rdlock(lock);
124243730Srwatson	PJDLOG_ASSERT(error == 0);
125243730Srwatson}
126243730Srwatsonstatic __inline void
127243730Srwatsonrw_wlock(pthread_rwlock_t *lock)
128243730Srwatson{
129243730Srwatson	int error;
130243730Srwatson
131243730Srwatson	error = pthread_rwlock_wrlock(lock);
132243730Srwatson	PJDLOG_ASSERT(error == 0);
133243730Srwatson}
134243730Srwatsonstatic __inline void
135243730Srwatsonrw_unlock(pthread_rwlock_t *lock)
136243730Srwatson{
137243730Srwatson	int error;
138243730Srwatson
139243730Srwatson	error = pthread_rwlock_unlock(lock);
140243730Srwatson	PJDLOG_ASSERT(error == 0);
141243730Srwatson}
142243730Srwatson
143243730Srwatsonstatic __inline void
144243730Srwatsoncv_init(pthread_cond_t *cv)
145243730Srwatson{
146243730Srwatson	pthread_condattr_t attr;
147243730Srwatson	int error;
148243730Srwatson
149243730Srwatson	error = pthread_condattr_init(&attr);
150243730Srwatson	PJDLOG_ASSERT(error == 0);
151243730Srwatson#ifdef HAVE_PTHREAD_CONDATTR_SETCLOCK
152243730Srwatson	error = pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
153243730Srwatson	PJDLOG_ASSERT(error == 0);
154243730Srwatson#endif
155243730Srwatson	error = pthread_cond_init(cv, &attr);
156243730Srwatson	PJDLOG_ASSERT(error == 0);
157243730Srwatson	error = pthread_condattr_destroy(&attr);
158243730Srwatson	PJDLOG_ASSERT(error == 0);
159243730Srwatson}
160243730Srwatsonstatic __inline void
161243730Srwatsoncv_wait(pthread_cond_t *cv, pthread_mutex_t *lock)
162243730Srwatson{
163243730Srwatson	int error;
164243730Srwatson
165243730Srwatson	error = pthread_cond_wait(cv, lock);
166243730Srwatson	PJDLOG_ASSERT(error == 0);
167243730Srwatson}
168243730Srwatsonstatic __inline bool
169243730Srwatsoncv_timedwait(pthread_cond_t *cv, pthread_mutex_t *lock, int timeout)
170243730Srwatson{
171243730Srwatson	struct timespec ts;
172243730Srwatson	int error;
173243730Srwatson
174243730Srwatson	if (timeout == 0) {
175243730Srwatson		cv_wait(cv, lock);
176243730Srwatson		return (false);
177243730Srwatson	}
178243730Srwatson
179243730Srwatson#ifdef HAVE_PTHREAD_CONDATTR_SETCLOCK
180243730Srwatson	error = clock_gettime(CLOCK_MONOTONIC, &ts);
181243730Srwatson	PJDLOG_ASSERT(error == 0);
182243730Srwatson	ts.tv_sec += timeout;
183243730Srwatson	error = pthread_cond_timedwait(cv, lock, &ts);
184243730Srwatson#elif HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE_NP
185243730Srwatson	ts.tv_sec = timeout;
186243730Srwatson	ts.tv_nsec = 0;
187243730Srwatson	error = pthread_cond_timedwait_relative_np(cv, lock, &ts);
188243730Srwatson#else
189243730Srwatson#error Neither pthread_condattr_setclock nor pthread_cond_timedwait_relative_np is available.
190243730Srwatson#endif
191243730Srwatson	PJDLOG_ASSERT(error == 0 || error == ETIMEDOUT);
192243730Srwatson	return (error == ETIMEDOUT);
193243730Srwatson}
194243730Srwatsonstatic __inline void
195243730Srwatsoncv_signal(pthread_cond_t *cv)
196243730Srwatson{
197243730Srwatson	int error;
198243730Srwatson
199243730Srwatson	error = pthread_cond_signal(cv);
200243730Srwatson	PJDLOG_ASSERT(error == 0);
201243730Srwatson}
202243730Srwatsonstatic __inline void
203243730Srwatsoncv_broadcast(pthread_cond_t *cv)
204243730Srwatson{
205243730Srwatson	int error;
206243730Srwatson
207243730Srwatson	error = pthread_cond_broadcast(cv);
208243730Srwatson	PJDLOG_ASSERT(error == 0);
209243730Srwatson}
210243730Srwatson#endif	/* !_SYNCH_H_ */
211