home *** CD-ROM | disk | FTP | other *** search
/ Internet Magazine 2003 May / INTERNET103.ISO / pc / software / windows / building / mysql / data1.cab / Development / include / my_pthread.h < prev    next >
Encoding:
C/C++ Source or Header  |  2003-01-22  |  22.2 KB  |  636 lines

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