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-thread2.diff < prev    next >
Encoding:
Text File  |  1999-11-08  |  10.1 KB  |  268 lines

  1. Fri Nov  5 02:32:32 1999  Mumit Khan  <khan@xraylith.wisc.edu>
  2.  
  3.     * gthr-win32.h (__gthread_active_p): Support Mingw MT runtime.
  4.     (__gthread_key_create): Likewise. 
  5.      (__gthread_key_dtor):  Likewise.
  6.     (__gthread_once): Fix logic.
  7.     (__gthread_key_delete): Cast away constness.
  8.  
  9.     * i386/cygwin.h (SUBTARGET_SWITCHES): Add -mthreads option.
  10.     * invoke.texi: Document.
  11.     * i386/mingw32.h (CPP_SPEC): Use.
  12.     (LIBGCC_SPEC): Likewise.
  13.     * i386/crtdll.h (LIBGCC_SPEC): Likewise.
  14.  
  15. Index: gcc-2.95.2/gcc/gthr-win32.h
  16. ===================================================================
  17. RCS file: /homes/khan/src/CVSROOT/gcc-2.95.2/gcc/gthr-win32.h,v
  18. retrieving revision 1.1
  19. diff -u -3 -p -r1.1 gthr-win32.h
  20. --- gcc-2.95.2/gcc/gthr-win32.h    1999/11/05 08:27:10    1.1
  21. +++ gcc-2.95.2/gcc/gthr-win32.h    1999/11/05 08:30:39
  22. @@ -34,23 +34,41 @@ Boston, MA 02111-1307, USA.  */
  23.     does not map well into pthread-inspired gcc's threading model, and so 
  24.     there are caveats one needs to be aware of.
  25.  
  26. -   1. The destructor supplied to __gthread_key_create is ignored. This
  27. -      will certainly cause memory leaks due to unreclaimed eh contexts
  28. -      (sizeof (eh_context) is at least 24 bytes for x86 currently).
  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.  
  34.        This memory leak may be significant for long-running applications
  35.        that make heavy use of C++ EH.
  36.  
  37. +      However, Mingw runtime (version 0.3 or newer) provides a mechanism
  38. +      to emulate pthreads key dtors; the runtime provides a special DLL,
  39. +      linked in if -mthreads option is specified, that runs the dtors in
  40. +      the reverse order of registration when each thread exits. If
  41. +      -mthreads option is not given, a stub is linked in instead of the
  42. +      DLL, which results in memory leak. Other x86-win32 ports can use 
  43. +      the same technique of course to avoid the leak.
  44. +
  45.     2. The error codes returned are non-POSIX like, and cast into ints.
  46.        This may cause incorrect error return due to truncation values on 
  47.        hw where sizeof (DWORD) > sizeof (int).
  48. +   
  49. +   3. We might consider using Critical Sections instead of Windows32 
  50. +      mutexes for better performance, but emulating __gthread_mutex_trylock 
  51. +      interface becomes more complicated (Win9x does not support
  52. +      TryEnterCriticalSectioni, while NT does).
  53.    
  54. -   The basic framework should work well enough. */
  55. +   The basic framework should work well enough. In the long term, GCC
  56. +   needs to use Structured Exception Handling on Windows32.  */
  57.  
  58.  #define __GTHREADS 1
  59.  
  60.  #include <windows.h>
  61.  #include <errno.h>
  62. +#ifdef __MINGW32__
  63. +#include <_mingw.h>
  64. +#endif
  65.  
  66.  typedef DWORD __gthread_key_t;
  67.  
  68. @@ -64,10 +82,24 @@ typedef HANDLE __gthread_mutex_t;
  69.  #define __GTHREAD_ONCE_INIT {FALSE, -1}
  70.  #define __GTHREAD_MUTEX_INIT_FUNCTION __gthread_mutex_init_function
  71.  
  72. +#if __MINGW32_MAJOR_VERSION >= 1 || \
  73. +  (__MINGW32_MAJOR_VERSION == 0 && __MINGW32_MINOR_VERSION > 2)
  74. +#define MINGW32_SUPPORTS_MT_EH 1
  75. +extern int __mingwthr_key_dtor PROTO((DWORD, void (*) (void *)));
  76. +/* Mingw runtime >= v0.3 provides a magic variable that is set to non-zero
  77. +   if -mthreads option was specified, or 0 otherwise. This is to get around 
  78. +   the lack of weak symbols in PE-COFF.  */
  79. +extern int _CRT_MT;
  80. +#endif
  81. +
  82.  static inline int
  83.  __gthread_active_p ()
  84.  {
  85. +#ifdef MINGW32_SUPPORTS_MT_EH
  86. +  return _CRT_MT;
  87. +#else
  88.    return 1;
  89. +#endif
  90.  }
  91.  
  92.  static inline int
  93. @@ -85,48 +117,52 @@ __gthread_once (__gthread_once_t *once, 
  94.        (*func) ();
  95.        once->done = TRUE;
  96.      }
  97. -    }
  98. -  else
  99. -    {
  100. -      /* Another thread is currently executing the code, so wait for it to
  101. -         finish; yield the CPU in the meantime.  */ 
  102. -      while (! once->done)
  103. -        Sleep (0);
  104. +      else
  105. +    {
  106. +      /* Another thread is currently executing the code, so wait for it 
  107. +         to finish; yield the CPU in the meantime.  If performance 
  108. +         does become an issue, the solution is to use an Event that 
  109. +         we wait on here (and set above), but that implies a place to 
  110. +         create the event before this routine is called.  */ 
  111. +      while (! once->done)
  112. +        Sleep (0);
  113. +    }
  114.      }
  115.    
  116.    return 0;
  117.  }
  118.  
  119. -/* Windows32 thread local keys don't support destructors; to avoid leaks,
  120. -   we will have to figure something out in the future.  */
  121. +/* Windows32 thread local keys don't support destructors; this leads to
  122. +   leaks, especially in threaded applications making extensive use of 
  123. +   C++ EH. Mingw uses a thread-support DLL to work-around this problem.  */
  124.  static inline int
  125. -__gthread_key_create (__gthread_key_t *key, 
  126. -                      void (*dtor) (void *) __attribute__((__unused__)))
  127. +__gthread_key_create (__gthread_key_t *key, void (*dtor) (void *))
  128.  {
  129.    int status = 0;
  130.    DWORD tls_index = TlsAlloc ();
  131.    if (tls_index != 0xFFFFFFFF)
  132. -    *key = tls_index;
  133. +    {
  134. +      *key = tls_index;
  135. +#ifdef MINGW32_SUPPORTS_MT_EH
  136. +      /* Mingw runtime will run the dtors in reverse order for each thread
  137. +         when the thread exits.  */
  138. +      status = __mingwthr_key_dtor (*key, dtor);
  139. +#endif
  140. +    }
  141.    else
  142.      status = (int) GetLastError ();
  143.    return status;
  144.  }
  145.  
  146. -/* Currently, this routine is never called since win32 keys don't support
  147. -   destructors. Hopefully we'll find a way in the future.  */
  148. +/* Currently, this routine is called only for Mingw runtime, and if
  149. +   -mthreads option is chosen to link in the thread support DLL.  */ 
  150.  static inline int
  151.  __gthread_key_dtor (__gthread_key_t key, void *ptr)
  152.  {
  153. -  int status = 0;
  154. -
  155. -  /* Just reset the key value to zero. */
  156. -  if (ptr)
  157. -    status = (TlsSetValue (key, 0) != 0) ? 0 : (int) GetLastError ();
  158. -  return status;
  159. +  /* Nothing needed. */
  160. +  return 0;
  161.  }
  162.  
  163. -/* Currently, this routine is never called since win32 keys don't support
  164. -   destructors. Hopefully we'll find a way in the future.  */
  165.  static inline int
  166.  __gthread_key_delete (__gthread_key_t key)
  167.  {
  168. @@ -142,7 +178,7 @@ __gthread_getspecific (__gthread_key_t k
  169.  static inline int
  170.  __gthread_setspecific (__gthread_key_t key, const void *ptr)
  171.  {
  172. -  return (TlsSetValue (key, ptr) != 0) ? 0 : (int) GetLastError ();
  173. +  return (TlsSetValue (key, (void*) ptr) != 0) ? 0 : (int) GetLastError ();
  174.  }
  175.  
  176.  static inline void
  177. Index: gcc-2.95.2/gcc/invoke.texi
  178. ===================================================================
  179. RCS file: /homes/khan/src/CVSROOT/gcc-2.95.2/gcc/invoke.texi,v
  180. retrieving revision 1.1.1.1
  181. diff -u -3 -p -r1.1.1.1 invoke.texi
  182. --- gcc-2.95.2/gcc/invoke.texi    1999/11/05 01:09:43    1.1.1.1
  183. +++ gcc-2.95.2/gcc/invoke.texi    1999/11/05 08:31:33
  184. @@ -343,6 +343,7 @@ in the following sections.
  185.  -mreg-alloc=@var{list}  -mregparm=@var{num}
  186.  -malign-jumps=@var{num}  -malign-loops=@var{num}
  187.  -malign-functions=@var{num} -mpreferred-stack-boundary=@var{num}
  188. +-mthreads
  189.  
  190.  @emph{HPPA Options}
  191.  -march=@var{architecture type}
  192. @@ -5220,6 +5221,14 @@ This extra alignment does consume extra 
  193.  to stack space usage, such as embedded systems and operating system kernels,
  194.  may want to reduce the preferred alignment to
  195.  @samp{-mpreferred-stack-boundary=2}.
  196. +
  197. +@item -mthreads
  198. +@kindex -mthreads
  199. +Support thread-safe exception handling on @samp{Mingw32}. Code that relies 
  200. +on thread-safe exception handling must compile and link all code with the 
  201. +@samp{-mthreads} option. When compiling, @samp{-mthreads} defines 
  202. +@samp{-D_MT}; when linking, it links in a special thread helper library 
  203. +@samp{-lmingwthrd} which cleans up per thread exception handling data.
  204.  @end table
  205.  
  206.  @node HPPA Options
  207. Index: gcc-2.95.2/gcc/config/i386/crtdll.h
  208. ===================================================================
  209. RCS file: /homes/khan/src/CVSROOT/gcc-2.95.2/gcc/config/i386/crtdll.h,v
  210. retrieving revision 1.2
  211. diff -u -3 -p -r1.2 crtdll.h
  212. --- gcc-2.95.2/gcc/config/i386/crtdll.h    1999/11/05 08:19:24    1.2
  213. +++ gcc-2.95.2/gcc/config/i386/crtdll.h    1999/11/05 08:29:27
  214. @@ -32,7 +32,8 @@ Boston, MA 02111-1307, USA. */
  215.    -Asystem(winnt) -Acpu(i386) -Amachine(i386)"
  216.  
  217.  #undef LIBGCC_SPEC
  218. -#define LIBGCC_SPEC "-lmingw32 -lgcc -lmoldname -lcrtdll"
  219. +#define LIBGCC_SPEC \
  220. +  "%{mthreads:-lmingwthrd} -lmingw32 -lgcc -lmoldname -lcrtdll"
  221.  
  222.  /* Specify a different entry point when linking a DLL */
  223.  #undef STARTFILE_SPEC
  224. Index: gcc-2.95.2/gcc/config/i386/cygwin.h
  225. ===================================================================
  226. RCS file: /homes/khan/src/CVSROOT/gcc-2.95.2/gcc/config/i386/cygwin.h,v
  227. retrieving revision 1.5
  228. diff -u -3 -p -r1.5 cygwin.h
  229. --- gcc-2.95.2/gcc/config/i386/cygwin.h    1999/11/05 08:21:44    1.5
  230. +++ gcc-2.95.2/gcc/config/i386/cygwin.h    1999/11/05 08:29:27
  231. @@ -51,7 +51,8 @@ Boston, MA 02111-1307, USA. */
  232.  { "console",          -MASK_WINDOWS, "Create console application" }, \
  233.  { "dll",          MASK_DLL, "Generate code for a DLL" },     \
  234.  { "nop-fun-dllimport",      MASK_NOP_FUN_DLLIMPORT, "Ignore dllimport for functions" }, \
  235. -{ "no-nop-fun-dllimport", -MASK_NOP_FUN_DLLIMPORT, "" },
  236. +{ "no-nop-fun-dllimport", -MASK_NOP_FUN_DLLIMPORT, "" }, \
  237. +{ "threads",          0, "Use Mingw-specific thread support" },
  238.  
  239.  
  240.  /* Support the __declspec keyword by turning them into attributes.
  241. Index: gcc-2.95.2/gcc/config/i386/mingw32.h
  242. ===================================================================
  243. RCS file: /homes/khan/src/CVSROOT/gcc-2.95.2/gcc/config/i386/mingw32.h,v
  244. retrieving revision 1.2
  245. diff -u -3 -p -r1.2 mingw32.h
  246. --- gcc-2.95.2/gcc/config/i386/mingw32.h    1999/11/05 08:19:24    1.2
  247. +++ gcc-2.95.2/gcc/config/i386/mingw32.h    1999/11/05 08:29:27
  248. @@ -45,7 +45,8 @@ Boston, MA 02111-1307, USA. */
  249.  #define STANDARD_INCLUDE_COMPONENT "MINGW32"
  250.  
  251.  #undef CPP_SPEC
  252. -#define CPP_SPEC "-remap %(cpp_cpu) %{posix:-D_POSIX_SOURCE}"
  253. +#define CPP_SPEC \
  254. +  "-remap %(cpp_cpu) %{posix:-D_POSIX_SOURCE} %{mthreads:-D_MT}"
  255.  
  256.  /* For Windows applications, include more libraries, but always include
  257.     kernel32.  */
  258. @@ -55,7 +56,8 @@ Boston, MA 02111-1307, USA. */
  259.  
  260.  /* Include in the mingw32 libraries with libgcc */
  261.  #undef LIBGCC_SPEC
  262. -#define LIBGCC_SPEC "-lmingw32 -lgcc -lmoldname -lmsvcrt"
  263. +#define LIBGCC_SPEC \
  264. +  "%{mthreads:-lmingwthrd} -lmingw32 -lgcc -lmoldname -lmsvcrt"
  265.  
  266.  #undef STARTFILE_SPEC
  267.  #define STARTFILE_SPEC "%{mdll:dllcrt2%O%s} %{!mdll:crt2%O%s} %{pg:gcrt2%O%s}"
  268.