1 /* GNU Objective C Runtime Thread Interface for POSIX compliant threads
2 Copyright (C) 1996, 1997 Free Software Foundation, Inc.
3 Contributed by Galen C. Hunt (gchunt@cs.rochester.edu)
4 Modified for Linux/Pthreads by Kai-Uwe Sattler (kus@iti.cs.uni-magdeburg.de)
5 Modified for posix compliance by Chris Ball (cball@fmco.com)
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under the
10 terms of the GNU General Public License as published by the Free Software
11 Foundation; either version 2, or (at your option) any later version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
23 /* As a special exception, if you link this library with files compiled with
24 GCC to produce an executable, this does not cause the resulting executable
25 to be covered by the GNU General Public License. This exception does not
26 however invalidate any other reasons why the executable file might be
27 covered by the GNU General Public License. */
33 /* Key structure for maintaining thread specific storage */
34 static pthread_key_t _objc_thread_storage
;
35 static pthread_attr_t _objc_thread_attribs
;
37 /* Backend initialization functions */
39 /* Initialize the threads subsystem. */
41 __objc_init_thread_system(void)
43 /* Initialize the thread storage key */
44 if (pthread_key_create(&_objc_thread_storage
, NULL
) == 0)
47 * The normal default detach state for threads is PTHREAD_CREATE_JOINABLE
48 * which causes threads to not die when you think they should.
50 if (pthread_attr_init(&_objc_thread_attribs
) == 0)
52 if (pthread_attr_setdetachstate(&_objc_thread_attribs
,
53 PTHREAD_CREATE_DETACHED
) == 0)
61 /* Close the threads subsystem. */
63 __objc_close_thread_system(void)
65 if (pthread_key_delete(_objc_thread_storage
) == 0)
67 if (pthread_attr_destroy(&_objc_thread_attribs
) == 0)
74 /* Backend thread functions */
76 /* Create a new thread of execution. */
78 __objc_thread_detach(void (*func
)(void *arg
), void *arg
)
80 objc_thread_t thread_id
;
81 pthread_t new_thread_handle
;
83 if (!(pthread_create(&new_thread_handle
, &_objc_thread_attribs
,
85 thread_id
= *(objc_thread_t
*)&new_thread_handle
;
92 /* Set the current thread's priority.
94 * Be aware that the default schedpolicy often disallows thread priorities.
97 __objc_thread_set_priority(int priority
)
99 pthread_t thread_id
= pthread_self();
101 struct sched_param params
;
102 int priority_min
, priority_max
;
104 if (pthread_getschedparam(thread_id
, &policy
, ¶ms
) == 0)
106 if ((priority_max
= sched_get_priority_max(policy
)) != 0)
109 if ((priority_min
= sched_get_priority_min(policy
)) != 0)
112 if (priority
> priority_max
)
113 priority
= priority_max
;
114 else if (priority
< priority_min
)
115 priority
= priority_min
;
116 params
.sched_priority
= priority
;
119 * The solaris 7 and several other man pages incorrectly state that
120 * this should be a pointer to policy but pthread.h is universally
123 if (pthread_setschedparam(thread_id
, policy
, ¶ms
) == 0)
129 /* Return the current thread's priority. */
131 __objc_thread_get_priority(void)
134 struct sched_param params
;
136 if (pthread_getschedparam(pthread_self(), &policy
, ¶ms
) == 0)
137 return params
.sched_priority
;
142 /* Yield our process time to another thread. */
144 __objc_thread_yield(void)
149 /* Terminate the current thread. */
151 __objc_thread_exit(void)
153 /* exit the thread */
154 pthread_exit(&__objc_thread_exit_status
);
156 /* Failed if we reached here */
160 /* Returns an integer value which uniquely describes a thread. */
162 __objc_thread_id(void)
164 pthread_t self
= pthread_self();
166 return *(objc_thread_t
*)&self
;
169 /* Sets the thread's local storage pointer. */
171 __objc_thread_set_data(void *value
)
173 if (pthread_setspecific(_objc_thread_storage
, value
) == 0)
179 /* Returns the thread's local storage pointer. */
181 __objc_thread_get_data(void)
183 return pthread_getspecific(_objc_thread_storage
);
186 /* Backend mutex functions */
188 /* Allocate a mutex. */
190 __objc_mutex_allocate(objc_mutex_t mutex
)
192 mutex
->backend
= objc_malloc(sizeof(pthread_mutex_t
));
194 if (pthread_mutex_init((pthread_mutex_t
*)mutex
->backend
, NULL
))
196 objc_free(mutex
->backend
);
197 mutex
->backend
= NULL
;
204 /* Deallocate a mutex. */
206 __objc_mutex_deallocate(objc_mutex_t mutex
)
211 * Posix Threads specifically require that the thread be unlocked for
212 * pthread_mutex_destroy to work.
217 if ((count
= pthread_mutex_unlock((pthread_mutex_t
*)mutex
->backend
)) < 0)
221 if (pthread_mutex_destroy((pthread_mutex_t
*)mutex
->backend
))
224 objc_free(mutex
->backend
);
225 mutex
->backend
= NULL
;
229 /* Grab a lock on a mutex. */
231 __objc_mutex_lock(objc_mutex_t mutex
)
233 if (pthread_mutex_lock((pthread_mutex_t
*)mutex
->backend
) == 0)
239 /* Try to grab a lock on a mutex. */
241 __objc_mutex_trylock(objc_mutex_t mutex
)
243 if (pthread_mutex_trylock((pthread_mutex_t
*)mutex
->backend
) == 0)
249 /* Unlock the mutex */
251 __objc_mutex_unlock(objc_mutex_t mutex
)
253 if (pthread_mutex_unlock((pthread_mutex_t
*)mutex
->backend
) == 0)
259 /* Backend condition mutex functions */
261 /* Allocate a condition. */
263 __objc_condition_allocate(objc_condition_t condition
)
265 condition
->backend
= objc_malloc(sizeof(pthread_cond_t
));
267 if (pthread_cond_init((pthread_cond_t
*)condition
->backend
, NULL
))
269 objc_free(condition
->backend
);
270 condition
->backend
= NULL
;
277 /* Deallocate a condition. */
279 __objc_condition_deallocate(objc_condition_t condition
)
281 if (pthread_cond_destroy((pthread_cond_t
*)condition
->backend
))
284 objc_free(condition
->backend
);
285 condition
->backend
= NULL
;
289 /* Wait on the condition */
291 __objc_condition_wait(objc_condition_t condition
, objc_mutex_t mutex
)
293 if (pthread_cond_wait((pthread_cond_t
*)condition
->backend
,
294 (pthread_mutex_t
*)mutex
->backend
) == 0)
300 /* Wake up all threads waiting on this condition. */
302 __objc_condition_broadcast(objc_condition_t condition
)
304 if (pthread_cond_broadcast((pthread_cond_t
*)condition
->backend
) == 0)
310 /* Wake up one thread waiting on this condition. */
312 __objc_condition_signal(objc_condition_t condition
)
314 if (pthread_cond_signal((pthread_cond_t
*)condition
->backend
) == 0)