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
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/>. */
28 #include "objc/runtime.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. */
41 __objc_init_thread_system(void)
43 /* Initialize the thread storage key */
44 if (thr_keycreate(&__objc_thread_data_key
, NULL
) == 0)
50 /* Close the threads subsystem. */
52 __objc_close_thread_system(void)
57 /* Backend thread functions */
59 /* Create a new thread of execution. */
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
,
69 thread_id
= *(objc_thread_t
*)&new_thread_id
;
76 /* Set the current thread's priority. */
78 __objc_thread_set_priority(int priority
)
84 case OBJC_THREAD_INTERACTIVE_PRIORITY
:
88 case OBJC_THREAD_BACKGROUND_PRIORITY
:
91 case OBJC_THREAD_LOW_PRIORITY
:
97 if (thr_setprio(thr_self(), sys_priority
) == 0)
103 /* Return the current thread's priority. */
105 __objc_thread_get_priority(void)
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. */
122 /* Yield our process time to another thread. */
124 __objc_thread_yield(void)
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 */
140 /* Returns an integer value which uniquely describes a thread. */
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)
157 /* Returns the thread's local storage pointer. */
159 __objc_thread_get_data(void)
163 if (thr_getspecific(__objc_thread_data_key
, &value
) == 0)
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))
182 /* Deallocate a mutex. */
184 __objc_mutex_deallocate(objc_mutex_t mutex
)
186 mutex_destroy((mutex_t
*)(&(mutex
->backend
)));
190 /* Grab a lock on a mutex. */
192 __objc_mutex_lock(objc_mutex_t mutex
)
194 if (mutex_lock((mutex_t
*)(&(mutex
->backend
))) != 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)
210 /* Unlock the mutex */
212 __objc_mutex_unlock(objc_mutex_t mutex
)
214 if (mutex_unlock((mutex_t
*)(&(mutex
->backend
))) != 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
)));