xref: /illumos-gate/usr/src/compat/bhyve/pthread.h (revision 7bb0eb34)
1*7bb0eb34SAndy Fiddaman /*
2*7bb0eb34SAndy Fiddaman  * This file and its contents are supplied under the terms of the
3*7bb0eb34SAndy Fiddaman  * Common Development and Distribution License ("CDDL"), version 1.0.
4*7bb0eb34SAndy Fiddaman  * You may only use this file in accordance with the terms of version
5*7bb0eb34SAndy Fiddaman  * 1.0 of the CDDL.
6*7bb0eb34SAndy Fiddaman  *
7*7bb0eb34SAndy Fiddaman  * A full copy of the text of the CDDL should have accompanied this
8*7bb0eb34SAndy Fiddaman  * source.  A copy of the CDDL is also available via the Internet at
9*7bb0eb34SAndy Fiddaman  * http://www.illumos.org/license/CDDL.
10*7bb0eb34SAndy Fiddaman  */
11*7bb0eb34SAndy Fiddaman 
12*7bb0eb34SAndy Fiddaman /*
13*7bb0eb34SAndy Fiddaman  * Copyright 2022 OmniOS Community Edition (OmniOSce) Association.
14*7bb0eb34SAndy Fiddaman  */
15*7bb0eb34SAndy Fiddaman 
16*7bb0eb34SAndy Fiddaman #ifndef _COMPAT_FREEBSD_PTHREAD_H_
17*7bb0eb34SAndy Fiddaman #define	_COMPAT_FREEBSD_PTHREAD_H_
18*7bb0eb34SAndy Fiddaman 
19*7bb0eb34SAndy Fiddaman #include <sys/debug.h>
20*7bb0eb34SAndy Fiddaman #include_next <pthread.h>
21*7bb0eb34SAndy Fiddaman 
22*7bb0eb34SAndy Fiddaman /*
23*7bb0eb34SAndy Fiddaman  * Mutexes on FreeBSD are error-checking by default. Wrap pthread_mutex_*()
24*7bb0eb34SAndy Fiddaman  * to deliver the same, and check for errors.
25*7bb0eb34SAndy Fiddaman  */
26*7bb0eb34SAndy Fiddaman 
27*7bb0eb34SAndy Fiddaman #undef PTHREAD_MUTEX_INITIALIZER
29*7bb0eb34SAndy Fiddaman 
30*7bb0eb34SAndy Fiddaman static __inline int
checked_pthread_mutex_init(pthread_mutex_t * restrict mutex,const pthread_mutexattr_t * restrict cattr)31*7bb0eb34SAndy Fiddaman checked_pthread_mutex_init(pthread_mutex_t *restrict mutex,
32*7bb0eb34SAndy Fiddaman     const pthread_mutexattr_t *restrict cattr)
33*7bb0eb34SAndy Fiddaman {
34*7bb0eb34SAndy Fiddaman 	if (cattr != NULL) {
35*7bb0eb34SAndy Fiddaman 		VERIFY0(pthread_mutex_init(mutex, cattr));
36*7bb0eb34SAndy Fiddaman 	} else {
37*7bb0eb34SAndy Fiddaman 		pthread_mutexattr_t attr = { 0 };
38*7bb0eb34SAndy Fiddaman 
39*7bb0eb34SAndy Fiddaman 		VERIFY0(pthread_mutexattr_init(&attr));
40*7bb0eb34SAndy Fiddaman 		VERIFY0(pthread_mutexattr_settype(&attr,
41*7bb0eb34SAndy Fiddaman 		    PTHREAD_MUTEX_ERRORCHECK));
42*7bb0eb34SAndy Fiddaman 		VERIFY0(pthread_mutex_init(mutex, &attr));
43*7bb0eb34SAndy Fiddaman 		VERIFY0(pthread_mutexattr_destroy(&attr));
44*7bb0eb34SAndy Fiddaman 	}
45*7bb0eb34SAndy Fiddaman 
46*7bb0eb34SAndy Fiddaman 	return (0);
47*7bb0eb34SAndy Fiddaman }
48*7bb0eb34SAndy Fiddaman 
49*7bb0eb34SAndy Fiddaman static __inline int
checked_pthread_mutex_destroy(pthread_mutex_t * mutex)50*7bb0eb34SAndy Fiddaman checked_pthread_mutex_destroy(pthread_mutex_t *mutex)
51*7bb0eb34SAndy Fiddaman {
52*7bb0eb34SAndy Fiddaman 	VERIFY0(pthread_mutex_destroy(mutex));
53*7bb0eb34SAndy Fiddaman 	return (0);
54*7bb0eb34SAndy Fiddaman }
55*7bb0eb34SAndy Fiddaman 
56*7bb0eb34SAndy Fiddaman #define	pthread_mutex_init(m, a)	checked_pthread_mutex_init(m, a)
57*7bb0eb34SAndy Fiddaman #define	pthread_mutex_destroy(m)	checked_pthread_mutex_destroy(m)
58*7bb0eb34SAndy Fiddaman #define	pthread_mutex_lock(m)		pthread_mutex_enter_np(m)
59*7bb0eb34SAndy Fiddaman #define	pthread_mutex_unlock(m)		pthread_mutex_exit_np(m)
60*7bb0eb34SAndy Fiddaman 
61*7bb0eb34SAndy Fiddaman #endif	/* _COMPAT_FREEBSD_PTHREAD_H_ */