OSDN Git Service

Backport from git Libtool:
[pf3gnuchains/gcc-fork.git] / libobjc / thr-os2.c
1 /* GNU Objective C Runtime Thread Interface - OS/2 emx Implementation
2    Copyright (C) 1996, 1997 Free Software Foundation, Inc.
3    Contributed by Thomas Baier (baier@ci.tuwien.ac.at)
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 2, 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 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING.  If not, write to
18 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, 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 "objc/runtime.h"
29
30 #define INCL_DOSSEMAPHORES
31 #define INCL_DOSPROCESS
32
33 /*
34  * conflicts with objc.h:       SEL, BOOL, id
35  * solution:  prefixing those with _OS2_ before including <os2.h>
36  */
37 #define SEL _OS2_SEL
38 #define BOOL _OS2_BOOL
39 #define id _OS2_id
40 #include <os2.h>
41 #undef id
42 #undef SEL
43 #undef BOOL
44
45 #include <stdlib.h>
46
47 /* Backend initialization functions */
48
49 /* Initialize the threads subsystem. */
50 int
51 __objc_init_thread_system(void)
52 {
53   return 0;
54 }
55
56 /* Close the threads subsystem. */
57 int
58 __objc_close_thread_system(void)
59 {
60   return 0;
61 }
62
63 /* Backend thread functions */
64
65 /* Create a new thread of execution. */
66 objc_thread_t
67 __objc_thread_detach(void (*func)(void *arg), void *arg)
68 {
69   int thread_id = 0;
70
71   if ((thread_id = _beginthread (func,NULL,32768,arg)) < 0)
72     thread_id = 0;
73   
74   return (objc_thread_t)thread_id;
75 }
76
77 /* Set the current thread's priority. */
78 int
79 __objc_thread_set_priority(int priority)
80 {
81   ULONG sys_class = 0;
82   ULONG sys_priority = 0;
83
84   /* OBJC_THREAD_INTERACTIVE_PRIORITY -> PRTYC_FOREGROUNDSERVER
85    * OBJC_THREAD_BACKGROUND_PRIORITY  -> PRTYC_REGULAR
86    * OBJC_THREAD_LOW_PRIORITY         -> PRTYC_IDLETIME */
87   
88   switch (priority) {
89   case OBJC_THREAD_INTERACTIVE_PRIORITY:
90     sys_class = PRTYC_REGULAR;
91     sys_priority = 10;
92     break;
93   default:
94   case OBJC_THREAD_BACKGROUND_PRIORITY:
95     sys_class = PRTYC_IDLETIME;
96     sys_priority = 25;
97     break;
98   case OBJC_THREAD_LOW_PRIORITY:
99     sys_class = PRTYC_IDLETIME;
100     sys_priority = 0;
101     break;
102   }
103
104   /* Change priority */
105   if (!DosSetPriority (PRTYS_THREAD,sys_class,sys_priority,*_threadid))
106     return 0;
107   else
108     return -1;
109 }
110
111 /* Return the current thread's priority. */
112 int
113 __objc_thread_get_priority(void)
114 {
115   PTIB ptib;
116   PPIB ppib;
117
118   /* get information about current thread */
119   DosGetInfoBlocks (&ptib,&ppib);
120
121   switch (ptib->tib_ptib2->tib2_ulpri)
122     {
123     case PRTYC_IDLETIME:
124     case PRTYC_REGULAR:
125     case PRTYC_TIMECRITICAL:
126     case PRTYC_FOREGROUNDSERVER:
127     default:
128       return OBJC_THREAD_INTERACTIVE_PRIORITY;
129     }
130
131   return -1;
132 }
133
134 /* Yield our process time to another thread. */
135 void
136 __objc_thread_yield(void)
137 {
138   DosSleep (0);
139 }
140
141 /* Terminate the current thread. */
142 int
143 __objc_thread_exit(void)
144 {
145   /* terminate the thread, NEVER use DosExit () */
146   _endthread ();
147
148   /* Failed if we reached here */
149   return -1;
150 }
151
152 /* Returns an integer value which uniquely describes a thread. */
153 objc_thread_t
154 __objc_thread_id(void)
155 {
156   return (objc_thread_t) *_threadid;
157 }
158
159 /* Sets the thread's local storage pointer. */
160 int
161 __objc_thread_set_data(void *value)
162 {
163   *_threadstore () = value;
164
165   return 0;
166 }
167
168 /* Returns the thread's local storage pointer. */
169 void *
170 __objc_thread_get_data(void)
171 {
172   return *_threadstore ();
173 }
174
175 /* Backend mutex functions */
176
177 /* Allocate a mutex. */
178 int
179 __objc_mutex_allocate(objc_mutex_t mutex)
180 {
181   if (DosCreateMutexSem (NULL, (HMTX)(&(mutex->backend)),0L,0) > 0)
182     return -1;
183   else
184     return 0;
185 }
186
187 /* Deallocate a mutex. */
188 int
189 __objc_mutex_deallocate(objc_mutex_t mutex)
190 {
191   DosCloseMutexSem ((HMTX)(mutex->backend));
192   return 0;
193 }
194
195 /* Grab a lock on a mutex. */
196 int
197 __objc_mutex_lock(objc_mutex_t mutex)
198 {
199   if (DosRequestMutexSem ((HMTX)(mutex->backend),-1L) != 0)
200     return -1;
201   else
202     return 0;
203 }
204
205 /* Try to grab a lock on a mutex. */
206 int
207 __objc_mutex_trylock(objc_mutex_t mutex)
208 {
209   if (DosRequestMutexSem ((HMTX)(mutex->backend),0L) != 0)
210     return -1;
211   else
212     return 0;
213 }
214
215 /* Unlock the mutex */
216 int
217 __objc_mutex_unlock(objc_mutex_t mutex)
218 {
219   if (DosReleaseMutexSem((HMTX)(mutex->backend)) != 0)
220     return -1;
221   else
222     return 0;
223 }
224
225 /* Backend condition mutex functions */
226
227 /* Allocate a condition. */
228 int
229 __objc_condition_allocate(objc_condition_t condition)
230 {
231   /* Unimplemented. */
232   return -1;
233 }
234
235 /* Deallocate a condition. */
236 int
237 __objc_condition_deallocate(objc_condition_t condition)
238 {
239   /* Unimplemented. */
240   return -1;
241 }
242
243 /* Wait on the condition */
244 int
245 __objc_condition_wait(objc_condition_t condition, objc_mutex_t mutex)
246 {
247   /* Unimplemented. */
248   return -1;
249 }
250
251 /* Wake up all threads waiting on this condition. */
252 int
253 __objc_condition_broadcast(objc_condition_t condition)
254 {
255   /* Unimplemented. */
256   return -1;
257 }
258
259 /* Wake up one thread waiting on this condition. */
260 int
261 __objc_condition_signal(objc_condition_t condition)
262 {
263   /* Unimplemented. */
264   return -1;
265 }
266
267 /* End of File */