1*e4b17023SJohn Marino /* Thread and mutex controls for Objective C. 2*e4b17023SJohn Marino Copyright (C) 1996, 1997, 2002, 2004, 2009, 2010 3*e4b17023SJohn Marino Free Software Foundation, Inc. 4*e4b17023SJohn Marino Contributed by Galen C. Hunt (gchunt@cs.rochester.edu) 5*e4b17023SJohn Marino 6*e4b17023SJohn Marino This file is part of GCC. 7*e4b17023SJohn Marino 8*e4b17023SJohn Marino GCC is free software; you can redistribute it and/or modify 9*e4b17023SJohn Marino it under the terms of the GNU General Public License as published by 10*e4b17023SJohn Marino the Free Software Foundation; either version 3, or (at your option) 11*e4b17023SJohn Marino any later version. 12*e4b17023SJohn Marino 13*e4b17023SJohn Marino GCC is distributed in the hope that it will be useful, 14*e4b17023SJohn Marino but WITHOUT ANY WARRANTY; without even the implied warranty of 15*e4b17023SJohn Marino MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16*e4b17023SJohn Marino GNU General Public License for more details. 17*e4b17023SJohn Marino 18*e4b17023SJohn Marino Under Section 7 of GPL version 3, you are granted additional 19*e4b17023SJohn Marino permissions described in the GCC Runtime Library Exception, version 20*e4b17023SJohn Marino 3.1, as published by the Free Software Foundation. 21*e4b17023SJohn Marino 22*e4b17023SJohn Marino You should have received a copy of the GNU General Public License and 23*e4b17023SJohn Marino a copy of the GCC Runtime Library Exception along with this program; 24*e4b17023SJohn Marino see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 25*e4b17023SJohn Marino <http://www.gnu.org/licenses/>. */ 26*e4b17023SJohn Marino 27*e4b17023SJohn Marino #ifndef __thread_INCLUDE_GNU 28*e4b17023SJohn Marino #define __thread_INCLUDE_GNU 29*e4b17023SJohn Marino 30*e4b17023SJohn Marino #include "objc.h" 31*e4b17023SJohn Marino 32*e4b17023SJohn Marino #ifdef __cplusplus 33*e4b17023SJohn Marino extern "C" { 34*e4b17023SJohn Marino #endif /* __cplusplus */ 35*e4b17023SJohn Marino 36*e4b17023SJohn Marino /************************************************************************* 37*e4b17023SJohn Marino * Universal static variables: 38*e4b17023SJohn Marino */ 39*e4b17023SJohn Marino extern int __objc_thread_exit_status; /* Global exit status. */ 40*e4b17023SJohn Marino 41*e4b17023SJohn Marino /******** 42*e4b17023SJohn Marino * Thread safe implementation types and functions. 43*e4b17023SJohn Marino */ 44*e4b17023SJohn Marino 45*e4b17023SJohn Marino /* Thread priorities */ 46*e4b17023SJohn Marino #define OBJC_THREAD_INTERACTIVE_PRIORITY 2 47*e4b17023SJohn Marino #define OBJC_THREAD_BACKGROUND_PRIORITY 1 48*e4b17023SJohn Marino #define OBJC_THREAD_LOW_PRIORITY 0 49*e4b17023SJohn Marino 50*e4b17023SJohn Marino /* A thread */ 51*e4b17023SJohn Marino typedef void * objc_thread_t; 52*e4b17023SJohn Marino 53*e4b17023SJohn Marino /* This structure represents a single mutual exclusion lock. */ 54*e4b17023SJohn Marino struct objc_mutex 55*e4b17023SJohn Marino { 56*e4b17023SJohn Marino volatile objc_thread_t owner; /* Id of thread that owns. */ 57*e4b17023SJohn Marino volatile int depth; /* # of acquires. */ 58*e4b17023SJohn Marino void * backend; /* Specific to backend */ 59*e4b17023SJohn Marino }; 60*e4b17023SJohn Marino typedef struct objc_mutex *objc_mutex_t; 61*e4b17023SJohn Marino 62*e4b17023SJohn Marino /* This structure represents a single condition mutex */ 63*e4b17023SJohn Marino struct objc_condition 64*e4b17023SJohn Marino { 65*e4b17023SJohn Marino void * backend; /* Specific to backend */ 66*e4b17023SJohn Marino }; 67*e4b17023SJohn Marino typedef struct objc_condition *objc_condition_t; 68*e4b17023SJohn Marino 69*e4b17023SJohn Marino /* Frontend mutex functions */ 70*e4b17023SJohn Marino objc_mutex_t objc_mutex_allocate (void); 71*e4b17023SJohn Marino int objc_mutex_deallocate (objc_mutex_t mutex); 72*e4b17023SJohn Marino int objc_mutex_lock (objc_mutex_t mutex); 73*e4b17023SJohn Marino int objc_mutex_unlock (objc_mutex_t mutex); 74*e4b17023SJohn Marino int objc_mutex_trylock (objc_mutex_t mutex); 75*e4b17023SJohn Marino 76*e4b17023SJohn Marino /* Frontend condition mutex functions */ 77*e4b17023SJohn Marino objc_condition_t objc_condition_allocate (void); 78*e4b17023SJohn Marino int objc_condition_deallocate (objc_condition_t condition); 79*e4b17023SJohn Marino int objc_condition_wait (objc_condition_t condition, objc_mutex_t mutex); 80*e4b17023SJohn Marino int objc_condition_signal (objc_condition_t condition); 81*e4b17023SJohn Marino int objc_condition_broadcast (objc_condition_t condition); 82*e4b17023SJohn Marino 83*e4b17023SJohn Marino /* Frontend thread functions */ 84*e4b17023SJohn Marino objc_thread_t objc_thread_detach (SEL selector, id object, id argument); 85*e4b17023SJohn Marino void objc_thread_yield (void); 86*e4b17023SJohn Marino int objc_thread_exit (void); 87*e4b17023SJohn Marino int objc_thread_set_priority (int priority); 88*e4b17023SJohn Marino int objc_thread_get_priority (void); 89*e4b17023SJohn Marino void * objc_thread_get_data (void); 90*e4b17023SJohn Marino int objc_thread_set_data (void *value); 91*e4b17023SJohn Marino objc_thread_t objc_thread_id (void); 92*e4b17023SJohn Marino void objc_thread_add (void); 93*e4b17023SJohn Marino void objc_thread_remove (void); 94*e4b17023SJohn Marino 95*e4b17023SJohn Marino /* 96*e4b17023SJohn Marino Use this to set the hook function that will be called when the 97*e4b17023SJohn Marino runtime initially becomes multi threaded. 98*e4b17023SJohn Marino The hook function is only called once, meaning only when the 99*e4b17023SJohn Marino 2nd thread is spawned, not for each and every thread. 100*e4b17023SJohn Marino 101*e4b17023SJohn Marino It returns the previous hook function or NULL if there is none. 102*e4b17023SJohn Marino 103*e4b17023SJohn Marino A program outside of the runtime could set this to some function so 104*e4b17023SJohn Marino it can be informed; for example, the GNUstep Base Library sets it 105*e4b17023SJohn Marino so it can implement the NSBecomingMultiThreaded notification. 106*e4b17023SJohn Marino */ 107*e4b17023SJohn Marino typedef void (*objc_thread_callback) (void); 108*e4b17023SJohn Marino objc_thread_callback objc_set_thread_callback (objc_thread_callback func); 109*e4b17023SJohn Marino 110*e4b17023SJohn Marino /* Backend initialization functions */ 111*e4b17023SJohn Marino int __objc_init_thread_system (void); 112*e4b17023SJohn Marino 113*e4b17023SJohn Marino #ifdef __cplusplus 114*e4b17023SJohn Marino } 115*e4b17023SJohn Marino #endif /* __cplusplus */ 116*e4b17023SJohn Marino 117*e4b17023SJohn Marino #endif /* not __thread_INCLUDE_GNU */ 118