OSDN Git Service

Move scheduling visualization code to separate file.
[pf3gnuchains/gcc-fork.git] / gcc / gthr-win32.h
1 /* Threads compatibility routines for libgcc2 and libobjc.  */
2 /* Compile this one with gcc.  */
3 /* Copyright (C) 1999, 2000 Free Software Foundation, Inc.
4    Contributed by Mumit Khan <khan@xraylith.wisc.edu>.
5
6 This file is part of GNU CC.
7
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING.  If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 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 __gthr_win32_h
31 #define __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 <windows.h>
68
69 #ifdef _LIBOBJC
70
71 /* Key structure for maintaining thread specific storage */
72 static DWORD    __gthread_objc_data_tls = (DWORD)-1;
73
74 /* Backend initialization functions */
75
76 /* Initialize the threads subsystem. */
77 int
78 __gthread_objc_init_thread_system(void)
79 {
80   /* Initialize the thread storage key */
81   if ((__gthread_objc_data_tls = TlsAlloc()) != (DWORD)-1)
82     return 0;
83   else
84     return -1;
85 }
86
87 /* Close the threads subsystem. */
88 int
89 __gthread_objc_close_thread_system(void)
90 {
91   if (__gthread_objc_data_tls != (DWORD)-1)
92     TlsFree(__gthread_objc_data_tls);
93   return 0;
94 }
95
96 /* Backend thread functions */
97
98 /* Create a new thread of execution. */
99 objc_thread_t
100 __gthread_objc_thread_detach(void (*func)(void *arg), void *arg)
101 {
102   DWORD thread_id = 0;
103   HANDLE win32_handle;
104
105   if (!(win32_handle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)func,
106                                    arg, 0, &thread_id)))
107     thread_id = 0;
108   
109   return (objc_thread_t)thread_id;
110 }
111
112 /* Set the current thread's priority. */
113 int
114 __gthread_objc_thread_set_priority(int priority)
115 {
116   int sys_priority = 0;
117
118   switch (priority)
119     {
120     case OBJC_THREAD_INTERACTIVE_PRIORITY:
121       sys_priority = THREAD_PRIORITY_NORMAL;
122       break;
123     default:
124     case OBJC_THREAD_BACKGROUND_PRIORITY:
125       sys_priority = THREAD_PRIORITY_BELOW_NORMAL;
126       break;
127     case OBJC_THREAD_LOW_PRIORITY:
128       sys_priority = THREAD_PRIORITY_LOWEST;
129       break;
130     }
131
132   /* Change priority */
133   if (SetThreadPriority(GetCurrentThread(), sys_priority))
134     return 0;
135   else
136     return -1;
137 }
138
139 /* Return the current thread's priority. */
140 int
141 __gthread_objc_thread_get_priority(void)
142 {
143   int sys_priority;
144
145   sys_priority = GetThreadPriority(GetCurrentThread());
146   
147   switch (sys_priority)
148     {
149     case THREAD_PRIORITY_HIGHEST:
150     case THREAD_PRIORITY_TIME_CRITICAL:
151     case THREAD_PRIORITY_ABOVE_NORMAL:
152     case THREAD_PRIORITY_NORMAL:
153       return OBJC_THREAD_INTERACTIVE_PRIORITY;
154
155     default:
156     case THREAD_PRIORITY_BELOW_NORMAL:
157       return OBJC_THREAD_BACKGROUND_PRIORITY;
158     
159     case THREAD_PRIORITY_IDLE:
160     case THREAD_PRIORITY_LOWEST:
161       return OBJC_THREAD_LOW_PRIORITY;
162     }
163
164   /* Couldn't get priority. */
165   return -1;
166 }
167
168 /* Yield our process time to another thread. */
169 void
170 __gthread_objc_thread_yield(void)
171 {
172   Sleep(0);
173 }
174
175 /* Terminate the current thread. */
176 int
177 __gthread_objc_thread_exit(void)
178 {
179   /* exit the thread */
180   ExitThread(__gthread_objc_thread_exit_status);
181
182   /* Failed if we reached here */
183   return -1;
184 }
185
186 /* Returns an integer value which uniquely describes a thread. */
187 objc_thread_t
188 __gthread_objc_thread_id(void)
189 {
190   return (objc_thread_t)GetCurrentThreadId();
191 }
192
193 /* Sets the thread's local storage pointer. */
194 int
195 __gthread_objc_thread_set_data(void *value)
196 {
197   if (TlsSetValue(__gthread_objc_data_tls, value))
198     return 0;
199   else
200     return -1;
201 }
202
203 /* Returns the thread's local storage pointer. */
204 void *
205 __gthread_objc_thread_get_data(void)
206 {
207   return TlsGetValue(__gthread_objc_data_tls);          /* Return thread data.      */
208 }
209
210 /* Backend mutex functions */
211
212 /* Allocate a mutex. */
213 int
214 __gthread_objc_mutex_allocate(objc_mutex_t mutex)
215 {
216   if ((mutex->backend = (void *)CreateMutex(NULL, 0, NULL)) == NULL)
217     return -1;
218   else
219     return 0;
220 }
221
222 /* Deallocate a mutex. */
223 int
224 __gthread_objc_mutex_deallocate(objc_mutex_t mutex)
225 {
226   CloseHandle((HANDLE)(mutex->backend));
227   return 0;
228 }
229
230 /* Grab a lock on a mutex. */
231 int
232 __gthread_objc_mutex_lock(objc_mutex_t mutex)
233 {
234   int status;
235
236   status = WaitForSingleObject((HANDLE)(mutex->backend), INFINITE);
237   if (status != WAIT_OBJECT_0 && status != WAIT_ABANDONED)
238     return -1;
239   else
240     return 0;
241 }
242
243 /* Try to grab a lock on a mutex. */
244 int
245 __gthread_objc_mutex_trylock(objc_mutex_t mutex)
246 {
247   int status;
248
249   status = WaitForSingleObject((HANDLE)(mutex->backend), 0);
250   if (status != WAIT_OBJECT_0 && status != WAIT_ABANDONED)
251     return -1;
252   else
253     return 0;
254 }
255
256 /* Unlock the mutex */
257 int
258 __gthread_objc_mutex_unlock(objc_mutex_t mutex)
259 {
260   if (ReleaseMutex((HANDLE)(mutex->backend)) == 0)
261     return -1;
262   else
263     return 0;
264 }
265
266 /* Backend condition mutex functions */
267
268 /* Allocate a condition. */
269 int
270 __gthread_objc_condition_allocate(objc_condition_t condition)
271 {
272   /* Unimplemented. */
273   return -1;
274 }
275
276 /* Deallocate a condition. */
277 int
278 __gthread_objc_condition_deallocate(objc_condition_t condition)
279 {
280   /* Unimplemented. */
281   return -1;
282 }
283
284 /* Wait on the condition */
285 int
286 __gthread_objc_condition_wait(objc_condition_t condition, objc_mutex_t mutex)
287 {
288   /* Unimplemented. */
289   return -1;
290 }
291
292 /* Wake up all threads waiting on this condition. */
293 int
294 __gthread_objc_condition_broadcast(objc_condition_t condition)
295 {
296   /* Unimplemented. */
297   return -1;
298 }
299
300 /* Wake up one thread waiting on this condition. */
301 int
302 __gthread_objc_condition_signal(objc_condition_t condition)
303 {
304   /* Unimplemented. */
305   return -1;
306 }
307
308 #else /* _LIBOBJC */
309
310 #ifdef __MINGW32__
311 #include <_mingw.h>
312 #endif
313
314 typedef DWORD __gthread_key_t;
315
316 typedef struct {
317   int done;
318   long started;
319 } __gthread_once_t;
320
321 typedef HANDLE __gthread_mutex_t;
322
323 #define __GTHREAD_ONCE_INIT {FALSE, -1}
324 #define __GTHREAD_MUTEX_INIT_FUNCTION __gthread_mutex_init_function
325
326 #if __MINGW32_MAJOR_VERSION >= 1 || \
327   (__MINGW32_MAJOR_VERSION == 0 && __MINGW32_MINOR_VERSION > 2)
328 #define MINGW32_SUPPORTS_MT_EH 1
329 extern int __mingwthr_key_dtor PARAMS ((DWORD, void (*) (void *)));
330 /* Mingw runtime >= v0.3 provides a magic variable that is set to non-zero
331    if -mthreads option was specified, or 0 otherwise. This is to get around 
332    the lack of weak symbols in PE-COFF.  */
333 extern int _CRT_MT;
334 #endif
335
336 static inline int
337 __gthread_active_p (void)
338 {
339 #ifdef MINGW32_SUPPORTS_MT_EH
340   return _CRT_MT;
341 #else
342   return 1;
343 #endif
344 }
345
346 static inline int
347 __gthread_once (__gthread_once_t *once, void (*func) (void))
348 {
349   if (! __gthread_active_p ())
350     return -1;
351   else if (once == NULL || func == NULL)
352     return EINVAL;
353
354   if (! once->done)
355     {
356       if (InterlockedIncrement (&(once->started)) == 0)
357         {
358           (*func) ();
359           once->done = TRUE;
360         }
361       else
362         {
363           /* Another thread is currently executing the code, so wait for it 
364              to finish; yield the CPU in the meantime.  If performance 
365              does become an issue, the solution is to use an Event that 
366              we wait on here (and set above), but that implies a place to 
367              create the event before this routine is called.  */ 
368           while (! once->done)
369             Sleep (0);
370         }
371     }
372   
373   return 0;
374 }
375
376 /* Windows32 thread local keys don't support destructors; this leads to
377    leaks, especially in threaded applications making extensive use of 
378    C++ EH. Mingw uses a thread-support DLL to work-around this problem.  */
379 static inline int
380 __gthread_key_create (__gthread_key_t *key, void (*dtor) (void *))
381 {
382   int status = 0;
383   DWORD tls_index = TlsAlloc ();
384   if (tls_index != 0xFFFFFFFF)
385     {
386       *key = tls_index;
387 #ifdef MINGW32_SUPPORTS_MT_EH
388       /* Mingw runtime will run the dtors in reverse order for each thread
389          when the thread exits.  */
390       status = __mingwthr_key_dtor (*key, dtor);
391 #endif
392     }
393   else
394     status = (int) GetLastError ();
395   return status;
396 }
397
398 /* Currently, this routine is called only for Mingw runtime, and if
399    -mthreads option is chosen to link in the thread support DLL.  */ 
400 static inline int
401 __gthread_key_dtor (__gthread_key_t key, void *ptr)
402 {
403   /* Nothing needed. */
404   return 0;
405 }
406
407 static inline int
408 __gthread_key_delete (__gthread_key_t key)
409 {
410   return (TlsFree (key) != 0) ? 0 : (int) GetLastError ();
411 }
412
413 static inline void *
414 __gthread_getspecific (__gthread_key_t key)
415 {
416   return TlsGetValue (key);
417 }
418
419 static inline int
420 __gthread_setspecific (__gthread_key_t key, const void *ptr)
421 {
422   return (TlsSetValue (key, (void*) ptr) != 0) ? 0 : (int) GetLastError ();
423 }
424
425 static inline void
426 __gthread_mutex_init_function (__gthread_mutex_t *mutex)
427 {
428   /* Create unnamed mutex with default security attr and no initial owner.  */ 
429   *mutex = CreateMutex (NULL, 0, NULL);
430 }
431
432 static inline int
433 __gthread_mutex_lock (__gthread_mutex_t *mutex)
434 {
435   int status = 0;
436
437   if (__gthread_active_p ())
438     {
439       if (WaitForSingleObject (*mutex, INFINITE) == WAIT_OBJECT_0)
440         status = 0;
441       else
442         status = 1;
443     }
444   return status;
445 }
446
447 static inline int
448 __gthread_mutex_trylock (__gthread_mutex_t *mutex)
449 {
450   int status = 0;
451
452   if (__gthread_active_p ())
453     {
454       if (WaitForSingleObject (*mutex, 0) == WAIT_OBJECT_0)
455         status = 0;
456       else
457         status = 1;
458     }
459   return status;
460 }
461
462 static inline int
463 __gthread_mutex_unlock (__gthread_mutex_t *mutex)
464 {
465   if (__gthread_active_p ())
466     return (ReleaseMutex (*mutex) != 0) ? 0 : 1;
467   else
468     return 0;
469 }
470
471 #endif /* _LIBOBJC */
472
473 #endif /* not __gthr_win32_h */
474