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