00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #include <windows.h>
00035 #ifndef __GTHREAD_HIDE_WIN32API
00036 # define __GTHREAD_HIDE_WIN32API 1
00037 #endif
00038 #undef __GTHREAD_I486_INLINE_LOCK_PRIMITIVES
00039 #define __GTHREAD_I486_INLINE_LOCK_PRIMITIVES
00040 #include <gthr-win32.h>
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073 int
00074 __gthr_win32_once (__gthread_once_t *once, void (*func) (void))
00075 {
00076 if (once == NULL || func == NULL)
00077 return EINVAL;
00078
00079 if (! once->done)
00080 {
00081 if (InterlockedIncrement (&(once->started)) == 0)
00082 {
00083 (*func) ();
00084 once->done = TRUE;
00085 }
00086 else
00087 {
00088
00089
00090
00091
00092
00093 while (! once->done)
00094 Sleep (0);
00095 }
00096 }
00097 return 0;
00098 }
00099
00100
00101
00102
00103
00104 int
00105 __gthr_win32_key_create (__gthread_key_t *key, void (*dtor) (void *))
00106 {
00107 int status = 0;
00108 DWORD tls_index = TlsAlloc ();
00109 if (tls_index != 0xFFFFFFFF)
00110 {
00111 *key = tls_index;
00112 #ifdef MINGW32_SUPPORTS_MT_EH
00113
00114
00115 status = __mingwthr_key_dtor (*key, dtor);
00116 #endif
00117 }
00118 else
00119 status = (int) GetLastError ();
00120 return status;
00121 }
00122
00123 int
00124 __gthr_win32_key_delete (__gthread_key_t key)
00125 {
00126 return (TlsFree (key) != 0) ? 0 : (int) GetLastError ();
00127 }
00128
00129 void *
00130 __gthr_win32_getspecific (__gthread_key_t key)
00131 {
00132 DWORD lasterror;
00133 void *ptr;
00134 lasterror = GetLastError();
00135 ptr = TlsGetValue(key);
00136 SetLastError( lasterror );
00137 return ptr;
00138 }
00139
00140 int
00141 __gthr_win32_setspecific (__gthread_key_t key, const void *ptr)
00142 {
00143 return (TlsSetValue (key, (void*) ptr) != 0) ? 0 : (int) GetLastError ();
00144 }
00145
00146 void
00147 __gthr_win32_mutex_init_function (__gthread_mutex_t *mutex)
00148 {
00149 mutex->counter = -1;
00150 mutex->sema = CreateSemaphore (NULL, 0, 65535, NULL);
00151 }
00152
00153 int
00154 __gthr_win32_mutex_lock (__gthread_mutex_t *mutex)
00155 {
00156 if (InterlockedIncrement (&mutex->counter) == 0 ||
00157 WaitForSingleObject (mutex->sema, INFINITE) == WAIT_OBJECT_0)
00158 return 0;
00159 else
00160 {
00161
00162
00163 InterlockedDecrement (&mutex->counter);
00164 return 1;
00165 }
00166 }
00167
00168 int
00169 __gthr_win32_mutex_trylock (__gthread_mutex_t *mutex)
00170 {
00171 if (__GTHR_W32_InterlockedCompareExchange (&mutex->counter, 0, -1) < 0)
00172 return 0;
00173 else
00174 return 1;
00175 }
00176
00177 int
00178 __gthr_win32_mutex_unlock (__gthread_mutex_t *mutex)
00179 {
00180 if (InterlockedDecrement (&mutex->counter) >= 0)
00181 return ReleaseSemaphore (mutex->sema, 1, NULL) ? 0 : 1;
00182 else
00183 return 0;
00184 }
00185
00186 void
00187 __gthr_win32_recursive_mutex_init_function (__gthread_recursive_mutex_t *mutex)
00188 {
00189 mutex->counter = -1;
00190 mutex->depth = 0;
00191 mutex->owner = 0;
00192 mutex->sema = CreateSemaphore (NULL, 0, 65535, NULL);
00193 }
00194
00195 int
00196 __gthr_win32_recursive_mutex_lock (__gthread_recursive_mutex_t *mutex)
00197 {
00198 DWORD me = GetCurrentThreadId();
00199 if (InterlockedIncrement (&mutex->counter) == 0)
00200 {
00201 mutex->depth = 1;
00202 mutex->owner = me;
00203 }
00204 else if (mutex->owner == me)
00205 {
00206 InterlockedDecrement (&mutex->counter);
00207 ++(mutex->depth);
00208 }
00209 else if (WaitForSingleObject (mutex->sema, INFINITE) == WAIT_OBJECT_0)
00210 {
00211 mutex->depth = 1;
00212 mutex->owner = me;
00213 }
00214 else
00215 {
00216
00217
00218 InterlockedDecrement (&mutex->counter);
00219 return 1;
00220 }
00221 return 0;
00222 }
00223
00224 int
00225 __gthr_win32_recursive_mutex_trylock (__gthread_recursive_mutex_t *mutex)
00226 {
00227 DWORD me = GetCurrentThreadId();
00228 if (__GTHR_W32_InterlockedCompareExchange (&mutex->counter, 0, -1) < 0)
00229 {
00230 mutex->depth = 1;
00231 mutex->owner = me;
00232 }
00233 else if (mutex->owner == me)
00234 ++(mutex->depth);
00235 else
00236 return 1;
00237
00238 return 0;
00239 }
00240
00241 int
00242 __gthr_win32_recursive_mutex_unlock (__gthread_recursive_mutex_t *mutex)
00243 {
00244 --(mutex->depth);
00245 if (mutex->depth == 0)
00246 {
00247 mutex->owner = 0;
00248
00249 if (InterlockedDecrement (&mutex->counter) >= 0)
00250 return ReleaseSemaphore (mutex->sema, 1, NULL) ? 0 : 1;
00251 }
00252
00253 return 0;
00254 }