Debian Codegraph @

libgcc/config/i386/gthr-win32.h

raw
1 /* Threads compatibility routines for libgcc2 and libobjc. */
2 /* Compile this one with gcc. */
3
4 /* Copyright (C) 1999-2025 Free Software Foundation, Inc.
5 Contributed by Mumit Khan <[email protected]>.
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
13
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
18
19 Under Section 7 of GPL version 3, you are granted additional
20 permissions described in the GCC Runtime Library Exception, version
21 3.1, as published by the Free Software Foundation.
22
23 You should have received a copy of the GNU General Public License and
24 a copy of the GCC Runtime Library Exception along with this program;
25 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
26 <http://www.gnu.org/licenses/>. */
27
28 #ifndef GCC_GTHR_WIN32_H
29 #define GCC_GTHR_WIN32_H
30
31 /* So we can test Windows version numbers. */
32 #include <stdlib.h>
33
34 /* The Windows threading model does not map well into the POSIX inspired
35 GCC threading model, so there are caveats one needs to be aware of.
36
37 1. The destructor supplied to __gthread_key_create is ignored for
38 generic Windows 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 Windows 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. POSIX-like condition variables are supported, but only on Vista and
58 Server 2008 or later versions.
59
60 4. Timed lock primitives are not supported. */
61
62 #define __GTHREADS 1
63
64 /* Condition variables are supported on Vista and Server 2008 or later. */
65 #if _WIN32_WINNT >= 0x0600
66 #define __GTHREAD_HAS_COND 1
67 #define __GTHREADS_CXX0X 1
68 #endif
69
70 #if _GTHREAD_USE_MUTEX_TIMEDLOCK
71 #error Timed lock primitives are not supported on Windows targets
72 #endif
73
74 #ifdef __has_attribute
75 # if __has_attribute(__always_inline__)
76 # define __GTHREAD_ALWAYS_INLINE __attribute__((__always_inline__))
77 # endif
78 #endif
79 #ifndef __GTHREAD_ALWAYS_INLINE
80 # define __GTHREAD_ALWAYS_INLINE
81 #endif
82
83 #ifdef __cplusplus
84 # define __GTHREAD_INLINE inline __GTHREAD_ALWAYS_INLINE
85 #else
86 # define __GTHREAD_INLINE static inline
87 #endif
88
89 /* Make sure CONST_CAST2 (origin in system.h) is declared. */
90 #ifndef CONST_CAST2
91 #ifdef __cplusplus
92 #define CONST_CAST2(TOTYPE,FROMTYPE,X) (const_cast<TOTYPE> (X))
93 #else
94 #define CONST_CAST2(TOTYPE,FROMTYPE,X) ((__extension__(union {FROMTYPE _q; TOTYPE _nq;})(X))._nq)
95 #endif
96 #endif
97
98 #ifndef ATTRIBUTE_UNUSED
99 #define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
100 #endif
101
102 #ifdef _LIBOBJC
103
104 /* This is necessary to prevent windef.h (included from windows.h) from
105 defining its own BOOL as a typedef. */
106 #ifndef __OBJC__
107 #define __OBJC__
108 #endif
109 #define WIN32_LEAN_AND_MEAN
110 #include <windows.h>
111 /* Now undef the windows BOOL and CC_NONE */
112 #undef BOOL
113 #undef CC_NONE
114
115 /* Key structure for maintaining thread specific storage */
116 static DWORD __gthread_objc_data_tls = TLS_OUT_OF_INDEXES;
117
118 /* Backend initialization functions */
119
120 /* Initialize the threads subsystem. */
121 int
122 __gthread_objc_init_thread_system (void)
123 {
124 /* Initialize the thread storage key. */
125 if ((__gthread_objc_data_tls = TlsAlloc ()) != TLS_OUT_OF_INDEXES)
126 return 0;
127 else
128 return -1;
129 }
130
131 /* Close the threads subsystem. */
132 int
133 __gthread_objc_close_thread_system (void)
134 {
135 if (__gthread_objc_data_tls != TLS_OUT_OF_INDEXES)
136 TlsFree (__gthread_objc_data_tls);
137 return 0;
138 }
139
140 /* Backend thread functions */
141
142 /* Create a new thread of execution. */
143 objc_thread_t
144 __gthread_objc_thread_detach (void (*func)(void *arg), void *arg)
145 {
146 DWORD thread_id = 0;
147 HANDLE win32_handle;
148
149 if (!(win32_handle = CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE) func,
150 arg, 0, &thread_id)))
151 thread_id = 0;
152
153 return (objc_thread_t) (INT_PTR) thread_id;
154 }
155
156 /* Set the current thread's priority. */
157 int
158 __gthread_objc_thread_set_priority (int priority)
159 {
160 int sys_priority = 0;
161
162 switch (priority)
163 {
164 case OBJC_THREAD_INTERACTIVE_PRIORITY:
165 sys_priority = THREAD_PRIORITY_NORMAL;
166 break;
167 default:
168 case OBJC_THREAD_BACKGROUND_PRIORITY:
169 sys_priority = THREAD_PRIORITY_BELOW_NORMAL;
170 break;
171 case OBJC_THREAD_LOW_PRIORITY:
172 sys_priority = THREAD_PRIORITY_LOWEST;
173 break;
174 }
175
176 /* Change priority */
177 if (SetThreadPriority (GetCurrentThread (), sys_priority))
178 return 0;
179 else
180 return -1;
181 }
182
183 /* Return the current thread's priority. */
184 int
185 __gthread_objc_thread_get_priority (void)
186 {
187 int sys_priority;
188
189 sys_priority = GetThreadPriority (GetCurrentThread ());
190
191 switch (sys_priority)
192 {
193 case THREAD_PRIORITY_HIGHEST:
194 case THREAD_PRIORITY_TIME_CRITICAL:
195 case THREAD_PRIORITY_ABOVE_NORMAL:
196 case THREAD_PRIORITY_NORMAL:
197 return OBJC_THREAD_INTERACTIVE_PRIORITY;
198
199 default:
200 case THREAD_PRIORITY_BELOW_NORMAL:
201 return OBJC_THREAD_BACKGROUND_PRIORITY;
202
203 case THREAD_PRIORITY_IDLE:
204 case THREAD_PRIORITY_LOWEST:
205 return OBJC_THREAD_LOW_PRIORITY;
206 }
207
208 /* Couldn't get priority. */
209 return -1;
210 }
211
212 /* Yield our process time to another thread. */
213 void
214 __gthread_objc_thread_yield (void)
215 {
216 Sleep (0);
217 }
218
219 /* Terminate the current thread. */
220 int
221 __gthread_objc_thread_exit (void)
222 {
223 /* exit the thread */
224 ExitThread (__objc_thread_exit_status);
225
226 /* Failed if we reached here */
227 return -1;
228 }
229
230 /* Returns an integer value which uniquely describes a thread. */
231 objc_thread_t
232 __gthread_objc_thread_id (void)
233 {
234 return (objc_thread_t) (INT_PTR) GetCurrentThreadId ();
235 }
236
237 /* Sets the thread's local storage pointer. */
238 int
239 __gthread_objc_thread_set_data (void *value)
240 {
241 if (TlsSetValue (__gthread_objc_data_tls, value))
242 return 0;
243 else
244 return -1;
245 }
246
247 /* Returns the thread's local storage pointer. */
248 void *
249 __gthread_objc_thread_get_data (void)
250 {
251 DWORD lasterror = GetLastError ();
252 void * ptr = TlsGetValue (__gthread_objc_data_tls);
253 SetLastError (lasterror);
254 return ptr;
255 }
256
257 /* Backend mutex functions */
258
259 /* Allocate a mutex. */
260 int
261 __gthread_objc_mutex_allocate (objc_mutex_t mutex)
262 {
263 if ((mutex->backend = (void *) CreateMutex (NULL, 0, NULL)) == NULL)
264 return -1;
265 else
266 return 0;
267 }
268
269 /* Deallocate a mutex. */
270 int
271 __gthread_objc_mutex_deallocate (objc_mutex_t mutex)
272 {
273 CloseHandle ((HANDLE) (mutex->backend));
274 return 0;
275 }
276
277 /* Grab a lock on a mutex. */
278 int
279 __gthread_objc_mutex_lock (objc_mutex_t mutex)
280 {
281 int status;
282
283 status = WaitForSingleObject ((HANDLE) (mutex->backend), INFINITE);
284 if (status != WAIT_OBJECT_0 && status != WAIT_ABANDONED)
285 return -1;
286 else
287 return 0;
288 }
289
290 /* Try to grab a lock on a mutex. */
291 int
292 __gthread_objc_mutex_trylock (objc_mutex_t mutex)
293 {
294 int status;
295
296 status = WaitForSingleObject ((HANDLE) (mutex->backend), 0);
297 if (status != WAIT_OBJECT_0 && status != WAIT_ABANDONED)
298 return -1;
299 else
300 return 0;
301 }
302
303 /* Unlock the mutex */
304 int
305 __gthread_objc_mutex_unlock (objc_mutex_t mutex)
306 {
307 if (ReleaseMutex ((HANDLE) (mutex->backend)) == 0)
308 return -1;
309 else
310 return 0;
311 }
312
313 /* Backend condition mutex functions */
314
315 /* Allocate a condition. */
316 int
317 __gthread_objc_condition_allocate (objc_condition_t condition ATTRIBUTE_UNUSED)
318 {
319 /* Unimplemented. */
320 return -1;
321 }
322
323 /* Deallocate a condition. */
324 int
325 __gthread_objc_condition_deallocate (objc_condition_t condition ATTRIBUTE_UNUSED)
326 {
327 /* Unimplemented. */
328 return -1;
329 }
330
331 /* Wait on the condition */
332 int
333 __gthread_objc_condition_wait (objc_condition_t condition ATTRIBUTE_UNUSED,
334 objc_mutex_t mutex ATTRIBUTE_UNUSED)
335 {
336 /* Unimplemented. */
337 return -1;
338 }
339
340 /* Wake up all threads waiting on this condition. */
341 int
342 __gthread_objc_condition_broadcast (objc_condition_t condition ATTRIBUTE_UNUSED)
343 {
344 /* Unimplemented. */
345 return -1;
346 }
347
348 /* Wake up one thread waiting on this condition. */
349 int
350 __gthread_objc_condition_signal (objc_condition_t condition ATTRIBUTE_UNUSED)
351 {
352 /* Unimplemented. */
353 return -1;
354 }
355
356 #else /* _LIBOBJC */
357
358 /* For struct timespec. Do not include <sys/time.h> here since Gnulib provides
359 its own version which drags the Win32 API definitions. */
360 #include <sys/timeb.h>
361
362 #ifdef __cplusplus
363 extern "C" {
364 #endif
365
366 typedef unsigned int __gthr_win32_DWORD;
367 typedef void *__gthr_win32_HANDLE;
368
369 typedef struct {
370 void *DebugInfo;
371 int LockCount;
372 int RecursionCount;
373 __gthr_win32_HANDLE OwningThread;
374 __gthr_win32_HANDLE LockSemaphore;
375 void *SpinCount;
376 } __gthr_win32_CRITICAL_SECTION;
377
378 typedef struct {
379 void *Ptr;
380 } __gthr_win32_CONDITION_VARIABLE;
381
382 typedef __gthr_win32_HANDLE __gthread_t;
383 typedef __gthr_win32_DWORD __gthread_key_t;
384 typedef struct { int done; long started; } __gthread_once_t;
385 typedef __gthr_win32_CRITICAL_SECTION __gthread_mutex_t;
386 typedef __gthr_win32_CRITICAL_SECTION __gthread_recursive_mutex_t;
387 #if __GTHREAD_HAS_COND
388 typedef __gthr_win32_CONDITION_VARIABLE __gthread_cond_t;
389 #endif
390 typedef struct timespec __gthread_time_t;
391
392 #define __GTHREAD_ONCE_INIT {0, -1}
393 #define __GTHREAD_MUTEX_INIT_FUNCTION __gthread_mutex_init_function
394 #define __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION \
395 __gthread_recursive_mutex_init_function
396 #define __GTHREAD_COND_INIT_FUNCTION __gthread_cond_init_function
397 #define __GTHREAD_TIME_INIT {0, 0}
398
399 // Libstdc++ std::basic_filebuf needs the old definition of __gthread_mutex_t
400 // for layout purposes, but doesn't actually use it.
401 typedef struct {
402 long __unused1;
403 void *__unused2;
404 } __gthr_win32_legacy_mutex_t;
405 #define __GTHREAD_LEGACY_MUTEX_T __gthr_win32_legacy_mutex_t
406
407 #if defined (_WIN32) && !defined(__CYGWIN__)
408 #define MINGW32_SUPPORTS_MT_EH 1
409 /* Mingw runtime >= v0.3 provides a magic variable that is set to nonzero
410 if -mthreads option was specified, or 0 otherwise. This is to get around
411 the lack of weak symbols in PE-COFF. */
412 extern int _CRT_MT;
413 extern int __mingwthr_key_dtor (unsigned long, void (*) (void *));
414 #endif /* _WIN32 && !__CYGWIN__ */
415
416 __GTHREAD_INLINE int
417 __gthread_active_p (void)
418 {
419 #ifdef MINGW32_SUPPORTS_MT_EH
420 return _CRT_MT;
421 #else
422 return 1;
423 #endif
424 }
425
426 extern int __gthr_win32_create (__gthread_t *, void *(*) (void*), void *);
427 extern int __gthr_win32_join (__gthread_t, void **);
428 extern __gthread_t __gthr_win32_self (void);
429 extern int __gthr_win32_once (__gthread_once_t *, void (*) (void));
430 extern int __gthr_win32_detach (__gthread_t);
431 extern int __gthr_win32_equal (__gthread_t, __gthread_t);
432 extern int __gthr_win32_yield (void);
433 extern int __gthr_win32_key_create (__gthread_key_t *, void (*) (void*));
434 extern int __gthr_win32_key_delete (__gthread_key_t);
435 extern void * __gthr_win32_getspecific (__gthread_key_t);
436 extern int __gthr_win32_setspecific (__gthread_key_t, const void *);
437 extern void __gthr_win32_mutex_init_function (__gthread_mutex_t *);
438 extern void __gthr_win32_mutex_destroy (__gthread_mutex_t *);
439 extern int __gthr_win32_mutex_lock (__gthread_mutex_t *);
440 extern int __gthr_win32_mutex_trylock (__gthread_mutex_t *);
441 extern int __gthr_win32_mutex_unlock (__gthread_mutex_t *);
442 extern int __gthr_win32_recursive_mutex_trylock (__gthread_recursive_mutex_t *);
443 #if __GTHREAD_HAS_COND
444 extern void __gthr_win32_cond_init_function (__gthread_cond_t *);
445 extern int __gthr_win32_cond_broadcast (__gthread_cond_t *);
446 extern int __gthr_win32_cond_signal (__gthread_cond_t *);
447 extern int __gthr_win32_cond_wait (__gthread_cond_t *, __gthread_mutex_t *);
448 extern int __gthr_win32_cond_timedwait (__gthread_cond_t *, __gthread_mutex_t *,
449 const __gthread_time_t *);
450 #endif
451
452 __GTHREAD_INLINE int
453 __gthread_create (__gthread_t *__thr, void *(*__func) (void*),
454 void *__args)
455 {
456 return __gthr_win32_create (__thr, __func, __args);
457 }
458
459 __GTHREAD_INLINE int
460 __gthread_join (__gthread_t __thr, void **__value_ptr)
461 {
462 return __gthr_win32_join (__thr, __value_ptr);
463 }
464
465 __GTHREAD_INLINE __gthread_t
466 __gthread_self (void)
467 {
468 return __gthr_win32_self ();
469 }
470
471 #if __GTHREAD_HIDE_WIN32API
472
473 /* The implementations are in config/i386/gthr-win32.c in libgcc.a.
474 Only stubs are exposed to avoid polluting the C++ namespace with
475 Win32 API definitions. */
476
477 __GTHREAD_INLINE int
478 __gthread_detach (__gthread_t __thr)
479 {
480 return __gthr_win32_detach (__thr);
481 }
482
483 __GTHREAD_INLINE int
484 __gthread_equal (__gthread_t __thr1, __gthread_t __thr2)
485 {
486 return __gthr_win32_equal (__thr1, __thr2);
487 }
488
489 __GTHREAD_INLINE int
490 __gthread_yield (void)
491 {
492 return __gthr_win32_yield ();
493 }
494
495 __GTHREAD_INLINE int
496 __gthread_once (__gthread_once_t *__once, void (*__func) (void))
497 {
498 if (__gthread_active_p ())
499 return __gthr_win32_once (__once, __func);
500 else
501 return -1;
502 }
503
504 __GTHREAD_INLINE int
505 __gthread_key_create (__gthread_key_t *__key, void (*__dtor) (void *))
506 {
507 return __gthr_win32_key_create (__key, __dtor);
508 }
509
510 __GTHREAD_INLINE int
511 __gthread_key_delete (__gthread_key_t __key)
512 {
513 return __gthr_win32_key_delete (__key);
514 }
515
516 __GTHREAD_INLINE void *
517 __gthread_getspecific (__gthread_key_t __key)
518 {
519 return __gthr_win32_getspecific (__key);
520 }
521
522 __GTHREAD_INLINE int
523 __gthread_setspecific (__gthread_key_t __key, const void *__ptr)
524 {
525 return __gthr_win32_setspecific (__key, __ptr);
526 }
527
528 __GTHREAD_INLINE void
529 __gthread_mutex_init_function (__gthread_mutex_t *__mutex)
530 {
531 __gthr_win32_mutex_init_function (__mutex);
532 }
533
534 __GTHREAD_INLINE void
535 __gthread_mutex_destroy (__gthread_mutex_t *__mutex)
536 {
537 __gthr_win32_mutex_destroy (__mutex);
538 }
539
540 __GTHREAD_INLINE int
541 __gthread_mutex_lock (__gthread_mutex_t *__mutex)
542 {
543 if (__gthread_active_p ())
544 return __gthr_win32_mutex_lock (__mutex);
545 else
546 return 0;
547 }
548
549 __GTHREAD_INLINE int
550 __gthread_mutex_trylock (__gthread_mutex_t *__mutex)
551 {
552 if (__gthread_active_p ())
553 return __gthr_win32_mutex_trylock (__mutex);
554 else
555 return 0;
556 }
557
558 __GTHREAD_INLINE int
559 __gthread_mutex_unlock (__gthread_mutex_t *__mutex)
560 {
561 if (__gthread_active_p ())
562 return __gthr_win32_mutex_unlock (__mutex);
563 else
564 return 0;
565 }
566
567 __GTHREAD_INLINE int
568 __gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t *__mutex)
569 {
570 if (__gthread_active_p ())
571 return __gthr_win32_recursive_mutex_trylock (__mutex);
572 else
573 return 0;
574 }
575
576 #if __GTHREAD_HAS_COND
577
578 __GTHREAD_INLINE void
579 __gthread_cond_init_function (__gthread_cond_t *__cond)
580 {
581 __gthr_win32_cond_init_function (__cond);
582 }
583
584 __GTHREAD_INLINE int
585 __gthread_cond_broadcast (__gthread_cond_t *__cond)
586 {
587 return __gthr_win32_cond_broadcast (__cond);
588 }
589
590 __GTHREAD_INLINE int
591 __gthread_cond_signal (__gthread_cond_t *__cond)
592 {
593 return __gthr_win32_cond_signal (__cond);
594 }
595
596 __GTHREAD_INLINE int
597 __gthread_cond_wait (__gthread_cond_t *__cond, __gthread_mutex_t *__mutex)
598 {
599 return __gthr_win32_cond_wait (__cond, __mutex);
600 }
601
602 __GTHREAD_INLINE int
603 __gthread_cond_timedwait (__gthread_cond_t *__cond, __gthread_mutex_t *__mutex,
604 const __gthread_time_t *__abs_time)
605 {
606 return __gthr_win32_cond_timedwait (__cond, __mutex, __abs_time);
607 }
608
609 #endif /* __GTHREAD_HAS_COND */
610
611 #else /* ! __GTHREAD_HIDE_WIN32API */
612
613 #ifndef __GTHREAD_WIN32_INLINE
614 #define __GTHREAD_WIN32_INLINE __GTHREAD_INLINE
615 #endif
616
617 #ifndef __GTHREAD_WIN32_COND_INLINE
618 #define __GTHREAD_WIN32_COND_INLINE __GTHREAD_INLINE
619 #endif
620
621 #ifndef __GTHREAD_WIN32_ACTIVE_P
622 #define __GTHREAD_WIN32_ACTIVE_P __gthread_active_p
623 #endif
624
625 #define WIN32_LEAN_AND_MEAN
626 #include <windows.h>
627 #undef CC_NONE
628
629 __GTHREAD_WIN32_INLINE int
630 __gthread_detach (__gthread_t __thr)
631 {
632 CloseHandle ((HANDLE) __thr);
633 return 0;
634 }
635
636 __GTHREAD_WIN32_INLINE int
637 __gthread_equal (__gthread_t __t1, __gthread_t __t2)
638 {
639 return GetThreadId ((HANDLE) __t1) == GetThreadId ((HANDLE) __t2);
640 }
641
642 __GTHREAD_WIN32_INLINE int
643 __gthread_yield (void)
644 {
645 Sleep (0);
646 return 0;
647 }
648
649 __GTHREAD_WIN32_INLINE int
650 __gthread_once (__gthread_once_t *__once, void (*__func) (void))
651 {
652 if (!__GTHREAD_WIN32_ACTIVE_P ())
653 return -1;
654
655 if (__builtin_expect (!__once->done, 0))
656 {
657 /* We rely on the memory model of the x86 architecture where every load
658 has acquire semantics and every store has release semantics. */
659 if (__atomic_add_fetch (&__once->started, 1, __ATOMIC_ACQ_REL) == 0)
660 {
661 (*__func) ();
662 __once->done = 1;
663 }
664 else
665 {
666 /* Another thread is currently executing the code, so wait for it
667 to finish and yield the CPU in the meantime. If performance
668 does become an issue, the solution is to use an Event that
669 we wait on here (and set above), but that implies a place to
670 create the event before this routine is called. */
671 while (!__once->done)
672 __gthread_yield ();
673 }
674 }
675
676 return 0;
677 }
678
679 /* Windows thread local keys don't support destructors; this leads to
680 leaks, especially in threaded applications making extensive use of
681 C++ EH. Mingw uses a thread-support DLL to work-around this problem. */
682 __GTHREAD_WIN32_INLINE int
683 __gthread_key_create (__gthread_key_t *__key,
684 void (*__dtor) (void *) ATTRIBUTE_UNUSED)
685 {
686 DWORD __tls_index = TlsAlloc ();
687 if (__tls_index != TLS_OUT_OF_INDEXES)
688 {
689 *__key = __tls_index;
690 #ifdef MINGW32_SUPPORTS_MT_EH
691 /* Mingw runtime will run the dtors in reverse order for each thread
692 when the thread exits. */
693 return __mingwthr_key_dtor (*__key, __dtor);
694 #else
695 return 0;
696 #endif
697 }
698 else
699 return (int) GetLastError ();
700 }
701
702 __GTHREAD_WIN32_INLINE int
703 __gthread_key_delete (__gthread_key_t __key)
704 {
705 if (TlsFree (__key))
706 return 0;
707 else
708 return (int) GetLastError ();
709 }
710
711 __GTHREAD_WIN32_INLINE void *
712 __gthread_getspecific (__gthread_key_t __key)
713 {
714 DWORD __lasterror = GetLastError ();
715 void *__ptr = TlsGetValue (__key);
716 SetLastError (__lasterror);
717 return __ptr;
718 }
719
720 __GTHREAD_WIN32_INLINE int
721 __gthread_setspecific (__gthread_key_t __key, const void *__ptr)
722 {
723 if (TlsSetValue (__key, CONST_CAST2(void *, const void *, __ptr)))
724 return 0;
725 else
726 return (int) GetLastError ();
727 }
728
729 __GTHREAD_WIN32_INLINE void
730 __gthread_mutex_init_function (__gthread_mutex_t *__mutex)
731 {
732 InitializeCriticalSection ((LPCRITICAL_SECTION) __mutex);
733 }
734
735 __GTHREAD_WIN32_INLINE void
736 __gthread_mutex_destroy (__gthread_mutex_t *__mutex)
737 {
738 DeleteCriticalSection ((LPCRITICAL_SECTION) __mutex);
739 }
740
741 __GTHREAD_WIN32_INLINE int
742 __gthread_mutex_lock (__gthread_mutex_t *__mutex)
743 {
744 if (__GTHREAD_WIN32_ACTIVE_P ())
745 EnterCriticalSection ((LPCRITICAL_SECTION) __mutex);
746 return 0;
747 }
748
749 __GTHREAD_WIN32_INLINE int
750 __gthread_mutex_trylock (__gthread_mutex_t *__mutex)
751 {
752 if (__GTHREAD_WIN32_ACTIVE_P ())
753 {
754 BOOL __ret = TryEnterCriticalSection ((LPCRITICAL_SECTION) __mutex);
755 if (__ret)
756 {
757 if (__mutex->RecursionCount > 1)
758 {
759 LeaveCriticalSection ((LPCRITICAL_SECTION) __mutex);
760 return 1;
761 }
762 else
763 return 0;
764 }
765 else
766 return 1;
767 }
768 else
769 return 0;
770 }
771
772 __GTHREAD_WIN32_INLINE int
773 __gthread_mutex_unlock (__gthread_mutex_t *__mutex)
774 {
775 if (__GTHREAD_WIN32_ACTIVE_P ())
776 LeaveCriticalSection ((LPCRITICAL_SECTION) __mutex);
777 return 0;
778 }
779
780 __GTHREAD_WIN32_INLINE int
781 __gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t *__mutex)
782 {
783 if (__GTHREAD_WIN32_ACTIVE_P ())
784 return TryEnterCriticalSection ((LPCRITICAL_SECTION) __mutex) ? 0 : 1;
785 else
786 return 0;
787 }
788
789 #if __GTHREAD_HAS_COND
790
791 __GTHREAD_WIN32_COND_INLINE void
792 __gthread_cond_init_function (__gthread_cond_t *__cond)
793 {
794 InitializeConditionVariable ((PCONDITION_VARIABLE) __cond);
795 }
796
797 __GTHREAD_WIN32_COND_INLINE int
798 __gthread_cond_broadcast (__gthread_cond_t *__cond)
799 {
800 WakeAllConditionVariable ((PCONDITION_VARIABLE) __cond);
801 return 0;
802 }
803
804 __GTHREAD_WIN32_COND_INLINE int
805 __gthread_cond_signal (__gthread_cond_t *__cond)
806 {
807 WakeConditionVariable ((PCONDITION_VARIABLE) __cond);
808 return 0;
809 }
810
811 __GTHREAD_WIN32_COND_INLINE int
812 __gthread_cond_wait (__gthread_cond_t *__cond, __gthread_mutex_t *__mutex)
813 {
814 if (SleepConditionVariableCS ((PCONDITION_VARIABLE) __cond,
815 (PCRITICAL_SECTION) __mutex,
816 INFINITE))
817 return 0;
818 else
819 return (int) GetLastError ();
820 }
821
822 extern DWORD __gthr_win32_abs_to_rel_time (const __gthread_time_t *);
823
824 __GTHREAD_WIN32_COND_INLINE int
825 __gthread_cond_timedwait (__gthread_cond_t *__cond,
826 __gthread_mutex_t *__mutex,
827 const __gthread_time_t *__abs_time)
828 {
829 DWORD __rel_time = __gthr_win32_abs_to_rel_time (__abs_time);
830 if (SleepConditionVariableCS ((PCONDITION_VARIABLE) __cond,
831 (PCRITICAL_SECTION) __mutex,
832 __rel_time))
833 return 0;
834 else
835 return (int) GetLastError ();
836 }
837
838 #endif /* __GTHREAD_HAS_COND */
839
840 #endif /* __GTHREAD_HIDE_WIN32API */
841
842 __GTHREAD_INLINE void
843 __gthread_recursive_mutex_init_function (__gthread_recursive_mutex_t *__mutex)
844 {
845 __gthread_mutex_init_function (__mutex);
846 }
847
848 __GTHREAD_INLINE void
849 __gthread_recursive_mutex_destroy (__gthread_recursive_mutex_t *__mutex)
850 {
851 __gthread_mutex_destroy (__mutex);
852 }
853
854 __GTHREAD_INLINE int
855 __gthread_recursive_mutex_lock (__gthread_recursive_mutex_t *__mutex)
856 {
857 return __gthread_mutex_lock (__mutex);
858 }
859
860 __GTHREAD_INLINE int
861 __gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t *__mutex)
862 {
863 return __gthread_mutex_unlock (__mutex);
864 }
865
866 #if __GTHREAD_HAS_COND
867
868 __GTHREAD_INLINE int
869 __gthread_cond_destroy (__gthread_cond_t *__cond ATTRIBUTE_UNUSED)
870 {
871 return 0;
872 }
873
874 __GTHREAD_INLINE int
875 __gthread_cond_wait_recursive (__gthread_cond_t *__cond,
876 __gthread_recursive_mutex_t *__mutex)
877 {
878 return __gthread_cond_wait (__cond, __mutex);
879 }
880
881 #endif
882
883 #ifdef __cplusplus
884 }
885 #endif
886
887 #endif /* _LIBOBJC */
888
889 #endif /* ! GCC_GTHR_WIN32_H */