source: trunk/third/gcc/libobjc/thr-win32.c @ 21199

Revision 21199, 5.9 KB checked in by ghudson, 20 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r21198, which included commits to RCS files with non-trunk default branches.
Line 
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)
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it under the
8terms of the GNU General Public License as published by the Free Software
9Foundation; either version 2, or (at your option) any later version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
14details.
15
16You should have received a copy of the GNU General Public License along with
17GCC; see the file COPYING.  If not, write to the Free Software
18Foundation, 59 Temple Place - Suite 330,
19Boston, MA 02111-1307, USA.  */
20
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.  */
26
27#include <objc/thr.h>
28#include "runtime.h"
29
30#ifndef __OBJC__
31#define __OBJC__
32#endif
33#include <windows.h>
34
35/* Key structure for maintaining thread specific storage */
36static DWORD    __objc_data_tls = (DWORD)-1;
37
38/* Backend initialization functions */
39
40/* Initialize the threads subsystem. */
41int
42__objc_init_thread_system(void)
43{
44  /* Initialize the thread storage key */
45  if ((__objc_data_tls = TlsAlloc()) != (DWORD)-1)
46    return 0;
47  else
48    return -1;
49}
50
51/* Close the threads subsystem. */
52int
53__objc_close_thread_system(void)
54{
55  if (__objc_data_tls != (DWORD)-1)
56    TlsFree(__objc_data_tls);
57  return 0;
58}
59
60/* Backend thread functions */
61
62/* Create a new thread of execution. */
63objc_thread_t
64__objc_thread_detach(void (*func)(void *arg), void *arg)
65{
66  DWORD thread_id = 0;
67  HANDLE win32_handle;
68
69  if (!(win32_handle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)func,
70                                   arg, 0, &thread_id)))
71    thread_id = 0;
72 
73  return (objc_thread_t)thread_id;
74}
75
76/* Set the current thread's priority. */
77int
78__objc_thread_set_priority(int priority)
79{
80  int sys_priority = 0;
81
82  switch (priority)
83    {
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;
94    }
95
96  /* Change priority */
97  if (SetThreadPriority(GetCurrentThread(), sys_priority))
98    return 0;
99  else
100    return -1;
101}
102
103/* Return the current thread's priority. */
104int
105__objc_thread_get_priority(void)
106{
107  int sys_priority;
108
109  sys_priority = GetThreadPriority(GetCurrentThread());
110 
111  switch (sys_priority)
112    {
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;
118
119    default:
120    case THREAD_PRIORITY_BELOW_NORMAL:
121      return OBJC_THREAD_BACKGROUND_PRIORITY;
122   
123    case THREAD_PRIORITY_IDLE:
124    case THREAD_PRIORITY_LOWEST:
125      return OBJC_THREAD_LOW_PRIORITY;
126    }
127
128  /* Couldn't get priority. */
129  return -1;
130}
131
132/* Yield our process time to another thread. */
133void
134__objc_thread_yield(void)
135{
136  Sleep(0);
137}
138
139/* Terminate the current thread. */
140int
141__objc_thread_exit(void)
142{
143  /* exit the thread */
144  ExitThread(__objc_thread_exit_status);
145
146  /* Failed if we reached here */
147  return -1;
148}
149
150/* Returns an integer value which uniquely describes a thread. */
151objc_thread_t
152__objc_thread_id(void)
153{
154  return (objc_thread_t)GetCurrentThreadId();
155}
156
157/* Sets the thread's local storage pointer. */
158int
159__objc_thread_set_data(void *value)
160{
161  if (TlsSetValue(__objc_data_tls, value))
162    return 0;
163  else
164    return -1;
165}
166
167/* Returns the thread's local storage pointer. */
168void *
169__objc_thread_get_data(void)
170{
171  return TlsGetValue(__objc_data_tls);          /* Return thread data.      */
172}
173
174/* Backend mutex functions */
175
176/* Allocate a mutex. */
177int
178__objc_mutex_allocate(objc_mutex_t mutex)
179{
180  if ((mutex->backend = (void *)CreateMutex(NULL, 0, NULL)) == NULL)
181    return -1;
182  else
183    return 0;
184}
185
186/* Deallocate a mutex. */
187int
188__objc_mutex_deallocate(objc_mutex_t mutex)
189{
190  CloseHandle((HANDLE)(mutex->backend));
191  return 0;
192}
193
194/* Grab a lock on a mutex. */
195int
196__objc_mutex_lock(objc_mutex_t mutex)
197{
198  int status;
199
200  status = WaitForSingleObject((HANDLE)(mutex->backend), INFINITE);
201  if (status != WAIT_OBJECT_0 && status != WAIT_ABANDONED)
202    return -1;
203  else
204    return 0;
205}
206
207/* Try to grab a lock on a mutex. */
208int
209__objc_mutex_trylock(objc_mutex_t mutex)
210{
211  int status;
212
213  status = WaitForSingleObject((HANDLE)(mutex->backend), 0);
214  if (status != WAIT_OBJECT_0 && status != WAIT_ABANDONED)
215    return -1;
216  else
217    return 0;
218}
219
220/* Unlock the mutex */
221int
222__objc_mutex_unlock(objc_mutex_t mutex)
223{
224  if (ReleaseMutex((HANDLE)(mutex->backend)) == 0)
225    return -1;
226  else
227    return 0;
228}
229
230/* Backend condition mutex functions */
231
232/* Allocate a condition. */
233int
234__objc_condition_allocate(objc_condition_t condition)
235{
236  /* Unimplemented. */
237  return -1;
238}
239
240/* Deallocate a condition. */
241int
242__objc_condition_deallocate(objc_condition_t condition)
243{
244  /* Unimplemented. */
245  return -1;
246}
247
248/* Wait on the condition */
249int
250__objc_condition_wait(objc_condition_t condition, objc_mutex_t mutex)
251{
252  /* Unimplemented. */
253  return -1;
254}
255
256/* Wake up all threads waiting on this condition. */
257int
258__objc_condition_broadcast(objc_condition_t condition)
259{
260  /* Unimplemented. */
261  return -1;
262}
263
264/* Wake up one thread waiting on this condition. */
265int
266__objc_condition_signal(objc_condition_t condition)
267{
268  /* Unimplemented. */
269  return -1;
270}
271
272/* End of File */
Note: See TracBrowser for help on using the repository browser.