PR c/529
[official-gcc.git] / libobjc / thr-posix.c
blob9871b57691fa8ccb34048cf4cfc3d1bbaba07cdb
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
16 details.
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. */
29 #include "objc/thr.h"
30 #include "objc/runtime.h"
31 #include <pthread.h>
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. */
40 int
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)
54 return 0;
58 return -1;
61 /* Close the threads subsystem. */
62 int
63 __objc_close_thread_system(void)
65 if (pthread_key_delete(_objc_thread_storage) == 0)
67 if (pthread_attr_destroy(&_objc_thread_attribs) == 0)
68 return 0;
71 return -1;
74 /* Backend thread functions */
76 /* Create a new thread of execution. */
77 objc_thread_t
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,
84 (void *)func, arg)))
85 thread_id = *(objc_thread_t *)&new_thread_handle;
86 else
87 thread_id = NULL;
89 return thread_id;
92 /* Set the current thread's priority.
94 * Be aware that the default schedpolicy often disallows thread priorities.
96 int
97 __objc_thread_set_priority(int priority)
99 pthread_t thread_id = pthread_self();
100 int policy;
101 struct sched_param params;
102 int priority_min, priority_max;
104 if (pthread_getschedparam(thread_id, &policy, &params) == 0)
106 if ((priority_max = sched_get_priority_max(policy)) != 0)
107 return -1;
109 if ((priority_min = sched_get_priority_min(policy)) != 0)
110 return -1;
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
121 * at odds with this.
123 if (pthread_setschedparam(thread_id, policy, &params) == 0)
124 return 0;
126 return -1;
129 /* Return the current thread's priority. */
131 __objc_thread_get_priority(void)
133 int policy;
134 struct sched_param params;
136 if (pthread_getschedparam(pthread_self(), &policy, &params) == 0)
137 return params.sched_priority;
138 else
139 return -1;
142 /* Yield our process time to another thread. */
143 void
144 __objc_thread_yield(void)
146 sched_yield();
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 */
157 return -1;
160 /* Returns an integer value which uniquely describes a thread. */
161 objc_thread_t
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)
174 return 0;
175 else
176 return -1;
179 /* Returns the thread's local storage pointer. */
180 void *
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;
198 return -1;
201 return 0;
204 /* Deallocate a mutex. */
206 __objc_mutex_deallocate(objc_mutex_t mutex)
208 int count = 1;
211 * Posix Threads specifically require that the thread be unlocked for
212 * pthread_mutex_destroy to work.
215 while (count)
217 if ((count = pthread_mutex_unlock((pthread_mutex_t*)mutex->backend)) < 0)
218 return -1;
221 if (pthread_mutex_destroy((pthread_mutex_t *)mutex->backend))
222 return -1;
224 objc_free(mutex->backend);
225 mutex->backend = NULL;
226 return 0;
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)
234 return 0;
235 else
236 return -1;
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)
244 return 0;
245 else
246 return -1;
249 /* Unlock the mutex */
251 __objc_mutex_unlock(objc_mutex_t mutex)
253 if (pthread_mutex_unlock((pthread_mutex_t *)mutex->backend) == 0)
254 return 0;
255 else
256 return -1;
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;
271 return -1;
274 return 0;
277 /* Deallocate a condition. */
279 __objc_condition_deallocate(objc_condition_t condition)
281 if (pthread_cond_destroy((pthread_cond_t *)condition->backend))
282 return -1;
284 objc_free(condition->backend);
285 condition->backend = NULL;
286 return 0;
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)
295 return 0;
296 else
297 return -1;
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)
305 return 0;
306 else
307 return -1;
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)
315 return 0;
316 else
317 return -1;