• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* $OpenBSD: thread_private.h,v 1.18 2006/02/22 07:16:31 otto Exp $ */
2 
3 /* PUBLIC DOMAIN: No Rights Reserved. Marco S Hyman <marc@snafu.org> */
4 
5 #ifndef _THREAD_PRIVATE_H_
6 #define _THREAD_PRIVATE_H_
7 
8 #include <pthread.h>
9 
10 /*
11  * This file defines the thread library interface to libc.  Thread
12  * libraries must implement the functions described here for proper
13  * inter-operation with libc.   libc contains weak versions of the
14  * described functions for operation in a non-threaded environment.
15  */
16 
17 /*
18  * This variable is 0 until a second thread is created.
19  */
20 extern int __isthreaded;
21 
22 /*
23  * helper macro to make unique names in the thread namespace
24  */
25 #define __THREAD_NAME(name)	__CONCAT(_thread_tagname_,name)
26 
27 struct __thread_private_tag_t {
28     pthread_mutex_t    _private_lock;
29     pthread_key_t      _private_key;
30 };
31 
32 #define _THREAD_PRIVATE_MUTEX(name)  \
33 	static struct __thread_private_tag_t  __THREAD_NAME(name) = { PTHREAD_MUTEX_INITIALIZER, -1 }
34 #define _THREAD_PRIVATE_MUTEX_LOCK(name)  \
35 	pthread_mutex_lock( &__THREAD_NAME(name)._private_lock )
36 #define _THREAD_PRIVATE_MUTEX_UNLOCK(name) \
37 	pthread_mutex_unlock( &__THREAD_NAME(name)._private_lock )
38 
39 void	_thread_atexit_lock(void);
40 void	_thread_atexit_unlock(void);
41 
42 #define _ATEXIT_LOCK()		do {					\
43 					if (__isthreaded)		\
44 						_thread_atexit_lock();	\
45 				} while (0)
46 #define _ATEXIT_UNLOCK()	do {					\
47 					if (__isthreaded)		\
48 						_thread_atexit_unlock();\
49 				} while (0)
50 
51 #endif /* _THREAD_PRIVATE_H_ */
52