stdint.m4 (GCC_HEADER_STDINT): Revert changes to this macro in the previous two patches.
[official-gcc.git] / libobjc / thr-win32.c
blob2d2f31e885e2f5ca00ff2e3dfe98ce4fce534027
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
14 details.
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/>. */
26 #include "objc/thr.h"
27 #include "objc/runtime.h"
29 #ifndef __OBJC__
30 #define __OBJC__
31 #endif
32 #include <windows.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. */
40 int
41 __objc_init_thread_system(void)
43 /* Initialize the thread storage key */
44 if ((__objc_data_tls = TlsAlloc()) != (DWORD)-1)
45 return 0;
46 else
47 return -1;
50 /* Close the threads subsystem. */
51 int
52 __objc_close_thread_system(void)
54 if (__objc_data_tls != (DWORD)-1)
55 TlsFree(__objc_data_tls);
56 return 0;
59 /* Backend thread functions */
61 /* Create a new thread of execution. */
62 objc_thread_t
63 __objc_thread_detach(void (*func)(void *arg), void *arg)
65 DWORD thread_id = 0;
66 HANDLE win32_handle;
68 if (!(win32_handle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)func,
69 arg, 0, &thread_id)))
70 thread_id = 0;
72 return (objc_thread_t)(size_t) thread_id;
75 /* Set the current thread's priority. */
76 int
77 __objc_thread_set_priority(int priority)
79 int sys_priority = 0;
81 switch (priority)
83 case OBJC_THREAD_INTERACTIVE_PRIORITY:
84 sys_priority = THREAD_PRIORITY_NORMAL;
85 break;
86 default:
87 case OBJC_THREAD_BACKGROUND_PRIORITY:
88 sys_priority = THREAD_PRIORITY_BELOW_NORMAL;
89 break;
90 case OBJC_THREAD_LOW_PRIORITY:
91 sys_priority = THREAD_PRIORITY_LOWEST;
92 break;
95 /* Change priority */
96 if (SetThreadPriority(GetCurrentThread(), sys_priority))
97 return 0;
98 else
99 return -1;
102 /* Return the current thread's priority. */
104 __objc_thread_get_priority(void)
106 int sys_priority;
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;
118 default:
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. */
128 return -1;
131 /* Yield our process time to another thread. */
132 void
133 __objc_thread_yield(void)
135 Sleep(0);
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 */
146 return -1;
149 /* Returns an integer value which uniquely describes a thread. */
150 objc_thread_t
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))
161 return 0;
162 else
163 return -1;
166 /* Returns the thread's local storage pointer. */
167 void *
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)
180 return -1;
181 else
182 return 0;
185 /* Deallocate a mutex. */
187 __objc_mutex_deallocate(objc_mutex_t mutex)
189 CloseHandle((HANDLE)(mutex->backend));
190 return 0;
193 /* Grab a lock on a mutex. */
195 __objc_mutex_lock(objc_mutex_t mutex)
197 int status;
199 status = WaitForSingleObject((HANDLE)(mutex->backend), INFINITE);
200 if (status != WAIT_OBJECT_0 && status != WAIT_ABANDONED)
201 return -1;
202 else
203 return 0;
206 /* Try to grab a lock on a mutex. */
208 __objc_mutex_trylock(objc_mutex_t mutex)
210 int status;
212 status = WaitForSingleObject((HANDLE)(mutex->backend), 0);
213 if (status != WAIT_OBJECT_0 && status != WAIT_ABANDONED)
214 return -1;
215 else
216 return 0;
219 /* Unlock the mutex */
221 __objc_mutex_unlock(objc_mutex_t mutex)
223 if (ReleaseMutex((HANDLE)(mutex->backend)) == 0)
224 return -1;
225 else
226 return 0;
229 /* Backend condition mutex functions */
231 /* Allocate a condition. */
233 __objc_condition_allocate(objc_condition_t condition)
235 /* Unimplemented. */
236 return -1;
239 /* Deallocate a condition. */
241 __objc_condition_deallocate(objc_condition_t condition)
243 /* Unimplemented. */
244 return -1;
247 /* Wait on the condition */
249 __objc_condition_wait(objc_condition_t condition, objc_mutex_t mutex)
251 /* Unimplemented. */
252 return -1;
255 /* Wake up all threads waiting on this condition. */
257 __objc_condition_broadcast(objc_condition_t condition)
259 /* Unimplemented. */
260 return -1;
263 /* Wake up one thread waiting on this condition. */
265 __objc_condition_signal(objc_condition_t condition)
267 /* Unimplemented. */
268 return -1;
271 /* End of File */