1 /* Copyright (c) 2000, 2014, Oracle and/or its affiliates.
2 Copyright (c) 2009, 2020, MariaDB Corporation.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; version 2 of the License.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA */
16
17 /* Defines to make different thread packages compatible */
18
19 #ifndef _my_pthread_h
20 #define _my_pthread_h
21
22 #ifndef ETIME
23 #define ETIME ETIMEDOUT /* For FreeBSD */
24 #endif
25
26 #include <my_atomic.h>
27
28 #ifdef __cplusplus
29 #define EXTERNC extern "C"
30 extern "C" {
31 #else
32 #define EXTERNC
33 #endif /* __cplusplus */
34
35 #if defined(__WIN__)
36 typedef CRITICAL_SECTION pthread_mutex_t;
37 typedef DWORD pthread_t;
38 typedef struct thread_attr {
39 DWORD dwStackSize ;
40 DWORD dwCreatingFlag ;
41 } pthread_attr_t ;
42
43 typedef struct { int dummy; } pthread_condattr_t;
44
45 /* Implementation of posix conditions */
46
47 typedef struct st_pthread_link {
48 DWORD thread_id;
49 struct st_pthread_link *next;
50 } pthread_link;
51
52 /**
53 Implementation of Windows condition variables.
54 We use native conditions on Vista and later, and fallback to own
55 implementation on earlier OS version.
56 */
57 typedef CONDITION_VARIABLE pthread_cond_t;
58
59
60 typedef int pthread_mutexattr_t;
61 #define pthread_self() GetCurrentThreadId()
62 #define pthread_handler_t EXTERNC void * __cdecl
63 typedef void * (__cdecl *pthread_handler)(void *);
64
65 typedef INIT_ONCE my_pthread_once_t;
66 #define MY_PTHREAD_ONCE_INIT INIT_ONCE_STATIC_INIT;
67
68 #if !STRUCT_TIMESPEC_HAS_TV_SEC || !STRUCT_TIMESPEC_HAS_TV_NSEC
69 struct timespec {
70 time_t tv_sec;
71 long tv_nsec;
72 };
73 #endif
74
75 int win_pthread_mutex_trylock(pthread_mutex_t *mutex);
76 int pthread_create(pthread_t *, const pthread_attr_t *, pthread_handler, void *);
77 int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr);
78 int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
79 int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
80 const struct timespec *abstime);
81 int pthread_cond_signal(pthread_cond_t *cond);
82 int pthread_cond_broadcast(pthread_cond_t *cond);
83 int pthread_cond_destroy(pthread_cond_t *cond);
84 int pthread_attr_init(pthread_attr_t *connect_att);
85 int pthread_attr_setstacksize(pthread_attr_t *connect_att,size_t stack);
86 int pthread_attr_destroy(pthread_attr_t *connect_att);
87 int my_pthread_once(my_pthread_once_t *once_control,void (*init_routine)(void));
88
localtime_r(const time_t * timep,struct tm * tmp)89 static inline struct tm *localtime_r(const time_t *timep, struct tm *tmp)
90 {
91 localtime_s(tmp, timep);
92 return tmp;
93 }
94
gmtime_r(const time_t * clock,struct tm * res)95 static inline struct tm *gmtime_r(const time_t *clock, struct tm *res)
96 {
97 gmtime_s(res, clock);
98 return res;
99 }
100
101 void pthread_exit(void *a);
102 int pthread_join(pthread_t thread, void **value_ptr);
103 int pthread_cancel(pthread_t thread);
104
105 #ifndef ETIMEDOUT
106 #define ETIMEDOUT 145 /* Win32 doesn't have this */
107 #endif
108
109 #define getpid() GetCurrentThreadId()
110 #define HAVE_LOCALTIME_R 1
111 #define _REENTRANT 1
112 #define HAVE_PTHREAD_ATTR_SETSTACKSIZE 1
113
114 #undef SAFE_MUTEX /* This will cause conflicts */
115 #define pthread_key(T,V) DWORD V
116 #define pthread_key_create(A,B) ((*A=TlsAlloc())==0xFFFFFFFF)
117 #define pthread_key_delete(A) TlsFree(A)
118 #define my_pthread_setspecific_ptr(T,V) (!TlsSetValue((T),(V)))
119 #define pthread_setspecific(A,B) (!TlsSetValue((A),(B)))
120 #define pthread_getspecific(A) (TlsGetValue(A))
121 #define my_pthread_getspecific(T,A) ((T) TlsGetValue(A))
122 #define my_pthread_getspecific_ptr(T,V) ((T) TlsGetValue(V))
123
124 #define pthread_equal(A,B) ((A) == (B))
125 #define pthread_mutex_init(A,B) (InitializeCriticalSection(A),0)
126 #define pthread_mutex_lock(A) (EnterCriticalSection(A),0)
127 #define pthread_mutex_trylock(A) win_pthread_mutex_trylock((A))
128 #define pthread_mutex_unlock(A) (LeaveCriticalSection(A), 0)
129 #define pthread_mutex_destroy(A) (DeleteCriticalSection(A), 0)
130 #define pthread_kill(A,B) pthread_dummy((A) ? 0 : ESRCH)
131
132
133 /* Dummy defines for easier code */
134 #define pthread_attr_setdetachstate(A,B) pthread_dummy(0)
135 #define pthread_attr_setscope(A,B)
136 #define pthread_detach_this_thread()
137 #define pthread_condattr_init(A)
138 #define pthread_condattr_destroy(A)
139 #define pthread_yield() SwitchToThread()
140 #define my_sigset(A,B) signal(A,B)
141
142 #else /* Normal threads */
143
144 #ifdef HAVE_rts_threads
145 #define sigwait org_sigwait
146 #include <signal.h>
147 #undef sigwait
148 #endif
149 #include <pthread.h>
150 #ifndef _REENTRANT
151 #define _REENTRANT
152 #endif
153 #ifdef HAVE_THR_SETCONCURRENCY
154 #include <thread.h> /* Probably solaris */
155 #endif
156 #ifdef HAVE_SCHED_H
157 #include <sched.h>
158 #endif
159 #ifdef HAVE_SYNCH_H
160 #include <synch.h>
161 #endif
162
163 #define pthread_key(T,V) pthread_key_t V
164 #define my_pthread_getspecific_ptr(T,V) my_pthread_getspecific(T,(V))
165 #define my_pthread_setspecific_ptr(T,V) pthread_setspecific(T,(void*) (V))
166 #define pthread_detach_this_thread()
167 #define pthread_handler_t EXTERNC void *
168 typedef void *(* pthread_handler)(void *);
169
170 #define my_pthread_once_t pthread_once_t
171 #if defined(PTHREAD_ONCE_INITIALIZER)
172 #define MY_PTHREAD_ONCE_INIT PTHREAD_ONCE_INITIALIZER
173 #else
174 #define MY_PTHREAD_ONCE_INIT PTHREAD_ONCE_INIT
175 #endif
176 #define my_pthread_once(C,F) pthread_once(C,F)
177
178 /* Test first for RTS or FSU threads */
179
180 #if defined(PTHREAD_SCOPE_GLOBAL) && !defined(PTHREAD_SCOPE_SYSTEM)
181 #define HAVE_rts_threads
182 extern int my_pthread_create_detached;
183 #define pthread_sigmask(A,B,C) sigprocmask((A),(B),(C))
184 #define PTHREAD_CREATE_DETACHED &my_pthread_create_detached
185 #define PTHREAD_SCOPE_SYSTEM PTHREAD_SCOPE_GLOBAL
186 #define PTHREAD_SCOPE_PROCESS PTHREAD_SCOPE_LOCAL
187 #define USE_ALARM_THREAD
188 #endif /* defined(PTHREAD_SCOPE_GLOBAL) && !defined(PTHREAD_SCOPE_SYSTEM) */
189
190 #if defined(_BSDI_VERSION) && _BSDI_VERSION < 199910
191 int sigwait(sigset_t *set, int *sig);
192 #endif
193
194 #define my_sigwait(A,B) sigwait((A),(B))
195
196 #if defined(HAVE_SIGTHREADMASK) && !defined(HAVE_PTHREAD_SIGMASK)
197 #define pthread_sigmask(A,B,C) sigthreadmask((A),(B),(C))
198 #endif
199
200 #if !defined(HAVE_SIGWAIT) && !defined(HAVE_rts_threads) && !defined(sigwait) && !defined(alpha_linux_port) && !defined(_AIX)
201 int sigwait(sigset_t *setp, int *sigp); /* Use our implementation */
202 #endif
203
204
205 /*
206 We define my_sigset() and use that instead of the system sigset() so that
207 we can favor an implementation based on sigaction(). On some systems, such
208 as Mac OS X, sigset() results in flags such as SA_RESTART being set, and
209 we want to make sure that no such flags are set.
210 */
211 #if defined(HAVE_SIGACTION) && !defined(my_sigset)
212 #define my_sigset(A,B) do { struct sigaction l_s; sigset_t l_set; \
213 DBUG_ASSERT((A) != 0); \
214 sigemptyset(&l_set); \
215 l_s.sa_handler = (B); \
216 l_s.sa_mask = l_set; \
217 l_s.sa_flags = 0; \
218 sigaction((A), &l_s, NULL); \
219 } while (0)
220 #elif defined(HAVE_SIGSET) && !defined(my_sigset)
221 #define my_sigset(A,B) sigset((A),(B))
222 #elif !defined(my_sigset)
223 #define my_sigset(A,B) signal((A),(B))
224 #endif
225
226 #if !defined(HAVE_PTHREAD_ATTR_SETSCOPE)
227 #define pthread_attr_setscope(A,B)
228 #undef HAVE_GETHOSTBYADDR_R /* No definition */
229 #endif
230
231 #define my_pthread_getspecific(A,B) ((A) pthread_getspecific(B))
232
233 #ifndef HAVE_LOCALTIME_R
234 struct tm *localtime_r(const time_t *clock, struct tm *res);
235 #endif
236
237 #ifndef HAVE_GMTIME_R
238 struct tm *gmtime_r(const time_t *clock, struct tm *res);
239 #endif
240
241 #ifdef HAVE_PTHREAD_CONDATTR_CREATE
242 /* DCE threads on HPUX 10.20 */
243 #define pthread_condattr_init pthread_condattr_create
244 #define pthread_condattr_destroy pthread_condattr_delete
245 #endif
246
247 /* FSU THREADS */
248 #if !defined(HAVE_PTHREAD_KEY_DELETE) && !defined(pthread_key_delete)
249 #define pthread_key_delete(A) pthread_dummy(0)
250 #endif
251
252 #if defined(HAVE_PTHREAD_ATTR_CREATE) && !defined(HAVE_SIGWAIT)
253 /* This is set on AIX_3_2 and Siemens unix (and DEC OSF/1 3.2 too) */
254 #define pthread_key_create(A,B) \
255 pthread_keycreate(A,(B) ?\
256 (pthread_destructor_t) (B) :\
257 (pthread_destructor_t) pthread_dummy)
258 #define pthread_attr_init(A) pthread_attr_create(A)
259 #define pthread_attr_destroy(A) pthread_attr_delete(A)
260 #define pthread_attr_setdetachstate(A,B) pthread_dummy(0)
261 #define pthread_create(A,B,C,D) pthread_create((A),*(B),(C),(D))
262 #ifndef pthread_sigmask
263 #define pthread_sigmask(A,B,C) sigprocmask((A),(B),(C))
264 #endif
265 #define pthread_kill(A,B) pthread_dummy((A) ? 0 : ESRCH)
266 #undef pthread_detach_this_thread
267 #define pthread_detach_this_thread() { pthread_t tmp=pthread_self() ; pthread_detach(&tmp); }
268 #else /* HAVE_PTHREAD_ATTR_CREATE && !HAVE_SIGWAIT */
269 #define HAVE_PTHREAD_KILL 1
270 #endif
271
272 #endif /* defined(__WIN__) */
273
274 #if defined(HPUX10) && !defined(DONT_REMAP_PTHREAD_FUNCTIONS)
275 #undef pthread_cond_timedwait
276 #define pthread_cond_timedwait(a,b,c) my_pthread_cond_timedwait((a),(b),(c))
277 int my_pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
278 struct timespec *abstime);
279 #endif
280
281 #if defined(HPUX10)
282 #define pthread_attr_getstacksize(A,B) my_pthread_attr_getstacksize(A,B)
283 void my_pthread_attr_getstacksize(pthread_attr_t *attrib, size_t *size);
284 #endif
285
286 #if defined(HAVE_POSIX1003_4a_MUTEX) && !defined(DONT_REMAP_PTHREAD_FUNCTIONS)
287 #undef pthread_mutex_trylock
288 #define pthread_mutex_trylock(a) my_pthread_mutex_trylock((a))
289 int my_pthread_mutex_trylock(pthread_mutex_t *mutex);
290 #endif
291
292 #ifdef HAVE_SCHED_YIELD
293 #define pthread_yield() sched_yield()
294 #else
295 #if !defined(HAVE_PTHREAD_YIELD_ZERO_ARG)
296 /* no pthread_yield() available */
297 #if defined(HAVE_PTHREAD_YIELD_NP) /* can be Mac OS X */
298 #define pthread_yield() pthread_yield_np()
299 #elif defined(HAVE_THR_YIELD)
300 #define pthread_yield() thr_yield()
301 #endif //defined(HAVE_PTHREAD_YIELD_NP)
302 #endif //!defined(HAVE_PTHREAD_YIELD_ZERO_ARG)
303 #endif //HAVE_SCHED_YIELD
304
305 /*
306 The defines set_timespec and set_timespec_nsec should be used
307 for calculating an absolute time at which
308 pthread_cond_timedwait should timeout
309 */
310 #define set_timespec(ABSTIME,SEC) set_timespec_nsec((ABSTIME),(SEC)*1000000000ULL)
311
312 #ifndef set_timespec_nsec
313 #define set_timespec_nsec(ABSTIME,NSEC) \
314 set_timespec_time_nsec((ABSTIME), my_hrtime_coarse().val*1000 + (NSEC))
315 #endif /* !set_timespec_nsec */
316
317 /* adapt for two different flavors of struct timespec */
318 #ifdef HAVE_TIMESPEC_TS_SEC
319 #define MY_tv_sec ts_sec
320 #define MY_tv_nsec ts_nsec
321 #else
322 #define MY_tv_sec tv_sec
323 #define MY_tv_nsec tv_nsec
324 #endif /* HAVE_TIMESPEC_TS_SEC */
325
326 /**
327 Compare two timespec structs.
328
329 @retval 1 If TS1 ends after TS2.
330
331 @retval 0 If TS1 is equal to TS2.
332
333 @retval -1 If TS1 ends before TS2.
334 */
335 #ifndef cmp_timespec
336 #define cmp_timespec(TS1, TS2) \
337 ((TS1.MY_tv_sec > TS2.MY_tv_sec || \
338 (TS1.MY_tv_sec == TS2.MY_tv_sec && TS1.MY_tv_nsec > TS2.MY_tv_nsec)) ? 1 : \
339 ((TS1.MY_tv_sec < TS2.MY_tv_sec || \
340 (TS1.MY_tv_sec == TS2.MY_tv_sec && TS1.MY_tv_nsec < TS2.MY_tv_nsec)) ? -1 : 0))
341 #endif /* !cmp_timespec */
342
343 #ifndef set_timespec_time_nsec
344 #define set_timespec_time_nsec(ABSTIME,NSEC) do { \
345 ulonglong _now_= (NSEC); \
346 (ABSTIME).MY_tv_sec= (_now_ / 1000000000ULL); \
347 (ABSTIME).MY_tv_nsec= (_now_ % 1000000000ULL); \
348 } while(0)
349 #endif /* !set_timespec_time_nsec */
350
351 #ifdef MYSQL_CLIENT
352 #define _current_thd() NULL
353 #elif defined(_WIN32)
354 #ifdef __cplusplus
355 extern "C"
356 #endif
357 MYSQL_THD _current_thd_noinline();
358 #define _current_thd() _current_thd_noinline()
359 #else
360 /*
361 THR_THD is a key which will be used to set/get THD* for a thread,
362 using my_pthread_setspecific_ptr()/my_thread_getspecific_ptr().
363 */
364 extern pthread_key(MYSQL_THD, THR_THD);
_current_thd(void)365 static inline MYSQL_THD _current_thd(void)
366 {
367 return my_pthread_getspecific_ptr(MYSQL_THD,THR_THD);
368 }
369 #endif
370
371 /* safe_mutex adds checking to mutex for easier debugging */
372 struct st_hash;
373 typedef struct st_safe_mutex_t
374 {
375 pthread_mutex_t global,mutex;
376 const char *file, *name;
377 uint line,count;
378 myf create_flags, active_flags;
379 ulong id;
380 pthread_t thread;
381 struct st_hash *locked_mutex, *used_mutex;
382 struct st_safe_mutex_t *prev, *next;
383 #ifdef SAFE_MUTEX_DETECT_DESTROY
384 struct st_safe_mutex_info_t *info; /* to track destroying of mutexes */
385 #endif
386 } safe_mutex_t;
387
388 typedef struct st_safe_mutex_deadlock_t
389 {
390 const char *file, *name;
391 safe_mutex_t *mutex;
392 uint line;
393 ulong count;
394 ulong id;
395 my_bool warning_only;
396 } safe_mutex_deadlock_t;
397
398 #ifdef SAFE_MUTEX_DETECT_DESTROY
399 /*
400 Used to track the destroying of mutexes. This needs to be a separate
401 structure because the safe_mutex_t structure could be freed before
402 the mutexes are destroyed.
403 */
404
405 typedef struct st_safe_mutex_info_t
406 {
407 struct st_safe_mutex_info_t *next;
408 struct st_safe_mutex_info_t *prev;
409 const char *init_file;
410 uint32 init_line;
411 } safe_mutex_info_t;
412 #endif /* SAFE_MUTEX_DETECT_DESTROY */
413
414 int safe_mutex_init(safe_mutex_t *mp, const pthread_mutexattr_t *attr,
415 const char *name, const char *file, uint line);
416 int safe_mutex_lock(safe_mutex_t *mp, myf my_flags, const char *file,
417 uint line);
418 int safe_mutex_unlock(safe_mutex_t *mp,const char *file, uint line);
419 int safe_mutex_destroy(safe_mutex_t *mp,const char *file, uint line);
420 int safe_cond_wait(pthread_cond_t *cond, safe_mutex_t *mp,const char *file,
421 uint line);
422 int safe_cond_timedwait(pthread_cond_t *cond, safe_mutex_t *mp,
423 const struct timespec *abstime,
424 const char *file, uint line);
425 void safe_mutex_global_init(void);
426 void safe_mutex_end(FILE *file);
427 void safe_mutex_free_deadlock_data(safe_mutex_t *mp);
428
429 /* Wrappers if safe mutex is actually used */
430 #define MYF_TRY_LOCK 1
431 #define MYF_NO_DEADLOCK_DETECTION 2
432
433 #ifdef SAFE_MUTEX
434 #define safe_mutex_assert_owner(mp) \
435 DBUG_ASSERT((mp)->count > 0 && \
436 pthread_equal(pthread_self(), (mp)->thread))
437 #define safe_mutex_assert_not_owner(mp) \
438 DBUG_ASSERT(! (mp)->count || \
439 ! pthread_equal(pthread_self(), (mp)->thread))
440 #define safe_mutex_setflags(mp, F) do { (mp)->create_flags|= (F); } while (0)
441 #define my_cond_timedwait(A,B,C) safe_cond_timedwait((A),(B),(C),__FILE__,__LINE__)
442 #define my_cond_wait(A,B) safe_cond_wait((A), (B), __FILE__, __LINE__)
443 #else
444
445 #define safe_mutex_assert_owner(mp) do {} while (0)
446 #define safe_mutex_assert_not_owner(mp) do {} while (0)
447 #define safe_mutex_setflags(mp, F) do {} while (0)
448
449 #define my_cond_timedwait(A,B,C) pthread_cond_timedwait((A),(B),(C))
450 #define my_cond_wait(A,B) pthread_cond_wait((A), (B))
451 #endif /* !SAFE_MUTEX */
452
453 /* READ-WRITE thread locking */
454
455 #if defined(USE_MUTEX_INSTEAD_OF_RW_LOCKS)
456 /* use these defs for simple mutex locking */
457 #define rw_lock_t pthread_mutex_t
458 #define my_rwlock_init(A,B) pthread_mutex_init((A),(B))
459 #define rw_rdlock(A) pthread_mutex_lock((A))
460 #define rw_wrlock(A) pthread_mutex_lock((A))
461 #define rw_tryrdlock(A) pthread_mutex_trylock((A))
462 #define rw_trywrlock(A) pthread_mutex_trylock((A))
463 #define rw_unlock(A) pthread_mutex_unlock((A))
464 #define rwlock_destroy(A) pthread_mutex_destroy((A))
465 #elif defined(HAVE_PTHREAD_RWLOCK_RDLOCK)
466 #define rw_lock_t pthread_rwlock_t
467 #define my_rwlock_init(A,B) pthread_rwlock_init((A),(B))
468 #define rw_rdlock(A) pthread_rwlock_rdlock(A)
469 #define rw_wrlock(A) pthread_rwlock_wrlock(A)
470 #define rw_tryrdlock(A) pthread_rwlock_tryrdlock((A))
471 #define rw_trywrlock(A) pthread_rwlock_trywrlock((A))
472 #define rw_unlock(A) pthread_rwlock_unlock(A)
473 #define rwlock_destroy(A) pthread_rwlock_destroy(A)
474 #elif defined(HAVE_RWLOCK_INIT)
475 #ifdef HAVE_RWLOCK_T /* For example Solaris 2.6-> */
476 #define rw_lock_t rwlock_t
477 #endif
478 #define my_rwlock_init(A,B) rwlock_init((A),USYNC_THREAD,0)
479 #else
480 /* Use our own version of read/write locks */
481 #define NEED_MY_RW_LOCK 1
482 #define rw_lock_t my_rw_lock_t
483 #define my_rwlock_init(A,B) my_rw_init((A))
484 #define rw_rdlock(A) my_rw_rdlock((A))
485 #define rw_wrlock(A) my_rw_wrlock((A))
486 #define rw_tryrdlock(A) my_rw_tryrdlock((A))
487 #define rw_trywrlock(A) my_rw_trywrlock((A))
488 #define rw_unlock(A) my_rw_unlock((A))
489 #define rwlock_destroy(A) my_rw_destroy((A))
490 #define rw_lock_assert_write_owner(A) my_rw_lock_assert_write_owner((A))
491 #define rw_lock_assert_not_write_owner(A) my_rw_lock_assert_not_write_owner((A))
492 #endif /* USE_MUTEX_INSTEAD_OF_RW_LOCKS */
493
494
495 /**
496 Portable implementation of special type of read-write locks.
497
498 These locks have two properties which are unusual for rwlocks:
499 1) They "prefer readers" in the sense that they do not allow
500 situations in which rwlock is rd-locked and there is a
501 pending rd-lock which is blocked (e.g. due to pending
502 request for wr-lock).
503 This is a stronger guarantee than one which is provided for
504 PTHREAD_RWLOCK_PREFER_READER_NP rwlocks in Linux.
505 MDL subsystem deadlock detector relies on this property for
506 its correctness.
507 2) They are optimized for uncontended wr-lock/unlock case.
508 This is a scenario in which they are most often used
509 within MDL subsystem. Optimizing for it gives significant
510 performance improvements in some of the tests involving many
511 connections.
512
513 Another important requirement imposed on this type of rwlock
514 by the MDL subsystem is that it should be OK to destroy rwlock
515 object which is in unlocked state even though some threads might
516 have not yet fully left unlock operation for it (of course there
517 is an external guarantee that no thread will try to lock rwlock
518 which is destroyed).
519 Putting it another way the unlock operation should not access
520 rwlock data after changing its state to unlocked.
521
522 TODO/FIXME: We should consider alleviating this requirement as
523 it blocks us from doing certain performance optimizations.
524 */
525
526 typedef struct st_rw_pr_lock_t {
527 /**
528 Lock which protects the structure.
529 Also held for the duration of wr-lock.
530 */
531 pthread_mutex_t lock;
532 /**
533 Condition variable which is used to wake-up
534 writers waiting for readers to go away.
535 */
536 pthread_cond_t no_active_readers;
537 /** Number of active readers. */
538 uint active_readers;
539 /** Number of writers waiting for readers to go away. */
540 uint writers_waiting_readers;
541 /** Indicates whether there is an active writer. */
542 my_bool active_writer;
543 #ifdef SAFE_MUTEX
544 /** Thread holding wr-lock (for debug purposes only). */
545 pthread_t writer_thread;
546 #endif
547 } rw_pr_lock_t;
548
549 extern int rw_pr_init(rw_pr_lock_t *);
550 extern int rw_pr_rdlock(rw_pr_lock_t *);
551 extern int rw_pr_wrlock(rw_pr_lock_t *);
552 extern int rw_pr_unlock(rw_pr_lock_t *);
553 extern int rw_pr_destroy(rw_pr_lock_t *);
554 #ifdef SAFE_MUTEX
555 #define rw_pr_lock_assert_write_owner(A) \
556 DBUG_ASSERT((A)->active_writer && pthread_equal(pthread_self(), \
557 (A)->writer_thread))
558 #define rw_pr_lock_assert_not_write_owner(A) \
559 DBUG_ASSERT(! (A)->active_writer || ! pthread_equal(pthread_self(), \
560 (A)->writer_thread))
561 #else
562 #define rw_pr_lock_assert_write_owner(A)
563 #define rw_pr_lock_assert_not_write_owner(A)
564 #endif /* SAFE_MUTEX */
565
566
567 #ifdef NEED_MY_RW_LOCK
568
569 #ifdef _WIN32
570
571 /**
572 Implementation of Windows rwlock.
573
574 We use native (slim) rwlocks on Win7 and later, and fallback to portable
575 implementation on earlier Windows.
576
577 slim rwlock are also available on Vista/WS2008, but we do not use it
578 ("trylock" APIs are missing on Vista)
579 */
580 typedef union
581 {
582 /* Native rwlock (is_srwlock == TRUE) */
583 struct
584 {
585 SRWLOCK srwlock; /* native reader writer lock */
586 BOOL have_exclusive_srwlock; /* used for unlock */
587 };
588
589 /*
590 Portable implementation (is_srwlock == FALSE)
591 Fields are identical with Unix my_rw_lock_t fields.
592 */
593 struct
594 {
595 pthread_mutex_t lock; /* lock for structure */
596 pthread_cond_t readers; /* waiting readers */
597 pthread_cond_t writers; /* waiting writers */
598 int state; /* -1:writer,0:free,>0:readers */
599 int waiters; /* number of waiting writers */
600 #ifdef SAFE_MUTEX
601 pthread_t write_thread;
602 #endif
603 };
604 } my_rw_lock_t;
605
606
607 #else /* _WIN32 */
608
609 /*
610 On systems which don't support native read/write locks we have
611 to use own implementation.
612 */
613 typedef struct st_my_rw_lock_t {
614 pthread_mutex_t lock; /* lock for structure */
615 pthread_cond_t readers; /* waiting readers */
616 pthread_cond_t writers; /* waiting writers */
617 int state; /* -1:writer,0:free,>0:readers */
618 int waiters; /* number of waiting writers */
619 #ifdef SAFE_MUTEX
620 pthread_t write_thread;
621 #endif
622 } my_rw_lock_t;
623
624 #endif /*! _WIN32 */
625
626 extern int my_rw_init(my_rw_lock_t *);
627 extern int my_rw_destroy(my_rw_lock_t *);
628 extern int my_rw_rdlock(my_rw_lock_t *);
629 extern int my_rw_wrlock(my_rw_lock_t *);
630 extern int my_rw_unlock(my_rw_lock_t *);
631 extern int my_rw_tryrdlock(my_rw_lock_t *);
632 extern int my_rw_trywrlock(my_rw_lock_t *);
633 #ifdef SAFE_MUTEX
634 #define my_rw_lock_assert_write_owner(A) \
635 DBUG_ASSERT((A)->state == -1 && pthread_equal(pthread_self(), \
636 (A)->write_thread))
637 #define my_rw_lock_assert_not_write_owner(A) \
638 DBUG_ASSERT((A)->state >= 0 || ! pthread_equal(pthread_self(), \
639 (A)->write_thread))
640 #else
641 #define my_rw_lock_assert_write_owner(A)
642 #define my_rw_lock_assert_not_write_owner(A)
643 #endif
644 #endif /* NEED_MY_RW_LOCK */
645
646
647 #define GETHOSTBYADDR_BUFF_SIZE 2048
648
649 #ifndef HAVE_THR_SETCONCURRENCY
650 #define thr_setconcurrency(A) pthread_dummy(0)
651 #endif
652 #if !defined(HAVE_PTHREAD_ATTR_SETSTACKSIZE) && ! defined(pthread_attr_setstacksize)
653 #define pthread_attr_setstacksize(A,B) pthread_dummy(0)
654 #endif
655
656 /* Define mutex types, see my_thr_init.c */
657 #define MY_MUTEX_INIT_SLOW NULL
658 #ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
659 extern pthread_mutexattr_t my_fast_mutexattr;
660 #define MY_MUTEX_INIT_FAST &my_fast_mutexattr
661 #else
662 #define MY_MUTEX_INIT_FAST NULL
663 #endif
664 #ifdef PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
665 extern pthread_mutexattr_t my_errorcheck_mutexattr;
666 #define MY_MUTEX_INIT_ERRCHK &my_errorcheck_mutexattr
667 #else
668 #define MY_MUTEX_INIT_ERRCHK NULL
669 #endif
670
671 #ifndef ESRCH
672 /* Define it to something */
673 #define ESRCH 1
674 #endif
675
676 typedef uint64 my_thread_id;
677
678 extern void my_threadattr_global_init(void);
679 extern my_bool my_thread_global_init(void);
680 extern void my_thread_global_reinit(void);
681 extern void my_thread_global_end(void);
682 extern my_bool my_thread_init(void);
683 extern void my_thread_end(void);
684 extern const char *my_thread_name(void);
685 extern my_thread_id my_thread_dbug_id(void);
686 extern int pthread_dummy(int);
687 extern void my_mutex_init(void);
688 extern void my_mutex_end(void);
689
690 /* All thread specific variables are in the following struct */
691
692 #define THREAD_NAME_SIZE 10
693 #ifndef DEFAULT_THREAD_STACK
694 /*
695 We need to have at least 256K stack to handle calls to myisamchk_init()
696 with the current number of keys and key parts.
697 */
698 #if defined(__SANITIZE_ADDRESS__) || defined(WITH_UBSAN)
699 #define DEFAULT_THREAD_STACK (383*1024L) /* 392192 */
700 #else
701 #define DEFAULT_THREAD_STACK (292*1024L) /* 299008 */
702 #endif
703 #endif
704
705 #define MY_PTHREAD_LOCK_READ 0
706 #define MY_PTHREAD_LOCK_WRITE 1
707
708 #include <mysql/psi/mysql_thread.h>
709
710 #define INSTRUMENT_ME 0
711
712 struct st_my_thread_var
713 {
714 int thr_errno;
715 mysql_cond_t suspend;
716 mysql_mutex_t mutex;
717 mysql_mutex_t * volatile current_mutex;
718 mysql_cond_t * volatile current_cond;
719 pthread_t pthread_self;
720 my_thread_id id, dbug_id;
721 int volatile abort;
722 my_bool init;
723 struct st_my_thread_var *next,**prev;
724 void *keycache_link;
725 uint lock_type; /* used by conditional release the queue */
726 void *stack_ends_here;
727 safe_mutex_t *mutex_in_use;
728 #ifndef DBUG_OFF
729 void *dbug;
730 char name[THREAD_NAME_SIZE+1];
731 #endif
732 };
733
734 struct st_my_thread_var *_my_thread_var(void);
735 extern void **my_thread_var_dbug(void);
736 extern safe_mutex_t **my_thread_var_mutex_in_use(void);
737 extern uint my_thread_end_wait_time;
738 extern my_bool safe_mutex_deadlock_detector;
739 #define my_thread_var (_my_thread_var())
740 #define my_errno my_thread_var->thr_errno
741 int set_mysys_var(struct st_my_thread_var *mysys_var);
742 /*
743 Keep track of shutdown,signal, and main threads so that my_end() will not
744 report errors with them
745 */
746
747 /* Which kind of thread library is in use */
748
749 #define THD_LIB_OTHER 1
750 #define THD_LIB_NPTL 2
751 #define THD_LIB_LT 4
752
753 extern uint thd_lib_detected;
754
755 /*
756 thread_safe_xxx functions are for critical statistic or counters.
757 The implementation is guaranteed to be thread safe, on all platforms.
758 Note that the calling code should *not* assume the counter is protected
759 by the mutex given, as the implementation of these helpers may change
760 to use my_atomic operations instead.
761 */
762
763 #ifndef thread_safe_increment
764 #ifdef _WIN32
765 #define thread_safe_increment(V,L) InterlockedIncrement((long*) &(V))
766 #define thread_safe_decrement(V,L) InterlockedDecrement((long*) &(V))
767 #else
768 #define thread_safe_increment(V,L) \
769 (mysql_mutex_lock((L)), (V)++, mysql_mutex_unlock((L)))
770 #define thread_safe_decrement(V,L) \
771 (mysql_mutex_lock((L)), (V)--, mysql_mutex_unlock((L)))
772 #endif
773 #endif
774
775 #ifndef thread_safe_add
776 #ifdef _WIN32
777 #define thread_safe_add(V,C,L) InterlockedExchangeAdd((long*) &(V),(C))
778 #define thread_safe_sub(V,C,L) InterlockedExchangeAdd((long*) &(V),-(long) (C))
779 #else
780 #define thread_safe_add(V,C,L) \
781 (mysql_mutex_lock((L)), (V)+=(C), mysql_mutex_unlock((L)))
782 #define thread_safe_sub(V,C,L) \
783 (mysql_mutex_lock((L)), (V)-=(C), mysql_mutex_unlock((L)))
784 #endif
785 #endif
786
787
788 /*
789 statistics_xxx functions are for non critical statistic,
790 maintained in global variables.
791 When compiling with SAFE_STATISTICS:
792 - race conditions can not occur.
793 - some locking occurs, which may cause performance degradation.
794
795 When compiling without SAFE_STATISTICS:
796 - race conditions can occur, making the result slightly inaccurate.
797 - the lock given is not honored.
798 */
799 #ifdef SAFE_STATISTICS
800 #define statistic_increment(V,L) thread_safe_increment((V),(L))
801 #define statistic_decrement(V,L) thread_safe_decrement((V),(L))
802 #define statistic_add(V,C,L) thread_safe_add((V),(C),(L))
803 #define statistic_sub(V,C,L) thread_safe_sub((V),(C),(L))
804 #else
805 #define statistic_decrement(V,L) (V)--
806 #define statistic_increment(V,L) (V)++
807 #define statistic_add(V,C,L) (V)+=(C)
808 #define statistic_sub(V,C,L) (V)-=(C)
809 #endif /* SAFE_STATISTICS */
810
thread_safe_increment32(int32 * value)811 static inline void thread_safe_increment32(int32 *value)
812 {
813 (void) my_atomic_add32_explicit(value, 1, MY_MEMORY_ORDER_RELAXED);
814 }
815
thread_safe_decrement32(int32 * value)816 static inline void thread_safe_decrement32(int32 *value)
817 {
818 (void) my_atomic_add32_explicit(value, -1, MY_MEMORY_ORDER_RELAXED);
819 }
820
thread_safe_increment64(int64 * value)821 static inline void thread_safe_increment64(int64 *value)
822 {
823 (void) my_atomic_add64_explicit(value, 1, MY_MEMORY_ORDER_RELAXED);
824 }
825
thread_safe_decrement64(int64 * value)826 static inline void thread_safe_decrement64(int64 *value)
827 {
828 (void) my_atomic_add64_explicit(value, -1, MY_MEMORY_ORDER_RELAXED);
829 }
830
831 /*
832 No locking needed, the counter is owned by the thread
833 */
834 #define status_var_increment(V) (V)++
835 #define status_var_decrement(V) (V)--
836 #define status_var_add(V,C) (V)+=(C)
837 #define status_var_sub(V,C) (V)-=(C)
838
839 #ifdef SAFE_MUTEX
840 #define mysql_mutex_record_order(A,B) \
841 do { \
842 mysql_mutex_lock(A); mysql_mutex_lock(B); \
843 mysql_mutex_unlock(B); mysql_mutex_unlock(A); \
844 } while(0)
845 #else
846 #define mysql_mutex_record_order(A,B) do { } while(0)
847 #endif
848
849 /* At least Windows and NetBSD do not have this definition */
850 #ifndef PTHREAD_STACK_MIN
851 #define PTHREAD_STACK_MIN 65536
852 #endif
853
854 #ifdef __cplusplus
855 }
856 #endif
857 #endif /* _my_ptread_h */
858