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