2010-01-15 Jing Yu <jingyu@google.com>
[official-gcc.git] / libobjc / thr-solaris.c
bloba08dabe7e343bfbc83ed332dae4d0d1012893bf4
1 /* GNU Objective C Runtime Thread Interface
2 Copyright (C) 1996, 1997, 2009 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 GCC.
8 GCC 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 3, or (at your option) any later version.
12 GCC 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.
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
27 #include "objc/thr.h"
28 #include "objc/runtime.h"
30 #include <thread.h>
31 #include <synch.h>
32 #include <errno.h>
34 /* Key structure for maintaining thread specific storage */
35 static thread_key_t __objc_thread_data_key;
37 /* Backend initialization functions */
39 /* Initialize the threads subsystem. */
40 int
41 __objc_init_thread_system(void)
43 /* Initialize the thread storage key */
44 if (thr_keycreate(&__objc_thread_data_key, NULL) == 0)
45 return 0;
46 else
47 return -1;
50 /* Close the threads subsystem. */
51 int
52 __objc_close_thread_system(void)
54 return 0;
57 /* Backend thread functions */
59 /* Create a new thread of execution. */
60 objc_thread_t
61 __objc_thread_detach(void (*func)(void *arg), void *arg)
63 objc_thread_t thread_id;
64 thread_t new_thread_id = 0;
66 if (thr_create(NULL, 0, (void *)func, arg,
67 THR_DETACHED | THR_NEW_LWP,
68 &new_thread_id) == 0)
69 thread_id = *(objc_thread_t *)&new_thread_id;
70 else
71 thread_id = NULL;
73 return thread_id;
76 /* Set the current thread's priority. */
77 int
78 __objc_thread_set_priority(int priority)
80 int sys_priority = 0;
82 switch (priority)
84 case OBJC_THREAD_INTERACTIVE_PRIORITY:
85 sys_priority = 300;
86 break;
87 default:
88 case OBJC_THREAD_BACKGROUND_PRIORITY:
89 sys_priority = 200;
90 break;
91 case OBJC_THREAD_LOW_PRIORITY:
92 sys_priority = 1000;
93 break;
96 /* Change priority */
97 if (thr_setprio(thr_self(), sys_priority) == 0)
98 return 0;
99 else
100 return -1;
103 /* Return the current thread's priority. */
105 __objc_thread_get_priority(void)
107 int sys_priority;
109 if (thr_getprio(thr_self(), &sys_priority) == 0)
111 if (sys_priority >= 250)
112 return OBJC_THREAD_INTERACTIVE_PRIORITY;
113 else if (sys_priority >= 150)
114 return OBJC_THREAD_BACKGROUND_PRIORITY;
115 return OBJC_THREAD_LOW_PRIORITY;
118 /* Couldn't get priority. */
119 return -1;
122 /* Yield our process time to another thread. */
123 void
124 __objc_thread_yield(void)
126 thr_yield();
129 /* Terminate the current thread. */
131 __objc_thread_exit(void)
133 /* exit the thread */
134 thr_exit(&__objc_thread_exit_status);
136 /* Failed if we reached here */
137 return -1;
140 /* Returns an integer value which uniquely describes a thread. */
141 objc_thread_t
142 __objc_thread_id(void)
144 return (objc_thread_t)thr_self();
147 /* Sets the thread's local storage pointer. */
149 __objc_thread_set_data(void *value)
151 if (thr_setspecific(__objc_thread_data_key, value) == 0)
152 return 0;
153 else
154 return -1;
157 /* Returns the thread's local storage pointer. */
158 void *
159 __objc_thread_get_data(void)
161 void *value = NULL;
163 if (thr_getspecific(__objc_thread_data_key, &value) == 0)
164 return value;
166 return NULL;
169 /* Backend mutex functions */
171 /* Allocate a mutex. */
173 __objc_mutex_allocate(objc_mutex_t mutex)
175 if (mutex_init( (mutex_t *)(&(mutex->backend)), USYNC_THREAD, 0))
176 return -1;
177 else
178 return 0;
182 /* Deallocate a mutex. */
184 __objc_mutex_deallocate(objc_mutex_t mutex)
186 mutex_destroy((mutex_t *)(&(mutex->backend)));
187 return 0;
190 /* Grab a lock on a mutex. */
192 __objc_mutex_lock(objc_mutex_t mutex)
194 if (mutex_lock((mutex_t *)(&(mutex->backend))) != 0)
195 return -1;
196 else
197 return 0;
200 /* Try to grab a lock on a mutex. */
202 __objc_mutex_trylock(objc_mutex_t mutex)
204 if (mutex_trylock((mutex_t *)(&(mutex->backend))) != 0)
205 return -1;
206 else
207 return 0;
210 /* Unlock the mutex */
212 __objc_mutex_unlock(objc_mutex_t mutex)
214 if (mutex_unlock((mutex_t *)(&(mutex->backend))) != 0)
215 return -1;
216 else
217 return 0;
220 /* Backend condition mutex functions */
222 /* Allocate a condition. */
224 __objc_condition_allocate(objc_condition_t condition)
226 return cond_init((cond_t *)(&(condition->backend)), USYNC_THREAD, NULL);
229 /* Deallocate a condition. */
231 __objc_condition_deallocate(objc_condition_t condition)
233 return cond_destroy((cond_t *)(&(condition->backend)));
236 /* Wait on the condition */
238 __objc_condition_wait(objc_condition_t condition, objc_mutex_t mutex)
240 return cond_wait((cond_t *)(&(condition->backend)),
241 (mutex_t *)(&(mutex->backend)));
244 /* Wake up all threads waiting on this condition. */
246 __objc_condition_broadcast(objc_condition_t condition)
248 return cond_broadcast((cond_t *)(&(condition->backend)));
251 /* Wake up one thread waiting on this condition. */
253 __objc_condition_signal(objc_condition_t condition)
255 return cond_signal((cond_t *)(&(condition->backend)));
258 /* End of File */