]>
Commit | Line | Data |
---|---|---|
88e17b57 BE |
1 | /* GNU Objective C Runtime Thread Implementation for PCThreads under GNU/Linux. |
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> | |
5 | ||
6 | This file is part of GNU CC. | |
7 | ||
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 | |
10 | Foundation; either version 2, or (at your option) any later version. | |
11 | ||
12 | GNU CC is distributed in the hope that it will be useful, but WITHOUT ANY | |
13 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | |
14 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | |
15 | details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with GNU CC; see the file COPYING. If not, write to | |
19 | the Free Software Foundation, 59 Temple Place - Suite 330, | |
20 | Boston, MA 02111-1307, USA. */ | |
21 | ||
22 | /* As a special exception, if you link this library with files compiled with | |
23 | GCC to produce an executable, this does not cause the resulting executable | |
24 | to be covered by the GNU General Public License. This exception does not | |
25 | however invalidate any other reasons why the executable file might be | |
26 | covered by the GNU General Public License. */ | |
27 | ||
28 | #include <pcthread.h> | |
29 | #include <objc/thr.h> | |
30 | #include "runtime.h" | |
31 | ||
32 | /* Key structure for maintaining thread specific storage */ | |
33 | static pthread_key_t _objc_thread_storage; | |
34 | ||
35 | /* Backend initialization functions */ | |
36 | ||
37 | /* Initialize the threads subsystem. */ | |
38 | int | |
39 | __objc_init_thread_system(void) | |
40 | { | |
41 | /* Initialize the thread storage key */ | |
42 | return pthread_key_create(&_objc_thread_storage, NULL); | |
43 | } | |
44 | ||
45 | /* Close the threads subsystem. */ | |
46 | int | |
47 | __objc_close_thread_system(void) | |
48 | { | |
49 | /* Destroy the thread storage key */ | |
50 | /* Not implemented yet */ | |
51 | /* return pthread_key_delete(&_objc_thread_storage); */ | |
52 | return 0; | |
53 | } | |
54 | ||
55 | /* Backend thread functions */ | |
56 | ||
57 | /* Create a new thread of execution. */ | |
58 | objc_thread_t | |
59 | __objc_thread_detach(void (*func)(void *arg), void *arg) | |
60 | { | |
61 | objc_thread_t thread_id; | |
62 | pthread_t new_thread_handle; | |
63 | ||
64 | if ( !(pthread_create(&new_thread_handle, NULL, (void *)func, arg)) ) | |
65 | thread_id = *(objc_thread_t *)&new_thread_handle; | |
66 | else | |
67 | thread_id = NULL; | |
68 | ||
69 | return thread_id; | |
70 | } | |
71 | ||
72 | /* Set the current thread's priority. */ | |
73 | int | |
74 | __objc_thread_set_priority(int priority) | |
75 | { | |
76 | /* Not implemented yet */ | |
77 | return -1; | |
78 | } | |
79 | ||
80 | /* Return the current thread's priority. */ | |
81 | int | |
82 | __objc_thread_get_priority(void) | |
83 | { | |
84 | /* Not implemented yet */ | |
85 | return OBJC_THREAD_INTERACTIVE_PRIORITY; | |
86 | } | |
87 | ||
88 | /* Yield our process time to another thread. */ | |
89 | void | |
90 | __objc_thread_yield(void) | |
91 | { | |
92 | pthread_yield(NULL); | |
93 | } | |
94 | ||
95 | /* Terminate the current thread. */ | |
96 | int | |
97 | __objc_thread_exit(void) | |
98 | { | |
99 | /* exit the thread */ | |
100 | pthread_exit(&__objc_thread_exit_status); | |
101 | ||
102 | /* Failed if we reached here */ | |
103 | return -1; | |
104 | } | |
105 | ||
106 | /* Returns an integer value which uniquely describes a thread. */ | |
107 | objc_thread_t | |
108 | __objc_thread_id(void) | |
109 | { | |
110 | pthread_t self = pthread_self(); | |
111 | ||
112 | return *(objc_thread_t *)&self; | |
113 | } | |
114 | ||
115 | /* Sets the thread's local storage pointer. */ | |
116 | int | |
117 | __objc_thread_set_data(void *value) | |
118 | { | |
119 | return pthread_setspecific(_objc_thread_storage, value); | |
120 | } | |
121 | ||
122 | /* Returns the thread's local storage pointer. */ | |
123 | void * | |
124 | __objc_thread_get_data(void) | |
125 | { | |
126 | void *value = NULL; | |
127 | ||
128 | if ( !(pthread_getspecific(_objc_thread_storage, &value)) ) | |
129 | return value; | |
130 | ||
131 | return NULL; | |
132 | } | |
133 | ||
134 | /* Backend mutex functions */ | |
135 | ||
136 | /* Allocate a mutex. */ | |
137 | int | |
138 | __objc_mutex_allocate(objc_mutex_t mutex) | |
139 | { | |
140 | if (pthread_mutex_init((pthread_mutex_t *)(&(mutex->backend)), NULL)) | |
141 | return -1; | |
142 | else | |
143 | return 0; | |
144 | } | |
145 | ||
146 | /* Deallocate a mutex. */ | |
147 | int | |
148 | __objc_mutex_deallocate(objc_mutex_t mutex) | |
149 | { | |
150 | if (pthread_mutex_destroy((pthread_mutex_t *)(&(mutex->backend)))) | |
151 | return -1; | |
152 | else | |
153 | return 0; | |
154 | } | |
155 | ||
156 | /* Grab a lock on a mutex. */ | |
157 | int | |
158 | __objc_mutex_lock(objc_mutex_t mutex) | |
159 | { | |
160 | return pthread_mutex_lock((pthread_mutex_t *)(&(mutex->backend))); | |
161 | } | |
162 | ||
163 | /* Try to grab a lock on a mutex. */ | |
164 | int | |
165 | __objc_mutex_trylock(objc_mutex_t mutex) | |
166 | { | |
167 | return pthread_mutex_trylock((pthread_mutex_t *)(&(mutex->backend))); | |
168 | } | |
169 | ||
170 | /* Unlock the mutex */ | |
171 | int | |
172 | __objc_mutex_unlock(objc_mutex_t mutex) | |
173 | { | |
174 | return pthread_mutex_unlock((pthread_mutex_t *)(&(mutex->backend))); | |
175 | } | |
176 | ||
177 | /* Backend condition mutex functions */ | |
178 | ||
179 | /* Allocate a condition. */ | |
180 | int | |
181 | __objc_condition_allocate(objc_condition_t condition) | |
182 | { | |
183 | if (pthread_cond_init((pthread_cond_t *)(&(condition->backend)), NULL)) | |
184 | return -1; | |
185 | else | |
186 | return 0; | |
187 | } | |
188 | ||
189 | /* Deallocate a condition. */ | |
190 | int | |
191 | __objc_condition_deallocate(objc_condition_t condition) | |
192 | { | |
193 | return pthread_cond_destroy((pthread_cond_t *)(&(condition->backend))); | |
194 | } | |
195 | ||
196 | /* Wait on the condition */ | |
197 | int | |
198 | __objc_condition_wait(objc_condition_t condition, objc_mutex_t mutex) | |
199 | { | |
200 | return pthread_cond_wait((pthread_cond_t *)(&(condition->backend)), | |
201 | (pthread_mutex_t *)(&(mutex->backend))); | |
202 | } | |
203 | ||
204 | /* Wake up all threads waiting on this condition. */ | |
205 | int | |
206 | __objc_condition_broadcast(objc_condition_t condition) | |
207 | { | |
208 | return pthread_cond_broadcast((pthread_cond_t *)(&(condition->backend))); | |
209 | } | |
210 | ||
211 | /* Wake up one thread waiting on this condition. */ | |
212 | int | |
213 | __objc_condition_signal(objc_condition_t condition) | |
214 | { | |
215 | return pthread_cond_signal((pthread_cond_t *)(&(condition->backend))); | |
216 | } | |
217 | ||
218 | /* End of File */ |