gthr-win32.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. /* Threads compatibility routines for libgcc2 and libobjc. */
  2. /* Compile this one with gcc. */
  3. /* Copyright (C) 1999, 2000, 2002 Free Software Foundation, Inc.
  4. Contributed by Mumit Khan <khan@xraylith.wisc.edu>.
  5. This file is part of GCC.
  6. GCC is free software; you can redistribute it and/or modify it under
  7. the terms of the GNU General Public License as published by the Free
  8. Software Foundation; either version 2, or (at your option) any later
  9. version.
  10. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with GCC; see the file COPYING. If not, write to the Free
  16. Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  17. 02111-1307, USA. */
  18. /* As a special exception, if you link this library with other files,
  19. some of which are compiled with GCC, to produce an executable,
  20. this library does not by itself cause the resulting executable
  21. to be covered by the GNU General Public License.
  22. This exception does not however invalidate any other reasons why
  23. the executable file might be covered by the GNU General Public License. */
  24. #ifndef GCC_GTHR_WIN32_H
  25. #define GCC_GTHR_WIN32_H
  26. /* Windows32 threads specific definitions. The windows32 threading model
  27. does not map well into pthread-inspired gcc's threading model, and so
  28. there are caveats one needs to be aware of.
  29. 1. The destructor supplied to __gthread_key_create is ignored for
  30. generic x86-win32 ports. This will certainly cause memory leaks
  31. due to unreclaimed eh contexts (sizeof (eh_context) is at least
  32. 24 bytes for x86 currently).
  33. This memory leak may be significant for long-running applications
  34. that make heavy use of C++ EH.
  35. However, Mingw runtime (version 0.3 or newer) provides a mechanism
  36. to emulate pthreads key dtors; the runtime provides a special DLL,
  37. linked in if -mthreads option is specified, that runs the dtors in
  38. the reverse order of registration when each thread exits. If
  39. -mthreads option is not given, a stub is linked in instead of the
  40. DLL, which results in memory leak. Other x86-win32 ports can use
  41. the same technique of course to avoid the leak.
  42. 2. The error codes returned are non-POSIX like, and cast into ints.
  43. This may cause incorrect error return due to truncation values on
  44. hw where sizeof (DWORD) > sizeof (int).
  45. 3. We might consider using Critical Sections instead of Windows32
  46. mutexes for better performance, but emulating __gthread_mutex_trylock
  47. interface becomes more complicated (Win9x does not support
  48. TryEnterCriticalSectioni, while NT does).
  49. The basic framework should work well enough. In the long term, GCC
  50. needs to use Structured Exception Handling on Windows32. */
  51. #define __GTHREADS 1
  52. #include <errno.h>
  53. #ifdef __MINGW32__
  54. #include <_mingw.h>
  55. #endif
  56. #ifdef _LIBOBJC
  57. /* This is necessary to prevent windef.h (included from windows.h) from
  58. defining it's own BOOL as a typedef. */
  59. #ifndef __OBJC__
  60. #define __OBJC__
  61. #endif
  62. #include <windows.h>
  63. /* Now undef the windows BOOL. */
  64. #undef BOOL
  65. /* Key structure for maintaining thread specific storage */
  66. static DWORD __gthread_objc_data_tls = (DWORD) -1;
  67. /* Backend initialization functions */
  68. /* Initialize the threads subsystem. */
  69. int
  70. __gthread_objc_init_thread_system (void)
  71. {
  72. /* Initialize the thread storage key */
  73. if ((__gthread_objc_data_tls = TlsAlloc ()) != (DWORD) -1)
  74. return 0;
  75. else
  76. return -1;
  77. }
  78. /* Close the threads subsystem. */
  79. int
  80. __gthread_objc_close_thread_system (void)
  81. {
  82. if (__gthread_objc_data_tls != (DWORD) -1)
  83. TlsFree (__gthread_objc_data_tls);
  84. return 0;
  85. }
  86. /* Backend thread functions */
  87. /* Create a new thread of execution. */
  88. objc_thread_t
  89. __gthread_objc_thread_detach (void (*func)(void *arg), void *arg)
  90. {
  91. DWORD thread_id = 0;
  92. HANDLE win32_handle;
  93. if (!(win32_handle = CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE) func,
  94. arg, 0, &thread_id)))
  95. thread_id = 0;
  96. return (objc_thread_t) thread_id;
  97. }
  98. /* Set the current thread's priority. */
  99. int
  100. __gthread_objc_thread_set_priority (int priority)
  101. {
  102. int sys_priority = 0;
  103. switch (priority)
  104. {
  105. case OBJC_THREAD_INTERACTIVE_PRIORITY:
  106. sys_priority = THREAD_PRIORITY_NORMAL;
  107. break;
  108. default:
  109. case OBJC_THREAD_BACKGROUND_PRIORITY:
  110. sys_priority = THREAD_PRIORITY_BELOW_NORMAL;
  111. break;
  112. case OBJC_THREAD_LOW_PRIORITY:
  113. sys_priority = THREAD_PRIORITY_LOWEST;
  114. break;
  115. }
  116. /* Change priority */
  117. if (SetThreadPriority (GetCurrentThread (), sys_priority))
  118. return 0;
  119. else
  120. return -1;
  121. }
  122. /* Return the current thread's priority. */
  123. int
  124. __gthread_objc_thread_get_priority (void)
  125. {
  126. int sys_priority;
  127. sys_priority = GetThreadPriority (GetCurrentThread ());
  128. switch (sys_priority)
  129. {
  130. case THREAD_PRIORITY_HIGHEST:
  131. case THREAD_PRIORITY_TIME_CRITICAL:
  132. case THREAD_PRIORITY_ABOVE_NORMAL:
  133. case THREAD_PRIORITY_NORMAL:
  134. return OBJC_THREAD_INTERACTIVE_PRIORITY;
  135. default:
  136. case THREAD_PRIORITY_BELOW_NORMAL:
  137. return OBJC_THREAD_BACKGROUND_PRIORITY;
  138. case THREAD_PRIORITY_IDLE:
  139. case THREAD_PRIORITY_LOWEST:
  140. return OBJC_THREAD_LOW_PRIORITY;
  141. }
  142. /* Couldn't get priority. */
  143. return -1;
  144. }
  145. /* Yield our process time to another thread. */
  146. void
  147. __gthread_objc_thread_yield (void)
  148. {
  149. Sleep (0);
  150. }
  151. /* Terminate the current thread. */
  152. int
  153. __gthread_objc_thread_exit (void)
  154. {
  155. /* exit the thread */
  156. ExitThread (__objc_thread_exit_status);
  157. /* Failed if we reached here */
  158. return -1;
  159. }
  160. /* Returns an integer value which uniquely describes a thread. */
  161. objc_thread_t
  162. __gthread_objc_thread_id (void)
  163. {
  164. return (objc_thread_t) GetCurrentThreadId ();
  165. }
  166. /* Sets the thread's local storage pointer. */
  167. int
  168. __gthread_objc_thread_set_data (void *value)
  169. {
  170. if (TlsSetValue (__gthread_objc_data_tls, value))
  171. return 0;
  172. else
  173. return -1;
  174. }
  175. /* Returns the thread's local storage pointer. */
  176. void *
  177. __gthread_objc_thread_get_data (void)
  178. {
  179. DWORD lasterror;
  180. void *ptr;
  181. lasterror = GetLastError ();
  182. ptr = TlsGetValue (__gthread_objc_data_tls); /* Return thread data. */
  183. SetLastError (lasterror);
  184. return ptr;
  185. }
  186. /* Backend mutex functions */
  187. /* Allocate a mutex. */
  188. int
  189. __gthread_objc_mutex_allocate (objc_mutex_t mutex)
  190. {
  191. if ((mutex->backend = (void *) CreateMutex (NULL, 0, NULL)) == NULL)
  192. return -1;
  193. else
  194. return 0;
  195. }
  196. /* Deallocate a mutex. */
  197. int
  198. __gthread_objc_mutex_deallocate (objc_mutex_t mutex)
  199. {
  200. CloseHandle ((HANDLE) (mutex->backend));
  201. return 0;
  202. }
  203. /* Grab a lock on a mutex. */
  204. int
  205. __gthread_objc_mutex_lock (objc_mutex_t mutex)
  206. {
  207. int status;
  208. status = WaitForSingleObject ((HANDLE) (mutex->backend), INFINITE);
  209. if (status != WAIT_OBJECT_0 && status != WAIT_ABANDONED)
  210. return -1;
  211. else
  212. return 0;
  213. }
  214. /* Try to grab a lock on a mutex. */
  215. int
  216. __gthread_objc_mutex_trylock (objc_mutex_t mutex)
  217. {
  218. int status;
  219. status = WaitForSingleObject ((HANDLE) (mutex->backend), 0);
  220. if (status != WAIT_OBJECT_0 && status != WAIT_ABANDONED)
  221. return -1;
  222. else
  223. return 0;
  224. }
  225. /* Unlock the mutex */
  226. int
  227. __gthread_objc_mutex_unlock (objc_mutex_t mutex)
  228. {
  229. if (ReleaseMutex ((HANDLE) (mutex->backend)) == 0)
  230. return -1;
  231. else
  232. return 0;
  233. }
  234. /* Backend condition mutex functions */
  235. /* Allocate a condition. */
  236. int
  237. __gthread_objc_condition_allocate (objc_condition_t condition)
  238. {
  239. /* Unimplemented. */
  240. return -1;
  241. }
  242. /* Deallocate a condition. */
  243. int
  244. __gthread_objc_condition_deallocate (objc_condition_t condition)
  245. {
  246. /* Unimplemented. */
  247. return -1;
  248. }
  249. /* Wait on the condition */
  250. int
  251. __gthread_objc_condition_wait (objc_condition_t condition, objc_mutex_t mutex)
  252. {
  253. /* Unimplemented. */
  254. return -1;
  255. }
  256. /* Wake up all threads waiting on this condition. */
  257. int
  258. __gthread_objc_condition_broadcast (objc_condition_t condition)
  259. {
  260. /* Unimplemented. */
  261. return -1;
  262. }
  263. /* Wake up one thread waiting on this condition. */
  264. int
  265. __gthread_objc_condition_signal (objc_condition_t condition)
  266. {
  267. /* Unimplemented. */
  268. return -1;
  269. }
  270. #else /* _LIBOBJC */
  271. #ifdef __cplusplus
  272. extern "C" {
  273. #endif
  274. typedef unsigned long __gthread_key_t;
  275. typedef struct {
  276. int done;
  277. long started;
  278. } __gthread_once_t;
  279. typedef void* __gthread_mutex_t;
  280. #define __GTHREAD_ONCE_INIT {0, -1}
  281. #define __GTHREAD_MUTEX_INIT_FUNCTION __gthread_mutex_init_function
  282. #define __GTHREAD_MUTEX_INIT_DEFAULT 0
  283. #if __MINGW32_MAJOR_VERSION >= 1 || \
  284. (__MINGW32_MAJOR_VERSION == 0 && __MINGW32_MINOR_VERSION > 2)
  285. #define MINGW32_SUPPORTS_MT_EH 1
  286. /* Mingw runtime >= v0.3 provides a magic variable that is set to nonzero
  287. if -mthreads option was specified, or 0 otherwise. This is to get around
  288. the lack of weak symbols in PE-COFF. */
  289. extern int _CRT_MT;
  290. extern int __mingwthr_key_dtor (unsigned long, void (*) (void *));
  291. #endif /* __MINGW32__ version */
  292. static inline int
  293. __gthread_active_p (void)
  294. {
  295. #ifdef MINGW32_SUPPORTS_MT_EH
  296. return _CRT_MT;
  297. #else
  298. return 1;
  299. #endif
  300. }
  301. #ifdef __GTHREAD_HIDE_WIN32API
  302. /* The implementations are in config/i386/gthr-win32.c in libgcc.a.
  303. Only stubs are exposed to avoid polluting the C++ namespace with
  304. windows api definitions. */
  305. extern int __gthr_win32_once (__gthread_once_t *, void (*) (void));
  306. extern int __gthr_win32_key_create (__gthread_key_t *, void (*) (void*));
  307. extern int __gthr_win32_key_delete (__gthread_key_t);
  308. extern void * __gthr_win32_getspecific (__gthread_key_t);
  309. extern int __gthr_win32_setspecific (__gthread_key_t, const void *);
  310. extern void __gthr_win32_mutex_init_function (__gthread_mutex_t *);
  311. extern int __gthr_win32_mutex_lock (__gthread_mutex_t *);
  312. extern int __gthr_win32_mutex_trylock (__gthread_mutex_t *);
  313. extern int __gthr_win32_mutex_unlock (__gthread_mutex_t *);
  314. static inline int
  315. __gthread_once (__gthread_once_t *once, void (*func) (void))
  316. {
  317. if (__gthread_active_p ())
  318. return __gthr_win32_once (once, func);
  319. else
  320. return -1;
  321. }
  322. static inline int
  323. __gthread_key_create (__gthread_key_t *key, void (*dtor) (void *))
  324. {
  325. return __gthr_win32_key_create (key, dtor);
  326. }
  327. static inline int
  328. __gthread_key_dtor (__gthread_key_t key, void *ptr)
  329. {
  330. /* Nothing needed. */
  331. return 0;
  332. }
  333. static inline int
  334. __gthread_key_delete (__gthread_key_t key)
  335. {
  336. return __gthr_win32_key_delete (key);
  337. }
  338. static inline void *
  339. __gthread_getspecific (__gthread_key_t key)
  340. {
  341. return __gthr_win32_getspecific (key);
  342. }
  343. static inline int
  344. __gthread_setspecific (__gthread_key_t key, const void *ptr)
  345. {
  346. return __gthr_win32_setspecific (key, ptr);
  347. }
  348. static inline void
  349. __gthread_mutex_init_function (__gthread_mutex_t *mutex)
  350. {
  351. __gthr_win32_mutex_init_function (mutex);
  352. }
  353. static inline int
  354. __gthread_mutex_lock (__gthread_mutex_t *mutex)
  355. {
  356. if (__gthread_active_p ())
  357. return __gthr_win32_mutex_lock (mutex);
  358. else
  359. return 0;
  360. }
  361. static inline int
  362. __gthread_mutex_trylock (__gthread_mutex_t *mutex)
  363. {
  364. if (__gthread_active_p ())
  365. return __gthr_win32_mutex_trylock (mutex);
  366. else
  367. return 0;
  368. }
  369. static inline int
  370. __gthread_mutex_unlock (__gthread_mutex_t *mutex)
  371. {
  372. if (__gthread_active_p ())
  373. return __gthr_win32_mutex_unlock (mutex);
  374. else
  375. return 0;
  376. }
  377. #else /* ! __GTHREAD_HIDE_WIN32API */
  378. #include <windows.h>
  379. #include <errno.h>
  380. static inline int
  381. __gthread_once (__gthread_once_t *once, void (*func) (void))
  382. {
  383. if (! __gthread_active_p ())
  384. return -1;
  385. else if (once == NULL || func == NULL)
  386. return EINVAL;
  387. if (! once->done)
  388. {
  389. if (InterlockedIncrement (&(once->started)) == 0)
  390. {
  391. (*func) ();
  392. once->done = TRUE;
  393. }
  394. else
  395. {
  396. /* Another thread is currently executing the code, so wait for it
  397. to finish; yield the CPU in the meantime. If performance
  398. does become an issue, the solution is to use an Event that
  399. we wait on here (and set above), but that implies a place to
  400. create the event before this routine is called. */
  401. while (! once->done)
  402. Sleep (0);
  403. }
  404. }
  405. return 0;
  406. }
  407. /* Windows32 thread local keys don't support destructors; this leads to
  408. leaks, especially in threaded applications making extensive use of
  409. C++ EH. Mingw uses a thread-support DLL to work-around this problem. */
  410. static inline int
  411. __gthread_key_create (__gthread_key_t *key, void (*dtor) (void *))
  412. {
  413. int status = 0;
  414. DWORD tls_index = TlsAlloc ();
  415. if (tls_index != 0xFFFFFFFF)
  416. {
  417. *key = tls_index;
  418. #ifdef MINGW32_SUPPORTS_MT_EH
  419. /* Mingw runtime will run the dtors in reverse order for each thread
  420. when the thread exits. */
  421. status = __mingwthr_key_dtor (*key, dtor);
  422. #endif
  423. }
  424. else
  425. status = (int) GetLastError ();
  426. return status;
  427. }
  428. /* Currently, this routine is called only for Mingw runtime, and if
  429. -mthreads option is chosen to link in the thread support DLL. */
  430. static inline int
  431. __gthread_key_dtor (__gthread_key_t key, void *ptr)
  432. {
  433. /* Nothing needed. */
  434. return 0;
  435. }
  436. static inline int
  437. __gthread_key_delete (__gthread_key_t key)
  438. {
  439. return (TlsFree (key) != 0) ? 0 : (int) GetLastError ();
  440. }
  441. static inline void *
  442. __gthread_getspecific (__gthread_key_t key)
  443. {
  444. DWORD lasterror;
  445. void *ptr;
  446. lasterror = GetLastError ();
  447. ptr = TlsGetValue (key);
  448. SetLastError (lasterror);
  449. return ptr;
  450. }
  451. static inline int
  452. __gthread_setspecific (__gthread_key_t key, const void *ptr)
  453. {
  454. return (TlsSetValue (key, (void*) ptr) != 0) ? 0 : (int) GetLastError ();
  455. }
  456. static inline void
  457. __gthread_mutex_init_function (__gthread_mutex_t *mutex)
  458. {
  459. /* Create unnamed mutex with default security attr and no initial owner. */
  460. *mutex = CreateMutex (NULL, 0, NULL);
  461. }
  462. static inline int
  463. __gthread_mutex_lock (__gthread_mutex_t *mutex)
  464. {
  465. int status = 0;
  466. if (__gthread_active_p ())
  467. {
  468. if (WaitForSingleObject (*mutex, INFINITE) == WAIT_OBJECT_0)
  469. status = 0;
  470. else
  471. status = 1;
  472. }
  473. return status;
  474. }
  475. static inline int
  476. __gthread_mutex_trylock (__gthread_mutex_t *mutex)
  477. {
  478. int status = 0;
  479. if (__gthread_active_p ())
  480. {
  481. if (WaitForSingleObject (*mutex, 0) == WAIT_OBJECT_0)
  482. status = 0;
  483. else
  484. status = 1;
  485. }
  486. return status;
  487. }
  488. static inline int
  489. __gthread_mutex_unlock (__gthread_mutex_t *mutex)
  490. {
  491. if (__gthread_active_p ())
  492. return (ReleaseMutex (*mutex) != 0) ? 0 : 1;
  493. else
  494. return 0;
  495. }
  496. #endif /* __GTHREAD_HIDE_WIN32API */
  497. #ifdef __cplusplus
  498. }
  499. #endif
  500. #endif /* _LIBOBJC */
  501. #endif /* ! GCC_GTHR_WIN32_H */