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>.
6 This file is part of GNU CC.
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)
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.
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. */
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. */
30 #ifndef __gthr_win32_h
31 #define __gthr_win32_h
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.
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).
42 This memory leak may be significant for long-running applications
43 that make heavy use of C++ EH.
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.
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).
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).
62 The basic framework should work well enough. In the long term, GCC
63 needs to use Structured Exception Handling on Windows32. */
71 /* Key structure for maintaining thread specific storage */
72 static DWORD __gthread_objc_data_tls
= (DWORD
)-1;
74 /* Backend initialization functions */
76 /* Initialize the threads subsystem. */
78 __gthread_objc_init_thread_system(void)
80 /* Initialize the thread storage key */
81 if ((__gthread_objc_data_tls
= TlsAlloc()) != (DWORD
)-1)
87 /* Close the threads subsystem. */
89 __gthread_objc_close_thread_system(void)
91 if (__gthread_objc_data_tls
!= (DWORD
)-1)
92 TlsFree(__gthread_objc_data_tls
);
96 /* Backend thread functions */
98 /* Create a new thread of execution. */
100 __gthread_objc_thread_detach(void (*func
)(void *arg
), void *arg
)
105 if (!(win32_handle
= CreateThread(NULL
, 0, (LPTHREAD_START_ROUTINE
)func
,
106 arg
, 0, &thread_id
)))
109 return (objc_thread_t
)thread_id
;
112 /* Set the current thread's priority. */
114 __gthread_objc_thread_set_priority(int priority
)
116 int sys_priority
= 0;
120 case OBJC_THREAD_INTERACTIVE_PRIORITY
:
121 sys_priority
= THREAD_PRIORITY_NORMAL
;
124 case OBJC_THREAD_BACKGROUND_PRIORITY
:
125 sys_priority
= THREAD_PRIORITY_BELOW_NORMAL
;
127 case OBJC_THREAD_LOW_PRIORITY
:
128 sys_priority
= THREAD_PRIORITY_LOWEST
;
132 /* Change priority */
133 if (SetThreadPriority(GetCurrentThread(), sys_priority
))
139 /* Return the current thread's priority. */
141 __gthread_objc_thread_get_priority(void)
145 sys_priority
= GetThreadPriority(GetCurrentThread());
147 switch (sys_priority
)
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
;
156 case THREAD_PRIORITY_BELOW_NORMAL
:
157 return OBJC_THREAD_BACKGROUND_PRIORITY
;
159 case THREAD_PRIORITY_IDLE
:
160 case THREAD_PRIORITY_LOWEST
:
161 return OBJC_THREAD_LOW_PRIORITY
;
164 /* Couldn't get priority. */
168 /* Yield our process time to another thread. */
170 __gthread_objc_thread_yield(void)
175 /* Terminate the current thread. */
177 __gthread_objc_thread_exit(void)
179 /* exit the thread */
180 ExitThread(__gthread_objc_thread_exit_status
);
182 /* Failed if we reached here */
186 /* Returns an integer value which uniquely describes a thread. */
188 __gthread_objc_thread_id(void)
190 return (objc_thread_t
)GetCurrentThreadId();
193 /* Sets the thread's local storage pointer. */
195 __gthread_objc_thread_set_data(void *value
)
197 if (TlsSetValue(__gthread_objc_data_tls
, value
))
203 /* Returns the thread's local storage pointer. */
205 __gthread_objc_thread_get_data(void)
210 lasterror
= GetLastError();
212 ptr
= TlsGetValue(__gthread_objc_data_tls
); /* Return thread data. */
214 SetLastError( lasterror
);
219 /* Backend mutex functions */
221 /* Allocate a mutex. */
223 __gthread_objc_mutex_allocate(objc_mutex_t mutex
)
225 if ((mutex
->backend
= (void *)CreateMutex(NULL
, 0, NULL
)) == NULL
)
231 /* Deallocate a mutex. */
233 __gthread_objc_mutex_deallocate(objc_mutex_t mutex
)
235 CloseHandle((HANDLE
)(mutex
->backend
));
239 /* Grab a lock on a mutex. */
241 __gthread_objc_mutex_lock(objc_mutex_t mutex
)
245 status
= WaitForSingleObject((HANDLE
)(mutex
->backend
), INFINITE
);
246 if (status
!= WAIT_OBJECT_0
&& status
!= WAIT_ABANDONED
)
252 /* Try to grab a lock on a mutex. */
254 __gthread_objc_mutex_trylock(objc_mutex_t mutex
)
258 status
= WaitForSingleObject((HANDLE
)(mutex
->backend
), 0);
259 if (status
!= WAIT_OBJECT_0
&& status
!= WAIT_ABANDONED
)
265 /* Unlock the mutex */
267 __gthread_objc_mutex_unlock(objc_mutex_t mutex
)
269 if (ReleaseMutex((HANDLE
)(mutex
->backend
)) == 0)
275 /* Backend condition mutex functions */
277 /* Allocate a condition. */
279 __gthread_objc_condition_allocate(objc_condition_t condition
)
285 /* Deallocate a condition. */
287 __gthread_objc_condition_deallocate(objc_condition_t condition
)
293 /* Wait on the condition */
295 __gthread_objc_condition_wait(objc_condition_t condition
, objc_mutex_t mutex
)
301 /* Wake up all threads waiting on this condition. */
303 __gthread_objc_condition_broadcast(objc_condition_t condition
)
309 /* Wake up one thread waiting on this condition. */
311 __gthread_objc_condition_signal(objc_condition_t condition
)
323 typedef DWORD __gthread_key_t
;
330 typedef HANDLE __gthread_mutex_t
;
332 #define __GTHREAD_ONCE_INIT {FALSE, -1}
333 #define __GTHREAD_MUTEX_INIT_FUNCTION __gthread_mutex_init_function
335 #if __MINGW32_MAJOR_VERSION >= 1 || \
336 (__MINGW32_MAJOR_VERSION == 0 && __MINGW32_MINOR_VERSION > 2)
337 #define MINGW32_SUPPORTS_MT_EH 1
338 extern int __mingwthr_key_dtor
PARAMS ((DWORD
, void (*) (void *)));
339 /* Mingw runtime >= v0.3 provides a magic variable that is set to non-zero
340 if -mthreads option was specified, or 0 otherwise. This is to get around
341 the lack of weak symbols in PE-COFF. */
346 __gthread_active_p (void)
348 #ifdef MINGW32_SUPPORTS_MT_EH
356 __gthread_once (__gthread_once_t
*once
, void (*func
) (void))
358 if (! __gthread_active_p ())
360 else if (once
== NULL
|| func
== NULL
)
365 if (InterlockedIncrement (&(once
->started
)) == 0)
372 /* Another thread is currently executing the code, so wait for it
373 to finish; yield the CPU in the meantime. If performance
374 does become an issue, the solution is to use an Event that
375 we wait on here (and set above), but that implies a place to
376 create the event before this routine is called. */
385 /* Windows32 thread local keys don't support destructors; this leads to
386 leaks, especially in threaded applications making extensive use of
387 C++ EH. Mingw uses a thread-support DLL to work-around this problem. */
389 __gthread_key_create (__gthread_key_t
*key
, void (*dtor
) (void *))
392 DWORD tls_index
= TlsAlloc ();
393 if (tls_index
!= 0xFFFFFFFF)
396 #ifdef MINGW32_SUPPORTS_MT_EH
397 /* Mingw runtime will run the dtors in reverse order for each thread
398 when the thread exits. */
399 status
= __mingwthr_key_dtor (*key
, dtor
);
403 status
= (int) GetLastError ();
407 /* Currently, this routine is called only for Mingw runtime, and if
408 -mthreads option is chosen to link in the thread support DLL. */
410 __gthread_key_dtor (__gthread_key_t key
, void *ptr
)
412 /* Nothing needed. */
417 __gthread_key_delete (__gthread_key_t key
)
419 return (TlsFree (key
) != 0) ? 0 : (int) GetLastError ();
423 __gthread_getspecific (__gthread_key_t key
)
428 lasterror
= GetLastError();
430 ptr
= TlsGetValue(key
);
432 SetLastError( lasterror
);
438 __gthread_setspecific (__gthread_key_t key
, const void *ptr
)
440 return (TlsSetValue (key
, (void*) ptr
) != 0) ? 0 : (int) GetLastError ();
444 __gthread_mutex_init_function (__gthread_mutex_t
*mutex
)
446 /* Create unnamed mutex with default security attr and no initial owner. */
447 *mutex
= CreateMutex (NULL
, 0, NULL
);
451 __gthread_mutex_lock (__gthread_mutex_t
*mutex
)
455 if (__gthread_active_p ())
457 if (WaitForSingleObject (*mutex
, INFINITE
) == WAIT_OBJECT_0
)
466 __gthread_mutex_trylock (__gthread_mutex_t
*mutex
)
470 if (__gthread_active_p ())
472 if (WaitForSingleObject (*mutex
, 0) == WAIT_OBJECT_0
)
481 __gthread_mutex_unlock (__gthread_mutex_t
*mutex
)
483 if (__gthread_active_p ())
484 return (ReleaseMutex (*mutex
) != 0) ? 0 : 1;
489 #endif /* _LIBOBJC */
491 #endif /* not __gthr_win32_h */