]> err.no Git - sope/blob - gnustep-objc/thr-backends/solaris.c
bringing gnustep-objc r114 into the main branch
[sope] / gnustep-objc / thr-backends / 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 <stdlib.h>
32 #include <thread.h>
33 #include <synch.h>
34 #include <errno.h>
35
36 #if OBJC_WITH_GC
37 #  include <gc.h>
38 #endif
39 #if OBJC_WITH_SGC
40 #  include <sgc.h>
41 #endif
42
43 /* Key structure for maintaining thread specific storage */
44 static thread_key_t __objc_thread_data_key = 0;
45
46 /* Backend initialization functions */
47
48 /* Initialize the threads subsystem. */
49 int __objc_init_thread_system(void) {
50   /* Initialize the thread storage key */
51
52   printf("Launching Objective-C threading backend: %s ..\n", __FILE__);
53   fflush(stdout);
54   
55   return (thr_keycreate(&__objc_thread_data_key, NULL) == 0) ? 0 : -1;
56 }
57
58 /* Close the threads subsystem. */
59 int __objc_close_thread_system(void) {
60   return 0;
61 }
62
63 /* Backend thread functions */
64
65 /* Create a new thread of execution. */
66 objc_thread_t __objc_thread_detach(void (*func)(void *arg), void *arg) {
67   thread_t new_thread_id = 0;
68
69   if (
70 #if OBJC_WITH_SGC
71       SGC_thr_create
72 #elif OBJC_WITH_GC
73       GC_thr_create
74 #else
75       thr_create
76 #endif
77       (NULL, 0, (void *)func, arg,
78        THR_DETACHED | THR_NEW_LWP,
79        &new_thread_id) == 0) {
80
81     if (sizeof(objc_thread_t) != sizeof(new_thread_id))
82       abort();
83
84     return (objc_thread_t)new_thread_id;
85   }
86   else
87     return NULL;
88 }
89
90 /* Set the current thread's priority. */
91 int __objc_thread_set_priority(int priority) {
92   int sys_priority = 0;
93
94   switch (priority) {
95     case OBJC_THREAD_INTERACTIVE_PRIORITY:
96       sys_priority = 300;
97       break;
98     default:
99     case OBJC_THREAD_BACKGROUND_PRIORITY:
100       sys_priority = 200;
101       break;
102     case OBJC_THREAD_LOW_PRIORITY:
103       sys_priority = 1000;
104       break;
105   }
106
107   /* Change priority */
108   return (thr_setprio(thr_self(), sys_priority) == 0) ? 0 : -1;
109 }
110
111 /* Return the current thread's priority. */
112 int __objc_thread_get_priority(void) {
113   int sys_priority;
114                                                    
115   if (thr_getprio(thr_self(), &sys_priority) == 0){
116     if (sys_priority >= 250)
117       return OBJC_THREAD_INTERACTIVE_PRIORITY;
118     else if (sys_priority >= 150)
119       return OBJC_THREAD_BACKGROUND_PRIORITY;
120     return OBJC_THREAD_LOW_PRIORITY;
121   }
122
123   /* Couldn't get priority. */
124   return -1;
125 }
126
127 /* Yield our process time to another thread. */
128 void __objc_thread_yield(void) {
129   thr_yield();
130 }
131
132 /* Terminate the current thread. */
133 int __objc_thread_exit(void) {
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 __objc_thread_id(void) {
143   if (sizeof(objc_thread_t) != sizeof(thread_t))
144     abort();
145   
146   return (objc_thread_t)thr_self();
147 }
148
149 /* Sets the thread's local storage pointer. */
150 int __objc_thread_set_data(void *value) {
151   return (thr_setspecific(__objc_thread_data_key, value) == 0) ? 0 : -1;
152 }
153
154 /* Returns the thread's local storage pointer. */
155 void *__objc_thread_get_data(void) {
156   void *value = NULL;
157
158   if (thr_getspecific(__objc_thread_data_key, &value) == 0)
159     return value;
160
161   return NULL;
162 }
163
164 /* Backend mutex functions */
165
166 int __objc_mutex_allocate(objc_mutex_t mutex) {
167   mutex->backend = calloc(1, sizeof(mutex_t));
168   if (mutex->backend == NULL) {
169     fprintf(stderr, "%s: could not allocate memory for Solaris mutex !\n"
170             __PRETTY_FUNCTION__);
171     return -1;
172   }
173   
174   if (mutex_init(mutex->backend, USYNC_THREAD, 0) != 0) {
175     free(mutex->backend); mutex->backend = NULL;
176     return -1;
177   }
178   return 0;
179 }
180
181 int __objc_mutex_deallocate(objc_mutex_t mutex) {
182   if (mutex->backend) {
183     mutex_destroy(mutex->backend);
184     free(mutex->backend); mutex->backend = NULL;
185     return 0;
186   }
187   else
188     return -1;
189 }
190
191 int __objc_mutex_lock(objc_mutex_t mutex) {
192   return (mutex_lock(mutex->backend) != 0) ? -1 : 0;
193 }
194
195 int __objc_mutex_trylock(objc_mutex_t mutex) {
196   return (mutex_trylock(mutex->backend) != 0) ? -1 : 0;
197 }
198
199 int __objc_mutex_unlock(objc_mutex_t mutex) {
200   return (mutex_unlock(mutex->backend) != 0) ? -1 : 0;
201 }
202
203 /* Backend condition mutex functions */
204
205 int __objc_condition_allocate(objc_condition_t condition) {
206   condition->backend = calloc(1, sizeof(cond_t));
207   if (condition->backend == NULL) {
208     fprintf(stderr, "%s: could not allocate memory for Solaris mutex !\n"
209             __PRETTY_FUNCTION__);
210     return -1;
211   }
212
213   if (cond_init(condition->backend, USYNC_THREAD, 0) != 0) {
214     free(condition->backend); condition->backend = NULL;
215     return -1;
216   }
217   return 0;
218 }
219
220 int __objc_condition_deallocate(objc_condition_t condition) {
221   if (condition->backend) {
222     cond_destroy(condition->backend);
223     free(condition->backend); condition->backend = NULL;
224     return 0;
225   }
226   else
227     return -1;
228 }
229
230 int __objc_condition_wait(objc_condition_t condition, objc_mutex_t mutex) {
231   return cond_wait(condition->backend, mutex->backend);
232 }
233
234 /* Wake up all threads waiting on this condition. */
235 int __objc_condition_broadcast(objc_condition_t condition) {
236   return cond_broadcast(condition->backend);
237 }
238
239 /* Wake up one thread waiting on this condition. */
240 int __objc_condition_signal(objc_condition_t condition) {
241   return cond_signal(condition->backend);
242 }