my_pthread.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  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; either version 2 of the License, or
  5. (at your option) any later version.
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
  13. /* Defines to make different thread packages compatible */
  14. #ifndef _my_pthread_h
  15. #define _my_pthread_h
  16. #include <errno.h>
  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__) || defined(OS2)
  27. #ifdef OS2
  28. typedef ULONG HANDLE;
  29. typedef ULONG DWORD;
  30. typedef int sigset_t;
  31. #endif
  32. #ifdef OS2
  33. typedef HMTX pthread_mutex_t;
  34. #else
  35. typedef CRITICAL_SECTION pthread_mutex_t;
  36. #endif
  37. typedef HANDLE pthread_t;
  38. typedef struct thread_attr {
  39. DWORD dwStackSize ;
  40. DWORD dwCreatingFlag ;
  41. int priority ;
  42. } pthread_attr_t ;
  43. typedef struct { int dummy; } pthread_condattr_t;
  44. /* Implementation of posix conditions */
  45. typedef struct st_pthread_link {
  46. DWORD thread_id;
  47. struct st_pthread_link *next;
  48. } pthread_link;
  49. typedef struct {
  50. uint32 waiting;
  51. #ifdef OS2
  52. HEV semaphore;
  53. #else
  54. HANDLE semaphore;
  55. #endif
  56. } pthread_cond_t;
  57. #ifndef OS2
  58. struct timespec { /* For pthread_cond_timedwait() */
  59. time_t tv_sec;
  60. long tv_nsec;
  61. };
  62. #endif
  63. typedef int pthread_mutexattr_t;
  64. #define win_pthread_self my_thread_var->pthread_self
  65. #ifdef OS2
  66. #define pthread_handler_t EXTERNC void * _Optlink
  67. typedef void * (_Optlink *pthread_handler)(void *);
  68. #else
  69. #define pthread_handler_t EXTERNC void * __cdecl
  70. typedef void * (__cdecl *pthread_handler)(void *);
  71. #endif
  72. void win_pthread_init(void);
  73. int win_pthread_setspecific(void *A,void *B,uint length);
  74. int pthread_create(pthread_t *,pthread_attr_t *,pthread_handler,void *);
  75. int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr);
  76. int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
  77. int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
  78. struct timespec *abstime);
  79. int pthread_cond_signal(pthread_cond_t *cond);
  80. int pthread_cond_broadcast(pthread_cond_t *cond);
  81. int pthread_cond_destroy(pthread_cond_t *cond);
  82. int pthread_attr_init(pthread_attr_t *connect_att);
  83. int pthread_attr_setstacksize(pthread_attr_t *connect_att,DWORD stack);
  84. int pthread_attr_setprio(pthread_attr_t *connect_att,int priority);
  85. int pthread_attr_destroy(pthread_attr_t *connect_att);
  86. struct tm *localtime_r(const time_t *timep,struct tm *tmp);
  87. struct tm *gmtime_r(const time_t *timep,struct tm *tmp);
  88. void pthread_exit(void *a); /* was #define pthread_exit(A) ExitThread(A)*/
  89. #ifndef OS2
  90. #define ETIMEDOUT 145 /* Win32 doesn't have this */
  91. #define getpid() GetCurrentThreadId()
  92. #endif
  93. #define pthread_self() win_pthread_self
  94. #define HAVE_LOCALTIME_R 1
  95. #define _REENTRANT 1
  96. #define HAVE_PTHREAD_ATTR_SETSTACKSIZE 1
  97. #ifdef USE_TLS /* For LIBMYSQL.DLL */
  98. #undef SAFE_MUTEX /* This will cause conflicts */
  99. #define pthread_key(T,V) DWORD V
  100. #define pthread_key_create(A,B) ((*A=TlsAlloc())==0xFFFFFFFF)
  101. #define pthread_key_delete(A) TlsFree(A)
  102. #define pthread_getspecific(A) (TlsGetValue(A))
  103. #define my_pthread_getspecific(T,A) ((T) TlsGetValue(A))
  104. #define my_pthread_getspecific_ptr(T,V) ((T) TlsGetValue(V))
  105. #define my_pthread_setspecific_ptr(T,V) (!TlsSetValue((T),(V)))
  106. #define pthread_setspecific(A,B) (!TlsSetValue((A),(B)))
  107. #else
  108. #define pthread_key(T,V) __declspec(thread) T V
  109. #define pthread_key_create(A,B) pthread_dummy(0)
  110. #define pthread_key_delete(A) pthread_dummy(0)
  111. #define pthread_getspecific(A) (&(A))
  112. #define my_pthread_getspecific(T,A) (&(A))
  113. #define my_pthread_getspecific_ptr(T,V) (V)
  114. #define my_pthread_setspecific_ptr(T,V) ((T)=(V),0)
  115. #define pthread_setspecific(A,B) win_pthread_setspecific(&(A),(B),sizeof(A))
  116. #endif /* USE_TLS */
  117. #define pthread_equal(A,B) ((A) == (B))
  118. #ifdef OS2
  119. extern int pthread_mutex_init (pthread_mutex_t *, const pthread_mutexattr_t *);
  120. extern int pthread_mutex_lock (pthread_mutex_t *);
  121. extern int pthread_mutex_unlock (pthread_mutex_t *);
  122. extern int pthread_mutex_destroy (pthread_mutex_t *);
  123. #define my_pthread_setprio(A,B) DosSetPriority(PRTYS_THREAD,PRTYC_NOCHANGE, B, A)
  124. #define pthread_kill(A,B) raise(B)
  125. #define pthread_exit(A) pthread_dummy()
  126. #else
  127. #define pthread_mutex_init(A,B) (InitializeCriticalSection(A),0)
  128. #define pthread_mutex_lock(A) (EnterCriticalSection(A),0)
  129. #define pthread_mutex_trylock(A) (WaitForSingleObject((A), 0) == WAIT_TIMEOUT)
  130. #define pthread_mutex_unlock(A) LeaveCriticalSection(A)
  131. #define pthread_mutex_destroy(A) DeleteCriticalSection(A)
  132. #define my_pthread_setprio(A,B) SetThreadPriority(GetCurrentThread(), (B))
  133. #define pthread_kill(A,B) pthread_dummy(0)
  134. #endif /* OS2 */
  135. /* Dummy defines for easier code */
  136. #define pthread_attr_setdetachstate(A,B) pthread_dummy(0)
  137. #define my_pthread_attr_setprio(A,B) pthread_attr_setprio(A,B)
  138. #define pthread_attr_setscope(A,B)
  139. #define pthread_detach_this_thread()
  140. #define pthread_condattr_init(A)
  141. #define pthread_condattr_destroy(A)
  142. /*Irena: compiler does not like this: */
  143. /*#define my_pthread_getprio(pthread_t thread_id) pthread_dummy(0) */
  144. #define my_pthread_getprio(thread_id) pthread_dummy(0)
  145. #elif defined(HAVE_UNIXWARE7_THREADS)
  146. #include <thread.h>
  147. #include <synch.h>
  148. #ifndef _REENTRANT
  149. #define _REENTRANT
  150. #endif
  151. #define HAVE_NONPOSIX_SIGWAIT
  152. #define pthread_t thread_t
  153. #define pthread_cond_t cond_t
  154. #define pthread_mutex_t mutex_t
  155. #define pthread_key_t thread_key_t
  156. typedef int pthread_attr_t; /* Needed by Unixware 7.0.0 */
  157. #define pthread_key_create(A,B) thr_keycreate((A),(B))
  158. #define pthread_key_delete(A) thr_keydelete(A)
  159. #define pthread_handler_t EXTERNC void *
  160. #define pthread_key(T,V) pthread_key_t V
  161. void * my_pthread_getspecific_imp(pthread_key_t key);
  162. #define my_pthread_getspecific(A,B) ((A) my_pthread_getspecific_imp(B))
  163. #define my_pthread_getspecific_ptr(T,V) my_pthread_getspecific(T,V)
  164. #define pthread_setspecific(A,B) thr_setspecific(A,B)
  165. #define my_pthread_setspecific_ptr(T,V) pthread_setspecific(T,V)
  166. #define pthread_create(A,B,C,D) thr_create(NULL,65536L,(C),(D),THR_DETACHED,(A))
  167. #define pthread_cond_init(a,b) cond_init((a),USYNC_THREAD,NULL)
  168. #define pthread_cond_destroy(a) cond_destroy(a)
  169. #define pthread_cond_signal(a) cond_signal(a)
  170. #define pthread_cond_wait(a,b) cond_wait((a),(b))
  171. #define pthread_cond_timedwait(a,b,c) cond_timedwait((a),(b),(c))
  172. #define pthread_cond_broadcast(a) cond_broadcast(a)
  173. #define pthread_mutex_init(a,b) mutex_init((a),USYNC_THREAD,NULL)
  174. #define pthread_mutex_lock(a) mutex_lock(a)
  175. #define pthread_mutex_unlock(a) mutex_unlock(a)
  176. #define pthread_mutex_destroy(a) mutex_destroy(a)
  177. #define pthread_self() thr_self()
  178. #define pthread_exit(A) thr_exit(A)
  179. #define pthread_equal(A,B) (((A) == (B)) ? 1 : 0)
  180. #define pthread_kill(A,B) thr_kill((A),(B))
  181. #define HAVE_PTHREAD_KILL
  182. #define pthread_sigmask(A,B,C) thr_sigsetmask((A),(B),(C))
  183. extern int my_sigwait(const sigset_t *set,int *sig);
  184. #define pthread_detach_this_thread() pthread_dummy(0)
  185. #define pthread_attr_init(A) pthread_dummy(0)
  186. #define pthread_attr_destroy(A) pthread_dummy(0)
  187. #define pthread_attr_setscope(A,B) pthread_dummy(0)
  188. #define pthread_attr_setdetachstate(A,B) pthread_dummy(0)
  189. #define my_pthread_setprio(A,B) pthread_dummy (0)
  190. #define my_pthread_getprio(A) pthread_dummy (0)
  191. #define my_pthread_attr_setprio(A,B) pthread_dummy(0)
  192. #else /* Normal threads */
  193. #ifdef HAVE_rts_threads
  194. #define sigwait org_sigwait
  195. #include <signal.h>
  196. #undef sigwait
  197. #endif
  198. #include <pthread.h>
  199. #ifndef _REENTRANT
  200. #define _REENTRANT
  201. #endif
  202. #ifdef HAVE_THR_SETCONCURRENCY
  203. #include <thread.h> /* Probably solaris */
  204. #endif
  205. #ifdef HAVE_SCHED_H
  206. #include <sched.h>
  207. #endif
  208. #ifdef HAVE_SYNCH_H
  209. #include <synch.h>
  210. #endif
  211. #if defined(__EMX__) && (!defined(EMX_PTHREAD_REV) || (EMX_PTHREAD_REV < 2))
  212. #error Requires at least rev 2 of EMX pthreads library.
  213. #endif
  214. #ifdef __NETWARE__
  215. void my_pthread_exit(void *status);
  216. #define pthread_exit(A) my_pthread_exit(A)
  217. #endif
  218. extern int my_pthread_getprio(pthread_t thread_id);
  219. #define pthread_key(T,V) pthread_key_t V
  220. #define my_pthread_getspecific_ptr(T,V) my_pthread_getspecific(T,(V))
  221. #define my_pthread_setspecific_ptr(T,V) pthread_setspecific(T,(void*) (V))
  222. #define pthread_detach_this_thread()
  223. #define pthread_handler_t EXTERNC void *
  224. typedef void *(* pthread_handler)(void *);
  225. /* Test first for RTS or FSU threads */
  226. #if defined(PTHREAD_SCOPE_GLOBAL) && !defined(PTHREAD_SCOPE_SYSTEM)
  227. #define HAVE_rts_threads
  228. extern int my_pthread_create_detached;
  229. #define pthread_sigmask(A,B,C) sigprocmask((A),(B),(C))
  230. #define PTHREAD_CREATE_DETACHED &my_pthread_create_detached
  231. #define PTHREAD_SCOPE_SYSTEM PTHREAD_SCOPE_GLOBAL
  232. #define PTHREAD_SCOPE_PROCESS PTHREAD_SCOPE_LOCAL
  233. #define USE_ALARM_THREAD
  234. #elif defined(HAVE_mit_thread)
  235. #define USE_ALARM_THREAD
  236. #undef HAVE_LOCALTIME_R
  237. #define HAVE_LOCALTIME_R
  238. #undef HAVE_GMTIME_R
  239. #define HAVE_GMTIME_R
  240. #undef HAVE_PTHREAD_ATTR_SETSCOPE
  241. #define HAVE_PTHREAD_ATTR_SETSCOPE
  242. #undef HAVE_GETHOSTBYNAME_R_GLIBC2_STYLE /* If we are running linux */
  243. #undef HAVE_RWLOCK_T
  244. #undef HAVE_RWLOCK_INIT
  245. #undef HAVE_PTHREAD_RWLOCK_RDLOCK
  246. #undef HAVE_SNPRINTF
  247. #define my_pthread_attr_setprio(A,B)
  248. #endif /* defined(PTHREAD_SCOPE_GLOBAL) && !defined(PTHREAD_SCOPE_SYSTEM) */
  249. #if defined(_BSDI_VERSION) && _BSDI_VERSION < 199910
  250. int sigwait(sigset_t *set, int *sig);
  251. #endif
  252. #ifndef HAVE_NONPOSIX_SIGWAIT
  253. #define my_sigwait(A,B) sigwait((A),(B))
  254. #else
  255. int my_sigwait(const sigset_t *set,int *sig);
  256. #endif
  257. #ifdef HAVE_NONPOSIX_PTHREAD_MUTEX_INIT
  258. #ifndef SAFE_MUTEX
  259. #define pthread_mutex_init(a,b) my_pthread_mutex_init((a),(b))
  260. extern int my_pthread_mutex_init(pthread_mutex_t *mp,
  261. const pthread_mutexattr_t *attr);
  262. #endif /* SAFE_MUTEX */
  263. #define pthread_cond_init(a,b) my_pthread_cond_init((a),(b))
  264. extern int my_pthread_cond_init(pthread_cond_t *mp,
  265. const pthread_condattr_t *attr);
  266. #endif /* HAVE_NONPOSIX_PTHREAD_MUTEX_INIT */
  267. #if defined(HAVE_SIGTHREADMASK) && !defined(HAVE_PTHREAD_SIGMASK)
  268. #define pthread_sigmask(A,B,C) sigthreadmask((A),(B),(C))
  269. #endif
  270. #if !defined(HAVE_SIGWAIT) && !defined(HAVE_mit_thread) && !defined(HAVE_rts_threads) && !defined(sigwait) && !defined(alpha_linux_port) && !defined(HAVE_NONPOSIX_SIGWAIT) && !defined(HAVE_DEC_3_2_THREADS) && !defined(_AIX)
  271. int sigwait(sigset_t *setp, int *sigp); /* Use our implemention */
  272. #endif
  273. /*
  274. We define my_sigset() and use that instead of the system sigset() so that
  275. we can favor an implementation based on sigaction(). On some systems, such
  276. as Mac OS X, sigset() results in flags such as SA_RESTART being set, and
  277. we want to make sure that no such flags are set.
  278. */
  279. #if defined(HAVE_SIGACTION) && !defined(my_sigset)
  280. #define my_sigset(A,B) do { struct sigaction s; sigset_t set; \
  281. sigemptyset(&set); \
  282. s.sa_handler = (B); \
  283. s.sa_mask = set; \
  284. s.sa_flags = 0; \
  285. sigaction((A), &s, (struct sigaction *) NULL); \
  286. } while (0)
  287. #elif defined(HAVE_SIGSET) && !defined(my_sigset)
  288. #define my_sigset(A,B) sigset((A),(B))
  289. #elif !defined(my_sigset)
  290. #define my_sigset(A,B) signal((A),(B))
  291. #endif
  292. #ifndef my_pthread_setprio
  293. #if defined(HAVE_PTHREAD_SETPRIO_NP) /* FSU threads */
  294. #define my_pthread_setprio(A,B) pthread_setprio_np((A),(B))
  295. #elif defined(HAVE_PTHREAD_SETPRIO)
  296. #define my_pthread_setprio(A,B) pthread_setprio((A),(B))
  297. #else
  298. extern void my_pthread_setprio(pthread_t thread_id,int prior);
  299. #endif
  300. #endif
  301. #ifndef my_pthread_attr_setprio
  302. #ifdef HAVE_PTHREAD_ATTR_SETPRIO
  303. #define my_pthread_attr_setprio(A,B) pthread_attr_setprio((A),(B))
  304. #else
  305. extern void my_pthread_attr_setprio(pthread_attr_t *attr, int priority);
  306. #endif
  307. #endif
  308. #if !defined(HAVE_PTHREAD_ATTR_SETSCOPE) || defined(HAVE_DEC_3_2_THREADS)
  309. #define pthread_attr_setscope(A,B)
  310. #undef HAVE_GETHOSTBYADDR_R /* No definition */
  311. #endif
  312. #if defined(HAVE_BROKEN_PTHREAD_COND_TIMEDWAIT) && !defined(SAFE_MUTEX)
  313. extern int my_pthread_cond_timedwait(pthread_cond_t *cond,
  314. pthread_mutex_t *mutex,
  315. struct timespec *abstime);
  316. #define pthread_cond_timedwait(A,B,C) my_pthread_cond_timedwait((A),(B),(C))
  317. #endif
  318. #if defined(OS2)
  319. #define my_pthread_getspecific(T,A) ((T) &(A))
  320. #define pthread_setspecific(A,B) win_pthread_setspecific(&(A),(B),sizeof(A))
  321. #elif !defined( HAVE_NONPOSIX_PTHREAD_GETSPECIFIC)
  322. #define my_pthread_getspecific(A,B) ((A) pthread_getspecific(B))
  323. #else
  324. #define my_pthread_getspecific(A,B) ((A) my_pthread_getspecific_imp(B))
  325. void *my_pthread_getspecific_imp(pthread_key_t key);
  326. #endif /* OS2 */
  327. #ifndef HAVE_LOCALTIME_R
  328. struct tm *localtime_r(const time_t *clock, struct tm *res);
  329. #endif
  330. #ifndef HAVE_GMTIME_R
  331. struct tm *gmtime_r(const time_t *clock, struct tm *res);
  332. #endif
  333. #ifdef HAVE_PTHREAD_CONDATTR_CREATE
  334. /* DCE threads on HPUX 10.20 */
  335. #define pthread_condattr_init pthread_condattr_create
  336. #define pthread_condattr_destroy pthread_condattr_delete
  337. #endif
  338. /* FSU THREADS */
  339. #if !defined(HAVE_PTHREAD_KEY_DELETE) && !defined(pthread_key_delete)
  340. #define pthread_key_delete(A) pthread_dummy(0)
  341. #endif
  342. #ifdef HAVE_CTHREADS_WRAPPER /* For MacOSX */
  343. #define pthread_cond_destroy(A) pthread_dummy(0)
  344. #define pthread_mutex_destroy(A) pthread_dummy(0)
  345. #define pthread_attr_delete(A) pthread_dummy(0)
  346. #define pthread_condattr_delete(A) pthread_dummy(0)
  347. #define pthread_attr_setstacksize(A,B) pthread_dummy(0)
  348. #define pthread_equal(A,B) ((A) == (B))
  349. #define pthread_cond_timedwait(a,b,c) pthread_cond_wait((a),(b))
  350. #define pthread_attr_init(A) pthread_attr_create(A)
  351. #define pthread_attr_destroy(A) pthread_attr_delete(A)
  352. #define pthread_attr_setdetachstate(A,B) pthread_dummy(0)
  353. #define pthread_create(A,B,C,D) pthread_create((A),*(B),(C),(D))
  354. #define pthread_sigmask(A,B,C) sigprocmask((A),(B),(C))
  355. #define pthread_kill(A,B) pthread_dummy(0)
  356. #undef pthread_detach_this_thread
  357. #define pthread_detach_this_thread() { pthread_t tmp=pthread_self() ; pthread_detach(&tmp); }
  358. #endif
  359. #ifdef HAVE_DARWIN5_THREADS
  360. #define pthread_sigmask(A,B,C) sigprocmask((A),(B),(C))
  361. #define pthread_kill(A,B) pthread_dummy(0)
  362. #define pthread_condattr_init(A) pthread_dummy(0)
  363. #define pthread_condattr_destroy(A) pthread_dummy(0)
  364. #undef pthread_detach_this_thread
  365. #define pthread_detach_this_thread() { pthread_t tmp=pthread_self() ; pthread_detach(tmp); }
  366. #endif
  367. #if ((defined(HAVE_PTHREAD_ATTR_CREATE) && !defined(HAVE_SIGWAIT)) || defined(HAVE_DEC_3_2_THREADS)) && !defined(HAVE_CTHREADS_WRAPPER)
  368. /* This is set on AIX_3_2 and Siemens unix (and DEC OSF/1 3.2 too) */
  369. #define pthread_key_create(A,B) \
  370. pthread_keycreate(A,(B) ?\
  371. (pthread_destructor_t) (B) :\
  372. (pthread_destructor_t) pthread_dummy)
  373. #define pthread_attr_init(A) pthread_attr_create(A)
  374. #define pthread_attr_destroy(A) pthread_attr_delete(A)
  375. #define pthread_attr_setdetachstate(A,B) pthread_dummy(0)
  376. #define pthread_create(A,B,C,D) pthread_create((A),*(B),(C),(D))
  377. #ifndef pthread_sigmask
  378. #define pthread_sigmask(A,B,C) sigprocmask((A),(B),(C))
  379. #endif
  380. #define pthread_kill(A,B) pthread_dummy(0)
  381. #undef pthread_detach_this_thread
  382. #define pthread_detach_this_thread() { pthread_t tmp=pthread_self() ; pthread_detach(&tmp); }
  383. #elif !defined(__NETWARE__) /* HAVE_PTHREAD_ATTR_CREATE && !HAVE_SIGWAIT */
  384. #define HAVE_PTHREAD_KILL
  385. #endif
  386. #endif /* defined(__WIN__) */
  387. #if defined(HPUX10) && !defined(DONT_REMAP_PTHREAD_FUNCTIONS)
  388. #undef pthread_cond_timedwait
  389. #define pthread_cond_timedwait(a,b,c) my_pthread_cond_timedwait((a),(b),(c))
  390. int my_pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
  391. struct timespec *abstime);
  392. #endif
  393. #if defined(HPUX10)
  394. #define pthread_attr_getstacksize(A,B) my_pthread_attr_getstacksize(A,B)
  395. void my_pthread_attr_getstacksize(pthread_attr_t *attrib, size_t *size);
  396. #endif
  397. #if defined(HAVE_POSIX1003_4a_MUTEX) && !defined(DONT_REMAP_PTHREAD_FUNCTIONS)
  398. #undef pthread_mutex_trylock
  399. #define pthread_mutex_trylock(a) my_pthread_mutex_trylock((a))
  400. int my_pthread_mutex_trylock(pthread_mutex_t *mutex);
  401. #endif
  402. /* safe_mutex adds checking to mutex for easier debugging */
  403. #if defined(__NETWARE__) && !defined(SAFE_MUTEX_DETECT_DESTROY)
  404. #define SAFE_MUTEX_DETECT_DESTROY
  405. #endif
  406. typedef struct st_safe_mutex_t
  407. {
  408. pthread_mutex_t global,mutex;
  409. const char *file;
  410. uint line,count;
  411. pthread_t thread;
  412. #ifdef SAFE_MUTEX_DETECT_DESTROY
  413. struct st_safe_mutex_info_t *info; /* to track destroying of mutexes */
  414. #endif
  415. } safe_mutex_t;
  416. #ifdef SAFE_MUTEX_DETECT_DESTROY
  417. /*
  418. Used to track the destroying of mutexes. This needs to be a seperate
  419. structure because the safe_mutex_t structure could be freed before
  420. the mutexes are destroyed.
  421. */
  422. typedef struct st_safe_mutex_info_t
  423. {
  424. struct st_safe_mutex_info_t *next;
  425. struct st_safe_mutex_info_t *prev;
  426. const char *init_file;
  427. uint32 init_line;
  428. } safe_mutex_info_t;
  429. #endif /* SAFE_MUTEX_DETECT_DESTROY */
  430. int safe_mutex_init(safe_mutex_t *mp, const pthread_mutexattr_t *attr,
  431. const char *file, uint line);
  432. int safe_mutex_lock(safe_mutex_t *mp,const char *file, uint line);
  433. int safe_mutex_unlock(safe_mutex_t *mp,const char *file, uint line);
  434. int safe_mutex_destroy(safe_mutex_t *mp,const char *file, uint line);
  435. int safe_cond_wait(pthread_cond_t *cond, safe_mutex_t *mp,const char *file,
  436. uint line);
  437. int safe_cond_timedwait(pthread_cond_t *cond, safe_mutex_t *mp,
  438. struct timespec *abstime, const char *file, uint line);
  439. void safe_mutex_global_init(void);
  440. void safe_mutex_end(FILE *file);
  441. /* Wrappers if safe mutex is actually used */
  442. #ifdef SAFE_MUTEX
  443. #undef pthread_mutex_init
  444. #undef pthread_mutex_lock
  445. #undef pthread_mutex_unlock
  446. #undef pthread_mutex_destroy
  447. #undef pthread_mutex_wait
  448. #undef pthread_mutex_timedwait
  449. #undef pthread_mutex_t
  450. #undef pthread_cond_wait
  451. #undef pthread_cond_timedwait
  452. #undef pthread_mutex_trylock
  453. #define pthread_mutex_init(A,B) safe_mutex_init((A),(B),__FILE__,__LINE__)
  454. #define pthread_mutex_lock(A) safe_mutex_lock((A),__FILE__,__LINE__)
  455. #define pthread_mutex_unlock(A) safe_mutex_unlock((A),__FILE__,__LINE__)
  456. #define pthread_mutex_destroy(A) safe_mutex_destroy((A),__FILE__,__LINE__)
  457. #define pthread_cond_wait(A,B) safe_cond_wait((A),(B),__FILE__,__LINE__)
  458. #define pthread_cond_timedwait(A,B,C) safe_cond_timedwait((A),(B),(C),__FILE__,__LINE__)
  459. #define pthread_mutex_trylock(A) pthread_mutex_lock(A)
  460. #define pthread_mutex_t safe_mutex_t
  461. #define safe_mutex_assert_owner(mp) \
  462. DBUG_ASSERT((mp)->count > 0 && \
  463. pthread_equal(pthread_self(), (mp)->thread))
  464. #define safe_mutex_assert_not_owner(mp) \
  465. DBUG_ASSERT(! (mp)->count || \
  466. ! pthread_equal(pthread_self(), (mp)->thread))
  467. #else
  468. #define safe_mutex_assert_owner(mp)
  469. #define safe_mutex_assert_not_owner(mp)
  470. #endif /* SAFE_MUTEX */
  471. /* READ-WRITE thread locking */
  472. #ifdef HAVE_BROKEN_RWLOCK /* For OpenUnix */
  473. #undef HAVE_PTHREAD_RWLOCK_RDLOCK
  474. #undef HAVE_RWLOCK_INIT
  475. #undef HAVE_RWLOCK_T
  476. #endif
  477. #if defined(USE_MUTEX_INSTEAD_OF_RW_LOCKS)
  478. /* use these defs for simple mutex locking */
  479. #define rw_lock_t pthread_mutex_t
  480. #define my_rwlock_init(A,B) pthread_mutex_init((A),(B))
  481. #define rw_rdlock(A) pthread_mutex_lock((A))
  482. #define rw_wrlock(A) pthread_mutex_lock((A))
  483. #define rw_tryrdlock(A) pthread_mutex_trylock((A))
  484. #define rw_trywrlock(A) pthread_mutex_trylock((A))
  485. #define rw_unlock(A) pthread_mutex_unlock((A))
  486. #define rwlock_destroy(A) pthread_mutex_destroy((A))
  487. #elif defined(HAVE_PTHREAD_RWLOCK_RDLOCK)
  488. #define rw_lock_t pthread_rwlock_t
  489. #define my_rwlock_init(A,B) pthread_rwlock_init((A),(B))
  490. #define rw_rdlock(A) pthread_rwlock_rdlock(A)
  491. #define rw_wrlock(A) pthread_rwlock_wrlock(A)
  492. #define rw_tryrdlock(A) pthread_rwlock_tryrdlock((A))
  493. #define rw_trywrlock(A) pthread_rwlock_trywrlock((A))
  494. #define rw_unlock(A) pthread_rwlock_unlock(A)
  495. #define rwlock_destroy(A) pthread_rwlock_destroy(A)
  496. #elif defined(HAVE_RWLOCK_INIT)
  497. #ifdef HAVE_RWLOCK_T /* For example Solaris 2.6-> */
  498. #define rw_lock_t rwlock_t
  499. #endif
  500. #define my_rwlock_init(A,B) rwlock_init((A),USYNC_THREAD,0)
  501. #else
  502. /* Use our own version of read/write locks */
  503. typedef struct _my_rw_lock_t {
  504. pthread_mutex_t lock; /* lock for structure */
  505. pthread_cond_t readers; /* waiting readers */
  506. pthread_cond_t writers; /* waiting writers */
  507. int state; /* -1:writer,0:free,>0:readers */
  508. int waiters; /* number of waiting writers */
  509. } my_rw_lock_t;
  510. #define rw_lock_t my_rw_lock_t
  511. #define rw_rdlock(A) my_rw_rdlock((A))
  512. #define rw_wrlock(A) my_rw_wrlock((A))
  513. #define rw_tryrdlock(A) my_rw_tryrdlock((A))
  514. #define rw_trywrlock(A) my_rw_trywrlock((A))
  515. #define rw_unlock(A) my_rw_unlock((A))
  516. #define rwlock_destroy(A) my_rwlock_destroy((A))
  517. extern int my_rwlock_init(my_rw_lock_t *, void *);
  518. extern int my_rwlock_destroy(my_rw_lock_t *);
  519. extern int my_rw_rdlock(my_rw_lock_t *);
  520. extern int my_rw_wrlock(my_rw_lock_t *);
  521. extern int my_rw_unlock(my_rw_lock_t *);
  522. extern int my_rw_tryrdlock(my_rw_lock_t *);
  523. extern int my_rw_trywrlock(my_rw_lock_t *);
  524. #endif /* USE_MUTEX_INSTEAD_OF_RW_LOCKS */
  525. #define GETHOSTBYADDR_BUFF_SIZE 2048
  526. #ifndef HAVE_THR_SETCONCURRENCY
  527. #define thr_setconcurrency(A) pthread_dummy(0)
  528. #endif
  529. #if !defined(HAVE_PTHREAD_ATTR_SETSTACKSIZE) && ! defined(pthread_attr_setstacksize)
  530. #define pthread_attr_setstacksize(A,B) pthread_dummy(0)
  531. #endif
  532. /* Define mutex types, see my_thr_init.c */
  533. #define MY_MUTEX_INIT_SLOW NULL
  534. #ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
  535. extern pthread_mutexattr_t my_fast_mutexattr;
  536. #define MY_MUTEX_INIT_FAST &my_fast_mutexattr
  537. #else
  538. #define MY_MUTEX_INIT_FAST NULL
  539. #endif
  540. #ifdef PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
  541. extern pthread_mutexattr_t my_errorcheck_mutexattr;
  542. #define MY_MUTEX_INIT_ERRCHK &my_errorcheck_mutexattr
  543. #else
  544. #define MY_MUTEX_INIT_ERRCHK NULL
  545. #endif
  546. extern my_bool my_thread_global_init(void);
  547. extern void my_thread_global_end(void);
  548. extern my_bool my_thread_init(void);
  549. extern void my_thread_end(void);
  550. extern const char *my_thread_name(void);
  551. extern long my_thread_id(void);
  552. extern int pthread_no_free(void *);
  553. extern int pthread_dummy(int);
  554. /* All thread specific variables are in the following struct */
  555. #define THREAD_NAME_SIZE 10
  556. #ifndef DEFAULT_THREAD_STACK
  557. #if SIZEOF_CHARP > 4
  558. /*
  559. MySQL can survive with 32K, but some glibc libraries require > 128K stack
  560. To resolve hostnames. Also recursive stored procedures needs stack.
  561. */
  562. #define DEFAULT_THREAD_STACK (256*1024L)
  563. #else
  564. #define DEFAULT_THREAD_STACK (192*1024)
  565. #endif
  566. #endif
  567. struct st_my_thread_var
  568. {
  569. int thr_errno;
  570. pthread_cond_t suspend;
  571. pthread_mutex_t mutex;
  572. pthread_mutex_t * volatile current_mutex;
  573. pthread_cond_t * volatile current_cond;
  574. pthread_t pthread_self;
  575. long id;
  576. int cmp_length;
  577. int volatile abort;
  578. my_bool init;
  579. struct st_my_thread_var *next,**prev;
  580. void *opt_info;
  581. #ifndef DBUG_OFF
  582. gptr dbug;
  583. char name[THREAD_NAME_SIZE+1];
  584. #endif
  585. };
  586. extern struct st_my_thread_var *_my_thread_var(void) __attribute__ ((const));
  587. #define my_thread_var (_my_thread_var())
  588. #define my_errno my_thread_var->thr_errno
  589. /*
  590. Keep track of shutdown,signal, and main threads so that my_end() will not
  591. report errors with them
  592. */
  593. extern pthread_t shutdown_th, main_th, signal_th;
  594. /* statistics_xxx functions are for not essential statistic */
  595. #ifndef thread_safe_increment
  596. #ifdef HAVE_ATOMIC_ADD
  597. #define thread_safe_increment(V,L) atomic_inc((atomic_t*) &V)
  598. #define thread_safe_decrement(V,L) atomic_dec((atomic_t*) &V)
  599. #define thread_safe_add(V,C,L) atomic_add((C),(atomic_t*) &V)
  600. #define thread_safe_sub(V,C,L) atomic_sub((C),(atomic_t*) &V)
  601. #else
  602. #define thread_safe_increment(V,L) \
  603. (pthread_mutex_lock((L)), (V)++, pthread_mutex_unlock((L)))
  604. #define thread_safe_decrement(V,L) \
  605. (pthread_mutex_lock((L)), (V)--, pthread_mutex_unlock((L)))
  606. #define thread_safe_add(V,C,L) (pthread_mutex_lock((L)), (V)+=(C), pthread_mutex_unlock((L)))
  607. #define thread_safe_sub(V,C,L) \
  608. (pthread_mutex_lock((L)), (V)-=(C), pthread_mutex_unlock((L)))
  609. #endif /* HAVE_ATOMIC_ADD */
  610. #ifdef SAFE_STATISTICS
  611. #define statistic_increment(V,L) thread_safe_increment((V),(L))
  612. #define statistic_decrement(V,L) thread_safe_decrement((V),(L))
  613. #define statistic_add(V,C,L) thread_safe_add((V),(C),(L))
  614. #else
  615. #define statistic_decrement(V,L) (V)--
  616. #define statistic_increment(V,L) (V)++
  617. #define statistic_add(V,C,L) (V)+=(C)
  618. #endif /* SAFE_STATISTICS */
  619. #endif /* thread_safe_increment */
  620. #ifdef __cplusplus
  621. }
  622. #endif
  623. #endif /* _my_ptread_h */