Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /* Threads compatibility routines for libgcc2 and libobjc.  */
  2. /* Compile this one with gcc.  */
  3.  
  4. /* Copyright (C) 1999-2015 Free Software Foundation, Inc.
  5.    Contributed by Mumit Khan <khan@xraylith.wisc.edu>.
  6.  
  7. This file is part of GCC.
  8.  
  9. GCC is free software; you can redistribute it and/or modify it under
  10. the terms of the GNU General Public License as published by the Free
  11. Software Foundation; either version 3, or (at your option) any later
  12. version.
  13.  
  14. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  15. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  16. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  17. for more details.
  18.  
  19. Under Section 7 of GPL version 3, you are granted additional
  20. permissions described in the GCC Runtime Library Exception, version
  21. 3.1, as published by the Free Software Foundation.
  22.  
  23. You should have received a copy of the GNU General Public License and
  24. a copy of the GCC Runtime Library Exception along with this program;
  25. see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
  26. <http://www.gnu.org/licenses/>.  */
  27.  
  28. #ifndef GCC_GTHR_KOS32_H
  29. #define GCC_GTHR_KOS32_H
  30.  
  31. /* Make sure CONST_CAST2 (origin in system.h) is declared.  */
  32. #ifndef CONST_CAST2
  33. #define CONST_CAST2(TOTYPE,FROMTYPE,X) ((__extension__(union {FROMTYPE _q; TOTYPE _nq;})(X))._nq)
  34. #endif
  35.  
  36.  
  37. #define __GTHREADS 1
  38.  
  39. #include <errno.h>
  40. #include <stddef.h>
  41.  
  42. #ifndef __UNUSED_PARAM
  43. #define __UNUSED_PARAM(x) x
  44. #endif
  45.  
  46.  
  47. #ifdef __cplusplus
  48. extern "C" {
  49. #endif
  50.  
  51. typedef unsigned long __gthread_key_t;
  52.  
  53. typedef struct
  54. {
  55.     int done;
  56.     long started;
  57. } __gthread_once_t;
  58.  
  59. typedef struct
  60. {
  61.     volatile int lock;
  62.     int handle;
  63. } __gthread_mutex_t;
  64.  
  65. typedef struct
  66. {
  67.     volatile int lock;
  68.     int handle;
  69.     long depth;
  70.     unsigned long owner;
  71. } __gthread_recursive_mutex_t;
  72.  
  73. #define __GTHREAD_ONCE_INIT {0, -1}
  74. #define __GTHREAD_MUTEX_INIT_FUNCTION __gthread_mutex_init_function
  75.  
  76. #define __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION \
  77.   __gthread_recursive_mutex_init_function
  78.  
  79.  
  80. #if defined (_WIN32) && !defined(__CYGWIN__)
  81. #define MINGW32_SUPPORTS_MT_EH 1
  82. /* Mingw runtime >= v0.3 provides a magic variable that is set to nonzero
  83.    if -mthreads option was specified, or 0 otherwise. This is to get around
  84.    the lack of weak symbols in PE-COFF.  */
  85. extern int _CRT_MT;
  86. extern int __mingwthr_key_dtor (unsigned long, void (*) (void *));
  87. #endif /* _WIN32 && !__CYGWIN__ */
  88.  
  89.  
  90. static inline int __gthread_active_p (void)
  91. {
  92. #ifdef MINGW32_SUPPORTS_MT_EH
  93.   return _CRT_MT;
  94. #else
  95.   return 1;
  96. #endif
  97. }
  98.  
  99. extern int __gthr_kos32_once (__gthread_once_t *, void (*) (void));
  100. extern int __gthr_kos32_key_create (__gthread_key_t *, void (*) (void*));
  101. extern int __gthr_kos32_key_delete (__gthread_key_t);
  102. extern void * __gthr_kos32_getspecific (__gthread_key_t);
  103. extern int __gthr_kos32_setspecific (__gthread_key_t, const void *);
  104. extern void __gthr_kos32_mutex_init_function (__gthread_mutex_t *);
  105. extern int __gthr_kos32_mutex_lock (__gthread_mutex_t *);
  106. extern int __gthr_kos32_mutex_trylock (__gthread_mutex_t *);
  107. extern int __gthr_kos32_mutex_unlock (__gthread_mutex_t *);
  108. extern void __gthr_kos32_recursive_mutex_init_function (__gthread_recursive_mutex_t *);
  109. extern int  __gthr_kos32_recursive_mutex_lock (__gthread_recursive_mutex_t *);
  110. extern int  __gthr_kos32_recursive_mutex_trylock (__gthread_recursive_mutex_t *);
  111. extern int  __gthr_kos32_recursive_mutex_unlock (__gthread_recursive_mutex_t *);
  112. extern void __gthr_kos32_mutex_destroy (__gthread_mutex_t *);
  113. extern int  __gthr_kos32_recursive_mutex_destroy (__gthread_recursive_mutex_t *);
  114.  
  115. static inline int __gthread_once (__gthread_once_t *__once, void (*__func) (void))
  116. {
  117.     if (__gthread_active_p ())
  118.         return __gthr_kos32_once (__once, __func);
  119.     else
  120.         return -1;
  121. }
  122.  
  123. static inline int __gthread_key_create (__gthread_key_t *__key, void (*__dtor) (void *))
  124. {
  125.     return __gthr_kos32_key_create (__key, __dtor);
  126. }
  127.  
  128. static inline int __gthread_key_delete (__gthread_key_t __key)
  129. {
  130.     return __gthr_kos32_key_delete (__key);
  131. }
  132.  
  133. static inline void* __gthread_getspecific (__gthread_key_t __key)
  134. {
  135.     return __gthr_kos32_getspecific (__key);
  136. }
  137.  
  138. static inline int __gthread_setspecific (__gthread_key_t __key, const void *__ptr)
  139. {
  140.     return __gthr_kos32_setspecific (__key, __ptr);
  141. }
  142.  
  143. static inline void __gthread_mutex_init_function (__gthread_mutex_t *__mutex)
  144. {
  145.     __gthr_kos32_mutex_init_function(__mutex);
  146. }
  147.  
  148. static inline void __gthread_mutex_destroy (__gthread_mutex_t *__mutex)
  149. {
  150.     __gthr_kos32_mutex_destroy (__mutex);
  151. }
  152.  
  153. static inline int __gthread_mutex_lock (__gthread_mutex_t *__mutex)
  154. {
  155.     if (__gthread_active_p ())
  156.         return __gthr_kos32_mutex_lock (__mutex);
  157.     else
  158.         return 0;
  159. }
  160.  
  161. static inline int __gthread_mutex_trylock (__gthread_mutex_t *__mutex)
  162. {
  163.     if (__gthread_active_p ())
  164.         return __gthr_kos32_mutex_trylock (__mutex);
  165.     else
  166.         return 0;
  167. }
  168.  
  169. static inline int __gthread_mutex_unlock (__gthread_mutex_t *__mutex)
  170. {
  171.     if (__gthread_active_p ())
  172.         return __gthr_kos32_mutex_unlock (__mutex);
  173.     else
  174.         return 0;
  175. }
  176.  
  177. static inline void __gthread_recursive_mutex_init_function (__gthread_recursive_mutex_t *__mutex)
  178. {
  179.     __gthr_kos32_recursive_mutex_init_function (__mutex);
  180. }
  181.  
  182. static inline int __gthread_recursive_mutex_lock (__gthread_recursive_mutex_t *__mutex)
  183. {
  184.     if (__gthread_active_p ())
  185.         return __gthr_kos32_recursive_mutex_lock (__mutex);
  186.     else
  187.         return 0;
  188. }
  189.  
  190. static inline int __gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t *__mutex)
  191. {
  192.     if (__gthread_active_p ())
  193.         return __gthr_kos32_recursive_mutex_trylock (__mutex);
  194.     else
  195.         return 0;
  196. }
  197.  
  198. static inline int __gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t *__mutex)
  199. {
  200.   if (__gthread_active_p ())
  201.         return __gthr_kos32_recursive_mutex_unlock (__mutex);
  202.   else
  203.         return 0;
  204. }
  205.  
  206. static inline int __gthread_recursive_mutex_destroy (__gthread_recursive_mutex_t *__mutex)
  207. {
  208.     return __gthr_kos32_recursive_mutex_destroy (__mutex);
  209. }
  210.  
  211.  
  212. #ifdef __cplusplus
  213. }
  214. #endif
  215.  
  216. #endif /* ! GCC_GTHR_WIN32_H */
  217.