Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

635 lines
22 KiB

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