thr.c 15 KB
Newer Older
1
/* GNU Objective C Runtime Thread Interface
2
   Copyright (C) 1996-2013 Free Software Foundation, Inc.
3 4
   Contributed by Galen C. Hunt (gchunt@cs.rochester.edu)

5
This file is part of GCC.
6

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.
10

11
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 13 14 15
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
details.

16 17 18 19 20 21 22 23
Under Section 7 of GPL version 3, you are granted additional
permissions described in the GCC Runtime Library Exception, version
3.1, as published by the Free Software Foundation.

You should have received a copy of the GNU General Public License and
a copy of the GCC Runtime Library Exception along with this program;
see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
<http://www.gnu.org/licenses/>.  */
24

Nicola Pero committed
25
#include "objc-private/common.h"
Nicola Pero committed
26
#include "objc-private/error.h"
27 28 29
#define _LIBOBJC
/* The line below is needed for declarations of functions such as
   pthread_mutexattr_settype, without which gthr-posix.h may fail to
30
   compile within libobjc.  */
31 32 33 34 35 36 37
#define _XOPEN_SOURCE 500
#include "config.h"
#include "tconfig.h"
#include "coretypes.h"
#include "tm.h"
#include "defaults.h"
#include "objc/thr.h"
38
#include "objc/message.h" /* For objc_msg_lookup().  */
39
#include "objc/runtime.h"
40
#include "objc-private/module-abi-8.h"
Nicola Pero committed
41
#include "objc-private/runtime.h"
42
#include <gthr.h>
43 44 45 46 47 48

#include <stdlib.h>

/* Global exit status. */
int __objc_thread_exit_status = 0;

49
/* Flag which lets us know if we ever became multi threaded.  */
50 51
int __objc_is_multi_threaded = 0;

52 53
/* The hook function called when the runtime becomes multi
   threaded.  */
54 55
objc_thread_callback _objc_became_multi_threaded = NULL;

56 57 58 59
/* Use this to set the hook function that will be called when the
   runtime initially becomes multi threaded.  The hook function is
   only called once, meaning only when the 2nd thread is spawned, not
   for each and every thread.
60

61
   It returns the previous hook function or NULL if there is none.
62

63 64 65
   A program outside of the runtime could set this to some function so
   it can be informed; for example, the GNUstep Base Library sets it
   so it can implement the NSBecomingMultiThreaded notification.  */
66
objc_thread_callback objc_set_thread_callback (objc_thread_callback func)
67 68 69 70 71 72
{
  objc_thread_callback temp = _objc_became_multi_threaded;
  _objc_became_multi_threaded = func;
  return temp;
}

73 74 75 76
/* Private functions.
   
   These functions are utilized by the runtime, but they are not
   considered part of the public interface.  */
77

78
/* Initialize the threads subsystem.  */
79 80 81 82 83 84
int
__objc_init_thread_system(void)
{
  return __gthread_objc_init_thread_system ();
}

85
/* First function called in a thread, starts everything else.
86

87 88
   This function is passed to the backend by objc_thread_detach as the
   starting function for a new thread.  */
89 90 91 92 93 94 95
struct __objc_thread_start_state
{
  SEL selector;
  id object;
  id argument;
};

96 97
static void __attribute__((noreturn))
__objc_thread_detach_function (struct __objc_thread_start_state *istate) 
98 99
{
  /* Valid state? */
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
  if (istate)
    {
      id (*imp) (id, SEL, id);
      SEL selector = istate->selector;
      id object   = istate->object;
      id argument = istate->argument;
      
      /* Don't need anymore so free it.  */
      objc_free (istate);

      /* Clear out the thread local storage.  */
      objc_thread_set_data (NULL);
      
      /* Check to see if we just became multi threaded. */
      if (! __objc_is_multi_threaded)
	{
	  __objc_is_multi_threaded = 1;
	  
	  /* Call the hook function.  */
	  if (_objc_became_multi_threaded != NULL)
	    (*_objc_became_multi_threaded) ();
	}
      
      /* Call the method.  */
      if ((imp = (id (*) (id, SEL, id))objc_msg_lookup (object, selector)))
125
	(*imp) (object, selector, argument);
126 127 128 129 130 131
      else
	{
	  /* FIXME: Should we abort here ? */
	  _objc_abort ("objc_thread_detach called with bad selector.\n");
	}
    }
132
  else
Nicola Pero committed
133 134 135 136
    {
      /* FIXME: Should we abort here ? */
      _objc_abort ("objc_thread_detach called with NULL state.\n");
    }
137 138
  
  /* Exit the thread.  */
139
  objc_thread_exit ();
140 141 142
  
  /* Make sure compiler detects no return.  */
  __builtin_trap ();
143 144
}

