home *** CD-ROM | disk | FTP | other *** search
/ PC World 2003 July & August / PCWorld_2003-07-08_cd.bin / Software / Vyzkuste / mysql / data1.cab / Development / include / my_pthread.h < prev    next >
C/C++ Source or Header  |  2003-05-17  |  25KB  |  685 lines

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