gthr-win32.c (132718) | gthr-win32.c (146895) |
---|---|
1/* Implementation of W32-specific threads compatibility routines for 2 libgcc2. */ 3 4/* Copyright (C) 1999, 2000, 2002, 2004 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 --- 17 unchanged lines hidden (view full) --- 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 | 1/* Implementation of W32-specific threads compatibility routines for 2 libgcc2. */ 3 4/* Copyright (C) 1999, 2000, 2002, 2004 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 --- 17 unchanged lines hidden (view full) --- 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#include <windows.h> |
|
34#ifndef __GTHREAD_HIDE_WIN32API 35# define __GTHREAD_HIDE_WIN32API 1 36#endif | 35#ifndef __GTHREAD_HIDE_WIN32API 36# define __GTHREAD_HIDE_WIN32API 1 37#endif |
38#undef __GTHREAD_I486_INLINE_LOCK_PRIMITIVES 39#define __GTHREAD_I486_INLINE_LOCK_PRIMITIVES |
|
37#include <gthr-win32.h> | 40#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 --- 9 unchanged lines hidden (view full) --- 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 | 41 42/* Windows32 threads specific definitions. The windows32 threading model 43 does not map well into pthread-inspired gcc's threading model, and so 44 there are caveats one needs to be aware of. 45 46 1. The destructor supplied to __gthread_key_create is ignored for 47 generic x86-win32 ports. This will certainly cause memory leaks 48 due to unreclaimed eh contexts (sizeof (eh_context) is at least --- 9 unchanged lines hidden (view full) --- 58 -mthreads option is not given, a stub is linked in instead of the 59 DLL, which results in memory leak. Other x86-win32 ports can use 60 the same technique of course to avoid the leak. 61 62 2. The error codes returned are non-POSIX like, and cast into ints. 63 This may cause incorrect error return due to truncation values on 64 hw where sizeof (DWORD) > sizeof (int). 65 |
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). | 66 3. We are currently using a special mutex instead of the Critical 67 Sections, since Win9x does not support TryEnterCriticalSection 68 (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 72int 73__gthr_win32_once (__gthread_once_t *once, void (*func) (void)) 74{ 75 if (once == NULL || func == NULL) --- 64 unchanged lines hidden (view full) --- 140__gthr_win32_setspecific (__gthread_key_t key, const void *ptr) 141{ 142 return (TlsSetValue (key, (void*) ptr) != 0) ? 0 : (int) GetLastError (); 143} 144 145void 146__gthr_win32_mutex_init_function (__gthread_mutex_t *mutex) 147{ | 69 70 The basic framework should work well enough. In the long term, GCC 71 needs to use Structured Exception Handling on Windows32. */ 72 73int 74__gthr_win32_once (__gthread_once_t *once, void (*func) (void)) 75{ 76 if (once == NULL || func == NULL) --- 64 unchanged lines hidden (view full) --- 141__gthr_win32_setspecific (__gthread_key_t key, const void *ptr) 142{ 143 return (TlsSetValue (key, (void*) ptr) != 0) ? 0 : (int) GetLastError (); 144} 145 146void 147__gthr_win32_mutex_init_function (__gthread_mutex_t *mutex) 148{ |
148 /* Create unnamed mutex with default security attr and no initial owner. */ 149 *mutex = CreateMutex (NULL, 0, NULL); | 149 mutex->counter = -1; 150 mutex->sema = CreateSemaphore (NULL, 0, 65535, NULL); |
150} 151 152int 153__gthr_win32_mutex_lock (__gthread_mutex_t *mutex) 154{ | 151} 152 153int 154__gthr_win32_mutex_lock (__gthread_mutex_t *mutex) 155{ |
155 if (WaitForSingleObject (*mutex, INFINITE) == WAIT_OBJECT_0) | 156 if (InterlockedIncrement (&mutex->counter) == 0 || 157 WaitForSingleObject (mutex->sema, INFINITE) == WAIT_OBJECT_0) |
156 return 0; 157 else | 158 return 0; 159 else |
158 return 1; | 160 { 161 /* WaitForSingleObject returns WAIT_FAILED, and we can only do 162 some best-effort cleanup here. */ 163 InterlockedDecrement (&mutex->counter); 164 return 1; 165 } |
159} 160 161int 162__gthr_win32_mutex_trylock (__gthread_mutex_t *mutex) 163{ | 166} 167 168int 169__gthr_win32_mutex_trylock (__gthread_mutex_t *mutex) 170{ |
164 if (WaitForSingleObject (*mutex, 0) == WAIT_OBJECT_0) | 171 if (__GTHR_W32_InterlockedCompareExchange (&mutex->counter, 0, -1) < 0) |
165 return 0; 166 else 167 return 1; 168} 169 170int 171__gthr_win32_mutex_unlock (__gthread_mutex_t *mutex) 172{ | 172 return 0; 173 else 174 return 1; 175} 176 177int 178__gthr_win32_mutex_unlock (__gthread_mutex_t *mutex) 179{ |
173 return (ReleaseMutex (*mutex) != 0) ? 0 : 1; | 180 if (InterlockedDecrement (&mutex->counter) >= 0) 181 return ReleaseSemaphore (mutex->sema, 1, NULL) ? 0 : 1; 182 else 183 return 0; |
174} | 184} |