OSDN Git Service

Two little fixes I commited to classpath but not to libgcj first
[pf3gnuchains/gcc-fork.git] / gcc / config / i386 / gthr-win32.c
1 /* Implementation of W32-specific threads compatibility routines for
2    libgcc2.   */ 
3
4 /* Copyright (C) 1999, 2000, 2002 Free Software Foundation, Inc.
5    Contributed by Mumit Khan <khan@xraylith.wisc.edu>.
6    Modified and moved to separate file by Danny Smith
7    <dannysmith@users.sourceforge.net>.
8
9 This file is part of GCC.
10
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 2, or (at your option) any later
14 version.
15
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19 for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING.  If not, write to the Free
23 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 02111-1307, USA.  */
25
26 /* As a special exception, if you link this library with other files,
27    some of which are compiled with GCC, to produce an executable,
28    this library does not by itself cause the resulting executable
29    to be covered by the GNU General Public License.
30    This exception does not however invalidate any other reasons why
31    the executable file might be covered by the GNU General Public License.  */
32
33
34 #ifndef __GTHREAD_HIDE_WIN32API
35 # define __GTHREAD_HIDE_WIN32API
36 #endif
37 #include <gthr-win32.h>
38 #include <windows.h>
39
40 /* Windows32 threads specific definitions. The windows32 threading model
41    does not map well into pthread-inspired gcc's threading model, and so 
42    there are caveats one needs to be aware of.
43
44    1. The destructor supplied to __gthread_key_create is ignored for
45       generic x86-win32 ports. This will certainly cause memory leaks 
46       due to unreclaimed eh contexts (sizeof (eh_context) is at least 
47       24 bytes for x86 currently).
48
49       This memory leak may be significant for long-running applications
50       that make heavy use of C++ EH.
51
52       However, Mingw runtime (version 0.3 or newer) provides a mechanism
53       to emulate pthreads key dtors; the runtime provides a special DLL,
54       linked in if -mthreads option is specified, that runs the dtors in
55       the reverse order of registration when each thread exits. If
56       -mthreads option is not given, a stub is linked in instead of the
57       DLL, which results in memory leak. Other x86-win32 ports can use 
58       the same technique of course to avoid the leak.
59
60    2. The error codes returned are non-POSIX like, and cast into ints.
61       This may cause incorrect error return due to truncation values on 
62       hw where sizeof (DWORD) > sizeof (int).
63    
64    3. We might consider using Critical Sections instead of Windows32 
65       mutexes for better performance, but emulating __gthread_mutex_trylock 
66       interface becomes more complicated (Win9x does not support
67       TryEnterCriticalSectioni, while NT does).
68   
69    The basic framework should work well enough. In the long term, GCC
70    needs to use Structured Exception Handling on Windows32.  */
71
72 int
73 __gthr_win32_once (__gthread_once_t *once, void (*func) (void))
74 {
75   if (once == NULL || func == NULL)
76     return EINVAL;
77
78   if (! once->done)
79     {
80       if (InterlockedIncrement (&(once->started)) == 0)
81         {
82           (*func) ();
83           once->done = TRUE;
84         }
85       else
86         {
87           /* Another thread is currently executing the code, so wait for it 
88              to finish; yield the CPU in the meantime.  If performance 
89              does become an issue, the solution is to use an Event that 
90              we wait on here (and set above), but that implies a place to 
91              create the event before this routine is called.  */ 
92           while (! once->done)
93             Sleep (0);
94         }
95     }
96   return 0;
97 }
98
99 /* Windows32 thread local keys don't support destructors; this leads to
100    leaks, especially in threaded applications making extensive use of 
101    C++ EH. Mingw uses a thread-support DLL to work-around this problem.  */
102
103 int
104 __gthr_win32_key_create (__gthread_key_t *key, void (*dtor) (void *))
105 {
106   int status = 0;
107   DWORD tls_index = TlsAlloc ();
108   if (tls_index != 0xFFFFFFFF)
109     {
110       *key = tls_index;
111 #ifdef MINGW32_SUPPORTS_MT_EH
112       /* Mingw runtime will run the dtors in reverse order for each thread
113          when the thread exits.  */
114       status = __mingwthr_key_dtor (*key, dtor);
115 #endif
116     }
117   else
118     status = (int) GetLastError ();
119   return status;
120 }
121
122 int
123 __gthr_win32_key_delete (__gthread_key_t key)
124 {
125   return (TlsFree (key) != 0) ? 0 : (int) GetLastError ();
126 }
127
128 void *
129 __gthr_win32_getspecific (__gthread_key_t key)
130 {
131   DWORD lasterror;
132   void *ptr;
133   lasterror = GetLastError();
134   ptr = TlsGetValue(key);
135   SetLastError( lasterror );
136   return ptr;
137 }
138
139 int
140 __gthr_win32_setspecific (__gthread_key_t key, const void *ptr)
141 {
142   return (TlsSetValue (key, (void*) ptr) != 0) ? 0 : (int) GetLastError ();
143 }
144
145 void
146 __gthr_win32_mutex_init_function (__gthread_mutex_t *mutex)
147 {
148   /* Create unnamed mutex with default security attr and no initial owner.  */ 
149   *mutex = CreateMutex (NULL, 0, NULL);
150 }
151
152 int
153 __gthr_win32_mutex_lock (__gthread_mutex_t *mutex)
154 {
155   if (WaitForSingleObject (*mutex, INFINITE) == WAIT_OBJECT_0)
156     return 0;
157   else
158     return 1;
159 }
160
161 int
162 __gthr_win32_mutex_trylock (__gthread_mutex_t *mutex)
163 {
164   if (WaitForSingleObject (*mutex, 0) == WAIT_OBJECT_0)
165     return 0;
166   else
167     return 1;
168 }
169
170 int
171 __gthr_win32_mutex_unlock (__gthread_mutex_t *mutex)
172 {
173     return (ReleaseMutex (*mutex) != 0) ? 0 : 1;
174 }