145
/* Public functions.
146

147 148
   These functions constitute the public interface to the Objective-C
   thread and mutex functionality.  */
149

150 151 152
/* Detach a new thread of execution and return its id.  Returns NULL
   if fails.  Thread is started by sending message with selector to
   object.  Message takes a single argument.  */
153
objc_thread_t
154
objc_thread_detach (SEL selector, id object, id argument)
155 156 157 158
{
  struct __objc_thread_start_state *istate;
  objc_thread_t        thread_id = NULL;

159 160 161
  /* Allocate the state structure.  */
  if (!(istate = (struct __objc_thread_start_state *)objc_malloc
	(sizeof (*istate))))
162
    return NULL;
163 164
  
  /* Initialize the state structure.  */
165 166 167 168
  istate->selector = selector;
  istate->object = object;
  istate->argument = argument;

169
  /* Lock access.  */
170
  objc_mutex_lock (__objc_runtime_mutex);
171

172
  /* Call the backend to spawn the thread.  */
173 174
  if ((thread_id = __gthread_objc_thread_detach ((void *)__objc_thread_detach_function,
						 istate)) == NULL)
175
    {
176
      /* Failed!  */
177 178
      objc_mutex_unlock (__objc_runtime_mutex);
      objc_free (istate);
179 180 181
      return NULL;
    }

182
  /* Increment our thread counter.  */
183
  __objc_runtime_threads_alive++;
184
  objc_mutex_unlock (__objc_runtime_mutex);
185 186 187 188

  return thread_id;
}

189
/* Set the current thread's priority.  */
190
int
191
objc_thread_set_priority (int priority)
192
{
193
  return __gthread_objc_thread_set_priority (priority);
194 195
}

196
/* Return the current thread's priority.  */
197
int
198
objc_thread_get_priority (void)
199
{
200
  return __gthread_objc_thread_get_priority ();
201 202
}

203 204 205
/* Yield our process time to another thread.  Any BUSY waiting that is
   done by a thread should use this function to make sure that other
   threads can make progress even on a lazy uniprocessor system.  */
206
void
207
objc_thread_yield (void)
208
{
209
  __gthread_objc_thread_yield ();
210 211
}

212 213
/* Terminate the current tread.  Doesn't return.  Actually, if it
   failed returns -1.  */
214
int
215
objc_thread_exit (void)
216
{
217
  /* Decrement our counter of the number of threads alive.  */
218
  objc_mutex_lock (__objc_runtime_mutex);
219
  __objc_runtime_threads_alive--;
220
  objc_mutex_unlock (__objc_runtime_mutex);
221

222
  /* Call the backend to terminate the thread.  */
223
  return __gthread_objc_thread_exit ();
224 225
}

226 227
/* Returns an integer value which uniquely describes a thread.  Must
   not be NULL which is reserved as a marker for "no thread".  */
228
objc_thread_t
229
objc_thread_id (void)
230
{
231
  return __gthread_objc_thread_id ();
232 233
}

234 235
/* Sets the thread's local storage pointer.  Returns 0 if successful
   or -1 if failed.  */
236
int
237
objc_thread_set_data (void *value)
238
{
239
  return __gthread_objc_thread_set_data (value);
240 241
}

242 243
/* Returns the thread's local storage pointer.  Returns NULL on
   failure.  */
244
void *
245
objc_thread_get_data (void)
246
{
247
  return __gthread_objc_thread_get_data ();
248 249
}

250
/* Public mutex functions */
251

252 253
/* Allocate a mutex.  Return the mutex pointer if successful or NULL
   if the allocation failed for any reason.  */
254
objc_mutex_t
255
objc_mutex_allocate (void)
256 257 258
{
  objc_mutex_t mutex;

259
  /* Allocate the mutex structure.  */
260
  if (! (mutex = (objc_mutex_t)objc_malloc (sizeof (struct objc_mutex))))
261 262
    return NULL;

263
  /* Call backend to create the mutex.  */
264
  if (__gthread_objc_mutex_allocate (mutex))
265
    {
266
      /* Failed!  */
267
      objc_free (mutex);
268 269 270
      return NULL;
    }

271
  /* Initialize mutex.  */
272 273 274 275 276
  mutex->owner = NULL;
  mutex->depth = 0;
  return mutex;
}

