home *** CD-ROM | disk | FTP | other *** search
/ Programming Win32 Under the API / ProgrammingWin32UnderTheApiPatVillani.iso / patches / gcc-2_95_2-x86-win32-patches.zi / gcc-2.95.2-patches / broken-down / gcc-2.95.2-win32-thread.diff < prev    next >
Encoding:
Text File  |  1999-11-08  |  5.6 KB  |  207 lines

  1. Tue Aug 17 21:36:08 1999  Mumit Khan  <khan@xraylith.wisc.edu>
  2.  
  3.     * gthr-win32.h: New file.
  4.  
  5. Index: gcc-2.95.2/gcc/gthr-win32.h
  6. ===================================================================
  7. RCS file: gthr-win32.h
  8. diff -N gthr-win32.h
  9. --- /dev/null    Tue May  5 15:32:27 1998
  10. +++ gcc-2.95.2/gcc/gthr-win32.h    Fri Nov  5 02:25:50 1999
  11. @@ -0,0 +1,195 @@
  12. +/* Threads compatibily routines for libgcc2.  */
  13. +/* Compile this one with gcc.  */
  14. +/* Copyright (C) 1999 Free Software Foundation, Inc.
  15. +   Contributed by Mumit Khan <khan@xraylith.wisc.edu>.
  16. +
  17. +This file is part of GNU CC.
  18. +
  19. +GNU CC is free software; you can redistribute it and/or modify
  20. +it under the terms of the GNU General Public License as published by
  21. +the Free Software Foundation; either version 2, or (at your option)
  22. +any later version.
  23. +
  24. +GNU CC is distributed in the hope that it will be useful,
  25. +but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  27. +GNU General Public License for more details.
  28. +
  29. +You should have received a copy of the GNU General Public License
  30. +along with GNU CC; see the file COPYING.  If not, write to
  31. +the Free Software Foundation, 59 Temple Place - Suite 330,
  32. +Boston, MA 02111-1307, USA.  */
  33. +
  34. +/* As a special exception, if you link this library with other files,
  35. +   some of which are compiled with GCC, to produce an executable,
  36. +   this library does not by itself cause the resulting executable
  37. +   to be covered by the GNU General Public License.
  38. +   This exception does not however invalidate any other reasons why
  39. +   the executable file might be covered by the GNU General Public License.  */
  40. +
  41. +#ifndef __gthr_win32_h
  42. +#define __gthr_win32_h
  43. +
  44. +/* Windows32 threads specific definitions. The windows32 threading model
  45. +   does not map well into pthread-inspired gcc's threading model, and so 
  46. +   there are caveats one needs to be aware of.
  47. +
  48. +   1. The destructor supplied to __gthread_key_create is ignored. This
  49. +      will certainly cause memory leaks due to unreclaimed eh contexts
  50. +      (sizeof (eh_context) is at least 24 bytes for x86 currently).
  51. +
  52. +      This memory leak may be significant for long-running applications
  53. +      that make heavy use of C++ EH.
  54. +
  55. +   2. The error codes returned are non-POSIX like, and cast into ints.
  56. +      This may cause incorrect error return due to truncation values on 
  57. +      hw where sizeof (DWORD) > sizeof (int).
  58. +  
  59. +   The basic framework should work well enough. */
  60. +
  61. +#define __GTHREADS 1
  62. +
  63. +#include <windows.h>
  64. +#include <errno.h>
  65. +
  66. +typedef DWORD __gthread_key_t;
  67. +
  68. +typedef struct {
  69. +  int done;
  70. +  long started;
  71. +} __gthread_once_t;
  72. +
  73. +typedef HANDLE __gthread_mutex_t;
  74. +
  75. +#define __GTHREAD_ONCE_INIT {FALSE, -1}
  76. +#define __GTHREAD_MUTEX_INIT_FUNCTION __gthread_mutex_init_function
  77. +
  78. +static inline int
  79. +__gthread_active_p ()
  80. +{
  81. +  return 1;
  82. +}
  83. +
  84. +static inline int
  85. +__gthread_once (__gthread_once_t *once, void (*func) ())
  86. +{
  87. +  if (! __gthread_active_p ())
  88. +    return -1;
  89. +  else if (once == NULL || func == NULL)
  90. +    return EINVAL;
  91. +
  92. +  if (! once->done)
  93. +    {
  94. +      if (InterlockedIncrement (&(once->started)) == 0)
  95. +        {
  96. +      (*func) ();
  97. +      once->done = TRUE;
  98. +    }
  99. +    }
  100. +  else
  101. +    {
  102. +      /* Another thread is currently executing the code, so wait for it to
  103. +         finish; yield the CPU in the meantime.  */ 
  104. +      while (! once->done)
  105. +        Sleep (0);
  106. +    }
  107. +  
  108. +  return 0;
  109. +}
  110. +
  111. +/* Windows32 thread local keys don't support destructors; to avoid leaks,
  112. +   we will have to figure something out in the future.  */
  113. +static inline int
  114. +__gthread_key_create (__gthread_key_t *key, 
  115. +                      void (*dtor) (void *) __attribute__((__unused__)))
  116. +{
  117. +  int status = 0;
  118. +  DWORD tls_index = TlsAlloc ();
  119. +  if (tls_index != 0xFFFFFFFF)
  120. +    *key = tls_index;
  121. +  else
  122. +    status = (int) GetLastError ();
  123. +  return status;
  124. +}
  125. +
  126. +/* Currently, this routine is never called since win32 keys don't support
  127. +   destructors. Hopefully we'll find a way in the future.  */
  128. +static inline int
  129. +__gthread_key_dtor (__gthread_key_t key, void *ptr)
  130. +{
  131. +  int status = 0;
  132. +
  133. +  /* Just reset the key value to zero. */
  134. +  if (ptr)
  135. +    status = (TlsSetValue (key, 0) != 0) ? 0 : (int) GetLastError ();
  136. +  return status;
  137. +}
  138. +
  139. +/* Currently, this routine is never called since win32 keys don't support
  140. +   destructors. Hopefully we'll find a way in the future.  */
  141. +static inline int
  142. +__gthread_key_delete (__gthread_key_t key)
  143. +{
  144. +  return (TlsFree (key) != 0) ? 0 : (int) GetLastError ();
  145. +}
  146. +
  147. +static inline void *
  148. +__gthread_getspecific (__gthread_key_t key)
  149. +{
  150. +  return TlsGetValue (key);
  151. +}
  152. +
  153. +static inline int
  154. +__gthread_setspecific (__gthread_key_t key, const void *ptr)
  155. +{
  156. +  return (TlsSetValue (key, ptr) != 0) ? 0 : (int) GetLastError ();
  157. +}
  158. +
  159. +static inline void
  160. +__gthread_mutex_init_function (__gthread_mutex_t *mutex)
  161. +{
  162. +  /* Create unnamed mutex with default security attr and no initial owner.  */ 
  163. +  *mutex = CreateMutex (NULL, 0, NULL);
  164. +}
  165. +
  166. +static inline int
  167. +__gthread_mutex_lock (__gthread_mutex_t *mutex)
  168. +{
  169. +  int status = 0;
  170. +
  171. +  if (__gthread_active_p ())
  172. +    {
  173. +      if (WaitForSingleObject (*mutex, INFINITE) == WAIT_OBJECT_0)
  174. +    status = 0;
  175. +      else
  176. +    status = 1;
  177. +    }
  178. +  return status;
  179. +}
  180. +
  181. +static inline int
  182. +__gthread_mutex_trylock (__gthread_mutex_t *mutex)
  183. +{
  184. +  int status = 0;
  185. +
  186. +  if (__gthread_active_p ())
  187. +    {
  188. +      if (WaitForSingleObject (*mutex, 0) == WAIT_OBJECT_0)
  189. +    status = 0;
  190. +      else
  191. +    status = 1;
  192. +    }
  193. +  return status;
  194. +}
  195. +
  196. +static inline int
  197. +__gthread_mutex_unlock (__gthread_mutex_t *mutex)
  198. +{
  199. +  if (__gthread_active_p ())
  200. +    return (ReleaseMutex (*mutex) != 0) ? 0 : 1;
  201. +  else
  202. +    return 0;
  203. +}
  204. +
  205. +#endif /* not __gthr_win32_h */
  206. +
  207.