summaryrefslogtreecommitdiff
path: root/src/mpfr-thread.h
blob: 5ded3908ec98c5af0b0b7a592b6babf3a6007ca6 (plain)
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
/* MPFR internal header related to thread-local variables.

Copyright 2005-2021 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
https://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 completely tested */

#define MPFR_THREAD_LOCK_METHOD "C11"

#include <threads.h>

#define MPFR_LOCK_DECL(_lock)                   \
  mtx_t _lock;

#define MPFR_LOCK_C(E)                          \
  do {                                          \
    if ((E) != thrd_success)                    \
      abort ();                                 \
  } while (0)

#define MPFR_LOCK_INIT(_lock)    MPFR_LOCK_C(mtx_init(&(_lock), mtx_plain))
#define MPFR_LOCK_CLEAR(_lock)   do { mtx_destroy(&(_lock)); } while (0)
#define MPFR_LOCK_READ(_lock)    MPFR_LOCK_C(mtx_lock(&(_lock)))
#define MPFR_UNLOCK_READ(_lock)  MPFR_LOCK_C(mtx_unlock(&(_lock)))
#define MPFR_LOCK_WRITE(_lock)   MPFR_LOCK_C(mtx_lock(&(_lock)))
#define MPFR_UNLOCK_WRITE(_lock) MPFR_LOCK_C(mtx_unlock(&(_lock)))

#define MPFR_LOCK_READ2WRITE(_lock) do {} while (0)
#define MPFR_LOCK_WRITE2READ(_lock) do {} while (0)

#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)

#define MPFR_THREAD_LOCK_METHOD "pthread"

#include <pthread.h>

#define MPFR_LOCK_DECL(_lock)                   \
  pthread_rwlock_t _lock;

#define MPFR_LOCK_C(E)                          \
  do {                                          \
    if ((E) != 0)                               \
      abort ();                                 \
  } while (0)

#define MPFR_LOCK_INIT(_lock) MPFR_LOCK_C(pthread_rwlock_init(&(_lock), NULL))

#define MPFR_LOCK_CLEAR(_lock) do {                     \
    pthread_rwlock_destroy(&(_lock));                   \
  } while (0)

#define MPFR_LOCK_READ(_lock)    MPFR_LOCK_C(pthread_rwlock_rdlock(&(_lock)))
#define MPFR_UNLOCK_READ(_lock)  MPFR_LOCK_C(pthread_rwlock_unlock(&(_lock)))
#define MPFR_LOCK_WRITE(_lock)   MPFR_LOCK_C(pthread_rwlock_wrlock(&(_lock)))
#define MPFR_UNLOCK_WRITE(_lock) MPFR_LOCK_C(pthread_rwlock_unlock(&(_lock)))

#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) \
  MPFR_LOCK_C(pthread_once (&(_once), (_func)))

#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