277 278 279 280 281
/* Deallocate a mutex.  Note that this includes an implicit mutex_lock
   to insure that no one else is using the lock.  It is legal to
   deallocate a lock if we have a lock on it, but illegal to
   deallocate a lock held by anyone else.  Returns the number of locks
   on the thread.  (1 for deallocate).  */
282
int
283
objc_mutex_deallocate (objc_mutex_t mutex)
284 285 286
{
  int depth;

287
  /* Valid mutex?  */
288
  if (! mutex)
289 290
    return -1;

291
  /* Acquire lock on mutex.  */
292
  depth = objc_mutex_lock (mutex);
293

294
  /* Call backend to destroy mutex.  */
295
  if (__gthread_objc_mutex_deallocate (mutex))
296 297
    return -1;

298
  /* Free the mutex structure.  */
299
  objc_free (mutex);
300

301
  /* Return last depth.  */
302 303 304
  return depth;
}

305 306 307 308
/* Grab a lock on a mutex.  If this thread already has a lock on this
   mutex then we increment the lock count.  If another thread has a
   lock on the mutex we block and wait for the thread to release the
   lock.  Returns the lock count on the mutex held by this thread.  */
309
int
310
objc_mutex_lock (objc_mutex_t mutex)
311 312 313 314
{
  objc_thread_t thread_id;
  int status;

315
  /* Valid mutex?  */
316
  if (! mutex)
317 318
    return -1;

319
  /* If we already own the lock then increment depth.  */
320
  thread_id = __gthread_objc_thread_id ();
321 322 323
  if (mutex->owner == thread_id)
    return ++mutex->depth;

324
  /* Call the backend to lock the mutex.  */
325
  status = __gthread_objc_mutex_lock (mutex);
326

327
  /* Failed?  */
328 329 330
  if (status)
    return status;

331
  /* Successfully locked the thread.  */
332 333 334 335
  mutex->owner = thread_id;
  return mutex->depth = 1;
}

336 337 338
/* Try to grab a lock on a mutex.  If this thread already has a lock
   on this mutex then we increment the lock count and return it.  If
   another thread has a lock on the mutex returns -1.  */
339
int
340
objc_mutex_trylock (objc_mutex_t mutex)
341 342 343 344
{
  objc_thread_t thread_id;
  int status;

345
  /* Valid mutex?  */
346
  if (! mutex)
347 348
    return -1;

349
  /* If we already own the lock then increment depth.  */
350
  thread_id = __gthread_objc_thread_id ();
351 352 353
  if (mutex->owner == thread_id)
    return ++mutex->depth;
    
354
  /* Call the backend to try to lock the mutex.  */
355
  status = __gthread_objc_mutex_trylock (mutex);
356

357
  /* Failed?  */
358 359 360
  if (status)
    return status;

361
  /* Successfully locked the thread.  */
362 363 364 365
  mutex->owner = thread_id;
  return mutex->depth = 1;
}

366 367 368 369 370
/* Unlocks the mutex by one level.  Decrements the lock count on this
   mutex by one.  If the lock count reaches zero, release the lock on
   the mutex.  Returns the lock count on the mutex.  It is an error to
   attempt to unlock a mutex which this thread doesn't hold in which
   case return -1 and the mutex is unaffected.  */
371
int
372
objc_mutex_unlock (objc_mutex_t mutex)
373 374 375 376
{
  objc_thread_t thread_id;
  int status;

377
  /* Valid mutex?  */
378
  if (! mutex)
379 380
    return -1;

381
  /* If another thread owns the lock then abort.  */
382
  thread_id = __gthread_objc_thread_id ();
383 384 385
  if (mutex->owner != thread_id)
    return -1;

386
  /* Decrement depth and return.  */
387 388 389
  if (mutex->depth > 1)
    return --mutex->depth;

390
  /* Depth down to zero so we are no longer the owner.  */
391 392 393
  mutex->depth = 0;
  mutex->owner = NULL;

394
  /* Have the backend unlock the mutex.  */
395
  status = __gthread_objc_mutex_unlock (mutex);
396

397
  /* Failed?  */
398 399 400 401 402 403
  if (status)
    return status;

  return 0;
}

404
/* Public condition mutex functions */
405

406 407
/* Allocate a condition.  Return the condition pointer if successful
   or NULL if the allocation failed for any reason.  */
