OSDN Git Service

In libobjc/:
[pf3gnuchains/gcc-fork.git] / libobjc / thr.c
1 /* GNU Objective C Runtime Thread Interface
2    Copyright (C) 1996, 1997, 2009, 2010 Free Software Foundation, Inc.
3    Contributed by Galen C. Hunt (gchunt@cs.rochester.edu)
4
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 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
14 details.
15
16 Under Section 7 of GPL version 3, you are granted additional
17 permissions described in the GCC Runtime Library Exception, version
18 3.1, as published by the Free Software Foundation.
19
20 You should have received a copy of the GNU General Public License and
21 a copy of the GCC Runtime Library Exception along with this program;
22 see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23 <http://www.gnu.org/licenses/>.  */
24
25 #define _LIBOBJC
26 /* The line below is needed for declarations of functions such as
27    pthread_mutexattr_settype, without which gthr-posix.h may fail to
28    compile within libobjc.  Unfortunately, this breaks compilation on
29    Tru64 UNIX V4.0F, so disable it there.  */
30 #ifndef __osf__
31 #define _XOPEN_SOURCE 500
32 #endif
33 #include "config.h"
34 #include "tconfig.h"
35 #include "coretypes.h"
36 #include "tm.h"
37 #include "defaults.h"
38 #include "objc/thr.h"
39 #include "objc/objc.h"
40 #include "objc/objc-api.h"
41 #include "objc-private/runtime.h"
42 #include <gthr.h>
43
44 #include <stdlib.h>
45
46 /* Global exit status. */
47 int __objc_thread_exit_status = 0;
48
49 /* Flag which lets us know if we ever became multi threaded */
50 int __objc_is_multi_threaded = 0;
51
52 /* The hook function called when the runtime becomes multi threaded */
53 objc_thread_callback _objc_became_multi_threaded = NULL;
54
55 /*
56   Use this to set the hook function that will be called when the 
57   runtime initially becomes multi threaded.
58   The hook function is only called once, meaning only when the 
59   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   A program outside of the runtime could set this to some function so
64   it can be informed; for example, the GNUstep Base Library sets it 
65   so it can implement the NSBecomingMultiThreaded notification.
66   */
67 objc_thread_callback objc_set_thread_callback (objc_thread_callback func)
68 {
69   objc_thread_callback temp = _objc_became_multi_threaded;
70   _objc_became_multi_threaded = func;
71   return temp;
72 }
73
74 /*
75   Private functions
76
77   These functions are utilized by the frontend, but they are not
78   considered part of the public interface.
79   */
80
81 /* Initialize the threads subsystem. */
82 int
83 __objc_init_thread_system(void)
84 {
85   return __gthread_objc_init_thread_system ();
86 }
87
88 /*
89   First function called in a thread, starts everything else.
90
91   This function is passed to the backend by objc_thread_detach
92   as the starting function for a new thread.
93  */
94 struct __objc_thread_start_state
95 {
96   SEL selector;
97   id object;
98   id argument;
99 };
100
101 static void __attribute__((noreturn))
102 __objc_thread_detach_function (struct __objc_thread_start_state *istate) 
103 {
104   /* Valid state? */
105   if (istate) {
106     id (*imp) (id, SEL, id);
107     SEL selector = istate->selector;
108     id object   = istate->object;
109     id argument = istate->argument;
110
111     /* Don't need anymore so free it */
112     objc_free (istate);
113
114     /* Clear out the thread local storage */
115     objc_thread_set_data (NULL);
116
117     /* Check to see if we just became multi threaded */
118     if (! __objc_is_multi_threaded)
119       {
120         __objc_is_multi_threaded = 1;
121
122         /* Call the hook function */
123         if (_objc_became_multi_threaded != NULL)
124           (*_objc_became_multi_threaded) ();
125       }
126
127     /* Call the method */
128     if ((imp = (id (*) (id, SEL, id))objc_msg_lookup (object, selector)))
129         (*imp) (object, selector, argument);
130     else
131       objc_error (object, OBJC_ERR_UNIMPLEMENTED,
132                   "objc_thread_detach called with bad selector.\n");
133   }
134   else
135     objc_error (nil, OBJC_ERR_BAD_STATE,
136                 "objc_thread_detach called with NULL state.\n");
137
138   /* Exit the thread */
139   objc_thread_exit ();
140   
141   /* Make sure compiler detects no return.  */
142   __builtin_trap ();
143 }
144
145 /*
146   Frontend functions
147
148   These functions constitute the public interface to the Objective-C thread
149   and mutex functionality.
150   */
151
152 /* Frontend thread functions */
153
154 /*
155   Detach a new thread of execution and return its id.  Returns NULL if fails.
156   Thread is started by sending message with selector to object.  Message
157   takes a single argument.
158   */
159 objc_thread_t
160 objc_thread_detach (SEL selector, id object, id argument)
161 {
162   struct __objc_thread_start_state *istate;
163   objc_thread_t        thread_id = NULL;
164
165   /* Allocate the state structure */
166   if (! (istate = (struct __objc_thread_start_state *)
167          objc_malloc (sizeof (*istate))))
168     return NULL;
169
170   /* Initialize the state structure */
171   istate->selector = selector;
172   istate->object = object;
173   istate->argument = argument;
174
175   /* lock access */
176   objc_mutex_lock (__objc_runtime_mutex);
177
178   /* Call the backend to spawn the thread */
179   if ((thread_id = __gthread_objc_thread_detach ((void *)__objc_thread_detach_function,
180                                                  istate)) == NULL)
181     {
182       /* failed! */
183       objc_mutex_unlock (__objc_runtime_mutex);
184       objc_free (istate);
185       return NULL;
186     }
187
188   /* Increment our thread counter */
189   __objc_runtime_threads_alive++;
190   objc_mutex_unlock (__objc_runtime_mutex);
191
192   return thread_id;
193 }
194
195 /* Set the current thread's priority. */
196 int
197 objc_thread_set_priority (int priority)
198 {
199   /* Call the backend */
200   return __gthread_objc_thread_set_priority (priority);
201 }
202
203 /* Return the current thread's priority. */
204 int
205 objc_thread_get_priority (void)
206 {
207   /* Call the backend */
208   return __gthread_objc_thread_get_priority ();
209 }
210
211 /*
212   Yield our process time to another thread.  Any BUSY waiting that is done
213   by a thread should use this function to make sure that other threads can
214   make progress even on a lazy uniprocessor system.
215   */
216 void
217 objc_thread_yield (void)
218 {
219   /* Call the backend */
220   __gthread_objc_thread_yield ();
221 }
222
223 /*
224   Terminate the current tread.  Doesn't return.
225   Actually, if it failed returns -1.
226   */
227 int
228 objc_thread_exit (void)
229 {
230   /* Decrement our counter of the number of threads alive */
231   objc_mutex_lock (__objc_runtime_mutex);
232   __objc_runtime_threads_alive--;
233   objc_mutex_unlock (__objc_runtime_mutex);
234
235   /* Call the backend to terminate the thread */
236   return __gthread_objc_thread_exit ();
237 }
238
239 /*
240   Returns an integer value which uniquely describes a thread.  Must not be
241   NULL which is reserved as a marker for "no thread".
242   */
243 objc_thread_t
244 objc_thread_id (void)
245 {
246   /* Call the backend */
247   return __gthread_objc_thread_id ();
248 }
249
250 /*
251   Sets the thread's local storage pointer. 
252   Returns 0 if successful or -1 if failed.
253   */
254 int
255 objc_thread_set_data (void *value)
256 {
257   /* Call the backend */
258   return __gthread_objc_thread_set_data (value);
259 }
260
261 /*
262   Returns the thread's local storage pointer.  Returns NULL on failure.
263   */
264 void *
265 objc_thread_get_data (void)
266 {
267   /* Call the backend */
268   return __gthread_objc_thread_get_data ();
269 }
270
271 /* Frontend mutex functions */
272
273 /*
274   Allocate a mutex.  Return the mutex pointer if successful or NULL if the
275   allocation failed for any reason.
276   */
277 objc_mutex_t
278 objc_mutex_allocate (void)
279 {
280   objc_mutex_t mutex;
281
282   /* Allocate the mutex structure */
283   if (! (mutex = (objc_mutex_t)objc_malloc (sizeof (struct objc_mutex))))
284     return NULL;
285
286   /* Call backend to create the mutex */
287   if (__gthread_objc_mutex_allocate (mutex))
288     {
289       /* failed! */
290       objc_free (mutex);
291       return NULL;
292     }
293
294   /* Initialize mutex */
295   mutex->owner = NULL;
296   mutex->depth = 0;
297   return mutex;
298 }
299
300 /*
301   Deallocate a mutex.  Note that this includes an implicit mutex_lock to
302   insure that no one else is using the lock.  It is legal to deallocate
303   a lock if we have a lock on it, but illegal to deallocate a lock held
304   by anyone else.
305   Returns the number of locks on the thread.  (1 for deallocate).
306   */
307 int
308 objc_mutex_deallocate (objc_mutex_t mutex)
309 {
310   int depth;
311
312   /* Valid mutex? */
313   if (! mutex)
314     return -1;
315
316   /* Acquire lock on mutex */
317   depth = objc_mutex_lock (mutex);
318
319   /* Call backend to destroy mutex */
320   if (__gthread_objc_mutex_deallocate (mutex))
321     return -1;
322
323   /* Free the mutex structure */
324   objc_free (mutex);
325
326   /* Return last depth */
327   return depth;
328 }
329
330 /*
331   Grab a lock on a mutex.  If this thread already has a lock on this mutex
332   then we increment the lock count.  If another thread has a lock on the 
333   mutex we block and wait for the thread to release the lock.
334   Returns the lock count on the mutex held by this thread.
335   */
336 int
337 objc_mutex_lock (objc_mutex_t mutex)
338 {
339   objc_thread_t thread_id;
340   int status;
341
342   /* Valid mutex? */
343   if (! mutex)
344     return -1;
345
346   /* If we already own the lock then increment depth */
347   thread_id = __gthread_objc_thread_id ();
348   if (mutex->owner == thread_id)
349     return ++mutex->depth;
350
351   /* Call the backend to lock the mutex */
352   status = __gthread_objc_mutex_lock (mutex);
353
354   /* Failed? */
355   if (status)
356     return status;
357
358   /* Successfully locked the thread */
359   mutex->owner = thread_id;
360   return mutex->depth = 1;
361 }
362
363 /*
364   Try to grab a lock on a mutex.  If this thread already has a lock on
365   this mutex then we increment the lock count and return it.  If another
366   thread has a lock on the mutex returns -1.
367   */
368 int
369 objc_mutex_trylock (objc_mutex_t mutex)
370 {
371   objc_thread_t thread_id;
372   int status;
373
374   /* Valid mutex? */
375   if (! mutex)
376     return -1;
377
378   /* If we already own the lock then increment depth */ 
379   thread_id = __gthread_objc_thread_id ();
380   if (mutex->owner == thread_id)
381     return ++mutex->depth;
382     
383   /* Call the backend to try to lock the mutex */
384   status = __gthread_objc_mutex_trylock (mutex);
385
386   /* Failed? */
387   if (status)
388     return status;
389
390   /* Successfully locked the thread */
391   mutex->owner = thread_id;
392   return mutex->depth = 1;
393 }
394
395 /* 
396   Unlocks the mutex by one level.
397   Decrements the lock count on this mutex by one.
398   If the lock count reaches zero, release the lock on the mutex.
399   Returns the lock count on the mutex.
400   It is an error to attempt to unlock a mutex which this thread 
401   doesn't hold in which case return -1 and the mutex is unaffected.
402   */
403 int
404 objc_mutex_unlock (objc_mutex_t mutex)
405 {
406   objc_thread_t thread_id;
407   int status;
408
409   /* Valid mutex? */
410   if (! mutex)
411     return -1;
412
413   /* If another thread owns the lock then abort */
414   thread_id = __gthread_objc_thread_id ();
415   if (mutex->owner != thread_id)
416     return -1;
417
418   /* Decrement depth and return */
419   if (mutex->depth > 1)
420     return --mutex->depth;
421
422   /* Depth down to zero so we are no longer the owner */
423   mutex->depth = 0;
424   mutex->owner = NULL;
425
426   /* Have the backend unlock the mutex */
427   status = __gthread_objc_mutex_unlock (mutex);
428
429   /* Failed? */
430   if (status)
431     return status;
432
433   return 0;
434 }
435
436 /* Frontend condition mutex functions */
437
438 /*
439   Allocate a condition.  Return the condition pointer if successful or NULL
440   if the allocation failed for any reason.
441   */
442 objc_condition_t 
443 objc_condition_allocate (void)
444 {
445   objc_condition_t condition;
446     
447   /* Allocate the condition mutex structure */
448   if (! (condition = 
449          (objc_condition_t) objc_malloc (sizeof (struct objc_condition))))
450     return NULL;
451
452   /* Call the backend to create the condition mutex */
453   if (__gthread_objc_condition_allocate (condition))
454     {
455       /* failed! */
456       objc_free (condition);
457       return NULL;
458     }
459
460   /* Success! */
461   return condition;
462 }
463
464 /*
465   Deallocate a condition. Note that this includes an implicit 
466   condition_broadcast to insure that waiting threads have the opportunity
467   to wake.  It is legal to dealloc a condition only if no other
468   thread is/will be using it. Here we do NOT check for other threads
469   waiting but just wake them up.
470   */
471 int
472 objc_condition_deallocate (objc_condition_t condition)
473 {
474   /* Broadcast the condition */
475   if (objc_condition_broadcast (condition))
476     return -1;
477
478   /* Call the backend to destroy */
479   if (__gthread_objc_condition_deallocate (condition))
480     return -1;
481
482   /* Free the condition mutex structure */
483   objc_free (condition);
484
485   return 0;
486 }
487
488 /*
489   Wait on the condition unlocking the mutex until objc_condition_signal ()
490   or objc_condition_broadcast () are called for the same condition. The
491   given mutex *must* have the depth set to 1 so that it can be unlocked
492   here, so that someone else can lock it and signal/broadcast the condition.
493   The mutex is used to lock access to the shared data that make up the
494   "condition" predicate.
495   */
496 int
497 objc_condition_wait (objc_condition_t condition, objc_mutex_t mutex)
498 {
499   objc_thread_t thread_id;
500
501   /* Valid arguments? */
502   if (! mutex || ! condition)
503     return -1;
504
505   /* Make sure we are owner of mutex */
506   thread_id = __gthread_objc_thread_id ();
507   if (mutex->owner != thread_id)
508     return -1;
509
510   /* Cannot be locked more than once */
511   if (mutex->depth > 1)
512     return -1;
513
514   /* Virtually unlock the mutex */
515   mutex->depth = 0;
516   mutex->owner = (objc_thread_t)NULL;
517
518   /* Call the backend to wait */
519   __gthread_objc_condition_wait (condition, mutex);
520
521   /* Make ourselves owner of the mutex */
522   mutex->owner = thread_id;
523   mutex->depth = 1;
524
525   return 0;
526 }
527
528 /*
529   Wake up all threads waiting on this condition. It is recommended that 
530   the called would lock the same mutex as the threads in objc_condition_wait
531   before changing the "condition predicate" and make this call and unlock it
532   right away after this call.
533   */
534 int
535 objc_condition_broadcast (objc_condition_t condition)
536 {
537   /* Valid condition mutex? */
538   if (! condition)
539     return -1;
540
541   return __gthread_objc_condition_broadcast (condition);
542 }
543
544 /*
545   Wake up one thread waiting on this condition. It is recommended that 
546   the called would lock the same mutex as the threads in objc_condition_wait
547   before changing the "condition predicate" and make this call and unlock it
548   right away after this call.
549   */
550 int
551 objc_condition_signal (objc_condition_t condition)
552 {
553   /* Valid condition mutex? */
554   if (! condition)
555     return -1;
556
557   return __gthread_objc_condition_signal (condition);
558 }
559
560 /* Make the objc thread system aware that a thread which is managed
561    (started, stopped) by external code could access objc facilities
562    from now on.  This is used when you are interfacing with some
563    external non-objc-based environment/system - you must call
564    objc_thread_add () before an alien thread makes any calls to
565    Objective-C.  Do not cause the _objc_became_multi_threaded hook to
566    be executed. */
567 void 
568 objc_thread_add (void)
569 {
570   objc_mutex_lock (__objc_runtime_mutex);
571   __objc_is_multi_threaded = 1;
572   __objc_runtime_threads_alive++;
573   objc_mutex_unlock (__objc_runtime_mutex);  
574 }
575
576 /* Make the objc thread system aware that a thread managed (started,
577    stopped) by some external code will no longer access objc and thus
578    can be forgotten by the objc thread system.  Call
579    objc_thread_remove () when your alien thread is done with making
580    calls to Objective-C. */
581 void
582 objc_thread_remove (void)
583 {
584   objc_mutex_lock (__objc_runtime_mutex);
585   __objc_runtime_threads_alive--;
586   objc_mutex_unlock (__objc_runtime_mutex);  
587 }
588
589 /* End of File */