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)
6 This file is part of GNU CC.
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.
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
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. */
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. */
43 /* Key structure for maintaining thread specific storage */
44 static thread_key_t __objc_thread_data_key = 0;
46 /* Backend initialization functions */
48 /* Initialize the threads subsystem. */
49 int __objc_init_thread_system(void) {
50 /* Initialize the thread storage key */
52 printf("Launching Objective-C threading backend: %s ..\n", __FILE__);
55 return (thr_keycreate(&__objc_thread_data_key, NULL) == 0) ? 0 : -1;
58 /* Close the threads subsystem. */
59 int __objc_close_thread_system(void) {
63 /* Backend thread functions */
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;
77 (NULL, 0, (void *)func, arg,
78 THR_DETACHED | THR_NEW_LWP,
79 &new_thread_id) == 0) {
81 if (sizeof(objc_thread_t) != sizeof(new_thread_id))
84 return (objc_thread_t)new_thread_id;
90 /* Set the current thread's priority. */
91 int __objc_thread_set_priority(int priority) {
95 case OBJC_THREAD_INTERACTIVE_PRIORITY:
99 case OBJC_THREAD_BACKGROUND_PRIORITY:
102 case OBJC_THREAD_LOW_PRIORITY:
107 /* Change priority */
108 return (thr_setprio(thr_self(), sys_priority) == 0) ? 0 : -1;
111 /* Return the current thread's priority. */
112 int __objc_thread_get_priority(void) {
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;
123 /* Couldn't get priority. */
127 /* Yield our process time to another thread. */
128 void __objc_thread_yield(void) {
132 /* Terminate the current thread. */
133 int __objc_thread_exit(void) {
134 /* exit the thread */
135 thr_exit(&__objc_thread_exit_status);
137 /* Failed if we reached here */
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))
146 return (objc_thread_t)thr_self();
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;
154 /* Returns the thread's local storage pointer. */
155 void *__objc_thread_get_data(void) {
158 if (thr_getspecific(__objc_thread_data_key, &value) == 0)
164 /* Backend mutex functions */
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__);
174 if (mutex_init(mutex->backend, USYNC_THREAD, 0) != 0) {
175 free(mutex->backend); mutex->backend = NULL;
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;
191 int __objc_mutex_lock(objc_mutex_t mutex) {
192 return (mutex_lock(mutex->backend) != 0) ? -1 : 0;
195 int __objc_mutex_trylock(objc_mutex_t mutex) {
196 return (mutex_trylock(mutex->backend) != 0) ? -1 : 0;
199 int __objc_mutex_unlock(objc_mutex_t mutex) {
200 return (mutex_unlock(mutex->backend) != 0) ? -1 : 0;
203 /* Backend condition mutex functions */
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__);
213 if (cond_init(condition->backend, USYNC_THREAD, 0) != 0) {
214 free(condition->backend); condition->backend = NULL;
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;
230 int __objc_condition_wait(objc_condition_t condition, objc_mutex_t mutex) {
231 return cond_wait(condition->backend, mutex->backend);
234 /* Wake up all threads waiting on this condition. */
235 int __objc_condition_broadcast(objc_condition_t condition) {
236 return cond_broadcast(condition->backend);
239 /* Wake up one thread waiting on this condition. */
240 int __objc_condition_signal(objc_condition_t condition) {
241 return cond_signal(condition->backend);