408
objc_condition_t 
409
objc_condition_allocate (void)
410 411 412
{
  objc_condition_t condition;
    
413
  /* Allocate the condition mutex structure.  */
414 415
  if (! (condition = 
	 (objc_condition_t) objc_malloc (sizeof (struct objc_condition))))
416 417
    return NULL;

418
  /* Call the backend to create the condition mutex.  */
419
  if (__gthread_objc_condition_allocate (condition))
420
    {
421
      /* Failed!  */
422
      objc_free (condition);
423 424 425
      return NULL;
    }

426
  /* Success!  */
427 428 429
  return condition;
}

430 431 432 433 434
/* Deallocate a condition. Note that this includes an implicit
   condition_broadcast to insure that waiting threads have the
   opportunity to wake.  It is legal to dealloc a condition only if no
   other thread is/will be using it. Here we do NOT check for other
   threads waiting but just wake them up.  */
435
int
436
objc_condition_deallocate (objc_condition_t condition)
437
{
438
  /* Broadcast the condition.  */
439
  if (objc_condition_broadcast (condition))
440 441
    return -1;

442
  /* Call the backend to destroy.  */
443
  if (__gthread_objc_condition_deallocate (condition))
444 445
    return -1;

446
  /* Free the condition mutex structure.  */
447
  objc_free (condition);
448 449 450 451

  return 0;
}

452 453 454 455 456 457 458
/* Wait on the condition unlocking the mutex until
   objc_condition_signal () or objc_condition_broadcast () are called
   for the same condition. The given mutex *must* have the depth set
   to 1 so that it can be unlocked here, so that someone else can lock
   it and signal/broadcast the condition.  The mutex is used to lock
   access to the shared data that make up the "condition"
   predicate.  */
459
int
460
objc_condition_wait (objc_condition_t condition, objc_mutex_t mutex)
461 462 463
{
  objc_thread_t thread_id;

464
  /* Valid arguments?  */
465
  if (! mutex || ! condition)
466 467
    return -1;

468
  /* Make sure we are owner of mutex.  */
469
  thread_id = __gthread_objc_thread_id ();
470 471 472
  if (mutex->owner != thread_id)
    return -1;

473
  /* Cannot be locked more than once.  */
474 475 476
  if (mutex->depth > 1)
    return -1;

477
  /* Virtually unlock the mutex.  */
478 479 480
  mutex->depth = 0;
  mutex->owner = (objc_thread_t)NULL;

481
  /* Call the backend to wait.  */
482
  __gthread_objc_condition_wait (condition, mutex);
483

484
  /* Make ourselves owner of the mutex.  */
485 486 487 488 489 490
  mutex->owner = thread_id;
  mutex->depth = 1;

  return 0;
}

491 492 493 494
/* Wake up all threads waiting on this condition. It is recommended
   that the called would lock the same mutex as the threads in
   objc_condition_wait before changing the "condition predicate" and
   make this call and unlock it right away after this call.  */
495
int
496
objc_condition_broadcast (objc_condition_t condition)
497
{
498
  /* Valid condition mutex?  */
499
  if (! condition)
500 501
    return -1;

502
  return __gthread_objc_condition_broadcast (condition);
503 504
}

505 506 507 508
/* Wake up one thread waiting on this condition. It is recommended
   that the called would lock the same mutex as the threads in
   objc_condition_wait before changing the "condition predicate" and
   make this call and unlock it right away after this call.  */
509
int
510
objc_condition_signal (objc_condition_t condition)
511
{
512
  /* Valid condition mutex?  */
513
  if (! condition)
514 515
    return -1;

516
  return __gthread_objc_condition_signal (condition);
517 518
}

519 520 521 522
/* Make the objc thread system aware that a thread which is managed
   (started, stopped) by external code could access objc facilities
   from now on.  This is used when you are interfacing with some
   external non-objc-based environment/system - you must call
523
   objc_thread_add () before an alien thread makes any calls to
524 525 526
   Objective-C.  Do not cause the _objc_became_multi_threaded hook to
   be executed. */
void 
527
objc_thread_add (void)
528
{
529
  objc_mutex_lock (__objc_runtime_mutex);
530 531
  __objc_is_multi_threaded = 1;
  __objc_runtime_threads_alive++;
532
  objc_mutex_unlock (__objc_runtime_mutex);  
533 534 535 536 537
}

/* Make the objc thread system aware that a thread managed (started,
   stopped) by some external code will no longer access objc and thus
   can be forgotten by the objc thread system.  Call
538
   objc_thread_remove () when your alien thread is done with making
539 540
   calls to Objective-C. */
void
541
objc_thread_remove (void)
542
{
543
  objc_mutex_lock (__objc_runtime_mutex);
544
  __objc_runtime_threads_alive--;
545
  objc_mutex_unlock (__objc_runtime_mutex);  
546 547
}