Merged with trunk at revision 162480.
[official-gcc.git] / libobjc / thr-pthreads.c
blobd0a46f864cff8da6b9d9b4c535a3b728db959366
1 /* GNU Objective C Runtime Thread Implementation for PCThreads under GNU/Linux.
2 Copyright (C) 1996, 1997, 2009 Free Software Foundation, Inc.
3 Contributed by Scott Christley <scottc@net-community.com>
4 Condition functions 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
15 details.
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/>. */
27 #include <pcthread.h>
28 #include "objc/thr.h"
29 #include "objc/runtime.h"
31 /* Key structure for maintaining thread specific storage */
32 static pthread_key_t _objc_thread_storage;
34 /* Backend initialization functions */
36 /* Initialize the threads subsystem. */
37 int
38 __objc_init_thread_system(void)
40 /* Initialize the thread storage key */
41 return pthread_key_create(&_objc_thread_storage, NULL);
44 /* Close the threads subsystem. */
45 int
46 __objc_close_thread_system(void)
48 /* Destroy the thread storage key */
49 /* Not implemented yet */
50 /* return pthread_key_delete(&_objc_thread_storage); */
51 return 0;
54 /* Backend thread functions */
56 /* Create a new thread of execution. */
57 objc_thread_t
58 __objc_thread_detach(void (*func)(void *arg), void *arg)
60 objc_thread_t thread_id;
61 pthread_t new_thread_handle;
63 if ( !(pthread_create(&new_thread_handle, NULL, (void *)func, arg)) )
64 thread_id = *(objc_thread_t *)&new_thread_handle;
65 else
66 thread_id = NULL;
68 return thread_id;
71 /* Set the current thread's priority. */
72 int
73 __objc_thread_set_priority(int priority)
75 /* Not implemented yet */
76 return -1;
79 /* Return the current thread's priority. */
80 int
81 __objc_thread_get_priority(void)
83 /* Not implemented yet */
84 return OBJC_THREAD_INTERACTIVE_PRIORITY;
87 /* Yield our process time to another thread. */
88 void
89 __objc_thread_yield(void)
91 pthread_yield(NULL);
94 /* Terminate the current thread. */
95 int
96 __objc_thread_exit(void)
98 /* exit the thread */
99 pthread_exit(&__objc_thread_exit_status);
101 /* Failed if we reached here */
102 return -1;
105 /* Returns an integer value which uniquely describes a thread. */
106 objc_thread_t
107 __objc_thread_id(void)
109 pthread_t self = pthread_self();
111 return *(objc_thread_t *)&self;
114 /* Sets the thread's local storage pointer. */
116 __objc_thread_set_data(void *value)
118 return pthread_setspecific(_objc_thread_storage, value);
121 /* Returns the thread's local storage pointer. */
122 void *
123 __objc_thread_get_data(void)
125 void *value = NULL;
127 if ( !(pthread_getspecific(_objc_thread_storage, &value)) )
128 return value;
130 return NULL;
133 /* Backend mutex functions */
135 /* Allocate a mutex. */
137 __objc_mutex_allocate(objc_mutex_t mutex)
139 if (pthread_mutex_init((pthread_mutex_t *)(&(mutex->backend)), NULL))
140 return -1;
141 else
142 return 0;
145 /* Deallocate a mutex. */
147 __objc_mutex_deallocate(objc_mutex_t mutex)
149 if (pthread_mutex_destroy((pthread_mutex_t *)(&(mutex->backend))))
150 return -1;
151 else
152 return 0;
155 /* Grab a lock on a mutex. */
157 __objc_mutex_lock(objc_mutex_t mutex)
159 return pthread_mutex_lock((pthread_mutex_t *)(&(mutex->backend)));
162 /* Try to grab a lock on a mutex. */
164 __objc_mutex_trylock(objc_mutex_t mutex)
166 return pthread_mutex_trylock((pthread_mutex_t *)(&(mutex->backend)));
169 /* Unlock the mutex */
171 __objc_mutex_unlock(objc_mutex_t mutex)
173 return pthread_mutex_unlock((pthread_mutex_t *)(&(mutex->backend)));
176 /* Backend condition mutex functions */
178 /* Allocate a condition. */
180 __objc_condition_allocate(objc_condition_t condition)
182 if (pthread_cond_init((pthread_cond_t *)(&(condition->backend)), NULL))
183 return -1;
184 else
185 return 0;
188 /* Deallocate a condition. */
190 __objc_condition_deallocate(objc_condition_t condition)
192 return pthread_cond_destroy((pthread_cond_t *)(&(condition->backend)));
195 /* Wait on the condition */
197 __objc_condition_wait(objc_condition_t condition, objc_mutex_t mutex)
199 return pthread_cond_wait((pthread_cond_t *)(&(condition->backend)),
200 (pthread_mutex_t *)(&(mutex->backend)));
203 /* Wake up all threads waiting on this condition. */
205 __objc_condition_broadcast(objc_condition_t condition)
207 return pthread_cond_broadcast((pthread_cond_t *)(&(condition->backend)));
210 /* Wake up one thread waiting on this condition. */
212 __objc_condition_signal(objc_condition_t condition)
214 return pthread_cond_signal((pthread_cond_t *)(&(condition->backend)));
217 /* End of File */