1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
|
/* MPFR internal header related to thread-local variables.
Copyright 2005-2017 Free Software Foundation, Inc.
Contributed by the AriC and Caramba projects, INRIA.
This file is part of the GNU MPFR Library.
The GNU MPFR Library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 3 of the License, or (at your
option) any later version.
The GNU MPFR Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with the GNU MPFR Library; see the file COPYING.LESSER. If not, see
http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
#ifndef __MPFR_THREAD_H__
#define __MPFR_THREAD_H__
/* Note: MPFR_NEED_THREAD_LOCK shall be defined before including this
header */
/* Note: Let's define MPFR_THREAD_ATTR even after a #error to make the
error message more visible (e.g. gcc doesn't immediately stop after
the #error line and outputs many error messages if MPFR_THREAD_ATTR
is not defined). But some compilers will just output a message and
may build MPFR "successfully" (without thread support). */
#ifndef MPFR_THREAD_ATTR
# ifdef MPFR_USE_THREAD_SAFE
# if defined(_MSC_VER)
# define MPFR_THREAD_ATTR __declspec( thread )
# elif defined(MPFR_USE_C11_THREAD_SAFE)
# define MPFR_THREAD_ATTR _Thread_local
# else
# define MPFR_THREAD_ATTR __thread
# endif
# else
# define MPFR_THREAD_ATTR
# endif
#endif
/* If MPFR needs a lock mechanism for thread synchro */
#ifdef MPFR_NEED_THREAD_LOCK
/**************************************************************************/
/**************************************************************************/
/* ISO C11 version */
/**************************************************************************/
/**************************************************************************/
#if defined (MPFR_HAVE_C11_LOCK)
/* NOTE: This version has not been completly tested */
#include <thread.h>
#define MPFR_LOCK_DECL(_lock) \
mtx_t _lock;
#define MPFR_LOCK_INIT(_lock) do { \
int error_code = mtx_init(&(_lock), mtx_plain); \
MPFR_ASSERTD( error_code == thrd_success); \
} while (0)
#define MPFR_LOCK_CLEAR(_lock) do { \
mtx_destroy(&(_lock)); \
} while (0)
#define MPFR_LOCK_READ(_lock) do { \
int error_code = mtx_lock(&(_lock)); \
MPFR_ASSERTD(error_code == thrd_success); \
} while (0)
#define MPFR_UNLOCK_READ(_lock) do { \
int error_code = mtx_unlock(&(_lock)); \
MPFR_ASSERTD(error_code == thrd_success); \
} while (0)
#define MPFR_LOCK_WRITE(_lock) do { \
int error_code = mtx_lock(&(_lock)); \
MPFR_ASSERTD(error_code == thrd_success); \
} while (0)
#define MPFR_UNLOCK_WRITE(_lock) do { \
int error_code = mtx_unlock(&(_lock)); \
MPFR_ASSERTD(error_code == thrd_success); \
} while (0)
#define MPFR_LOCK_READ2WRITE(_lock)
#define MPFR_LOCK_WRITE2READ(_lock)
#define MPFR_ONCE_DECL(_once) \
once_flag _once;
#define MPFR_ONCE_INIT_VALUE ONCE_FLAG_INIT
#define MPFR_ONCE_CALL(_once, _func) do { \
call_once(&(_once), (_func)); \
} while (0)
#define MPFR_NEED_DEFERRED_INIT 1
/**************************************************************************/
/**************************************************************************/
/* POSIX version */
/**************************************************************************/
/**************************************************************************/
#elif defined (HAVE_PTHREAD)
# include <pthread.h>
#define MPFR_LOCK_DECL(_lock) \
pthread_rwlock_t _lock;
#define MPFR_LOCK_INIT(_lock) do { \
int error_code = pthread_rwlock_init(&(_lock), NULL); \
MPFR_ASSERTD( error_code == 0); \
} while (0)
#define MPFR_LOCK_CLEAR(_lock) do { \
pthread_rwlock_destroy(&(_lock)); \
} while (0)
#define MPFR_LOCK_READ(_lock) do { \
int error_code = pthread_rwlock_rdlock(&(_lock)); \
MPFR_ASSERTD(error_code == 0); \
} while (0)
#define MPFR_UNLOCK_READ(_lock) do { \
int error_code = pthread_rwlock_unlock(&(_lock)); \
MPFR_ASSERTD(error_code == 0); \
} while (0)
#define MPFR_LOCK_WRITE(_lock) do { \
int error_code = pthread_rwlock_wrlock(&(_lock)); \
MPFR_ASSERTD(error_code == 0); \
} while (0)
#define MPFR_UNLOCK_WRITE(_lock) do { \
int error_code = pthread_rwlock_unlock(&(_lock)); \
MPFR_ASSERTD(error_code == 0); \
} while (0)
#define MPFR_LOCK_READ2WRITE(_lock) do { \
MPFR_UNLOCK_READ(_lock); \
MPFR_LOCK_WRITE(_lock); \
} while (0)
#define MPFR_LOCK_WRITE2READ(_lock) do { \
MPFR_UNLOCK_WRITE(_lock); \
MPFR_LOCK_READ(_lock); \
} while (0)
#define MPFR_ONCE_DECL(_once) \
pthread_once_t _once ;
#define MPFR_ONCE_INIT_VALUE PTHREAD_ONCE_INIT
#define MPFR_ONCE_CALL(_once, _func) do { \
int error_code = pthread_once (&(_once), (_func)); \
MPFR_ASSERTD (error_code == 0); \
} while (0)
#define MPFR_NEED_DEFERRED_INIT 1
#else
/* TODO: Win32 */
# error "No thread lock / unsupported OS."
#endif
#else
/**************************************************************************/
/**************************************************************************/
/* No lock thread version */
/**************************************************************************/
/**************************************************************************/
#define MPFR_LOCK_DECL(_lock)
#define MPFR_LOCK_INIT(_lock) do {} while (0)
#define MPFR_LOCK_CLEAR(_lock) do {} while (0)
#define MPFR_LOCK_READ(_lock) do {} while (0)
#define MPFR_UNLOCK_READ(_lock) do {} while (0)
#define MPFR_LOCK_WRITE(_lock) do {} while (0)
#define MPFR_UNLOCK_WRITE(_lock) do {} while (0)
#define MPFR_LOCK_READ2WRITE(_lock) do {} while (0)
#define MPFR_LOCK_WRITE2READ(_lock) do {} while (0)
#define MPFR_ONCE_INIT_VALUE
#define MPFR_ONCE_DECL(_once)
#define MPFR_ONCE_CALL(_once,_func) do {} while (0)
#endif
/**************************************************************************/
/**************************************************************************/
/**************************************************************************/
/**************************************************************************/
/* If MPFR Needs a way to init data before using them */
#if defined(MPFR_NEED_DEFERRED_INIT)
#if defined(MPFR_HAVE_CONSTRUCTOR_ATTR)
/*********************** Use constructor extension ************************/
#define MPFR_DEFERRED_INIT_MASTER_DECL(_id, _init, _clear) \
__attribute__ ((constructor)) static void \
mpfr_init_cache_ ## _id (void) { \
_init ; \
} \
__attribute__ ((destructor)) static void \
mpfr_clean_cache_ ## _id (void) { \
_clear; \
}
#define MPFR_DEFERRED_INIT_CALL(_master) do {} while (0)
#define MPFR_DEFERRED_INIT_SLAVE_DECL()
#define MPFR_DEFERRED_INIT_SLAVE_VALUE(_id)
#else
/**************************** Use once semantic ***************************/
#define MPFR_DEFERRED_INIT_MASTER_DECL(_id, _init, _clear) \
static void mpfr_once_ ## _id ## _clear_func (void) { \
_clear ; \
} \
static void mpfr_once_ ## _id ## _init_func (void) { \
_init; \
atexit(mpfr_once_ ## _id ## _clear_func); \
}
#define MPFR_DEFERRED_INIT_CALL(_master) \
MPFR_ONCE_CALL((_master)->once, (_master)->init_once)
#define MPFR_DEFERRED_INIT_SLAVE_DECL() \
MPFR_ONCE_DECL(once) \
void (*init_once)(void);
#define MPFR_DEFERRED_INIT_SLAVE_VALUE(_id) \
, MPFR_ONCE_INIT_VALUE, mpfr_once_ ## _id ## _init_func
#endif
#else
/* No need */
#define MPFR_DEFERRED_INIT_MASTER_DECL(_id_lock, _init, _clear)
#define MPFR_DEFERRED_INIT_CALL(_master) do {} while (0)
#define MPFR_DEFERRED_INIT_SLAVE_DECL()
#define MPFR_DEFERRED_INIT_SLAVE_VALUE(_id)
#endif
/**************************************************************************/
#endif
|