move gcc changes to gcc subdirectory
[official-gcc.git] / libobjc / thr-win32.c
blob8570ffd997ef58bfedfc769b4b1dcaa411c05928
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 GNU CC.
7 GNU CC 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 GNU CC 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 You should have received a copy of the GNU General Public License along with
17 GNU CC; see the file COPYING. If not, write to the Free Software
18 Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, 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. */
27 #include <objc/thr.h>
28 #include "runtime.h"
30 #ifndef __OBJC__
31 #define __OBJC__
32 #endif
33 #include <windows.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. */
41 int
42 __objc_init_thread_system(void)
44 /* Initialize the thread storage key */
45 if ((__objc_data_tls = TlsAlloc()) != (DWORD)-1)
46 return 0;
47 else
48 return -1;
51 /* Close the threads subsystem. */
52 int
53 __objc_close_thread_system(void)
55 if (__objc_data_tls != (DWORD)-1)
56 TlsFree(__objc_data_tls);
57 return 0;
60 /* Backend thread functions */
62 /* Create a new thread of execution. */
63 objc_thread_t
64 __objc_thread_detach(void (*func)(void *arg), void *arg)
66 DWORD thread_id = 0;
67 HANDLE win32_handle;
69 if (!(win32_handle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)func,
70 arg, 0, &thread_id)))
71 thread_id = 0;
73 return (objc_thread_t)thread_id;
76 /* Set the current thread's priority. */
77 int
78 __objc_thread_set_priority(int priority)
80 int sys_priority = 0;
82 switch (priority)
84 case OBJC_THREAD_INTERACTIVE_PRIORITY:
85 sys_priority = THREAD_PRIORITY_NORMAL;
86 break;
87 default:
88 case OBJC_THREAD_BACKGROUND_PRIORITY:
89 sys_priority = THREAD_PRIORITY_BELOW_NORMAL;
90 break;
91 case OBJC_THREAD_LOW_PRIORITY:
92 sys_priority = THREAD_PRIORITY_LOWEST;
93 break;
96 /* Change priority */
97 if (SetThreadPriority(GetCurrentThread(), sys_priority))
98 return 0;
99 else
100 return -1;
103 /* Return the current thread's priority. */
105 __objc_thread_get_priority(void)
107 int sys_priority;
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;
119 default:
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. */
129 return -1;
132 /* Yield our process time to another thread. */
133 void
134 __objc_thread_yield(void)
136 Sleep(0);
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 */
147 return -1;
150 /* Returns an integer value which uniquely describes a thread. */
151 objc_thread_t
152 __objc_thread_id(void)
154 return (objc_thread_t)GetCurrentThreadId();
157 /* Sets the thread's local storage pointer. */
159 __objc_thread_set_data(void *value)
161 if (TlsSetValue(__objc_data_tls, value))
162 return 0;
163 else
164 return -1;
167 /* Returns the thread's local storage pointer. */
168 void *
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)
181 return -1;
182 else
183 return 0;
186 /* Deallocate a mutex. */
188 __objc_mutex_deallocate(objc_mutex_t mutex)
190 CloseHandle((HANDLE)(mutex->backend));
191 return 0;
194 /* Grab a lock on a mutex. */
196 __objc_mutex_lock(objc_mutex_t mutex)
198 int status;
200 status = WaitForSingleObject((HANDLE)(mutex->backend), INFINITE);
201 if (status != WAIT_OBJECT_0 && status != WAIT_ABANDONED)
202 return -1;
203 else
204 return 0;
207 /* Try to grab a lock on a mutex. */
209 __objc_mutex_trylock(objc_mutex_t mutex)
211 int status;
213 status = WaitForSingleObject((HANDLE)(mutex->backend), 0);
214 if (status != WAIT_OBJECT_0 && status != WAIT_ABANDONED)
215 return -1;
216 else
217 return 0;
220 /* Unlock the mutex */
222 __objc_mutex_unlock(objc_mutex_t mutex)
224 if (ReleaseMutex((HANDLE)(mutex->backend)) == 0)
225 return -1;
226 else
227 return 0;
230 /* Backend condition mutex functions */
232 /* Allocate a condition. */
234 __objc_condition_allocate(objc_condition_t condition)
236 /* Unimplemented. */
237 return -1;
240 /* Deallocate a condition. */
242 __objc_condition_deallocate(objc_condition_t condition)
244 /* Unimplemented. */
245 return -1;
248 /* Wait on the condition */
250 __objc_condition_wait(objc_condition_t condition, objc_mutex_t mutex)
252 /* Unimplemented. */
253 return -1;
256 /* Wake up all threads waiting on this condition. */
258 __objc_condition_broadcast(objc_condition_t condition)
260 /* Unimplemented. */
261 return -1;
264 /* Wake up one thread waiting on this condition. */
266 __objc_condition_signal(objc_condition_t condition)
268 /* Unimplemented. */
269 return -1;
272 /* End of File */