OSDN Git Service

Add NIOS2 support. Code from SourceyG++.
[pf3gnuchains/gcc-fork.git] / libobjc / thr-dce.c
1 /* GNU Objective C Runtime Thread Interface
2    Copyright (C) 1996, 1997, 2009 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
26 #include <pthread.h>
27 #include "objc/thr.h"
28 #include "objc/runtime.h"
29
30 /* Key structure for maintaining thread specific storage */
31 static pthread_key_t _objc_thread_storage;
32
33 /* Backend initialization functions */
34
35 /* Initialize the threads subsystem. */
36 int
37 __objc_init_thread_system(void)
38 {
39   /* Initialize the thread storage key */
40   return pthread_keycreate(&_objc_thread_storage, NULL);
41 }
42
43 /* Close the threads subsystem. */
44 int
45 __objc_close_thread_system(void)
46 {
47   /* Destroy the thread storage key */
48   /* Not implemented yet */
49   /* return pthread_key_delete(&_objc_thread_storage); */
50   return 0;
51 }
52
53 /* Backend thread functions */
54
55 /* Create a new thread of execution. */
56 objc_thread_t
57 __objc_thread_detach(void (*func)(void *arg), void *arg)
58 {
59   objc_thread_t thread_id;
60   pthread_t new_thread_handle;
61
62   if (pthread_create(&new_thread_handle, pthread_attr_default,
63                      (void *)func, arg) == 0)
64     {
65       /* ??? May not work! (64bit) */
66       thread_id = *(objc_thread_t *)&new_thread_handle; 
67       pthread_detach(&new_thread_handle);     /* Fully detach thread.     */
68     }
69   else
70     thread_id = NULL;
71   
72   return thread_id;
73 }
74
75 /* Set the current thread's priority. */
76 int
77 __objc_thread_set_priority(int priority)
78 {
79   int sys_priority = 0;
80
81   switch (priority)
82     {
83     case OBJC_THREAD_INTERACTIVE_PRIORITY:
84       sys_priority = (PRI_FG_MIN_NP + PRI_FG_MAX_NP) / 2;
85       break;
86     default:
87     case OBJC_THREAD_BACKGROUND_PRIORITY:
88       sys_priority = (PRI_BG_MIN_NP + PRI_BG_MAX_NP) / 2;
89       break;
90     case OBJC_THREAD_LOW_PRIORITY:
91       sys_priority = (PRI_BG_MIN_NP + PRI_BG_MAX_NP) / 2;
92       break;
93     }
94     
95   /* Change the priority. */
96   if (pthread_setprio(pthread_self(), sys_priority) >= 0)
97     return 0;
98   else
99     /* Failed */
100     return -1;
101 }
102
103 /* Return the current thread's priority. */
104 int
105 __objc_thread_get_priority(void)
106 {
107   int sys_priority;
108     
109   if ((sys_priority = pthread_getprio(pthread_self())) >= 0) {
110     if (sys_priority >= PRI_FG_MIN_NP && sys_priority <= PRI_FG_MAX_NP)
111       return OBJC_THREAD_INTERACTIVE_PRIORITY;
112     if (sys_priority >= PRI_BG_MIN_NP && sys_priority <= PRI_BG_MAX_NP)
113       return OBJC_THREAD_BACKGROUND_PRIORITY;
114     return OBJC_THREAD_LOW_PRIORITY;
115   }
116
117   /* Failed */
118   return -1;
119 }
120
121 /* Yield our process time to another thread. */
122 void
123 __objc_thread_yield(void)
124 {
125   pthread_yield();
126 }
127
128 /* Terminate the current thread. */
129 int
130 __objc_thread_exit(void)
131 {
132   /* exit the thread */
133   pthread_exit(&__objc_thread_exit_status);
134
135   /* Failed if we reached here */
136   return -1;
137 }
138
139 /* Returns an integer value which uniquely describes a thread. */
140 objc_thread_t
141 __objc_thread_id(void)
142 {
143   pthread_t self = pthread_self();
144
145   return (objc_thread_t) pthread_getunique_np (&self);
146 }
147
148 /* Sets the thread's local storage pointer. */
149 int
150 __objc_thread_set_data(void *value)
151 {
152   return pthread_setspecific(_objc_thread_storage, value);
153 }
154
155 /* Returns the thread's local storage pointer. */
156 void *
157 __objc_thread_get_data(void)
158 {
159   void *value = NULL;
160
161   if ( !(pthread_getspecific(_objc_thread_storage, &value)) )
162     return value;
163
164   return NULL;
165 }
166
167 /* Backend mutex functions */
168
169 /* Allocate a mutex. */
170 int
171 __objc_mutex_allocate(objc_mutex_t mutex)
172 {
173   if (pthread_mutex_init((pthread_mutex_t *)(&(mutex->backend)), 
174                          pthread_mutexattr_default))
175     return -1;
176   else
177     return 0;
178 }
179
180 /* Deallocate a mutex. */
181 int
182 __objc_mutex_deallocate(objc_mutex_t mutex)
183 {
184   if (pthread_mutex_destroy((pthread_mutex_t *)(&(mutex->backend))))
185     return -1;
186   else
187     return 0;
188 }
189
190 /* Grab a lock on a mutex. */
191 int
192 __objc_mutex_lock(objc_mutex_t mutex)
193 {
194   return pthread_mutex_lock((pthread_mutex_t *)(&(mutex->backend)));
195 }
196
197 /* Try to grab a lock on a mutex. */
198 int
199 __objc_mutex_trylock(objc_mutex_t mutex)
200 {
201   if (pthread_mutex_trylock((pthread_mutex_t *)(&(mutex->backend))) != 1)
202     return -1;
203   else
204     return 0;
205 }
206
207 /* Unlock the mutex */
208 int
209 __objc_mutex_unlock(objc_mutex_t mutex)
210 {
211   return pthread_mutex_unlock((pthread_mutex_t *)(&(mutex->backend)));
212 }
213
214 /* Backend condition mutex functions */
215
216 /* Allocate a condition. */
217 int
218 __objc_condition_allocate(objc_condition_t condition)
219 {
220   /* Unimplemented. */
221   return -1;
222
223   /*
224   if (pthread_cond_init((pthread_cond_t *)(&(condition->backend)), NULL))
225     return -1;
226   else
227     return 0;
228     */
229 }
230
231 /* Deallocate a condition. */
232 int
233 __objc_condition_deallocate(objc_condition_t condition)
234 {
235   /* Unimplemented. */
236   return -1;
237
238   /*
239   return pthread_cond_destroy((pthread_cond_t *)(&(condition->backend)));
240   */
241 }
242
243 /* Wait on the condition */
244 int
245 __objc_condition_wait(objc_condition_t condition, objc_mutex_t mutex)
246 {
247   /* Unimplemented. */
248   return -1;
249
250   /*
251   return pthread_cond_wait((pthread_cond_t *)(&(condition->backend)),
252                            (pthread_mutex_t *)(&(mutex->backend)));
253                            */
254 }
255
256 /* Wake up all threads waiting on this condition. */
257 int
258 __objc_condition_broadcast(objc_condition_t condition)
259 {
260   /* Unimplemented. */
261   return -1;
262
263   /*
264   return pthread_cond_broadcast((pthread_cond_t *)(&(condition->backend)));
265   */
266 }
267
268 /* Wake up one thread waiting on this condition. */
269 int
270 __objc_condition_signal(objc_condition_t condition)
271 {
272   /* Unimplemented. */
273   return -1;
274
275   /*
276   return pthread_cond_signal((pthread_cond_t *)(&(condition->backend)));
277   */
278 }
279
280 /* End of File */