Scrap DEC Alpha support.
[dragonfly.git] / contrib / libobjc / thr-solaris.c
1 /* GNU Objective C Runtime Thread Interface
2    Copyright (C) 1996, 1997 Free Software Foundation, Inc.
3    Contributed by Galen C. Hunt (gchunt@cs.rochester.edu)
4    Conditions added by Mircea Oancea (mircea@first.elcom.pub.ro)
5       
6 This file is part of GNU CC.
7
8 GNU CC is free software; you can redistribute it and/or modify it under the
9 terms of the GNU General Public License as published by the Free Software
10 Foundation; either version 2, or (at your option) any later version.
11
12 GNU CC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
15 details.
16
17 You should have received a copy of the GNU General Public License along with
18 GNU CC; see the file COPYING.  If not, write to the Free Software
19 Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
21
22 /* As a special exception, if you link this library with files compiled with
23    GCC to produce an executable, this does not cause the resulting executable
24    to be covered by the GNU General Public License. This exception does not
25    however invalidate any other reasons why the executable file might be
26    covered by the GNU General Public License.  */
27
28 #include <objc/thr.h>
29 #include "runtime.h"
30
31 #include <thread.h>
32 #include <synch.h>
33 #include <errno.h>
34
35 /* Key structure for maintaining thread specific storage */
36 static thread_key_t     __objc_thread_data_key;
37
38 /* Backend initialization functions */
39
40 /* Initialize the threads subsystem. */
41 int
42 __objc_init_thread_system(void)
43 {
44   /* Initialize the thread storage key */
45   if (thr_keycreate(&__objc_thread_data_key, NULL) == 0)
46     return 0;
47   else
48     return -1;
49 }
50
51 /* Close the threads subsystem. */
52 int
53 __objc_close_thread_system(void)
54 {
55   return 0;
56 }
57
58 /* Backend thread functions */
59
60 /* Create a new thread of execution. */
61 objc_thread_t
62 __objc_thread_detach(void (*func)(void *arg), void *arg)
63 {
64   objc_thread_t thread_id;
65   thread_t new_thread_id = 0;
66
67   if (thr_create(NULL, 0, (void *)func, arg,
68                  THR_DETACHED | THR_NEW_LWP,
69                  &new_thread_id) == 0)
70     thread_id = *(objc_thread_t *)&new_thread_id;
71   else
72     thread_id = NULL;
73   
74   return thread_id;
75 }
76
77 /* Set the current thread's priority. */
78 int
79 __objc_thread_set_priority(int priority)
80 {
81   int sys_priority = 0;
82
83   switch (priority)
84     {
85     case OBJC_THREAD_INTERACTIVE_PRIORITY:
86       sys_priority = 300;
87       break;
88     default:
89     case OBJC_THREAD_BACKGROUND_PRIORITY:
90       sys_priority = 200;
91       break;
92     case OBJC_THREAD_LOW_PRIORITY:
93       sys_priority = 1000;
94       break;
95     }
96
97   /* Change priority */
98   if (thr_setprio(thr_self(), sys_priority) == 0)
99     return 0;
100   else
101     return -1;
102 }
103
104 /* Return the current thread's priority. */
105 int
106 __objc_thread_get_priority(void)
107 {
108   int sys_priority;
109                                                    
110   if (thr_getprio(thr_self(), &sys_priority) == 0)
111     {
112       if (sys_priority >= 250)
113         return OBJC_THREAD_INTERACTIVE_PRIORITY;
114       else if (sys_priority >= 150)
115         return OBJC_THREAD_BACKGROUND_PRIORITY;
116       return OBJC_THREAD_LOW_PRIORITY;
117     }
118
119   /* Couldn't get priority. */
120   return -1;
121 }
122
123 /* Yield our process time to another thread. */
124 void
125 __objc_thread_yield(void)
126 {
127   thr_yield();
128 }
129
130 /* Terminate the current thread. */
131 int
132 __objc_thread_exit(void)
133 {
134   /* exit the thread */
135   thr_exit(&__objc_thread_exit_status);
136
137   /* Failed if we reached here */
138   return -1;
139 }
140
141 /* Returns an integer value which uniquely describes a thread. */
142 objc_thread_t
143 __objc_thread_id(void)
144 {
145   return (objc_thread_t)thr_self();
146 }
147
148 /* Sets the thread's local storage pointer. */
149 int
150 __objc_thread_set_data(void *value)
151 {
152   if (thr_setspecific(__objc_thread_data_key, value) == 0)
153     return 0;
154   else
155     return -1;
156 }
157
158 /* Returns the thread's local storage pointer. */
159 void *
160 __objc_thread_get_data(void)
161 {
162   void *value = NULL;
163
164   if (thr_getspecific(__objc_thread_data_key, &value) == 0)
165     return value;
166
167   return NULL;
168 }
169
170 /* Backend mutex functions */
171
172 /* Allocate a mutex. */
173 int
174 __objc_mutex_allocate(objc_mutex_t mutex)
175 {
176   if (mutex_init( (mutex_t *)(&(mutex->backend)), USYNC_THREAD, 0))
177     return -1;
178   else
179     return 0;
180 }
181
182
183 /* Deallocate a mutex. */
184 int
185 __objc_mutex_deallocate(objc_mutex_t mutex)
186 {
187   mutex_destroy((mutex_t *)(&(mutex->backend)));
188   return 0;
189 }
190
191 /* Grab a lock on a mutex. */
192 int
193 __objc_mutex_lock(objc_mutex_t mutex)
194 {
195   if (mutex_lock((mutex_t *)(&(mutex->backend))) != 0)
196     return -1;
197   else
198     return 0;
199 }
200
201 /* Try to grab a lock on a mutex. */
202 int
203 __objc_mutex_trylock(objc_mutex_t mutex)
204 {
205   if (mutex_trylock((mutex_t *)(&(mutex->backend))) != 0)
206     return -1;
207   else
208     return 0;
209 }
210
211 /* Unlock the mutex */
212 int
213 __objc_mutex_unlock(objc_mutex_t mutex)
214 {
215   if (mutex_unlock((mutex_t *)(&(mutex->backend))) != 0)
216     return -1;
217   else
218     return 0;
219 }
220
221 /* Backend condition mutex functions */
222
223 /* Allocate a condition. */
224 int
225 __objc_condition_allocate(objc_condition_t condition)
226 {
227   return cond_init((cond_t *)(&(condition->backend)), USYNC_THREAD, NULL);
228 }
229
230 /* Deallocate a condition. */
231 int
232 __objc_condition_deallocate(objc_condition_t condition)
233 {
234   return cond_destroy((cond_t *)(&(condition->backend)));
235 }
236
237 /* Wait on the condition */
238 int
239 __objc_condition_wait(objc_condition_t condition, objc_mutex_t mutex)
240 {
241   return cond_wait((cond_t *)(&(condition->backend)),
242                    (mutex_t *)(&(mutex->backend)));
243 }
244
245 /* Wake up all threads waiting on this condition. */
246 int
247 __objc_condition_broadcast(objc_condition_t condition)
248 {
249   return cond_broadcast((cond_t *)(&(condition->backend)));
250 }
251
252 /* Wake up one thread waiting on this condition. */
253 int
254 __objc_condition_signal(objc_condition_t condition)
255 {
256   return cond_signal((cond_t *)(&(condition->backend)));
257 }
258
259 /* End of File */