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