- Tie battd into the build
[dragonfly.git] / contrib / libobjc / thr-posix.c
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
6 This file is part of GNU CC.
7
8 GNU CC 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 2, or (at your option) any later version.
11
12 GNU CC 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.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING.  If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
21
22 /* As a special exception, if you link this library with files compiled with
23    GCC to produce an executable, this does not cause the resulting executable
24    to be covered by the GNU General Public License. This exception does not
25    however invalidate any other reasons why the executable file might be
26    covered by the GNU General Public License.  */
27
28 #include <objc/thr.h>
29 #include "runtime.h"
30 #include <pthread.h>
31
32 /* Key structure for maintaining thread specific storage */
33 static pthread_key_t _objc_thread_storage;
34
35 /* Backend initialization functions */
36
37 /* Initialize the threads subsystem. */
38 int
39 __objc_init_thread_system(void)
40 {
41   /* Initialize the thread storage key */
42   return pthread_key_create(&_objc_thread_storage, NULL);
43 }
44
45 /* Close the threads subsystem. */
46 int
47 __objc_close_thread_system(void)
48 {
49   return 0;
50 }
51
52 /* Backend thread functions */
53
54 /* Create a new thread of execution. */
55 objc_thread_t
56 __objc_thread_detach(void (*func)(void *arg), void *arg)
57 {
58   objc_thread_t thread_id;
59   pthread_t new_thread_handle;
60
61   if ( !(pthread_create(&new_thread_handle, NULL, (void *)func, arg)) )
62       thread_id = *(objc_thread_t *)&new_thread_handle;
63   else
64     thread_id = NULL;
65   
66   return thread_id;
67 }
68
69 /* Set the current thread's priority. */
70 int
71 __objc_thread_set_priority(int priority)
72 {
73   /* Not implemented yet */
74   return -1;
75 }
76
77 /* Return the current thread's priority. */
78 int
79 __objc_thread_get_priority(void)
80 {
81   /* Not implemented yet */
82   return -1;
83 }
84
85 /* Yield our process time to another thread. */
86 void
87 __objc_thread_yield(void)
88 {
89   sched_yield();
90 }
91
92 /* Terminate the current thread. */
93 int
94 __objc_thread_exit(void)
95 {
96   /* exit the thread */
97   pthread_exit(&__objc_thread_exit_status);
98
99   /* Failed if we reached here */
100   return -1;
101 }
102
103 /* Returns an integer value which uniquely describes a thread. */
104 objc_thread_t
105 __objc_thread_id(void)
106 {
107   pthread_t self = pthread_self();
108
109   return *(objc_thread_t *)&self;
110 }
111
112 /* Sets the thread's local storage pointer. */
113 int
114 __objc_thread_set_data(void *value)
115 {
116   return pthread_setspecific(_objc_thread_storage, value);
117 }
118
119 /* Returns the thread's local storage pointer. */
120 void *
121 __objc_thread_get_data(void)
122 {
123   return pthread_getspecific(_objc_thread_storage);
124 }
125
126 /* Backend mutex functions */
127
128 /* Allocate a mutex. */
129 int
130 __objc_mutex_allocate(objc_mutex_t mutex)
131 {
132   mutex->backend = objc_malloc(sizeof(pthread_mutex_t));
133
134   if (pthread_mutex_init((pthread_mutex_t *)mutex->backend, NULL))
135     {
136       objc_free(mutex->backend);
137       mutex->backend = NULL;
138       return -1;
139     }
140
141   return 0;
142 }
143
144 /* Deallocate a mutex. */
145 int
146 __objc_mutex_deallocate(objc_mutex_t mutex)
147 {
148   if (pthread_mutex_destroy((pthread_mutex_t *)mutex->backend))
149     return -1;
150
151   objc_free(mutex->backend);
152   mutex->backend = NULL;
153   return 0;
154 }
155
156 /* Grab a lock on a mutex. */
157 int
158 __objc_mutex_lock(objc_mutex_t mutex)
159 {
160   return pthread_mutex_lock((pthread_mutex_t *)mutex->backend);
161 }
162
163 /* Try to grab a lock on a mutex. */
164 int
165 __objc_mutex_trylock(objc_mutex_t mutex)
166 {
167   return pthread_mutex_trylock((pthread_mutex_t *)mutex->backend);
168 }
169
170 /* Unlock the mutex */
171 int
172 __objc_mutex_unlock(objc_mutex_t mutex)
173 {
174   return pthread_mutex_unlock((pthread_mutex_t *)mutex->backend);
175 }
176
177 /* Backend condition mutex functions */
178
179 /* Allocate a condition. */
180 int
181 __objc_condition_allocate(objc_condition_t condition)
182 {
183   condition->backend = objc_malloc(sizeof(pthread_cond_t));
184
185   if (pthread_cond_init((pthread_cond_t *)condition->backend, NULL))
186     {
187       objc_free(condition->backend);
188       condition->backend = NULL;
189       return -1;
190     }
191
192   return 0;
193 }
194
195 /* Deallocate a condition. */
196 int
197 __objc_condition_deallocate(objc_condition_t condition)
198 {
199   if (pthread_cond_destroy((pthread_cond_t *)condition->backend))
200     return -1;
201
202   objc_free(condition->backend);
203   condition->backend = NULL;
204   return 0;
205 }
206
207 /* Wait on the condition */
208 int
209 __objc_condition_wait(objc_condition_t condition, objc_mutex_t mutex)
210 {
211   return pthread_cond_wait((pthread_cond_t *)condition->backend,
212                            (pthread_mutex_t *)mutex->backend);
213 }
214
215 /* Wake up all threads waiting on this condition. */
216 int
217 __objc_condition_broadcast(objc_condition_t condition)
218 {
219   return pthread_cond_broadcast((pthread_cond_t *)condition->backend);
220 }
221
222 /* Wake up one thread waiting on this condition. */
223 int
224 __objc_condition_signal(objc_condition_t condition)
225 {
226   return pthread_cond_signal((pthread_cond_t *)condition->backend);
227 }
228
229 /* End of File */