2009-07-05 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / libobjc / thr-dce.c
blobad31c8b1f0901fd8df5f82c212aa23f9fa07963a
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
14 details.
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/>. */
26 #include <pthread.h>
27 #include "objc/thr.h"
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. */
36 int
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. */
44 int
45 __objc_close_thread_system(void)
47 /* Destroy the thread storage key */
48 /* Not implemented yet */
49 /* return pthread_key_delete(&_objc_thread_storage); */
50 return 0;
53 /* Backend thread functions */
55 /* Create a new thread of execution. */
56 objc_thread_t
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. */
69 else
70 thread_id = NULL;
72 return thread_id;
75 /* Set the current thread's priority. */
76 int
77 __objc_thread_set_priority(int priority)
79 int sys_priority = 0;
81 switch (priority)
83 case OBJC_THREAD_INTERACTIVE_PRIORITY:
84 sys_priority = (PRI_FG_MIN_NP + PRI_FG_MAX_NP) / 2;
85 break;
86 default:
87 case OBJC_THREAD_BACKGROUND_PRIORITY:
88 sys_priority = (PRI_BG_MIN_NP + PRI_BG_MAX_NP) / 2;
89 break;
90 case OBJC_THREAD_LOW_PRIORITY:
91 sys_priority = (PRI_BG_MIN_NP + PRI_BG_MAX_NP) / 2;
92 break;
95 /* Change the priority. */
96 if (pthread_setprio(pthread_self(), sys_priority) >= 0)
97 return 0;
98 else
99 /* Failed */
100 return -1;
103 /* Return the current thread's priority. */
105 __objc_thread_get_priority(void)
107 int sys_priority;
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;
117 /* Failed */
118 return -1;
121 /* Yield our process time to another thread. */
122 void
123 __objc_thread_yield(void)
125 pthread_yield();
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 */
136 return -1;
139 /* Returns an integer value which uniquely describes a thread. */
140 objc_thread_t
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. */
156 void *
157 __objc_thread_get_data(void)
159 void *value = NULL;
161 if ( !(pthread_getspecific(_objc_thread_storage, &value)) )
162 return value;
164 return NULL;
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))
175 return -1;
176 else
177 return 0;
180 /* Deallocate a mutex. */
182 __objc_mutex_deallocate(objc_mutex_t mutex)
184 if (pthread_mutex_destroy((pthread_mutex_t *)(&(mutex->backend))))
185 return -1;
186 else
187 return 0;
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)
202 return -1;
203 else
204 return 0;
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)
220 /* Unimplemented. */
221 return -1;
224 if (pthread_cond_init((pthread_cond_t *)(&(condition->backend)), NULL))
225 return -1;
226 else
227 return 0;
231 /* Deallocate a condition. */
233 __objc_condition_deallocate(objc_condition_t condition)
235 /* Unimplemented. */
236 return -1;
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)
247 /* Unimplemented. */
248 return -1;
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)
260 /* Unimplemented. */
261 return -1;
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)
272 /* Unimplemented. */
273 return -1;
276 return pthread_cond_signal((pthread_cond_t *)(&(condition->backend)));
280 /* End of File */