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