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