my_pthread.h 27 KB

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