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)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under the
8 terms of the GNU General Public License as published by the Free Software
9 Foundation; either version 3, or (at your option) any later version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16 Under Section 7 of GPL version 3, you are granted additional
17 permissions described in the GCC Runtime Library Exception, version
18 3.1, as published by the Free Software Foundation.
20 You should have received a copy of the GNU General Public License and
21 a copy of the GCC Runtime Library Exception along with this program;
22 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 <http://www.gnu.org/licenses/>. */
28 #include "objc/runtime.h"
30 /* Key structure for maintaining thread specific storage */
31 static pthread_key_t _objc_thread_storage
;
33 /* Backend initialization functions */
35 /* Initialize the threads subsystem. */
37 __objc_init_thread_system(void)
39 /* Initialize the thread storage key */
40 return pthread_keycreate(&_objc_thread_storage
, NULL
);
43 /* Close the threads subsystem. */
45 __objc_close_thread_system(void)
47 /* Destroy the thread storage key */
48 /* Not implemented yet */
49 /* return pthread_key_delete(&_objc_thread_storage); */
53 /* Backend thread functions */
55 /* Create a new thread of execution. */
57 __objc_thread_detach(void (*func
)(void *arg
), void *arg
)
59 objc_thread_t thread_id
;
60 pthread_t new_thread_handle
;
62 if (pthread_create(&new_thread_handle
, pthread_attr_default
,
63 (void *)func
, arg
) == 0)
65 /* ??? May not work! (64bit) */
66 thread_id
= *(objc_thread_t
*)&new_thread_handle
;
67 pthread_detach(&new_thread_handle
); /* Fully detach thread. */
75 /* Set the current thread's priority. */
77 __objc_thread_set_priority(int priority
)
83 case OBJC_THREAD_INTERACTIVE_PRIORITY
:
84 sys_priority
= (PRI_FG_MIN_NP
+ PRI_FG_MAX_NP
) / 2;
87 case OBJC_THREAD_BACKGROUND_PRIORITY
:
88 sys_priority
= (PRI_BG_MIN_NP
+ PRI_BG_MAX_NP
) / 2;
90 case OBJC_THREAD_LOW_PRIORITY
:
91 sys_priority
= (PRI_BG_MIN_NP
+ PRI_BG_MAX_NP
) / 2;
95 /* Change the priority. */
96 if (pthread_setprio(pthread_self(), sys_priority
) >= 0)
103 /* Return the current thread's priority. */
105 __objc_thread_get_priority(void)
109 if ((sys_priority
= pthread_getprio(pthread_self())) >= 0) {
110 if (sys_priority
>= PRI_FG_MIN_NP
&& sys_priority
<= PRI_FG_MAX_NP
)
111 return OBJC_THREAD_INTERACTIVE_PRIORITY
;
112 if (sys_priority
>= PRI_BG_MIN_NP
&& sys_priority
<= PRI_BG_MAX_NP
)
113 return OBJC_THREAD_BACKGROUND_PRIORITY
;
114 return OBJC_THREAD_LOW_PRIORITY
;
121 /* Yield our process time to another thread. */
123 __objc_thread_yield(void)
128 /* Terminate the current thread. */
130 __objc_thread_exit(void)
132 /* exit the thread */
133 pthread_exit(&__objc_thread_exit_status
);
135 /* Failed if we reached here */
139 /* Returns an integer value which uniquely describes a thread. */
141 __objc_thread_id(void)
143 pthread_t self
= pthread_self();
145 return (objc_thread_t
) pthread_getunique_np (&self
);
148 /* Sets the thread's local storage pointer. */
150 __objc_thread_set_data(void *value
)
152 return pthread_setspecific(_objc_thread_storage
, value
);
155 /* Returns the thread's local storage pointer. */
157 __objc_thread_get_data(void)
161 if ( !(pthread_getspecific(_objc_thread_storage
, &value
)) )
167 /* Backend mutex functions */
169 /* Allocate a mutex. */
171 __objc_mutex_allocate(objc_mutex_t mutex
)
173 if (pthread_mutex_init((pthread_mutex_t
*)(&(mutex
->backend
)),
174 pthread_mutexattr_default
))
180 /* Deallocate a mutex. */
182 __objc_mutex_deallocate(objc_mutex_t mutex
)
184 if (pthread_mutex_destroy((pthread_mutex_t
*)(&(mutex
->backend
))))
190 /* Grab a lock on a mutex. */
192 __objc_mutex_lock(objc_mutex_t mutex
)
194 return pthread_mutex_lock((pthread_mutex_t
*)(&(mutex
->backend
)));
197 /* Try to grab a lock on a mutex. */
199 __objc_mutex_trylock(objc_mutex_t mutex
)
201 if (pthread_mutex_trylock((pthread_mutex_t
*)(&(mutex
->backend
))) != 1)
207 /* Unlock the mutex */
209 __objc_mutex_unlock(objc_mutex_t mutex
)
211 return pthread_mutex_unlock((pthread_mutex_t
*)(&(mutex
->backend
)));
214 /* Backend condition mutex functions */
216 /* Allocate a condition. */
218 __objc_condition_allocate(objc_condition_t condition
)
224 if (pthread_cond_init((pthread_cond_t *)(&(condition->backend)), NULL))
231 /* Deallocate a condition. */
233 __objc_condition_deallocate(objc_condition_t condition
)
239 return pthread_cond_destroy((pthread_cond_t *)(&(condition->backend)));
243 /* Wait on the condition */
245 __objc_condition_wait(objc_condition_t condition
, objc_mutex_t mutex
)
251 return pthread_cond_wait((pthread_cond_t *)(&(condition->backend)),
252 (pthread_mutex_t *)(&(mutex->backend)));
256 /* Wake up all threads waiting on this condition. */
258 __objc_condition_broadcast(objc_condition_t condition
)
264 return pthread_cond_broadcast((pthread_cond_t *)(&(condition->backend)));
268 /* Wake up one thread waiting on this condition. */
270 __objc_condition_signal(objc_condition_t condition
)
276 return pthread_cond_signal((pthread_cond_t *)(&(condition->backend)));