C Standard Library Extensions  1.1
cxthread.h
1 /* $Id$
2  *
3  * This file is part of the ESO C Extension Library
4  * Copyright (C) 2001-2011 European Southern Observatory
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 /*
22  * $Author$
23  * $Date$
24  * $Revision$
25  * $Name$
26  */
27 
28 #ifndef CXTHREAD_H_
29 #define CXTHREAD_H_
30 
31 #if HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34 
35 #ifdef HAVE_PTHREAD_H
36 # include <pthread.h>
37 #endif
38 
39 #include <cxtypes.h>
40 
41 
42 /*
43  * Map local types and functions to the POSIX thread model implementation
44  */
45 
46 #if defined(CX_THREADS_ENABLED)
47 
48 #if defined(HAVE_PTHREAD_H)
49 
50 #define CX_STATIC_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
51 #define CX_STATIC_ONCE_INIT PTHREAD_ONCE_INIT
52 
53 typedef pthread_mutex_t cx_mutex;
54 typedef pthread_once_t cx_once;
55 typedef pthread_key_t cx_private;
56 
57 #define cx_mutex_lock(mutex) pthread_mutex_lock((mutex))
58 #define cx_mutex_trylock(mutex) pthread_mutex_trylock((mutex))
59 #define cx_mutex_unlock(mutex) pthread_mutex_unlock((mutex))
60 
61 #define cx_thread_once(name, func, args) pthread_once(&(name), (func))
62 
63 #define cx_private_init(name, func) pthread_key_create(&(name), (func))
64 #define cx_private_set(name, data) pthread_setspecific((name), (data))
65 #define cx_private_get(name) pthread_getspecific((name))
66 
67 #else /* !HAVE_PTHREAD_H */
68 # error "Thread support is requested, but POSIX thread model is not present!"
69 #endif /* !HAVE_PTHREAD_H */
70 
71 #else /* !CX_THREADS_ENABLED */
72 
73 typedef struct cx_private cx_private;
74 
75 #define cx_mutex_lock(mutex) /* empty */
76 #define cx_mutex_trylock(mutex) /* empty */
77 #define cx_mutex_unlock(mutex) /* empty */
78 
79 #define cx_thread_once(name, func, args) (func)()
80 
81 #define cx_private_init(name, func) /* empty */
82 #define cx_private_set(name, data) ((name) = (data))
83 #define cx_private_get(name) (name)
84 
85 #endif /* !CX_THREADS_ENABLED */
86 
87 
88 /*
89  * Convenience macros to setup locks for global variables.
90  * These macros expand to nothing, if thread support was not enabled.
91  */
92 
93 #define CX_LOCK_NAME(name) _cx__ ## name ## _lock
94 
95 #if defined(CX_THREADS_ENABLED)
96 
97 # define CX_LOCK_DEFINE_STATIC(name) static CX_LOCK_DEFINE(name)
98 # define CX_LOCK_DEFINE(name) cx_mutex CX_LOCK_NAME(name) = CX_STATIC_MUTEX_INIT
99 # define CX_LOCK_EXTERN(name) extern cx_mutex CX_LOCK_NAME(name)
100 
101 # define CX_LOCK(name) cx_mutex_lock(&CX_LOCK_NAME(name))
102 # define CX_TRYLOCK(name) cx_mutex_trylock(&CX_LOCK_NAME(name))
103 # define CX_UNLOCK(name) cx_mutex_unlock(&CX_LOCK_NAME(name))
104 
105 #else /* !CX_THREADS_ENABLED */
106 
107 # define CX_LOCK_DEFINE_STATIC(name) /* empty */
108 # define CX_LOCK_DEFINE(name) /* empty */
109 # define CX_LOCK_EXTERN(name) /* empty */
110 
111 # define CX_LOCK(name) /* empty */
112 # define CX_TRYLOCK(name) (TRUE)
113 # define CX_UNLOCK(name) /* empty */
114 
115 #endif /* !CX_THREADS_ENABLED */
116 
117 
118 /*
119  * Convenience macros for setting up mutexes for one time initalizations
120  */
121 
122 #if defined(CX_THREADS_ENABLED)
123 
124 # define CX_ONCE_DEFINE_STATIC(name) static CX_ONCE_DEFINE(name)
125 # define CX_ONCE_DEFINE(name) cx_once (name) = CX_STATIC_ONCE_INIT
126 
127 #else /* !CX_THREADS_ENABLED */
128 
129 # define CX_ONCE_DEFINE_STATIC(name) /* empty */
130 # define CX_ONCE_DEFINE(name) /* empty */
131 
132 #endif /* !CX_THREADS_ENABLED */
133 
134 
135 /*
136  * Convenience macros for setting up thread-specific data
137  */
138 
139 #if defined(CX_THREADS_ENABLED)
140 
141 # define CX_PRIVATE_DEFINE_STATIC(name) cx_private (name)
142 
143 #else /* !CX_THREADS_ENABLED */
144 
145 # define CX_PRIVATE_DEFINE_STATIC(name) static cx_private *(name)
146 
147 #endif /* !CX_THREADS_ENABLED */
148 
149 #endif /* CXTHREAD_H_ */