my_pthread.h
上传用户:dzyhzl
上传日期:2019-04-29
资源大小:56270k
文件大小:23k
源码类别:

模拟服务器

开发平台:

C/C++

  1. /* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
  2.    
  3.    This library is free software; you can redistribute it and/or
  4.    modify it under the terms of the GNU Library General Public
  5.    License as published by the Free Software Foundation; either
  6.    version 2 of the License, or (at your option) any later version.
  7.    
  8.    This library is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  11.    Library General Public License for more details.
  12.    
  13.    You should have received a copy of the GNU Library General Public
  14.    License along with this library; if not, write to the Free
  15.    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  16.    MA 02111-1307, USA */
  17. /* Defines to make different thread packages compatible */
  18. #ifndef _my_pthread_h
  19. #define _my_pthread_h
  20. #include <errno.h>
  21. #ifndef ETIME
  22. #define ETIME ETIMEDOUT /* For FreeBSD */
  23. #endif
  24. #ifdef  __cplusplus
  25. extern "C" {
  26. #endif /* __cplusplus */ 
  27. #if defined(__WIN__) || defined(OS2)
  28. #ifdef OS2
  29. typedef ULONG     HANDLE;
  30. typedef ULONG     DWORD;
  31. typedef int sigset_t;
  32. #endif
  33. #ifdef OS2
  34. typedef HMTX             pthread_mutex_t;
  35. #else
  36. typedef CRITICAL_SECTION pthread_mutex_t;
  37. #endif
  38. typedef HANDLE  pthread_t;
  39. typedef struct thread_attr {
  40.     DWORD dwStackSize ;
  41.     DWORD dwCreatingFlag ;
  42.     int priority ;
  43. } pthread_attr_t ;
  44. typedef struct { int dummy; } pthread_condattr_t;
  45. /* Implementation of posix conditions */
  46. typedef struct st_pthread_link {
  47.   DWORD thread_id;
  48.   struct st_pthread_link *next;
  49. } pthread_link;
  50. typedef struct {
  51.   uint32 waiting;
  52. #ifdef OS2
  53.   HEV    semaphore;
  54. #else
  55.   HANDLE semaphore;
  56. #endif
  57. } pthread_cond_t;
  58. #ifndef OS2
  59. struct timespec { /* For pthread_cond_timedwait() */
  60.     time_t tv_sec;
  61.     long tv_nsec;
  62. };
  63. #endif
  64. typedef int pthread_mutexattr_t;
  65. #define win_pthread_self my_thread_var->pthread_self
  66. #ifdef OS2
  67. #define pthread_handler_decl(A,B) void * _Optlink A(void *B)
  68. typedef void * (_Optlink *pthread_handler)(void *);
  69. #else
  70. #define pthread_handler_decl(A,B) void * __cdecl A(void *B)
  71. typedef void * (__cdecl *pthread_handler)(void *);
  72. #endif
  73. void win_pthread_init(void);
  74. int win_pthread_setspecific(void *A,void *B,uint length);
  75. int pthread_create(pthread_t *,pthread_attr_t *,pthread_handler,void *);
  76. int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr);
  77. int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
  78. int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
  79.    struct timespec *abstime);
  80. int pthread_cond_signal(pthread_cond_t *cond);
  81. int pthread_cond_broadcast(pthread_cond_t *cond);
  82. int pthread_cond_destroy(pthread_cond_t *cond);
  83. int pthread_attr_init(pthread_attr_t *connect_att);
  84. int pthread_attr_setstacksize(pthread_attr_t *connect_att,DWORD stack);
  85. int pthread_attr_setprio(pthread_attr_t *connect_att,int priority);
  86. int pthread_attr_destroy(pthread_attr_t *connect_att);
  87. struct tm *localtime_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_getspecific(A) (TlsGetValue(A))
  102. #define my_pthread_getspecific(T,A) ((T) TlsGetValue(A))
  103. #define my_pthread_getspecific_ptr(T,V) ((T) TlsGetValue(V))
  104. #define my_pthread_setspecific_ptr(T,V) (!TlsSetValue((T),(V)))
  105. #define pthread_setspecific(A,B) (!TlsSetValue((A),(B)))
  106. #else
  107. #define pthread_key(T,V) __declspec(thread) T V
  108. #define pthread_key_create(A,B) pthread_dummy(0)
  109. #define pthread_getspecific(A) (&(A))
  110. #define my_pthread_getspecific(T,A) (&(A))
  111. #define my_pthread_getspecific_ptr(T,V) (V)
  112. #define my_pthread_setspecific_ptr(T,V) ((T)=(V),0)
  113. #define pthread_setspecific(A,B) win_pthread_setspecific(&(A),(B),sizeof(A))
  114. #endif /* USE_TLS */
  115. #define pthread_equal(A,B) ((A) == (B))
  116. #ifdef OS2
  117. extern int pthread_mutex_init (pthread_mutex_t *, const pthread_mutexattr_t *);
  118. extern int pthread_mutex_lock (pthread_mutex_t *);
  119. extern int pthread_mutex_unlock (pthread_mutex_t *);
  120. extern int pthread_mutex_destroy (pthread_mutex_t *);
  121. #define my_pthread_setprio(A,B)  DosSetPriority(PRTYS_THREAD,PRTYC_NOCHANGE, B, A)
  122. #define pthread_kill(A,B) raise(B)
  123. #define pthread_exit(A) pthread_dummy()
  124. #else
  125. #define pthread_mutex_init(A,B)  InitializeCriticalSection(A)
  126. #define pthread_mutex_lock(A)  (EnterCriticalSection(A),0)
  127. #define pthread_mutex_trylock(A) (WaitForSingleObject((A), 0) == WAIT_TIMEOUT)
  128. #define pthread_mutex_unlock(A)  LeaveCriticalSection(A)
  129. #define pthread_mutex_destroy(A) DeleteCriticalSection(A)
  130. #define my_pthread_setprio(A,B)  SetThreadPriority(GetCurrentThread(), (B))
  131. #define pthread_kill(A,B) pthread_dummy(0)
  132. #endif /* OS2 */
  133. /* Dummy defines for easier code */
  134. #define pthread_attr_setdetachstate(A,B) pthread_dummy(0)
  135. #define my_pthread_attr_setprio(A,B) pthread_attr_setprio(A,B)
  136. #define pthread_attr_setscope(A,B)
  137. #define pthread_detach_this_thread()
  138. #define pthread_condattr_init(A)
  139. #define pthread_condattr_destroy(A)
  140. /*Irena: compiler does not like this: */
  141. /*#define my_pthread_getprio(pthread_t thread_id) pthread_dummy(0) */
  142. #define my_pthread_getprio(thread_id) pthread_dummy(0)
  143. #elif defined(HAVE_UNIXWARE7_THREADS)
  144. #include <thread.h>
  145. #include <synch.h>
  146. #ifndef _REENTRANT
  147. #define _REENTRANT
  148. #endif
  149. #define HAVE_NONPOSIX_SIGWAIT
  150. #define pthread_t thread_t
  151. #define pthread_cond_t cond_t
  152. #define pthread_mutex_t mutex_t
  153. #define pthread_key_t thread_key_t
  154. typedef int pthread_attr_t; /* Needed by Unixware 7.0.0 */
  155. #define pthread_key_create(A,B) thr_keycreate((A),(B))
  156. #define pthread_handler_decl(A,B) void *A(void *B)
  157. #define pthread_key(T,V) pthread_key_t V
  158. void * my_pthread_getspecific_imp(pthread_key_t key);
  159. #define my_pthread_getspecific(A,B) ((A) my_pthread_getspecific_imp(B))
  160. #define my_pthread_getspecific_ptr(T,V) my_pthread_getspecific(T,V)
  161. #define pthread_setspecific(A,B) thr_setspecific(A,B)
  162. #define my_pthread_setspecific_ptr(T,V) pthread_setspecific(T,V)
  163. #define pthread_create(A,B,C,D) thr_create(NULL,65536L,(C),(D),THR_DETACHED,(A))
  164. #define pthread_cond_init(a,b) cond_init((a),USYNC_THREAD,NULL)
  165. #define pthread_cond_destroy(a) cond_destroy(a)
  166. #define pthread_cond_signal(a) cond_signal(a)
  167. #define pthread_cond_wait(a,b) cond_wait((a),(b))
  168. #define pthread_cond_timedwait(a,b,c) cond_timedwait((a),(b),(c))
  169. #define pthread_cond_broadcast(a) cond_broadcast(a)
  170. #define pthread_mutex_init(a,b) mutex_init((a),USYNC_THREAD,NULL)
  171. #define pthread_mutex_lock(a) mutex_lock(a)
  172. #define pthread_mutex_unlock(a) mutex_unlock(a)
  173. #define pthread_mutex_destroy(a) mutex_destroy(a)
  174. #define pthread_self() thr_self()
  175. #define pthread_exit(A) thr_exit(A)
  176. #define pthread_equal(A,B) (((A) == (B)) ? 1 : 0)
  177. #define pthread_kill(A,B) thr_kill((A),(B))
  178. #define HAVE_PTHREAD_KILL
  179. #define pthread_sigmask(A,B,C) thr_sigsetmask((A),(B),(C))
  180. extern int my_sigwait(const sigset_t *set,int *sig);
  181. #define pthread_detach_this_thread() pthread_dummy(0)
  182. #define pthread_attr_init(A) pthread_dummy(0)
  183. #define pthread_attr_destroy(A) pthread_dummy(0)
  184. #define pthread_attr_setscope(A,B) pthread_dummy(0)
  185. #define pthread_attr_setdetachstate(A,B) pthread_dummy(0)
  186. #define my_pthread_setprio(A,B) pthread_dummy (0)
  187. #define my_pthread_getprio(A) pthread_dummy (0)
  188. #define my_pthread_attr_setprio(A,B) pthread_dummy(0)
  189. #else /* Normal threads */
  190. #ifdef HAVE_rts_threads
  191. #define sigwait org_sigwait
  192. #include <signal.h>
  193. #undef sigwait
  194. #endif
  195. #undef _REENTRANT /* Fix if _REENTRANT is in pthread.h */
  196. #include <pthread.h>
  197. #ifndef _REENTRANT
  198. #define _REENTRANT
  199. #endif
  200. #ifdef HAVE_THR_SETCONCURRENCY
  201. #include <thread.h> /* Probably solaris */
  202. #endif
  203. #ifdef HAVE_SCHED_H
  204. #include <sched.h>
  205. #endif
  206. #ifdef HAVE_SYNCH_H
  207. #include <synch.h>
  208. #endif
  209. #if defined(__EMX__) && (!defined(EMX_PTHREAD_REV) || (EMX_PTHREAD_REV < 2))
  210. #error Requires at least rev 2 of EMX pthreads library.
  211. #endif
  212. extern int my_pthread_getprio(pthread_t thread_id);
  213. #define pthread_key(T,V) pthread_key_t V
  214. #define my_pthread_getspecific_ptr(T,V) my_pthread_getspecific(T,(V))
  215. #define my_pthread_setspecific_ptr(T,V) pthread_setspecific(T,(void*) (V))
  216. #define pthread_detach_this_thread()
  217. #define pthread_handler_decl(A,B) void *A(void *B)
  218. typedef void *(* pthread_handler)(void *);
  219. /* Test first for RTS or FSU threads */
  220. #if defined(PTHREAD_SCOPE_GLOBAL) && !defined(PTHREAD_SCOPE_SYSTEM)
  221. #define HAVE_rts_threads
  222. extern int my_pthread_create_detached;
  223. #define pthread_sigmask(A,B,C) sigprocmask((A),(B),(C))
  224. #define PTHREAD_CREATE_DETACHED &my_pthread_create_detached
  225. #define PTHREAD_SCOPE_SYSTEM  PTHREAD_SCOPE_GLOBAL
  226. #define PTHREAD_SCOPE_PROCESS PTHREAD_SCOPE_LOCAL
  227. #define USE_ALARM_THREAD
  228. #elif defined(HAVE_mit_thread)
  229. #define USE_ALARM_THREAD
  230. #undef HAVE_LOCALTIME_R
  231. #define HAVE_LOCALTIME_R
  232. #undef HAVE_PTHREAD_ATTR_SETSCOPE
  233. #define HAVE_PTHREAD_ATTR_SETSCOPE
  234. #undef HAVE_GETHOSTBYNAME_R_GLIBC2_STYLE /* If we are running linux */
  235. #undef HAVE_RWLOCK_T
  236. #undef HAVE_RWLOCK_INIT
  237. #undef HAVE_PTHREAD_RWLOCK_RDLOCK
  238. #undef HAVE_SNPRINTF
  239. #define sigset(A,B) pthread_signal((A),(void (*)(int)) (B))
  240. #define signal(A,B) pthread_signal((A),(void (*)(int)) (B))
  241. #define my_pthread_attr_setprio(A,B)
  242. #endif /* defined(PTHREAD_SCOPE_GLOBAL) && !defined(PTHREAD_SCOPE_SYSTEM) */
  243. #if defined(_BSDI_VERSION) && _BSDI_VERSION < 199910
  244. int sigwait(sigset_t *set, int *sig);
  245. #endif
  246. #if defined(HAVE_UNIXWARE7_POSIX)
  247. #undef HAVE_NONPOSIX_SIGWAIT
  248. #define HAVE_NONPOSIX_SIGWAIT /* sigwait takes only 1 argument */
  249. #endif
  250. #ifndef HAVE_NONPOSIX_SIGWAIT
  251. #define my_sigwait(A,B) sigwait((A),(B))
  252. #else
  253. int my_sigwait(const sigset_t *set,int *sig);
  254. #endif
  255. #ifdef HAVE_NONPOSIX_PTHREAD_MUTEX_INIT
  256. #ifndef SAFE_MUTEX
  257. #define pthread_mutex_init(a,b) my_pthread_mutex_init((a),(b))
  258. extern int my_pthread_mutex_init(pthread_mutex_t *mp,
  259.  const pthread_mutexattr_t *attr);
  260. #endif /* SAFE_MUTEX */
  261. #define pthread_cond_init(a,b) my_pthread_cond_init((a),(b))
  262. extern int my_pthread_cond_init(pthread_cond_t *mp,
  263. const pthread_condattr_t *attr);
  264. #endif /* HAVE_NONPOSIX_PTHREAD_MUTEX_INIT */
  265. #if defined(HAVE_SIGTHREADMASK) && !defined(HAVE_PTHREAD_SIGMASK)
  266. #define pthread_sigmask(A,B,C) sigthreadmask((A),(B),(C))
  267. #endif
  268. #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)
  269. int sigwait(sigset_t *setp, int *sigp); /* Use our implemention */
  270. #endif
  271. #if !defined(HAVE_SIGSET) && !defined(HAVE_mit_thread) && !defined(sigset)
  272. #define sigset(A,B) do { struct sigaction s; sigset_t set;              
  273.                          sigemptyset(&set);                             
  274.                          s.sa_handler = (B);                            
  275.                          s.sa_mask    = set;                            
  276.                          s.sa_flags   = 0;                              
  277.                          sigaction((A), &s, (struct sigaction *) NULL); 
  278.                        } while (0)
  279. #endif
  280. #ifndef my_pthread_setprio
  281. #if defined(HAVE_PTHREAD_SETPRIO_NP) /* FSU threads */
  282. #define my_pthread_setprio(A,B) pthread_setprio_np((A),(B))
  283. #elif defined(HAVE_PTHREAD_SETPRIO)
  284. #define my_pthread_setprio(A,B) pthread_setprio((A),(B))
  285. #else
  286. extern void my_pthread_setprio(pthread_t thread_id,int prior);
  287. #endif
  288. #endif
  289. #ifndef my_pthread_attr_setprio
  290. #ifdef HAVE_PTHREAD_ATTR_SETPRIO
  291. #define my_pthread_attr_setprio(A,B) pthread_attr_setprio((A),(B))
  292. #else
  293. extern void my_pthread_attr_setprio(pthread_attr_t *attr, int priority);
  294. #endif
  295. #endif
  296. #if !defined(HAVE_PTHREAD_ATTR_SETSCOPE) || defined(HAVE_DEC_3_2_THREADS)
  297. #define pthread_attr_setscope(A,B)
  298. #undef HAVE_GETHOSTBYADDR_R /* No definition */
  299. #endif
  300. #if defined(HAVE_BROKEN_PTHREAD_COND_TIMEDWAIT) && !defined(SAFE_MUTEX)
  301. extern int my_pthread_cond_timedwait(pthread_cond_t *cond,
  302.      pthread_mutex_t *mutex,
  303.      struct timespec *abstime);
  304. #define pthread_cond_timedwait(A,B,C) my_pthread_cond_timedwait((A),(B),(C))
  305. #endif
  306. #if defined(OS2)
  307. #define my_pthread_getspecific(T,A) ((T) &(A))
  308. #define pthread_setspecific(A,B) win_pthread_setspecific(&(A),(B),sizeof(A))
  309. #elif !defined( HAVE_NONPOSIX_PTHREAD_GETSPECIFIC)
  310. #define my_pthread_getspecific(A,B) ((A) pthread_getspecific(B))
  311. #else
  312. #define my_pthread_getspecific(A,B) ((A) my_pthread_getspecific_imp(B))
  313. void *my_pthread_getspecific_imp(pthread_key_t key);
  314. #endif /* OS2 */
  315. #ifndef HAVE_LOCALTIME_R
  316. struct tm *localtime_r(const time_t *clock, struct tm *res);
  317. #endif
  318. #ifdef HAVE_PTHREAD_CONDATTR_CREATE
  319. /* DCE threads on HPUX 10.20 */
  320. #define pthread_condattr_init pthread_condattr_create
  321. #define pthread_condattr_destroy pthread_condattr_delete
  322. #endif
  323. #ifdef HAVE_CTHREADS_WRAPPER /* For MacOSX */
  324. #define pthread_cond_destroy(A) pthread_dummy(0)
  325. #define pthread_mutex_destroy(A) pthread_dummy(0)
  326. #define pthread_attr_delete(A) pthread_dummy(0)
  327. #define pthread_condattr_delete(A) pthread_dummy(0)
  328. #define pthread_attr_setstacksize(A,B) pthread_dummy(0)
  329. #define pthread_equal(A,B) ((A) == (B))
  330. #define pthread_cond_timedwait(a,b,c) pthread_cond_wait((a),(b))
  331. #define pthread_attr_init(A) pthread_attr_create(A)
  332. #define pthread_attr_destroy(A) pthread_attr_delete(A)
  333. #define pthread_attr_setdetachstate(A,B) pthread_dummy(0)
  334. #define pthread_create(A,B,C,D) pthread_create((A),*(B),(C),(D))
  335. #define pthread_sigmask(A,B,C) sigprocmask((A),(B),(C))
  336. #define pthread_kill(A,B) pthread_dummy(0)
  337. #undef pthread_detach_this_thread
  338. #define pthread_detach_this_thread() { pthread_t tmp=pthread_self() ; pthread_detach(&tmp); }
  339. #endif
  340. #ifdef HAVE_DARWIN_THREADS
  341. #define pthread_sigmask(A,B,C) sigprocmask((A),(B),(C))
  342. #define pthread_kill(A,B) pthread_dummy(0)
  343. #define pthread_condattr_init(A) pthread_dummy(0)
  344. #define pthread_condattr_destroy(A) pthread_dummy(0)
  345. #define pthread_signal(A,B) pthread_dummy(0)
  346. #undef pthread_detach_this_thread
  347. #define pthread_detach_this_thread() { pthread_t tmp=pthread_self() ; pthread_detach(tmp); }
  348. #undef sigset
  349. #define sigset(A,B) pthread_signal((A),(void (*)(int)) (B))
  350. #endif
  351. #if ((defined(HAVE_PTHREAD_ATTR_CREATE) && !defined(HAVE_SIGWAIT)) || defined(HAVE_DEC_3_2_THREADS)) && !defined(HAVE_CTHREADS_WRAPPER)
  352. /* This is set on AIX_3_2 and Siemens unix (and DEC OSF/1 3.2 too) */
  353. #define pthread_key_create(A,B) 
  354. pthread_keycreate(A,(B) ?
  355.   (pthread_destructor_t) (B) :
  356.   (pthread_destructor_t) pthread_dummy)
  357. #define pthread_attr_init(A) pthread_attr_create(A)
  358. #define pthread_attr_destroy(A) pthread_attr_delete(A)
  359. #define pthread_attr_setdetachstate(A,B) pthread_dummy(0)
  360. #define pthread_create(A,B,C,D) pthread_create((A),*(B),(C),(D))
  361. #ifndef pthread_sigmask
  362. #define pthread_sigmask(A,B,C) sigprocmask((A),(B),(C))
  363. #endif
  364. #define pthread_kill(A,B) pthread_dummy(0)
  365. #undef pthread_detach_this_thread
  366. #define pthread_detach_this_thread() { pthread_t tmp=pthread_self() ; pthread_detach(&tmp); }
  367. #else /* HAVE_PTHREAD_ATTR_CREATE && !HAVE_SIGWAIT */
  368. #define HAVE_PTHREAD_KILL
  369. #endif
  370. #endif /* defined(__WIN__) */
  371. #if defined(HPUX) && !defined(DONT_REMAP_PTHREAD_FUNCTIONS)
  372. #undef pthread_cond_timedwait
  373. #define pthread_cond_timedwait(a,b,c) my_pthread_cond_timedwait((a),(b),(c))
  374. int my_pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
  375.       struct timespec *abstime);
  376. #endif
  377. #if defined(HAVE_POSIX1003_4a_MUTEX) && !defined(DONT_REMAP_PTHREAD_FUNCTIONS)
  378. #undef pthread_mutex_trylock
  379. #define pthread_mutex_trylock(a) my_pthread_mutex_trylock((a))
  380. int my_pthread_mutex_trylock(pthread_mutex_t *mutex);
  381. #endif
  382. /* safe_mutex adds checking to mutex for easier debugging */
  383. typedef struct st_safe_mutex_t
  384. {
  385.   pthread_mutex_t global,mutex;
  386.   char *file;
  387.   uint line,count;
  388.   pthread_t thread;
  389. } safe_mutex_t;
  390. int safe_mutex_init(safe_mutex_t *mp, const pthread_mutexattr_t *attr);
  391. int safe_mutex_lock(safe_mutex_t *mp,const char *file, uint line);
  392. int safe_mutex_unlock(safe_mutex_t *mp,const char *file, uint line);
  393. int safe_mutex_destroy(safe_mutex_t *mp,const char *file, uint line);
  394. int safe_cond_wait(pthread_cond_t *cond, safe_mutex_t *mp,const char *file,
  395.    uint line);
  396. int safe_cond_timedwait(pthread_cond_t *cond, safe_mutex_t *mp,
  397. struct timespec *abstime, const char *file, uint line);
  398. /* Wrappers if safe mutex is actually used */
  399. #ifdef SAFE_MUTEX
  400. #undef pthread_mutex_init
  401. #undef pthread_mutex_lock
  402. #undef pthread_mutex_unlock
  403. #undef pthread_mutex_destroy
  404. #undef pthread_mutex_wait
  405. #undef pthread_mutex_timedwait
  406. #undef pthread_mutex_t
  407. #undef pthread_cond_wait
  408. #undef pthread_cond_timedwait
  409. #undef pthread_mutex_trylock
  410. #define pthread_mutex_init(A,B) safe_mutex_init((A),(B))
  411. #define pthread_mutex_lock(A) safe_mutex_lock((A),__FILE__,__LINE__)
  412. #define pthread_mutex_unlock(A) safe_mutex_unlock((A),__FILE__,__LINE__)
  413. #define pthread_mutex_destroy(A) safe_mutex_destroy((A),__FILE__,__LINE__)
  414. #define pthread_cond_wait(A,B) safe_cond_wait((A),(B),__FILE__,__LINE__)
  415. #define pthread_cond_timedwait(A,B,C) safe_cond_timedwait((A),(B),(C),__FILE__,__LINE__)
  416. #define pthread_mutex_trylock(A) pthread_mutex_lock(A)
  417. #define pthread_mutex_t safe_mutex_t
  418. #define safe_mutex_assert_owner(mp) DBUG_ASSERT((mp)->count > 0 && pthread_equal(pthread_self(),(mp)->thread))
  419. #else
  420. #define safe_mutex_assert_owner(mp)
  421. #endif /* SAFE_MUTEX */
  422. /* READ-WRITE thread locking */
  423. #if defined(USE_MUTEX_INSTEAD_OF_RW_LOCKS)
  424. /* use these defs for simple mutex locking */
  425. #define rw_lock_t pthread_mutex_t
  426. #define my_rwlock_init(A,B) pthread_mutex_init((A),(B))
  427. #define rw_rdlock(A) pthread_mutex_lock((A))
  428. #define rw_wrlock(A) pthread_mutex_lock((A))
  429. #define rw_tryrdlock(A) pthread_mutex_trylock((A))
  430. #define rw_trywrlock(A) pthread_mutex_trylock((A))
  431. #define rw_unlock(A) pthread_mutex_unlock((A))
  432. #define rwlock_destroy(A) pthread_mutex_destroy((A))
  433. #elif defined(HAVE_PTHREAD_RWLOCK_RDLOCK)
  434. #define rw_lock_t pthread_rwlock_t
  435. #define my_rwlock_init(A,B) pthread_rwlock_init((A),(B))
  436. #define rw_rdlock(A) pthread_rwlock_rdlock(A)
  437. #define rw_wrlock(A) pthread_rwlock_wrlock(A)
  438. #define rw_tryrdlock(A) pthread_rwlock_tryrdlock((A))
  439. #define rw_trywrlock(A) pthread_rwlock_trywrlock((A))
  440. #define rw_unlock(A) pthread_rwlock_unlock(A)
  441. #define rwlock_destroy(A) pthread_rwlock_destroy(A)
  442. #elif defined(HAVE_RWLOCK_INIT)
  443. #ifdef HAVE_RWLOCK_T /* For example Solaris 2.6-> */
  444. #define rw_lock_t rwlock_t
  445. #endif
  446. #define my_rwlock_init(A,B) rwlock_init((A),USYNC_THREAD,0)
  447. #else
  448. /* Use our own version of read/write locks */
  449. typedef struct _my_rw_lock_t {
  450. pthread_mutex_t lock; /* lock for structure */
  451. pthread_cond_t readers; /* waiting readers */
  452. pthread_cond_t writers; /* waiting writers */
  453. int state; /* -1:writer,0:free,>0:readers */
  454. int waiters; /* number of waiting writers */
  455. } my_rw_lock_t;
  456. #define rw_lock_t my_rw_lock_t
  457. #define rw_rdlock(A) my_rw_rdlock((A))
  458. #define rw_wrlock(A) my_rw_wrlock((A))
  459. #define rw_tryrdlock(A) my_rw_tryrdlock((A))
  460. #define rw_trywrlock(A) my_rw_trywrlock((A))
  461. #define rw_unlock(A) my_rw_unlock((A))
  462. #define rwlock_destroy(A) my_rwlock_destroy((A))
  463. extern int my_rwlock_init(my_rw_lock_t *, void *);
  464. extern int my_rwlock_destroy(my_rw_lock_t *);
  465. extern int my_rw_rdlock(my_rw_lock_t *);
  466. extern int my_rw_wrlock(my_rw_lock_t *);
  467. extern int my_rw_unlock(my_rw_lock_t *);
  468. extern int my_rw_tryrdlock(my_rw_lock_t *);
  469. extern int my_rw_trywrlock(my_rw_lock_t *);
  470. #endif /* USE_MUTEX_INSTEAD_OF_RW_LOCKS */
  471. #define GETHOSTBYADDR_BUFF_SIZE 2048
  472. #ifndef HAVE_THR_SETCONCURRENCY
  473. #define thr_setconcurrency(A) pthread_dummy(0)
  474. #endif
  475. #if !defined(HAVE_PTHREAD_ATTR_SETSTACKSIZE) && ! defined(pthread_attr_setstacksize)
  476. #define pthread_attr_setstacksize(A,B) pthread_dummy(0)
  477. #endif
  478. /* Define mutex types */
  479. #define MY_MUTEX_INIT_SLOW   NULL
  480. #define MY_MUTEX_INIT_FAST   NULL
  481. #define MY_MUTEX_INIT_ERRCHK NULL
  482. #ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
  483. extern pthread_mutexattr_t my_fast_mutexattr;
  484. #undef  MY_MUTEX_INIT_FAST
  485. #define MY_MUTEX_INIT_FAST &my_fast_mutexattr
  486. #endif
  487. #ifdef PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
  488. extern pthread_mutexattr_t my_errchk_mutexattr;
  489. #undef MY_INIT_MUTEX_ERRCHK
  490. #define MY_INIT_MUTEX_ERRCHK &my_errchk_mutexattr
  491. #endif
  492. extern my_bool my_thread_global_init(void);
  493. extern void my_thread_global_end(void);
  494. extern my_bool my_thread_init(void);
  495. extern void my_thread_end(void);
  496. extern const char *my_thread_name(void);
  497. extern long my_thread_id(void);
  498. extern int pthread_no_free(void *);
  499. extern int pthread_dummy(int);
  500. /* All thread specific variables are in the following struct */
  501. #define THREAD_NAME_SIZE 10
  502. #if defined(__ia64__)
  503. #define DEFAULT_THREAD_STACK (128*1024)
  504. #else
  505. #define DEFAULT_THREAD_STACK (64*1024)
  506. #endif
  507. struct st_my_thread_var
  508. {
  509.   int thr_errno;
  510.   pthread_cond_t suspend;
  511.   pthread_mutex_t mutex;
  512.   pthread_mutex_t * volatile current_mutex;
  513.   pthread_cond_t * volatile current_cond;
  514.   pthread_t pthread_self;
  515.   long id;
  516.   int cmp_length;
  517.   int volatile abort;
  518. #ifndef DBUG_OFF
  519.   gptr dbug;
  520.   char name[THREAD_NAME_SIZE+1];
  521. #endif
  522. };
  523. extern struct st_my_thread_var *_my_thread_var(void) __attribute__ ((const));
  524. #define my_thread_var (_my_thread_var())
  525. #define my_errno my_thread_var->thr_errno
  526. /* statistics_xxx functions are for not essential statistic */
  527. #ifndef thread_safe_increment
  528. #ifdef HAVE_ATOMIC_ADD
  529. #define thread_safe_increment(V,L) atomic_add(1,(atomic_t*) &V);
  530. #define thread_safe_add(V,C,L)     atomic_add((C),(atomic_t*) &V);
  531. #define thread_safe_sub(V,C,L)     atomic_sub((C),(atomic_t*) &V);
  532. #define statistic_increment(V,L)   thread_safe_increment((V),(L))
  533. #define statistic_add(V,C,L)       thread_safe_add((V),(C),(L))
  534. #else
  535. #define thread_safe_increment(V,L) 
  536. pthread_mutex_lock((L)); (V)++; pthread_mutex_unlock((L));
  537. #define thread_safe_add(V,C,L) 
  538. pthread_mutex_lock((L)); (V)+=(C); pthread_mutex_unlock((L));
  539. #define thread_safe_sub(V,C,L) 
  540. pthread_mutex_lock((L)); (V)-=(C); pthread_mutex_unlock((L));
  541. #ifdef SAFE_STATISTICS
  542. #define statistic_increment(V,L)   thread_safe_increment((V),(L))
  543. #define statistic_add(V,C,L)       thread_safe_add((V),(C),(L))
  544. #else
  545. #define statistic_increment(V,L) (V)++
  546. #define statistic_add(V,C,L)     (V)+=(C)
  547. #endif /* SAFE_STATISTICS */
  548. #endif /* HAVE_ATOMIC_ADD */
  549. #endif /* thread_safe_increment */
  550. #ifdef  __cplusplus
  551. }
  552. #endif
  553. #endif /* _my_ptread_h */