OSDN Git Service

ChangeLog for compare-all-tests addition
[pf3gnuchains/gcc-fork.git] / libobjc / thr-win32.c
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)
4
5 This file is part of GCC.
6
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.
10
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.
15
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.
19
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/>.  */
24
25
26 #include "objc/thr.h"
27 #include "objc/runtime.h"
28
29 #ifndef __OBJC__
30 #define __OBJC__
31 #endif
32 #include <windows.h>
33
34 /* Key structure for maintaining thread specific storage */
35 static DWORD    __objc_data_tls = (DWORD)-1;
36
37 /* Backend initialization functions */
38
39 /* Initialize the threads subsystem. */
40 int
41 __objc_init_thread_system(void)
42 {
43   /* Initialize the thread storage key */
44   if ((__objc_data_tls = TlsAlloc()) != (DWORD)-1)
45     return 0;
46   else
47     return -1;
48 }
49
50 /* Close the threads subsystem. */
51 int
52 __objc_close_thread_system(void)
53 {
54   if (__objc_data_tls != (DWORD)-1)
55     TlsFree(__objc_data_tls);
56   return 0;
57 }
58
59 /* Backend thread functions */
60
61 /* Create a new thread of execution. */
62 objc_thread_t
63 __objc_thread_detach(void (*func)(void *arg), void *arg)
64 {
65   DWORD thread_id = 0;
66   HANDLE win32_handle;
67
68   if (!(win32_handle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)func,
69                                    arg, 0, &thread_id)))
70     thread_id = 0;
71   
72   return (objc_thread_t)(size_t) thread_id;
73 }
74
75 /* Set the current thread's priority. */
76 int
77 __objc_thread_set_priority(int priority)
78 {
79   int sys_priority = 0;
80
81   switch (priority)
82     {
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;
93     }
94
95   /* Change priority */
96   if (SetThreadPriority(GetCurrentThread(), sys_priority))
97     return 0;
98   else
99     return -1;
100 }
101
102 /* Return the current thread's priority. */
103 int
104 __objc_thread_get_priority(void)
105 {
106   int sys_priority;
107
108   sys_priority = GetThreadPriority(GetCurrentThread());
109   
110   switch (sys_priority)
111     {
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;
117
118     default:
119     case THREAD_PRIORITY_BELOW_NORMAL:
120       return OBJC_THREAD_BACKGROUND_PRIORITY;
121     
122     case THREAD_PRIORITY_IDLE:
123     case THREAD_PRIORITY_LOWEST:
124       return OBJC_THREAD_LOW_PRIORITY;
125     }
126
127   /* Couldn't get priority. */
128   return -1;
129 }
130
131 /* Yield our process time to another thread. */
132 void
133 __objc_thread_yield(void)
134 {
135   Sleep(0);
136 }
137
138 /* Terminate the current thread. */
139 int
140 __objc_thread_exit(void)
141 {
142   /* exit the thread */
143   ExitThread(__objc_thread_exit_status);
144
145   /* Failed if we reached here */
146   return -1;
147 }
148
149 /* Returns an integer value which uniquely describes a thread. */
150 objc_thread_t
151 __objc_thread_id(void)
152 {
153   return (objc_thread_t)(size_t) GetCurrentThreadId();
154 }
155
156 /* Sets the thread's local storage pointer. */
157 int
158 __objc_thread_set_data(void *value)
159 {
160   if (TlsSetValue(__objc_data_tls, value))
161     return 0;
162   else
163     return -1;
164 }
165
166 /* Returns the thread's local storage pointer. */
167 void *
168 __objc_thread_get_data(void)
169 {
170   return TlsGetValue(__objc_data_tls);          /* Return thread data.      */
171 }
172
173 /* Backend mutex functions */
174
175 /* Allocate a mutex. */
176 int
177 __objc_mutex_allocate(objc_mutex_t mutex)
178 {
179   if ((mutex->backend = (void *)CreateMutex(NULL, 0, NULL)) == NULL)
180     return -1;
181   else
182     return 0;
183 }
184
185 /* Deallocate a mutex. */
186 int
187 __objc_mutex_deallocate(objc_mutex_t mutex)
188 {
189   CloseHandle((HANDLE)(mutex->backend));
190   return 0;
191 }
192
193 /* Grab a lock on a mutex. */
194 int
195 __objc_mutex_lock(objc_mutex_t mutex)
196 {
197   int status;
198
199   status = WaitForSingleObject((HANDLE)(mutex->backend), INFINITE);
200   if (status != WAIT_OBJECT_0 && status != WAIT_ABANDONED)
201     return -1;
202   else
203     return 0;
204 }
205
206 /* Try to grab a lock on a mutex. */
207 int
208 __objc_mutex_trylock(objc_mutex_t mutex)
209 {
210   int status;
211
212   status = WaitForSingleObject((HANDLE)(mutex->backend), 0);
213   if (status != WAIT_OBJECT_0 && status != WAIT_ABANDONED)
214     return -1;
215   else
216     return 0;
217 }
218
219 /* Unlock the mutex */
220 int
221 __objc_mutex_unlock(objc_mutex_t mutex)
222 {
223   if (ReleaseMutex((HANDLE)(mutex->backend)) == 0)
224     return -1;
225   else
226     return 0;
227 }
228
229 /* Backend condition mutex functions */
230
231 /* Allocate a condition. */
232 int
233 __objc_condition_allocate(objc_condition_t condition)
234 {
235   /* Unimplemented. */
236   return -1;
237 }
238
239 /* Deallocate a condition. */
240 int
241 __objc_condition_deallocate(objc_condition_t condition)
242 {
243   /* Unimplemented. */
244   return -1;
245 }
246
247 /* Wait on the condition */
248 int
249 __objc_condition_wait(objc_condition_t condition, objc_mutex_t mutex)
250 {
251   /* Unimplemented. */
252   return -1;
253 }
254
255 /* Wake up all threads waiting on this condition. */
256 int
257 __objc_condition_broadcast(objc_condition_t condition)
258 {
259   /* Unimplemented. */
260   return -1;
261 }
262
263 /* Wake up one thread waiting on this condition. */
264 int
265 __objc_condition_signal(objc_condition_t condition)
266 {
267   /* Unimplemented. */
268   return -1;
269 }
270
271 /* End of File */