Initial import from FreeBSD RELENG_4:
[dragonfly.git] / contrib / libobjc / objc / thr.h
1 /* Thread and mutex controls for Objective C.
2    Copyright (C) 1996, 1997 Free Software Foundation, Inc.
3    Contributed by Galen C. Hunt (gchunt@cs.rochester.edu)
4
5 This file is part of GNU CC.
6
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 GNU CC is free software; you can redistribute it and/or modify it under the
18 terms of the GNU General Public License as published by the Free Software
19 Foundation; either version 2, or (at your option) any later version.
20
21 GNU CC is distributed in the hope that it will be useful, but WITHOUT ANY
22 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
23 FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
24 details.
25
26 You should have received a copy of the GNU General Public License along with
27 GNU CC; see the file COPYING.  If not, write to the Free Software
28 Foundation, 59 Temple Place - Suite 330,
29 Boston, MA 02111-1307, USA.  */
30
31 /* As a special exception, if you link this library with files
32    compiled with GCC to produce an executable, this does not cause
33    the resulting executable to be covered by the GNU General Public License.
34    This exception does not however invalidate any other reasons why
35    the executable file might be covered by the GNU General Public License.  */
36
37
38 #ifndef __thread_INCLUDE_GNU
39 #define __thread_INCLUDE_GNU
40
41 #include "objc/objc.h"
42
43 /*************************************************************************
44  *  Universal static variables:
45  */
46 extern int __objc_thread_exit_status;      /* Global exit status.   */
47
48 /********
49  *  Thread safe implementation types and functions.  
50  */
51
52 /* Thread priorities */
53 #define OBJC_THREAD_INTERACTIVE_PRIORITY        2
54 #define OBJC_THREAD_BACKGROUND_PRIORITY         1
55 #define OBJC_THREAD_LOW_PRIORITY                0
56
57 /* A thread */
58 typedef void * objc_thread_t;
59
60 /* This structure represents a single mutual exclusion lock. */
61 struct objc_mutex
62 {
63   volatile objc_thread_t owner;     /* Id of thread that owns. */
64   volatile int depth;               /* # of acquires. */
65   void * backend;                   /* Specific to backend */
66 };
67 typedef struct objc_mutex *objc_mutex_t;
68
69 /* This structure represents a single condition mutex */
70 struct objc_condition
71 {
72   void * backend;                   /* Specific to backend */
73 };
74 typedef struct objc_condition *objc_condition_t;
75
76 /* Frontend mutex functions */
77 objc_mutex_t objc_mutex_allocate(void);
78 int objc_mutex_deallocate(objc_mutex_t mutex);
79 int objc_mutex_lock(objc_mutex_t mutex);
80 int objc_mutex_unlock(objc_mutex_t mutex);
81 int objc_mutex_trylock(objc_mutex_t mutex);
82
83 /* Frontend condition mutex functions */
84 objc_condition_t objc_condition_allocate(void);
85 int objc_condition_deallocate(objc_condition_t condition);
86 int objc_condition_wait(objc_condition_t condition, objc_mutex_t mutex);
87 int objc_condition_signal(objc_condition_t condition);
88 int objc_condition_broadcast(objc_condition_t condition);
89
90 /* Frontend thread functions */
91 objc_thread_t objc_thread_detach(SEL selector, id object, id argument);
92 void objc_thread_yield(void);
93 int objc_thread_exit(void);
94 int objc_thread_set_priority(int priority);
95 int objc_thread_get_priority(void);
96 void * objc_thread_get_data(void);
97 int objc_thread_set_data(void *value);
98 objc_thread_t objc_thread_id(void);
99
100 /*
101   Use this to set the hook function that will be called when the 
102   runtime initially becomes multi threaded.
103   The hook function is only called once, meaning only when the 
104   2nd thread is spawned, not for each and every thread.
105
106   It returns the previous hook function or NULL if there is none.
107
108   A program outside of the runtime could set this to some function so
109   it can be informed; for example, the GNUstep Base Library sets it 
110   so it can implement the NSBecomingMultiThreaded notification.
111   */
112 typedef void (*objc_thread_callback)();
113 objc_thread_callback objc_set_thread_callback(objc_thread_callback func);
114
115 /* Backend initialization functions */
116 int __objc_init_thread_system(void);
117 int __objc_fini_thread_system(void);
118
119 /* Backend mutex functions */
120 int __objc_mutex_allocate(objc_mutex_t mutex);
121 int __objc_mutex_deallocate(objc_mutex_t mutex);
122 int __objc_mutex_lock(objc_mutex_t mutex);
123 int __objc_mutex_trylock(objc_mutex_t mutex);
124 int __objc_mutex_unlock(objc_mutex_t mutex);
125
126 /* Backend condition mutex functions */
127 int __objc_condition_allocate(objc_condition_t condition);
128 int __objc_condition_deallocate(objc_condition_t condition);
129 int __objc_condition_wait(objc_condition_t condition, objc_mutex_t mutex);
130 int __objc_condition_broadcast(objc_condition_t condition);
131 int __objc_condition_signal(objc_condition_t condition);
132
133 /* Backend thread functions */
134 objc_thread_t __objc_thread_detach(void (*func)(void *arg), void *arg);
135 int __objc_thread_set_priority(int priority);
136 int __objc_thread_get_priority(void);
137 void __objc_thread_yield(void);
138 int __objc_thread_exit(void);
139 objc_thread_t __objc_thread_id(void);
140 int __objc_thread_set_data(void *value);
141 void * __objc_thread_get_data(void);
142
143 #endif /* not __thread_INCLUDE_GNU */