1 /* GNU Objective C Runtime Thread Interface - Win32 Implementation
2 Copyright (C) 1996, 1997 Free Software Foundation, Inc.
3 Contributed by Galen C. Hunt (gchunt@cs.rochester.edu)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under the
8 terms of the GNU General Public License as published by the Free Software
9 Foundation; either version 2, or (at your option) any later version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16 You should have received a copy of the GNU General Public License along with
17 GCC; see the file COPYING. If not, write to the Free Software
18 Foundation, 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA. */
21 /* As a special exception, if you link this library with files compiled with
22 GCC to produce an executable, this does not cause the resulting executable
23 to be covered by the GNU General Public License. This exception does not
24 however invalidate any other reasons why the executable file might be
25 covered by the GNU General Public License. */
28 #include "objc/runtime.h"
35 /* Key structure for maintaining thread specific storage */
36 static DWORD __objc_data_tls
= (DWORD
)-1;
38 /* Backend initialization functions */
40 /* Initialize the threads subsystem. */
42 __objc_init_thread_system(void)
44 /* Initialize the thread storage key */
45 if ((__objc_data_tls
= TlsAlloc()) != (DWORD
)-1)
51 /* Close the threads subsystem. */
53 __objc_close_thread_system(void)
55 if (__objc_data_tls
!= (DWORD
)-1)
56 TlsFree(__objc_data_tls
);
60 /* Backend thread functions */
62 /* Create a new thread of execution. */
64 __objc_thread_detach(void (*func
)(void *arg
), void *arg
)
69 if (!(win32_handle
= CreateThread(NULL
, 0, (LPTHREAD_START_ROUTINE
)func
,
73 return (objc_thread_t
)(size_t) thread_id
;
76 /* Set the current thread's priority. */
78 __objc_thread_set_priority(int priority
)
84 case OBJC_THREAD_INTERACTIVE_PRIORITY
:
85 sys_priority
= THREAD_PRIORITY_NORMAL
;
88 case OBJC_THREAD_BACKGROUND_PRIORITY
:
89 sys_priority
= THREAD_PRIORITY_BELOW_NORMAL
;
91 case OBJC_THREAD_LOW_PRIORITY
:
92 sys_priority
= THREAD_PRIORITY_LOWEST
;
97 if (SetThreadPriority(GetCurrentThread(), sys_priority
))
103 /* Return the current thread's priority. */
105 __objc_thread_get_priority(void)
109 sys_priority
= GetThreadPriority(GetCurrentThread());
111 switch (sys_priority
)
113 case THREAD_PRIORITY_HIGHEST
:
114 case THREAD_PRIORITY_TIME_CRITICAL
:
115 case THREAD_PRIORITY_ABOVE_NORMAL
:
116 case THREAD_PRIORITY_NORMAL
:
117 return OBJC_THREAD_INTERACTIVE_PRIORITY
;
120 case THREAD_PRIORITY_BELOW_NORMAL
:
121 return OBJC_THREAD_BACKGROUND_PRIORITY
;
123 case THREAD_PRIORITY_IDLE
:
124 case THREAD_PRIORITY_LOWEST
:
125 return OBJC_THREAD_LOW_PRIORITY
;
128 /* Couldn't get priority. */
132 /* Yield our process time to another thread. */
134 __objc_thread_yield(void)
139 /* Terminate the current thread. */
141 __objc_thread_exit(void)
143 /* exit the thread */
144 ExitThread(__objc_thread_exit_status
);
146 /* Failed if we reached here */
150 /* Returns an integer value which uniquely describes a thread. */
152 __objc_thread_id(void)
154 return (objc_thread_t
)(size_t) GetCurrentThreadId();
157 /* Sets the thread's local storage pointer. */
159 __objc_thread_set_data(void *value
)
161 if (TlsSetValue(__objc_data_tls
, value
))
167 /* Returns the thread's local storage pointer. */
169 __objc_thread_get_data(void)
171 return TlsGetValue(__objc_data_tls
); /* Return thread data. */
174 /* Backend mutex functions */
176 /* Allocate a mutex. */
178 __objc_mutex_allocate(objc_mutex_t mutex
)
180 if ((mutex
->backend
= (void *)CreateMutex(NULL
, 0, NULL
)) == NULL
)
186 /* Deallocate a mutex. */
188 __objc_mutex_deallocate(objc_mutex_t mutex
)
190 CloseHandle((HANDLE
)(mutex
->backend
));
194 /* Grab a lock on a mutex. */
196 __objc_mutex_lock(objc_mutex_t mutex
)
200 status
= WaitForSingleObject((HANDLE
)(mutex
->backend
), INFINITE
);
201 if (status
!= WAIT_OBJECT_0
&& status
!= WAIT_ABANDONED
)
207 /* Try to grab a lock on a mutex. */
209 __objc_mutex_trylock(objc_mutex_t mutex
)
213 status
= WaitForSingleObject((HANDLE
)(mutex
->backend
), 0);
214 if (status
!= WAIT_OBJECT_0
&& status
!= WAIT_ABANDONED
)
220 /* Unlock the mutex */
222 __objc_mutex_unlock(objc_mutex_t mutex
)
224 if (ReleaseMutex((HANDLE
)(mutex
->backend
)) == 0)
230 /* Backend condition mutex functions */
232 /* Allocate a condition. */
234 __objc_condition_allocate(objc_condition_t condition
)
240 /* Deallocate a condition. */
242 __objc_condition_deallocate(objc_condition_t condition
)
248 /* Wait on the condition */
250 __objc_condition_wait(objc_condition_t condition
, objc_mutex_t mutex
)
256 /* Wake up all threads waiting on this condition. */
258 __objc_condition_broadcast(objc_condition_t condition
)
264 /* Wake up one thread waiting on this condition. */
266 __objc_condition_signal(objc_condition_t condition
)