my_pthread.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. /* Copyright (C) 2000 MySQL AB
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; version 2 of the License.
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License
  10. along with this program; if not, write to the Free Software
  11. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
  12. /* Defines to make different thread packages compatible */
  13. #ifndef _my_pthread_h
  14. #define _my_pthread_h
  15. #ifndef ETIME
  16. #define ETIME ETIMEDOUT /* For FreeBSD */
  17. #endif
  18. #ifdef __cplusplus
  19. #define EXTERNC extern "C"
  20. extern "C" {
  21. #else
  22. #define EXTERNC
  23. #endif /* __cplusplus */
  24. #if defined(__WIN__)
  25. typedef CRITICAL_SECTION pthread_mutex_t;
  26. typedef HANDLE pthread_t;
  27. typedef struct thread_attr {
  28. DWORD dwStackSize ;
  29. DWORD dwCreatingFlag ;
  30. int priority ;
  31. } pthread_attr_t ;
  32. typedef struct { int dummy; } pthread_condattr_t;
  33. /* Implementation of posix conditions */
  34. typedef struct st_pthread_link {
  35. DWORD thread_id;
  36. struct st_pthread_link *next;
  37. } pthread_link;
  38. typedef struct {
  39. uint32 waiting;
  40. CRITICAL_SECTION lock_waiting;
  41. enum {
  42. SIGNAL= 0,
  43. BROADCAST= 1,
  44. MAX_EVENTS= 2
  45. } EVENTS;
  46. HANDLE events[MAX_EVENTS];
  47. HANDLE broadcast_block_event;
  48. } pthread_cond_t;
  49. typedef int pthread_mutexattr_t;
  50. #define win_pthread_self my_thread_var->pthread_self
  51. #define pthread_self() win_pthread_self
  52. #define pthread_handler_t EXTERNC void * __cdecl
  53. typedef void * (__cdecl *pthread_handler)(void *);
  54. typedef volatile LONG my_pthread_once_t;
  55. #define MY_PTHREAD_ONCE_INIT 0
  56. #define MY_PTHREAD_ONCE_INPROGRESS 1
  57. #define MY_PTHREAD_ONCE_DONE 2
  58. /*
  59. Struct and macros to be used in combination with the
  60. windows implementation of pthread_cond_timedwait
  61. */
  62. /*
  63. Declare a union to make sure FILETIME is properly aligned
  64. so it can be used directly as a 64 bit value. The value
  65. stored is in 100ns units.
  66. */
  67. union ft64 {
  68. FILETIME ft;
  69. __int64 i64;
  70. };
  71. struct timespec {
  72. union ft64 tv;
  73. /* The max timeout value in millisecond for pthread_cond_timedwait */
  74. long max_timeout_msec;
  75. };
  76. #define set_timespec(ABSTIME,SEC) { \
  77. GetSystemTimeAsFileTime(&((ABSTIME).tv.ft)); \
  78. (ABSTIME).tv.i64+= (__int64)(SEC)*10000000; \
  79. (ABSTIME).max_timeout_msec= (long)((SEC)*1000); \
  80. }
  81. #define set_timespec_nsec(ABSTIME,NSEC) { \
  82. GetSystemTimeAsFileTime(&((ABSTIME).tv.ft)); \
  83. (ABSTIME).tv.i64+= (__int64)(NSEC)/100; \
  84. (ABSTIME).max_timeout_msec= (long)((NSEC)/1000000); \
  85. }
  86. void win_pthread_init(void);
  87. int win_pthread_setspecific(void *A,void *B,uint length);
  88. int win_pthread_mutex_trylock(pthread_mutex_t *mutex);
  89. int pthread_create(pthread_t *,pthread_attr_t *,pthread_handler,void *);
  90. int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr);
  91. int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
  92. int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
  93. struct timespec *abstime);
  94. int pthread_cond_signal(pthread_cond_t *cond);
  95. int pthread_cond_broadcast(pthread_cond_t *cond);
  96. int pthread_cond_destroy(pthread_cond_t *cond);
  97. int pthread_attr_init(pthread_attr_t *connect_att);
  98. int pthread_attr_setstacksize(pthread_attr_t *connect_att,DWORD stack);
  99. int pthread_attr_setprio(pthread_attr_t *connect_att,int priority);
  100. int pthread_attr_destroy(pthread_attr_t *connect_att);
  101. int my_pthread_once(my_pthread_once_t *once_control,void (*init_routine)(void));
  102. struct tm *localtime_r(const time_t *timep,struct tm *tmp);
  103. struct tm *gmtime_r(const time_t *timep,struct tm *tmp);
  104. void pthread_exit(void *a); /* was #define pthread_exit(A) ExitThread(A)*/
  105. #define ETIMEDOUT 145 /* Win32 doesn't have this */
  106. #define getpid() GetCurrentThreadId()
  107. #define HAVE_LOCALTIME_R 1
  108. #define _REENTRANT 1
  109. #define HAVE_PTHREAD_ATTR_SETSTACKSIZE 1
  110. /*
  111. Windows has two ways to use thread local storage. The most efficient
  112. is using __declspec(thread), but that does not work properly when
  113. used in a .dll that is loaded at runtime, after program load. So for
  114. libmysql.dll and libmysqld.dll we define USE_TLS in order to use the
  115. TlsXxx() API instead, which works in all cases.
  116. */
  117. #ifdef USE_TLS /* For LIBMYSQL.DLL */
  118. #undef SAFE_MUTEX /* This will cause conflicts */
  119. #define pthread_key(T,V) DWORD V
  120. #define pthread_key_create(A,B) ((*A=TlsAlloc())==0xFFFFFFFF)
  121. #define pthread_key_delete(A) TlsFree(A)
  122. #define pthread_getspecific(A) (TlsGetValue(A))
  123. #define my_pthread_getspecific(T,A) ((T) TlsGetValue(A))
  124. #define my_pthread_getspecific_ptr(T,V) ((T) TlsGetValue(V))
  125. #define my_pthread_setspecific_ptr(T,V) (!TlsSetValue((T),(V)))
  126. #define pthread_setspecific(A,B) (!TlsSetValue((A),(B)))
  127. #else
  128. #define pthread_key(T,V) __declspec(thread) T V
  129. #define pthread_key_create(A,B) pthread_dummy(0)
  130. #define pthread_key_delete(A) pthread_dummy(0)
  131. #define pthread_getspecific(A) (&(A))
  132. #define my_pthread_getspecific(T,A) (&(A))
  133. #define my_pthread_getspecific_ptr(T,V) (V)
  134. #define my_pthread_setspecific_ptr(T,V) ((T)=(V),0)
  135. #define pthread_setspecific(A,B) win_pthread_setspecific(&(A),(B),sizeof(A))
  136. #endif /* USE_TLS */
  137. #define pthread_equal(A,B) ((A) == (B))
  138. #define pthread_mutex_init(A,B) (InitializeCriticalSection(A),0)
  139. #define pthread_mutex_lock(A) (EnterCriticalSection(A),0)
  140. #define pthread_mutex_trylock(A) win_pthread_mutex_trylock((A))
  141. #define pthread_mutex_unlock(A) LeaveCriticalSection(A)
  142. #define pthread_mutex_destroy(A) DeleteCriticalSection(A)
  143. #define my_pthread_setprio(A,B) SetThreadPriority(GetCurrentThread(), (B))
  144. #define pthread_kill(A,B) pthread_dummy((A) ? 0 : ESRCH)
  145. #define pthread_join(A,B) (WaitForSingleObject((A), INFINITE) != WAIT_OBJECT_0)
  146. /* Dummy defines for easier code */
  147. #define pthread_attr_setdetachstate(A,B) pthread_dummy(0)
  148. #define my_pthread_attr_setprio(A,B) pthread_attr_setprio(A,B)
  149. #define pthread_attr_setscope(A,B)
  150. #define pthread_detach_this_thread()
  151. #define pthread_condattr_init(A)
  152. #define pthread_condattr_destroy(A)
  153. #define my_pthread_getprio(thread_id) pthread_dummy(0)
  154. #else /* Normal threads */
  155. #ifdef HAVE_rts_threads
  156. #define sigwait org_sigwait
  157. #include <signal.h>
  158. #undef sigwait
  159. #endif
  160. #include <pthread.h>
  161. #ifndef _REENTRANT
  162. #define _REENTRANT
  163. #endif
  164. #ifdef HAVE_THR_SETCONCURRENCY
  165. #include <thread.h> /* Probably solaris */
  166. #endif
  167. #ifdef HAVE_SCHED_H
  168. #include <sched.h>
  169. #endif
  170. #ifdef HAVE_SYNCH_H
  171. #include <synch.h>
  172. #endif
  173. #ifdef __NETWARE__
  174. void my_pthread_exit(void *status);
  175. #define pthread_exit(A) my_pthread_exit(A)
  176. #endif
  177. extern int my_pthread_getprio(pthread_t thread_id);
  178. #define pthread_key(T,V) pthread_key_t V
  179. #define my_pthread_getspecific_ptr(T,V) my_pthread_getspecific(T,(V))
  180. #define my_pthread_setspecific_ptr(T,V) pthread_setspecific(T,(void*) (V))
  181. #define pthread_detach_this_thread()
  182. #define pthread_handler_t EXTERNC void *
  183. typedef void *(* pthread_handler)(void *);
  184. #define my_pthread_once_t pthread_once_t
  185. #define MY_PTHREAD_ONCE_INIT PTHREAD_ONCE_INIT
  186. #define my_pthread_once(C,F) pthread_once(C,F)
  187. /* Test first for RTS or FSU threads */
  188. #if defined(PTHREAD_SCOPE_GLOBAL) && !defined(PTHREAD_SCOPE_SYSTEM)
  189. #define HAVE_rts_threads
  190. extern int my_pthread_create_detached;
  191. #define pthread_sigmask(A,B,C) sigprocmask((A),(B),(C))
  192. #define PTHREAD_CREATE_DETACHED &my_pthread_create_detached
  193. #define PTHREAD_SCOPE_SYSTEM PTHREAD_SCOPE_GLOBAL
  194. #define PTHREAD_SCOPE_PROCESS PTHREAD_SCOPE_LOCAL
  195. #define USE_ALARM_THREAD
  196. #endif /* defined(PTHREAD_SCOPE_GLOBAL) && !defined(PTHREAD_SCOPE_SYSTEM) */
  197. #if defined(_BSDI_VERSION) && _BSDI_VERSION < 199910
  198. int sigwait(sigset_t *set, int *sig);
  199. #endif
  200. #ifndef HAVE_NONPOSIX_SIGWAIT
  201. #define my_sigwait(A,B) sigwait((A),(B))
  202. #else
  203. int my_sigwait(const sigset_t *set,int *sig);
  204. #endif
  205. #ifdef HAVE_NONPOSIX_PTHREAD_MUTEX_INIT
  206. #ifndef SAFE_MUTEX
  207. #define pthread_mutex_init(a,b) my_pthread_mutex_init((a),(b))
  208. extern int my_pthread_mutex_init(pthread_mutex_t *mp,
  209. const pthread_mutexattr_t *attr);
  210. #endif /* SAFE_MUTEX */
  211. #define pthread_cond_init(a,b) my_pthread_cond_init((a),(b))
  212. extern int my_pthread_cond_init(pthread_cond_t *mp,
  213. const pthread_condattr_t *attr);
  214. #endif /* HAVE_NONPOSIX_PTHREAD_MUTEX_INIT */
  215. #if defined(HAVE_SIGTHREADMASK) && !defined(HAVE_PTHREAD_SIGMASK)
  216. #define pthread_sigmask(A,B,C) sigthreadmask((A),(B),(C))
  217. #endif
  218. #if !defined(HAVE_SIGWAIT) && !defined(HAVE_rts_threads) && !defined(sigwait) && !defined(alpha_linux_port) && !defined(HAVE_NONPOSIX_SIGWAIT) && !defined(HAVE_DEC_3_2_THREADS) && !defined(_AIX)
  219. int sigwait(sigset_t *setp, int *sigp); /* Use our implemention */
  220. #endif
  221. /*
  222. We define my_sigset() and use that instead of the system sigset() so that
  223. we can favor an implementation based on sigaction(). On some systems, such
  224. as Mac OS X, sigset() results in flags such as SA_RESTART being set, and
  225. we want to make sure that no such flags are set.
  226. */
  227. #if defined(HAVE_SIGACTION) && !defined(my_sigset)
  228. #define my_sigset(A,B) do { struct sigaction l_s; sigset_t l_set; int l_rc; \
  229. DBUG_ASSERT((A) != 0); \
  230. sigemptyset(&l_set); \
  231. l_s.sa_handler = (B); \
  232. l_s.sa_mask = l_set; \
  233. l_s.sa_flags = 0; \
  234. l_rc= sigaction((A), &l_s, (struct sigaction *) NULL);\
  235. DBUG_ASSERT(l_rc == 0); \
  236. } while (0)
  237. #elif defined(HAVE_SIGSET) && !defined(my_sigset)
  238. #define my_sigset(A,B) sigset((A),(B))
  239. #elif !defined(my_sigset)
  240. #define my_sigset(A,B) signal((A),(B))
  241. #endif
  242. #ifndef my_pthread_setprio
  243. #if defined(HAVE_PTHREAD_SETPRIO_NP) /* FSU threads */
  244. #define my_pthread_setprio(A,B) pthread_setprio_np((A),(B))
  245. #elif defined(HAVE_PTHREAD_SETPRIO)
  246. #define my_pthread_setprio(A,B) pthread_setprio((A),(B))
  247. #elif defined(HAVE_PTHREAD_SETSCHEDPRIO)
  248. #define my_pthread_setprio(A,B) pthread_setschedprio((A),(B))
  249. #else
  250. extern void my_pthread_setprio(pthread_t thread_id,int prior);
  251. #endif
  252. #endif
  253. #ifndef my_pthread_attr_setprio
  254. #ifdef HAVE_PTHREAD_ATTR_SETPRIO
  255. #define my_pthread_attr_setprio(A,B) pthread_attr_setprio((A),(B))
  256. #else
  257. extern void my_pthread_attr_setprio(pthread_attr_t *attr, int priority);
  258. #endif
  259. #endif
  260. #if !defined(HAVE_PTHREAD_ATTR_SETSCOPE) || defined(HAVE_DEC_3_2_THREADS)
  261. #define pthread_attr_setscope(A,B)
  262. #undef HAVE_GETHOSTBYADDR_R /* No definition */
  263. #endif
  264. #if defined(HAVE_BROKEN_PTHREAD_COND_TIMEDWAIT) && !defined(SAFE_MUTEX)
  265. extern int my_pthread_cond_timedwait(pthread_cond_t *cond,
  266. pthread_mutex_t *mutex,
  267. struct timespec *abstime);
  268. #define pthread_cond_timedwait(A,B,C) my_pthread_cond_timedwait((A),(B),(C))
  269. #endif
  270. #if !defined( HAVE_NONPOSIX_PTHREAD_GETSPECIFIC)
  271. #define my_pthread_getspecific(A,B) ((A) pthread_getspecific(B))
  272. #else
  273. #define my_pthread_getspecific(A,B) ((A) my_pthread_getspecific_imp(B))
  274. void *my_pthread_getspecific_imp(pthread_key_t key);
  275. #endif
  276. #ifndef HAVE_LOCALTIME_R
  277. struct tm *localtime_r(const time_t *clock, struct tm *res);
  278. #endif
  279. #ifndef HAVE_GMTIME_R
  280. struct tm *gmtime_r(const time_t *clock, struct tm *res);
  281. #endif
  282. #ifdef HAVE_PTHREAD_CONDATTR_CREATE
  283. /* DCE threads on HPUX 10.20 */
  284. #define pthread_condattr_init pthread_condattr_create
  285. #define pthread_condattr_destroy pthread_condattr_delete
  286. #endif
  287. /* FSU THREADS */
  288. #if !defined(HAVE_PTHREAD_KEY_DELETE) && !defined(pthread_key_delete)
  289. #define pthread_key_delete(A) pthread_dummy(0)
  290. #endif
  291. #ifdef HAVE_CTHREADS_WRAPPER /* For MacOSX */
  292. #define pthread_cond_destroy(A) pthread_dummy(0)
  293. #define pthread_mutex_destroy(A) pthread_dummy(0)
  294. #define pthread_attr_delete(A) pthread_dummy(0)
  295. #define pthread_condattr_delete(A) pthread_dummy(0)
  296. #define pthread_attr_setstacksize(A,B) pthread_dummy(0)
  297. #define pthread_equal(A,B) ((A) == (B))
  298. #define pthread_cond_timedwait(a,b,c) pthread_cond_wait((a),(b))
  299. #define pthread_attr_init(A) pthread_attr_create(A)
  300. #define pthread_attr_destroy(A) pthread_attr_delete(A)
  301. #define pthread_attr_setdetachstate(A,B) pthread_dummy(0)
  302. #define pthread_create(A,B,C,D) pthread_create((A),*(B),(C),(D))
  303. #define pthread_sigmask(A,B,C) sigprocmask((A),(B),(C))
  304. #define pthread_kill(A,B) pthread_dummy((A) ? 0 : ESRCH)
  305. #undef pthread_detach_this_thread
  306. #define pthread_detach_this_thread() { pthread_t tmp=pthread_self() ; pthread_detach(&tmp); }
  307. #endif
  308. #ifdef HAVE_DARWIN5_THREADS
  309. #define pthread_sigmask(A,B,C) sigprocmask((A),(B),(C))
  310. #define pthread_kill(A,B) pthread_dummy((A) ? 0 : ESRCH)
  311. #define pthread_condattr_init(A) pthread_dummy(0)
  312. #define pthread_condattr_destroy(A) pthread_dummy(0)
  313. #undef pthread_detach_this_thread
  314. #define pthread_detach_this_thread() { pthread_t tmp=pthread_self() ; pthread_detach(tmp); }
  315. #endif
  316. #if ((defined(HAVE_PTHREAD_ATTR_CREATE) && !defined(HAVE_SIGWAIT)) || defined(HAVE_DEC_3_2_THREADS)) && !defined(HAVE_CTHREADS_WRAPPER)
  317. /* This is set on AIX_3_2 and Siemens unix (and DEC OSF/1 3.2 too) */
  318. #define pthread_key_create(A,B) \
  319. pthread_keycreate(A,(B) ?\
  320. (pthread_destructor_t) (B) :\
  321. (pthread_destructor_t) pthread_dummy)
  322. #define pthread_attr_init(A) pthread_attr_create(A)
  323. #define pthread_attr_destroy(A) pthread_attr_delete(A)
  324. #define pthread_attr_setdetachstate(A,B) pthread_dummy(0)
  325. #define pthread_create(A,B,C,D) pthread_create((A),*(B),(C),(D))
  326. #ifndef pthread_sigmask
  327. #define pthread_sigmask(A,B,C) sigprocmask((A),(B),(C))
  328. #endif
  329. #define pthread_kill(A,B) pthread_dummy((A) ? 0 : ESRCH)
  330. #undef pthread_detach_this_thread
  331. #define pthread_detach_this_thread() { pthread_t tmp=pthread_self() ; pthread_detach(&tmp); }
  332. #elif !defined(__NETWARE__) /* HAVE_PTHREAD_ATTR_CREATE && !HAVE_SIGWAIT */
  333. #define HAVE_PTHREAD_KILL
  334. #endif
  335. #endif /* defined(__WIN__) */
  336. #if defined(HPUX10) && !defined(DONT_REMAP_PTHREAD_FUNCTIONS)
  337. #undef pthread_cond_timedwait
  338. #define pthread_cond_timedwait(a,b,c) my_pthread_cond_timedwait((a),(b),(c))
  339. int my_pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
  340. struct timespec *abstime);
  341. #endif
  342. #if defined(HPUX10)
  343. #define pthread_attr_getstacksize(A,B) my_pthread_attr_getstacksize(A,B)
  344. void my_pthread_attr_getstacksize(pthread_attr_t *attrib, size_t *size);
  345. #endif
  346. #if defined(HAVE_POSIX1003_4a_MUTEX) && !defined(DONT_REMAP_PTHREAD_FUNCTIONS)
  347. #undef pthread_mutex_trylock
  348. #define pthread_mutex_trylock(a) my_pthread_mutex_trylock((a))
  349. int my_pthread_mutex_trylock(pthread_mutex_t *mutex);
  350. #endif
  351. /*
  352. The defines set_timespec and set_timespec_nsec should be used
  353. for calculating an absolute time at which
  354. pthread_cond_timedwait should timeout
  355. */
  356. #ifdef HAVE_TIMESPEC_TS_SEC
  357. #ifndef set_timespec
  358. #define set_timespec(ABSTIME,SEC) \
  359. { \
  360. (ABSTIME).ts_sec=time(0) + (time_t) (SEC); \
  361. (ABSTIME).ts_nsec=0; \
  362. }
  363. #endif /* !set_timespec */
  364. #ifndef set_timespec_nsec
  365. #define set_timespec_nsec(ABSTIME,NSEC) \
  366. { \
  367. ulonglong now= my_getsystime() + (NSEC/100); \
  368. (ABSTIME).ts_sec= (now / ULL(10000000)); \
  369. (ABSTIME).ts_nsec= (now % ULL(10000000) * 100 + ((NSEC) % 100)); \
  370. }
  371. #endif /* !set_timespec_nsec */
  372. #else
  373. #ifndef set_timespec
  374. #define set_timespec(ABSTIME,SEC) \
  375. {\
  376. struct timeval tv;\
  377. gettimeofday(&tv,0);\
  378. (ABSTIME).tv_sec=tv.tv_sec+(time_t) (SEC);\
  379. (ABSTIME).tv_nsec=tv.tv_usec*1000;\
  380. }
  381. #endif /* !set_timespec */
  382. #ifndef set_timespec_nsec
  383. #define set_timespec_nsec(ABSTIME,NSEC) \
  384. {\
  385. ulonglong now= my_getsystime() + (NSEC/100); \
  386. (ABSTIME).tv_sec= (time_t) (now / ULL(10000000)); \
  387. (ABSTIME).tv_nsec= (long) (now % ULL(10000000) * 100 + ((NSEC) % 100)); \
  388. }
  389. #endif /* !set_timespec_nsec */
  390. #endif /* HAVE_TIMESPEC_TS_SEC */
  391. /* safe_mutex adds checking to mutex for easier debugging */
  392. #if defined(__NETWARE__) && !defined(SAFE_MUTEX_DETECT_DESTROY)
  393. #define SAFE_MUTEX_DETECT_DESTROY
  394. #endif
  395. typedef struct st_safe_mutex_t
  396. {
  397. pthread_mutex_t global,mutex;
  398. const char *file;
  399. uint line,count;
  400. pthread_t thread;
  401. #ifdef SAFE_MUTEX_DETECT_DESTROY
  402. struct st_safe_mutex_info_t *info; /* to track destroying of mutexes */
  403. #endif
  404. } safe_mutex_t;
  405. #ifdef SAFE_MUTEX_DETECT_DESTROY
  406. /*
  407. Used to track the destroying of mutexes. This needs to be a seperate
  408. structure because the safe_mutex_t structure could be freed before
  409. the mutexes are destroyed.
  410. */
  411. typedef struct st_safe_mutex_info_t
  412. {
  413. struct st_safe_mutex_info_t *next;
  414. struct st_safe_mutex_info_t *prev;
  415. const char *init_file;
  416. uint32 init_line;
  417. } safe_mutex_info_t;
  418. #endif /* SAFE_MUTEX_DETECT_DESTROY */
  419. int safe_mutex_init(safe_mutex_t *mp, const pthread_mutexattr_t *attr,
  420. const char *file, uint line);
  421. int safe_mutex_lock(safe_mutex_t *mp, my_bool try_lock, const char *file, uint line);
  422. int safe_mutex_unlock(safe_mutex_t *mp,const char *file, uint line);
  423. int safe_mutex_destroy(safe_mutex_t *mp,const char *file, uint line);
  424. int safe_cond_wait(pthread_cond_t *cond, safe_mutex_t *mp,const char *file,
  425. uint line);
  426. int safe_cond_timedwait(pthread_cond_t *cond, safe_mutex_t *mp,
  427. struct timespec *abstime, const char *file, uint line);
  428. void safe_mutex_global_init(void);
  429. void safe_mutex_end(FILE *file);
  430. /* Wrappers if safe mutex is actually used */
  431. #ifdef SAFE_MUTEX
  432. #undef pthread_mutex_init
  433. #undef pthread_mutex_lock
  434. #undef pthread_mutex_unlock
  435. #undef pthread_mutex_destroy
  436. #undef pthread_mutex_wait
  437. #undef pthread_mutex_timedwait
  438. #undef pthread_mutex_t
  439. #undef pthread_cond_wait
  440. #undef pthread_cond_timedwait
  441. #undef pthread_mutex_trylock
  442. #define pthread_mutex_init(A,B) safe_mutex_init((A),(B),__FILE__,__LINE__)
  443. #define pthread_mutex_lock(A) safe_mutex_lock((A), FALSE, __FILE__, __LINE__)
  444. #define pthread_mutex_unlock(A) safe_mutex_unlock((A),__FILE__,__LINE__)
  445. #define pthread_mutex_destroy(A) safe_mutex_destroy((A),__FILE__,__LINE__)
  446. #define pthread_cond_wait(A,B) safe_cond_wait((A),(B),__FILE__,__LINE__)
  447. #define pthread_cond_timedwait(A,B,C) safe_cond_timedwait((A),(B),(C),__FILE__,__LINE__)
  448. #define pthread_mutex_trylock(A) safe_mutex_lock((A), TRUE, __FILE__, __LINE__)
  449. #define pthread_mutex_t safe_mutex_t
  450. #define safe_mutex_assert_owner(mp) \
  451. DBUG_ASSERT((mp)->count > 0 && \
  452. pthread_equal(pthread_self(), (mp)->thread))
  453. #define safe_mutex_assert_not_owner(mp) \
  454. DBUG_ASSERT(! (mp)->count || \
  455. ! pthread_equal(pthread_self(), (mp)->thread))
  456. #else
  457. #define safe_mutex_assert_owner(mp)
  458. #define safe_mutex_assert_not_owner(mp)
  459. #endif /* SAFE_MUTEX */
  460. #if defined(MY_PTHREAD_FASTMUTEX) && !defined(SAFE_MUTEX)
  461. typedef struct st_my_pthread_fastmutex_t
  462. {
  463. pthread_mutex_t mutex;
  464. uint spins;
  465. uint rng_state;
  466. } my_pthread_fastmutex_t;
  467. void fastmutex_global_init(void);
  468. int my_pthread_fastmutex_init(my_pthread_fastmutex_t *mp,
  469. const pthread_mutexattr_t *attr);
  470. int my_pthread_fastmutex_lock(my_pthread_fastmutex_t *mp);
  471. #undef pthread_mutex_init
  472. #undef pthread_mutex_lock
  473. #undef pthread_mutex_unlock
  474. #undef pthread_mutex_destroy
  475. #undef pthread_mutex_wait
  476. #undef pthread_mutex_timedwait
  477. #undef pthread_mutex_t
  478. #undef pthread_cond_wait
  479. #undef pthread_cond_timedwait
  480. #undef pthread_mutex_trylock
  481. #define pthread_mutex_init(A,B) my_pthread_fastmutex_init((A),(B))
  482. #define pthread_mutex_lock(A) my_pthread_fastmutex_lock(A)
  483. #define pthread_mutex_unlock(A) pthread_mutex_unlock(&(A)->mutex)
  484. #define pthread_mutex_destroy(A) pthread_mutex_destroy(&(A)->mutex)
  485. #define pthread_cond_wait(A,B) pthread_cond_wait((A),&(B)->mutex)
  486. #define pthread_cond_timedwait(A,B,C) pthread_cond_timedwait((A),&(B)->mutex,(C))
  487. #define pthread_mutex_trylock(A) pthread_mutex_trylock(&(A)->mutex)
  488. #define pthread_mutex_t my_pthread_fastmutex_t
  489. #endif /* defined(MY_PTHREAD_FASTMUTEX) && !defined(SAFE_MUTEX) */
  490. /* READ-WRITE thread locking */
  491. #ifdef HAVE_BROKEN_RWLOCK /* For OpenUnix */
  492. #undef HAVE_PTHREAD_RWLOCK_RDLOCK
  493. #undef HAVE_RWLOCK_INIT
  494. #undef HAVE_RWLOCK_T
  495. #endif
  496. #if defined(USE_MUTEX_INSTEAD_OF_RW_LOCKS)
  497. /* use these defs for simple mutex locking */
  498. #define rw_lock_t pthread_mutex_t
  499. #define my_rwlock_init(A,B) pthread_mutex_init((A),(B))
  500. #define rw_rdlock(A) pthread_mutex_lock((A))
  501. #define rw_wrlock(A) pthread_mutex_lock((A))
  502. #define rw_tryrdlock(A) pthread_mutex_trylock((A))
  503. #define rw_trywrlock(A) pthread_mutex_trylock((A))
  504. #define rw_unlock(A) pthread_mutex_unlock((A))
  505. #define rwlock_destroy(A) pthread_mutex_destroy((A))
  506. #elif defined(HAVE_PTHREAD_RWLOCK_RDLOCK)
  507. #define rw_lock_t pthread_rwlock_t
  508. #define my_rwlock_init(A,B) pthread_rwlock_init((A),(B))
  509. #define rw_rdlock(A) pthread_rwlock_rdlock(A)
  510. #define rw_wrlock(A) pthread_rwlock_wrlock(A)
  511. #define rw_tryrdlock(A) pthread_rwlock_tryrdlock((A))
  512. #define rw_trywrlock(A) pthread_rwlock_trywrlock((A))
  513. #define rw_unlock(A) pthread_rwlock_unlock(A)
  514. #define rwlock_destroy(A) pthread_rwlock_destroy(A)
  515. #elif defined(HAVE_RWLOCK_INIT)
  516. #ifdef HAVE_RWLOCK_T /* For example Solaris 2.6-> */
  517. #define rw_lock_t rwlock_t
  518. #endif
  519. #define my_rwlock_init(A,B) rwlock_init((A),USYNC_THREAD,0)
  520. #else
  521. /* Use our own version of read/write locks */
  522. typedef struct _my_rw_lock_t {
  523. pthread_mutex_t lock; /* lock for structure */
  524. pthread_cond_t readers; /* waiting readers */
  525. pthread_cond_t writers; /* waiting writers */
  526. int state; /* -1:writer,0:free,>0:readers */
  527. int waiters; /* number of waiting writers */
  528. } my_rw_lock_t;
  529. #define rw_lock_t my_rw_lock_t
  530. #define rw_rdlock(A) my_rw_rdlock((A))
  531. #define rw_wrlock(A) my_rw_wrlock((A))
  532. #define rw_tryrdlock(A) my_rw_tryrdlock((A))
  533. #define rw_trywrlock(A) my_rw_trywrlock((A))
  534. #define rw_unlock(A) my_rw_unlock((A))
  535. #define rwlock_destroy(A) my_rwlock_destroy((A))
  536. extern int my_rwlock_init(my_rw_lock_t *, void *);
  537. extern int my_rwlock_destroy(my_rw_lock_t *);
  538. extern int my_rw_rdlock(my_rw_lock_t *);
  539. extern int my_rw_wrlock(my_rw_lock_t *);
  540. extern int my_rw_unlock(my_rw_lock_t *);
  541. extern int my_rw_tryrdlock(my_rw_lock_t *);
  542. extern int my_rw_trywrlock(my_rw_lock_t *);
  543. #endif /* USE_MUTEX_INSTEAD_OF_RW_LOCKS */
  544. #define GETHOSTBYADDR_BUFF_SIZE 2048
  545. #ifndef HAVE_THR_SETCONCURRENCY
  546. #define thr_setconcurrency(A) pthread_dummy(0)
  547. #endif
  548. #if !defined(HAVE_PTHREAD_ATTR_SETSTACKSIZE) && ! defined(pthread_attr_setstacksize)
  549. #define pthread_attr_setstacksize(A,B) pthread_dummy(0)
  550. #endif
  551. /* Define mutex types, see my_thr_init.c */
  552. #define MY_MUTEX_INIT_SLOW NULL
  553. #ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
  554. extern pthread_mutexattr_t my_fast_mutexattr;
  555. #define MY_MUTEX_INIT_FAST &my_fast_mutexattr
  556. #else
  557. #define MY_MUTEX_INIT_FAST NULL
  558. #endif
  559. #ifdef PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
  560. extern pthread_mutexattr_t my_errorcheck_mutexattr;
  561. #define MY_MUTEX_INIT_ERRCHK &my_errorcheck_mutexattr
  562. #else
  563. #define MY_MUTEX_INIT_ERRCHK NULL
  564. #endif
  565. #ifndef ESRCH
  566. /* Define it to something */
  567. #define ESRCH 1
  568. #endif
  569. typedef ulong my_thread_id;
  570. extern my_bool my_thread_global_init(void);
  571. extern void my_thread_global_end(void);
  572. extern my_bool my_thread_init(void);
  573. extern void my_thread_end(void);
  574. extern const char *my_thread_name(void);
  575. extern my_thread_id my_thread_dbug_id(void);
  576. extern int pthread_no_free(void *);
  577. extern int pthread_dummy(int);
  578. /* All thread specific variables are in the following struct */
  579. #define THREAD_NAME_SIZE 10
  580. #ifndef DEFAULT_THREAD_STACK
  581. #if SIZEOF_CHARP > 4
  582. /*
  583. MySQL can survive with 32K, but some glibc libraries require > 128K stack
  584. To resolve hostnames. Also recursive stored procedures needs stack.
  585. */
  586. #define DEFAULT_THREAD_STACK (256*1024L)
  587. #else
  588. #define DEFAULT_THREAD_STACK (192*1024)
  589. #endif
  590. #endif
  591. struct st_my_thread_var
  592. {
  593. int thr_errno;
  594. pthread_cond_t suspend;
  595. pthread_mutex_t mutex;
  596. pthread_mutex_t * volatile current_mutex;
  597. pthread_cond_t * volatile current_cond;
  598. pthread_t pthread_self;
  599. my_thread_id id;
  600. int cmp_length;
  601. int volatile abort;
  602. my_bool init;
  603. struct st_my_thread_var *next,**prev;
  604. void *opt_info;
  605. #ifndef DBUG_OFF
  606. void *dbug;
  607. char name[THREAD_NAME_SIZE+1];
  608. #endif
  609. };
  610. extern struct st_my_thread_var *_my_thread_var(void) __attribute__ ((const));
  611. extern uint my_thread_end_wait_time;
  612. #define my_thread_var (_my_thread_var())
  613. #define my_errno my_thread_var->thr_errno
  614. /*
  615. Keep track of shutdown,signal, and main threads so that my_end() will not
  616. report errors with them
  617. */
  618. /* Which kind of thread library is in use */
  619. #define THD_LIB_OTHER 1
  620. #define THD_LIB_NPTL 2
  621. #define THD_LIB_LT 4
  622. extern uint thd_lib_detected;
  623. /*
  624. thread_safe_xxx functions are for critical statistic or counters.
  625. The implementation is guaranteed to be thread safe, on all platforms.
  626. Note that the calling code should *not* assume the counter is protected
  627. by the mutex given, as the implementation of these helpers may change
  628. to use my_atomic operations instead.
  629. */
  630. /*
  631. Warning:
  632. When compiling without threads, this file is not included.
  633. See the *other* declarations of thread_safe_xxx in include/my_global.h
  634. Second warning:
  635. See include/config-win.h, for yet another implementation.
  636. */
  637. #ifdef THREAD
  638. #ifndef thread_safe_increment
  639. #define thread_safe_increment(V,L) \
  640. (pthread_mutex_lock((L)), (V)++, pthread_mutex_unlock((L)))
  641. #define thread_safe_decrement(V,L) \
  642. (pthread_mutex_lock((L)), (V)--, pthread_mutex_unlock((L)))
  643. #endif
  644. #ifndef thread_safe_add
  645. #define thread_safe_add(V,C,L) \
  646. (pthread_mutex_lock((L)), (V)+=(C), pthread_mutex_unlock((L)))
  647. #define thread_safe_sub(V,C,L) \
  648. (pthread_mutex_lock((L)), (V)-=(C), pthread_mutex_unlock((L)))
  649. #endif
  650. #endif
  651. /*
  652. statistics_xxx functions are for non critical statistic,
  653. maintained in global variables.
  654. When compiling with SAFE_STATISTICS:
  655. - race conditions can not occur.
  656. - some locking occurs, which may cause performance degradation.
  657. When compiling without SAFE_STATISTICS:
  658. - race conditions can occur, making the result slightly inaccurate.
  659. - the lock given is not honored.
  660. */
  661. #ifdef SAFE_STATISTICS
  662. #define statistic_increment(V,L) thread_safe_increment((V),(L))
  663. #define statistic_decrement(V,L) thread_safe_decrement((V),(L))
  664. #define statistic_add(V,C,L) thread_safe_add((V),(C),(L))
  665. #define statistic_sub(V,C,L) thread_safe_sub((V),(C),(L))
  666. #else
  667. #define statistic_decrement(V,L) (V)--
  668. #define statistic_increment(V,L) (V)++
  669. #define statistic_add(V,C,L) (V)+=(C)
  670. #define statistic_sub(V,C,L) (V)-=(C)
  671. #endif /* SAFE_STATISTICS */
  672. /*
  673. No locking needed, the counter is owned by the thread
  674. */
  675. #define status_var_increment(V) (V)++
  676. #define status_var_decrement(V) (V)--
  677. #define status_var_add(V,C) (V)+=(C)
  678. #define status_var_sub(V,C) (V)-=(C)
  679. #ifdef __cplusplus
  680. }
  681. #endif
  682. #endif /* _my_ptread_h */