]> gcc.gnu.org Git - gcc.git/blame - gcc/gthr-posix.h
C/C++ io merge/sync.
[gcc.git] / gcc / gthr-posix.h
CommitLineData
15794a95 1/* Threads compatibility routines for libgcc2 and libobjc. */
f24af81b 2/* Compile this one with gcc. */
15794a95 3/* Copyright (C) 1997, 1999, 2000 Free Software Foundation, Inc.
f24af81b
TT
4
5This file is part of GNU CC.
6
7GNU CC is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12GNU CC is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU CC; see the file COPYING. If not, write to
19the Free Software Foundation, 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
21
22/* As a special exception, if you link this library with other files,
23 some of which are compiled with GCC, to produce an executable,
24 this library does not by itself cause the resulting executable
25 to be covered by the GNU General Public License.
26 This exception does not however invalidate any other reasons why
27 the executable file might be covered by the GNU General Public License. */
28
29#ifndef __gthr_posix_h
30#define __gthr_posix_h
31
32/* POSIX threads specific definitions.
33 Easy, since the interface is just one-to-one mapping. */
34
35#define __GTHREADS 1
36
37#include <pthread.h>
38
39typedef pthread_key_t __gthread_key_t;
40typedef pthread_once_t __gthread_once_t;
41typedef pthread_mutex_t __gthread_mutex_t;
42
43#define __GTHREAD_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
44#define __GTHREAD_ONCE_INIT PTHREAD_ONCE_INIT
45
46#if SUPPORTS_WEAK && GTHREAD_USE_WEAK
47
15794a95
L
48#pragma weak pthread_once
49#pragma weak pthread_key_create
50#pragma weak pthread_key_delete
51#pragma weak pthread_getspecific
52#pragma weak pthread_setspecific
53#pragma weak pthread_create
36244024 54
15794a95
L
55#pragma weak pthread_mutex_lock
56#pragma weak pthread_mutex_trylock
57#pragma weak pthread_mutex_unlock
58
59#ifdef _LIBOBJC
60/* Objective C. */
61#pragma weak pthread_cond_broadcast
62#pragma weak pthread_cond_destroy
63#pragma weak pthread_cond_init
64#pragma weak pthread_cond_signal
65#pragma weak pthread_cond_wait
66#pragma weak pthread_exit
67#pragma weak pthread_mutex_init
68#pragma weak pthread_mutex_destroy
69#pragma weak pthread_self
70#pragma weak sched_yield
71#endif
f24af81b
TT
72
73static void *__gthread_active_ptr = &pthread_create;
74
75static inline int
d1e51320 76__gthread_active_p (void)
f24af81b
TT
77{
78 return __gthread_active_ptr != 0;
79}
80
81#else /* not SUPPORTS_WEAK */
82
83static inline int
d1e51320 84__gthread_active_p (void)
f24af81b
TT
85{
86 return 1;
87}
88
89#endif /* SUPPORTS_WEAK */
90
15794a95
L
91#ifdef _LIBOBJC
92
93/* Key structure for maintaining thread specific storage */
94static pthread_key_t _objc_thread_storage;
447c11a5 95static pthread_attr_t _objc_thread_attribs;
15794a95
L
96
97/* Thread local storage for a single thread */
98static void *thread_local_storage = NULL;
99
100/* Backend initialization functions */
101
102/* Initialize the threads subsystem. */
103static inline int
104__gthread_objc_init_thread_system(void)
105{
106 if (__gthread_active_p ())
447c11a5
OP
107 {
108 /* Initialize the thread storage key */
109 if (pthread_key_create(&_objc_thread_storage, NULL) == 0)
110 {
111 /* The normal default detach state for threads is
112 * PTHREAD_CREATE_JOINABLE which causes threads to not die
113 * when you think they should. */
114 if (pthread_attr_init(&_objc_thread_attribs) == 0
115 && pthread_attr_setdetachstate(&_objc_thread_attribs,
116 PTHREAD_CREATE_DETACHED) == 0)
117 return 0;
118 }
119 }
15794a95
L
120 else
121 return -1;
122}
123
124/* Close the threads subsystem. */
125static inline int
126__gthread_objc_close_thread_system(void)
127{
447c11a5
OP
128 if (__gthread_active_p ()
129 && pthread_key_delete(_objc_thread_storage) == 0
130 && pthread_attr_destroy(&_objc_thread_attribs) == 0)
15794a95 131 return 0;
447c11a5
OP
132
133 return -1;
15794a95
L
134}
135
136/* Backend thread functions */
137
138/* Create a new thread of execution. */
139static inline objc_thread_t
140__gthread_objc_thread_detach(void (*func)(void *), void *arg)
141{
142 objc_thread_t thread_id;
143 pthread_t new_thread_handle;
144
145 if (!__gthread_active_p ())
146 return NULL;
147
148 if ( !(pthread_create(&new_thread_handle, NULL, (void *)func, arg)) )
149 thread_id = *(objc_thread_t *)&new_thread_handle;
150 else
151 thread_id = NULL;
152
153 return thread_id;
154}
155
156/* Set the current thread's priority. */
157static inline int
158__gthread_objc_thread_set_priority(int priority)
159{
447c11a5
OP
160 if (!__gthread_active_p())
161 return -1;
162 else {
163 pthread_t thread_id = pthread_self();
164 int policy;
165 struct sched_param params;
166 int priority_min, priority_max;
167
168 if (pthread_getschedparam(thread_id, &policy, &params) == 0)
169 {
170 if ((priority_max = sched_get_priority_max(policy)) != 0)
171 return -1;
172
173 if ((priority_min = sched_get_priority_min(policy)) != 0)
174 return -1;
175
176 if (priority > priority_max)
177 priority = priority_max;
178 else if (priority < priority_min)
179 priority = priority_min;
180 params.sched_priority = priority;
181
182 /*
183 * The solaris 7 and several other man pages incorrectly state that
184 * this should be a pointer to policy but pthread.h is universally
185 * at odds with this.
186 */
187 if (pthread_setschedparam(thread_id, policy, &params) == 0)
188 return 0;
189 }
190 return -1;
191 }
15794a95
L
192}
193
194/* Return the current thread's priority. */
195static inline int
196__gthread_objc_thread_get_priority(void)
197{
198 if (__gthread_active_p ())
447c11a5
OP
199 {
200 int policy;
201 struct sched_param params;
202
203 if (pthread_getschedparam(pthread_self(), &policy, &params) == 0)
204 return params.sched_priority;
205 else
206 return -1;
207 }
15794a95
L
208 else
209 return OBJC_THREAD_INTERACTIVE_PRIORITY;
210}
211
212/* Yield our process time to another thread. */
213static inline void
214__gthread_objc_thread_yield(void)
215{
216 if (__gthread_active_p ())
217 sched_yield();
218}
219
220/* Terminate the current thread. */
221static inline int
222__gthread_objc_thread_exit(void)
223{
224 if (__gthread_active_p ())
225 /* exit the thread */
226 pthread_exit(&__objc_thread_exit_status);
227
228 /* Failed if we reached here */
229 return -1;
230}
231
232/* Returns an integer value which uniquely describes a thread. */
233static inline objc_thread_t
234__gthread_objc_thread_id(void)
235{
236 if (__gthread_active_p ())
237 {
238 pthread_t self = pthread_self();
239
240 return *(objc_thread_t *)&self;
241 }
242 else
243 return (objc_thread_t)1;
244}
245
246/* Sets the thread's local storage pointer. */
247static inline int
248__gthread_objc_thread_set_data(void *value)
249{
250 if (__gthread_active_p ())
251 return pthread_setspecific(_objc_thread_storage, value);
252 else
253 {
254 thread_local_storage = value;
255 return 0;
256 }
257}
258
259/* Returns the thread's local storage pointer. */
260static inline void *
261__gthread_objc_thread_get_data(void)
262{
263 if (__gthread_active_p ())
264 return pthread_getspecific(_objc_thread_storage);
265 else
266 return thread_local_storage;
267}
268
269/* Backend mutex functions */
270
271/* Allocate a mutex. */
272static inline int
273__gthread_objc_mutex_allocate(objc_mutex_t mutex)
274{
275 if (__gthread_active_p ())
276 {
277 mutex->backend = objc_malloc(sizeof(pthread_mutex_t));
278
279 if (pthread_mutex_init((pthread_mutex_t *)mutex->backend, NULL))
280 {
281 objc_free(mutex->backend);
282 mutex->backend = NULL;
283 return -1;
284 }
285 }
286
287 return 0;
288}
289
290/* Deallocate a mutex. */
291static inline int
292__gthread_objc_mutex_deallocate(objc_mutex_t mutex)
293{
294 if (__gthread_active_p ())
295 {
296 int count;
297
298 /*
299 * Posix Threads specifically require that the thread be unlocked
300 * for pthread_mutex_destroy to work.
301 */
302
303 do
304 {
305 count = pthread_mutex_unlock((pthread_mutex_t *)mutex->backend);
306 if (count < 0)
307 return -1;
308 }
309 while (count);
310
311 if (pthread_mutex_destroy((pthread_mutex_t *)mutex->backend))
312 return -1;
313
314 objc_free(mutex->backend);
315 mutex->backend = NULL;
316 }
317 return 0;
318}
319
320/* Grab a lock on a mutex. */
321static inline int
322__gthread_objc_mutex_lock(objc_mutex_t mutex)
323{
324 if (__gthread_active_p ())
325 return pthread_mutex_lock((pthread_mutex_t *)mutex->backend);
326 else
327 return 0;
328}
329
330/* Try to grab a lock on a mutex. */
331static inline int
332__gthread_objc_mutex_trylock(objc_mutex_t mutex)
333{
334 if (__gthread_active_p ())
335 return pthread_mutex_trylock((pthread_mutex_t *)mutex->backend);
336 else
337 return 0;
338}
339
340/* Unlock the mutex */
341static inline int
342__gthread_objc_mutex_unlock(objc_mutex_t mutex)
343{
344 if (__gthread_active_p ())
345 return pthread_mutex_unlock((pthread_mutex_t *)mutex->backend);
346 else
347 return 0;
348}
349
350/* Backend condition mutex functions */
351
352/* Allocate a condition. */
353static inline int
354__gthread_objc_condition_allocate(objc_condition_t condition)
355{
356 if (__gthread_active_p ())
357 {
358 condition->backend = objc_malloc(sizeof(pthread_cond_t));
359
360 if (pthread_cond_init((pthread_cond_t *)condition->backend, NULL))
361 {
362 objc_free(condition->backend);
363 condition->backend = NULL;
364 return -1;
365 }
366 }
367
368 return 0;
369}
370
371/* Deallocate a condition. */
372static inline int
373__gthread_objc_condition_deallocate(objc_condition_t condition)
374{
375 if (__gthread_active_p ())
376 {
377 if (pthread_cond_destroy((pthread_cond_t *)condition->backend))
378 return -1;
379
380 objc_free(condition->backend);
381 condition->backend = NULL;
382 }
383 return 0;
384}
385
386/* Wait on the condition */
387static inline int
388__gthread_objc_condition_wait(objc_condition_t condition, objc_mutex_t mutex)
389{
390 if (__gthread_active_p ())
391 return pthread_cond_wait((pthread_cond_t *)condition->backend,
392 (pthread_mutex_t *)mutex->backend);
393 else
394 return 0;
395}
396
397/* Wake up all threads waiting on this condition. */
398static inline int
399__gthread_objc_condition_broadcast(objc_condition_t condition)
400{
401 if (__gthread_active_p ())
402 return pthread_cond_broadcast((pthread_cond_t *)condition->backend);
403 else
404 return 0;
405}
406
407/* Wake up one thread waiting on this condition. */
408static inline int
409__gthread_objc_condition_signal(objc_condition_t condition)
410{
411 if (__gthread_active_p ())
412 return pthread_cond_signal((pthread_cond_t *)condition->backend);
413 else
414 return 0;
415}
416
417#else /* _LIBOBJC */
418
f24af81b 419static inline int
d1e51320 420__gthread_once (__gthread_once_t *once, void (*func) (void))
f24af81b
TT
421{
422 if (__gthread_active_p ())
423 return pthread_once (once, func);
424 else
425 return -1;
426}
427
428static inline int
429__gthread_key_create (__gthread_key_t *key, void (*dtor) (void *))
430{
431 return pthread_key_create (key, dtor);
432}
433
434static inline int
435__gthread_key_dtor (__gthread_key_t key, void *ptr)
436{
437 /* Just reset the key value to zero. */
438 if (ptr)
439 return pthread_setspecific (key, 0);
440 else
441 return 0;
442}
443
444static inline int
445__gthread_key_delete (__gthread_key_t key)
446{
447 return pthread_key_delete (key);
448}
449
450static inline void *
451__gthread_getspecific (__gthread_key_t key)
452{
453 return pthread_getspecific (key);
454}
455
456static inline int
457__gthread_setspecific (__gthread_key_t key, const void *ptr)
458{
459 return pthread_setspecific (key, ptr);
460}
461
462static inline int
463__gthread_mutex_lock (__gthread_mutex_t *mutex)
464{
465 if (__gthread_active_p ())
466 return pthread_mutex_lock (mutex);
467 else
468 return 0;
469}
470
471static inline int
472__gthread_mutex_trylock (__gthread_mutex_t *mutex)
473{
474 if (__gthread_active_p ())
475 return pthread_mutex_trylock (mutex);
476 else
477 return 0;
478}
479
480static inline int
481__gthread_mutex_unlock (__gthread_mutex_t *mutex)
482{
483 if (__gthread_active_p ())
484 return pthread_mutex_unlock (mutex);
485 else
486 return 0;
487}
488
15794a95
L
489#endif /* _LIBOBJC */
490
f24af81b 491#endif /* not __gthr_posix_h */
This page took 0.658553 seconds and 5 git commands to generate.