1
/* GNU Objective C Runtime Thread Implementation for PCThreads under Darwin.
2
Copyright (C) 1996, 1997 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 GNU CC.
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
11
version 2, or (at your option) any later version.
13
GNU CC 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
18
You should have received a copy of the GNU General Public License
19
along with GNU CC; see the file COPYING. If not, write to
20
the Free Software Foundation, 51 Franklin Street, Fifth Floor,
21
Boston, MA 02110-1301, 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. */
31
#include "GNUstepBase/objc-gnu2next.h"
34
/* Key structure for maintaining thread specific storage */
35
static pthread_key_t _objc_thread_storage;
36
static pthread_attr_t _objc_thread_attribs;
38
/* Global exit status. */
39
int __objc_thread_exit_status = 0;
41
/* Number of threads alive */
42
int __objc_runtime_threads_alive = 0;
44
/* Thread create/exit mutex */
45
struct objc_mutex* __objc_runtime_mutex = NULL;
47
/* Flag which lets us know if we ever became multi threaded */
48
int __objc_is_multi_threaded = 0;
50
/* The hook function called when the runtime becomes multi threaded */
51
objc_thread_callback _objc_became_multi_threaded = NULL;
54
Use this to set the hook function that will be called when the
55
runtime initially becomes multi threaded.
56
The hook function is only called once, meaning only when the
57
2nd thread is spawned, not for each and every thread.
59
It returns the previous hook function or NULL if there is none.
61
A program outside of the runtime could set this to some function so
62
it can be informed; for example, the GNUstep Base Library sets it
63
so it can implement the NSBecomingMultiThreaded notification.
65
objc_thread_callback objc_set_thread_callback(objc_thread_callback func)
67
objc_thread_callback temp = _objc_became_multi_threaded;
68
_objc_became_multi_threaded = func;
75
These functions are utilized by the frontend, but they are not
76
considered part of the public interface.
80
First function called in a thread, starts everything else.
82
This function is passed to the backend by objc_thread_detach
83
as the starting function for a new thread.
85
struct __objc_thread_start_state
93
__objc_thread_detach(void (*func)(void *arg), void *arg);
96
__objc_thread_detach_function(struct __objc_thread_start_state *istate)
100
id (*imp)(id,SEL,id);
101
SEL selector = istate->selector;
102
id object = istate->object;
103
id argument = istate->argument;
105
/* Don't need anymore so free it */
108
/* Clear out the thread local storage */
109
objc_thread_set_data(NULL);
111
/* Check to see if we just became multi threaded */
112
if (!__objc_is_multi_threaded)
114
__objc_is_multi_threaded = 1;
116
/* Call the hook function */
117
if (_objc_became_multi_threaded != NULL)
118
(*_objc_became_multi_threaded)();
121
/* Call the method */
122
if ((imp = (id(*)(id, SEL, id))objc_msg_lookup(object, selector)))
123
(*imp)(object, selector, argument);
125
objc_error(object, OBJC_ERR_UNIMPLEMENTED,
126
"objc_thread_detach called with bad selector.\n");
129
objc_error(nil, OBJC_ERR_BAD_STATE,
130
"objc_thread_detach called with NULL state.\n");
132
/* Exit the thread */
139
These functions constitute the public interface to the Objective-C thread
140
and mutex functionality.
143
/* Frontend thread functions */
146
Detach a new thread of execution and return its id. Returns NULL if fails.
147
Thread is started by sending message with selector to object. Message
148
takes a single argument.
151
objc_thread_detach(SEL selector, id object, id argument)
153
struct __objc_thread_start_state *istate;
154
objc_thread_t thread_id = NULL;
156
/* Allocate the state structure */
157
if (!(istate = (struct __objc_thread_start_state *)
158
objc_malloc(sizeof(*istate))))
161
/* Initialize the state structure */
162
istate->selector = selector;
163
istate->object = object;
164
istate->argument = argument;
167
objc_mutex_lock(__objc_runtime_mutex);
169
/* Call the backend to spawn the thread */
170
if ((thread_id = __objc_thread_detach((void *)__objc_thread_detach_function,
174
objc_mutex_unlock(__objc_runtime_mutex);
179
/* Increment our thread counter */
180
__objc_runtime_threads_alive++;
181
objc_mutex_unlock(__objc_runtime_mutex);
186
/* Backend initialization functions */
188
/* Initialize the threads subsystem. */
190
__objc_init_thread_system(void)
192
/* Initialize the thread storage key */
193
if (pthread_key_create(&_objc_thread_storage, NULL) == 0)
196
* The normal default detach state for threads is PTHREAD_CREATE_JOINABLE
197
* which causes threads to not die when you think they should.
199
if (pthread_attr_init(&_objc_thread_attribs) == 0)
201
if (pthread_attr_setdetachstate(&_objc_thread_attribs,
202
PTHREAD_CREATE_DETACHED) == 0)
210
/* Close the threads subsystem. */
212
__objc_close_thread_system(void)
214
/* Destroy the thread storage key */
215
if (pthread_key_delete(_objc_thread_storage) == 0)
217
if (pthread_attr_destroy(&_objc_thread_attribs) == 0)
224
/* Backend thread functions */
226
/* Create a new thread of execution. */
228
__objc_thread_detach(void (*func)(void *arg), void *arg)
230
objc_thread_t thread_id;
231
pthread_t new_thread_handle;
233
if (!(pthread_create(&new_thread_handle, &_objc_thread_attribs,
235
thread_id = *(objc_thread_t *)&new_thread_handle;
242
/* Set the current thread's priority. */
244
objc_thread_set_priority(int priority)
246
/* Not implemented yet */
250
/* Return the current thread's priority. */
252
objc_thread_get_priority(void)
254
/* Not implemented yet */
255
return OBJC_THREAD_INTERACTIVE_PRIORITY;
258
/* Yield our process time to another thread. */
260
objc_thread_yield(void)
262
#if !defined(__APPLE__)
263
/* Not defined in darwin? */
270
/* Terminate the current thread. */
272
objc_thread_exit(void)
274
/* Decrement our counter of the number of threads alive */
275
objc_mutex_lock(__objc_runtime_mutex);
276
__objc_runtime_threads_alive--;
277
objc_mutex_unlock(__objc_runtime_mutex);
279
/* exit the thread */
280
pthread_exit(&__objc_thread_exit_status);
282
/* Failed if we reached here */
286
/* Returns an integer value which uniquely describes a thread. */
290
pthread_t self = pthread_self();
292
return *(objc_thread_t *)&self;
295
/* Sets the thread's local storage pointer. */
297
objc_thread_set_data(void *value)
299
return pthread_setspecific(_objc_thread_storage, value);
302
/* Returns the thread's local storage pointer. */
304
objc_thread_get_data(void)
306
return pthread_getspecific(_objc_thread_storage);
309
/* Backend mutex functions */
311
/* Allocate a mutex. */
313
objc_mutex_allocate(void)
317
/* Allocate the mutex structure */
318
if (!(mutex = (objc_mutex_t)objc_malloc(sizeof(struct objc_mutex))))
321
/* Call backend to create the mutex */
322
if (pthread_mutex_init((pthread_mutex_t *)(&(mutex->backend)), NULL))
329
/* Initialize mutex */
335
/* Deallocate a mutex. */
337
objc_mutex_deallocate(objc_mutex_t mutex)
345
/* Acquire lock on mutex */
346
depth = objc_mutex_lock(mutex);
348
if (pthread_mutex_destroy((pthread_mutex_t *)(&(mutex->backend))))
351
/* Free the mutex structure */
354
/* Return last depth */
358
/* Grab a lock on a mutex. */
360
objc_mutex_lock(objc_mutex_t mutex)
362
objc_thread_t thread_id;
369
/* If we already own the lock then increment depth */
370
thread_id = objc_thread_id();
371
if (mutex->owner == thread_id)
372
return ++mutex->depth;
374
/* Call the backend to lock the mutex */
375
status = pthread_mutex_lock((pthread_mutex_t *)(&(mutex->backend)));
381
/* Successfully locked the thread */
382
mutex->owner = thread_id;
383
return mutex->depth = 1;
386
/* Try to grab a lock on a mutex. */
388
objc_mutex_trylock(objc_mutex_t mutex)
390
objc_thread_t thread_id;
397
/* If we already own the lock then increment depth */
398
thread_id = objc_thread_id();
399
if (mutex->owner == thread_id)
400
return ++mutex->depth;
402
/* Call the backend to try to lock the mutex */
403
status = pthread_mutex_trylock((pthread_mutex_t *)(&(mutex->backend)));
409
/* Successfully locked the thread */
410
mutex->owner = thread_id;
411
return mutex->depth = 1;
414
/* Unlock the mutex */
416
objc_mutex_unlock(objc_mutex_t mutex)
418
objc_thread_t thread_id;
424
/* If another thread owns the lock then abort */
425
thread_id = objc_thread_id();
426
if (mutex->owner != thread_id)
429
/* Decrement depth and return */
430
if (mutex->depth > 1)
431
return --mutex->depth;
433
/* Depth down to zero so we are no longer the owner */
437
/* Have the backend unlock the mutex */
438
return pthread_mutex_unlock((pthread_mutex_t *)(&(mutex->backend)));
441
/* Backend condition mutex functions */
443
/* Allocate a condition. */
445
objc_condition_allocate(void)
447
objc_condition_t condition;
449
/* Allocate the condition mutex structure */
451
(objc_condition_t)objc_malloc(sizeof(struct objc_condition))))
454
/* Call the backend to create the condition mutex */
455
if (pthread_cond_init((pthread_cond_t *)(&(condition->backend)), NULL))
458
objc_free(condition);
466
/* Deallocate a condition. */
468
objc_condition_deallocate(objc_condition_t condition)
470
/* Broadcast the condition */
471
if (objc_condition_broadcast(condition))
474
/* Call the backend to destroy */
475
if (pthread_cond_destroy((pthread_cond_t *)(&(condition->backend))))
478
/* Free the condition mutex structure */
479
objc_free(condition);
484
/* Wait on the condition */
486
objc_condition_wait(objc_condition_t condition, objc_mutex_t mutex)
488
objc_thread_t thread_id;
490
/* Valid arguments? */
491
if (!mutex || !condition)
494
/* Make sure we are owner of mutex */
495
thread_id = objc_thread_id();
496
if (mutex->owner != thread_id)
499
/* Cannot be locked more than once */
500
if (mutex->depth > 1)
503
/* Virtually unlock the mutex */
505
mutex->owner = (objc_thread_t)NULL;
507
/* Call the backend to wait */
508
pthread_cond_wait((pthread_cond_t *)(&(condition->backend)),
509
(pthread_mutex_t *)(&(mutex->backend)));
511
/* Make ourselves owner of the mutex */
512
mutex->owner = thread_id;
518
/* Wake up all threads waiting on this condition. */
520
objc_condition_broadcast(objc_condition_t condition)
522
/* Valid condition mutex? */
526
return pthread_cond_broadcast((pthread_cond_t *)(&(condition->backend)));
529
/* Wake up one thread waiting on this condition. */
531
objc_condition_signal(objc_condition_t condition)
533
/* Valid condition mutex? */
537
return pthread_cond_signal((pthread_cond_t *)(&(condition->backend)));
540
/* Make the objc thread system aware that a thread which is managed
541
(started, stopped) by external code could access objc facilities
542
from now on. This is used when you are interfacing with some
543
external non-objc-based environment/system - you must call
544
objc_thread_add() before an alien thread makes any calls to
545
Objective-C. Do not cause the _objc_became_multi_threaded hook to
548
objc_thread_add(void)
550
objc_mutex_lock(__objc_runtime_mutex);
551
__objc_is_multi_threaded = 1;
552
__objc_runtime_threads_alive++;
553
objc_mutex_unlock(__objc_runtime_mutex);
556
/* Make the objc thread system aware that a thread managed (started,
557
stopped) by some external code will no longer access objc and thus
558
can be forgotten by the objc thread system. Call
559
objc_thread_remove() when your alien thread is done with making
560
calls to Objective-C. */
562
objc_thread_remove(void)
564
objc_mutex_lock(__objc_runtime_mutex);
565
__objc_runtime_threads_alive--;
566
objc_mutex_unlock(__objc_runtime_mutex);