home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 14 / hacker14.iso / programacao / cwin / c.exe / $INSTDIR / include / c++ / mingw32 / bits / gthr-default.h < prev    next >
Encoding:
C/C++ Source or Header  |  2003-12-15  |  14.5 KB  |  612 lines

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