1 /* GNU Objective C Runtime Thread Interface - Win32 Implementation
2 Copyright (C) 1996, 1997, 2009 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 3, 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 Under Section 7 of GPL version 3, you are granted additional
17 permissions described in the GCC Runtime Library Exception, version
18 3.1, as published by the Free Software Foundation.
20 You should have received a copy of the GNU General Public License and
21 a copy of the GCC Runtime Library Exception along with this program;
22 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 <http://www.gnu.org/licenses/>. */
27 #include "objc/runtime.h"
34 /* Key structure for maintaining thread specific storage */
35 static DWORD __objc_data_tls
= (DWORD
)-1;
37 /* Backend initialization functions */
39 /* Initialize the threads subsystem. */
41 __objc_init_thread_system(void)
43 /* Initialize the thread storage key */
44 if ((__objc_data_tls
= TlsAlloc()) != (DWORD
)-1)
50 /* Close the threads subsystem. */
52 __objc_close_thread_system(void)
54 if (__objc_data_tls
!= (DWORD
)-1)
55 TlsFree(__objc_data_tls
);
59 /* Backend thread functions */
61 /* Create a new thread of execution. */
63 __objc_thread_detach(void (*func
)(void *arg
), void *arg
)
68 if (!(win32_handle
= CreateThread(NULL
, 0, (LPTHREAD_START_ROUTINE
)func
,
72 return (objc_thread_t
)(size_t) thread_id
;
75 /* Set the current thread's priority. */
77 __objc_thread_set_priority(int priority
)
83 case OBJC_THREAD_INTERACTIVE_PRIORITY
:
84 sys_priority
= THREAD_PRIORITY_NORMAL
;
87 case OBJC_THREAD_BACKGROUND_PRIORITY
:
88 sys_priority
= THREAD_PRIORITY_BELOW_NORMAL
;
90 case OBJC_THREAD_LOW_PRIORITY
:
91 sys_priority
= THREAD_PRIORITY_LOWEST
;
96 if (SetThreadPriority(GetCurrentThread(), sys_priority
))
102 /* Return the current thread's priority. */
104 __objc_thread_get_priority(void)
108 sys_priority
= GetThreadPriority(GetCurrentThread());
110 switch (sys_priority
)
112 case THREAD_PRIORITY_HIGHEST
:
113 case THREAD_PRIORITY_TIME_CRITICAL
:
114 case THREAD_PRIORITY_ABOVE_NORMAL
:
115 case THREAD_PRIORITY_NORMAL
:
116 return OBJC_THREAD_INTERACTIVE_PRIORITY
;
119 case THREAD_PRIORITY_BELOW_NORMAL
:
120 return OBJC_THREAD_BACKGROUND_PRIORITY
;
122 case THREAD_PRIORITY_IDLE
:
123 case THREAD_PRIORITY_LOWEST
:
124 return OBJC_THREAD_LOW_PRIORITY
;
127 /* Couldn't get priority. */
131 /* Yield our process time to another thread. */
133 __objc_thread_yield(void)
138 /* Terminate the current thread. */
140 __objc_thread_exit(void)
142 /* exit the thread */
143 ExitThread(__objc_thread_exit_status
);
145 /* Failed if we reached here */
149 /* Returns an integer value which uniquely describes a thread. */
151 __objc_thread_id(void)
153 return (objc_thread_t
)(size_t) GetCurrentThreadId();
156 /* Sets the thread's local storage pointer. */
158 __objc_thread_set_data(void *value
)
160 if (TlsSetValue(__objc_data_tls
, value
))
166 /* Returns the thread's local storage pointer. */
168 __objc_thread_get_data(void)
170 return TlsGetValue(__objc_data_tls
); /* Return thread data. */
173 /* Backend mutex functions */
175 /* Allocate a mutex. */
177 __objc_mutex_allocate(objc_mutex_t mutex
)
179 if ((mutex
->backend
= (void *)CreateMutex(NULL
, 0, NULL
)) == NULL
)
185 /* Deallocate a mutex. */
187 __objc_mutex_deallocate(objc_mutex_t mutex
)
189 CloseHandle((HANDLE
)(mutex
->backend
));
193 /* Grab a lock on a mutex. */
195 __objc_mutex_lock(objc_mutex_t mutex
)
199 status
= WaitForSingleObject((HANDLE
)(mutex
->backend
), INFINITE
);
200 if (status
!= WAIT_OBJECT_0
&& status
!= WAIT_ABANDONED
)
206 /* Try to grab a lock on a mutex. */
208 __objc_mutex_trylock(objc_mutex_t mutex
)
212 status
= WaitForSingleObject((HANDLE
)(mutex
->backend
), 0);
213 if (status
!= WAIT_OBJECT_0
&& status
!= WAIT_ABANDONED
)
219 /* Unlock the mutex */
221 __objc_mutex_unlock(objc_mutex_t mutex
)
223 if (ReleaseMutex((HANDLE
)(mutex
->backend
)) == 0)
229 /* Backend condition mutex functions */
231 /* Allocate a condition. */
233 __objc_condition_allocate(objc_condition_t condition
)
239 /* Deallocate a condition. */
241 __objc_condition_deallocate(objc_condition_t condition
)
247 /* Wait on the condition */
249 __objc_condition_wait(objc_condition_t condition
, objc_mutex_t mutex
)
255 /* Wake up all threads waiting on this condition. */
257 __objc_condition_broadcast(objc_condition_t condition
)
263 /* Wake up one thread waiting on this condition. */
265 __objc_condition_signal(objc_condition_t condition
)