OSDN Git Service

* store-motion.c Do not include params.h
[pf3gnuchains/gcc-fork.git] / libobjc / thr-solaris.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    Conditions added by Mircea Oancea (mircea@first.elcom.pub.ro)
5       
6 This file is part of GCC.
7
8 GCC 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 3, or (at your option) any later version.
11
12 GCC 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 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
20
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
24 <http://www.gnu.org/licenses/>.  */
25
26
27 #include "objc/thr.h"
28 #include "objc/runtime.h"
29
30 #include <thread.h>
31 #include <synch.h>
32 #include <errno.h>
33
34 /* Key structure for maintaining thread specific storage */
35 static thread_key_t     __objc_thread_data_key;
36
37 /* Backend initialization functions */
38
39 /* Initialize the threads subsystem. */
40 int
41 __objc_init_thread_system(void)
42 {
43   /* Initialize the thread storage key */
44   if (thr_keycreate(&__objc_thread_data_key, NULL) == 0)
45     return 0;
46   else
47     return -1;
48 }
49
50 /* Close the threads subsystem. */
51 int
52 __objc_close_thread_system(void)
53 {
54   return 0;
55 }
56
57 /* Backend thread functions */
58
59 /* Create a new thread of execution. */
60 objc_thread_t
61 __objc_thread_detach(void (*func)(void *arg), void *arg)
62 {
63   objc_thread_t thread_id;
64   thread_t new_thread_id = 0;
65
66   if (thr_create(NULL, 0, (void *)func, arg,
67                  THR_DETACHED | THR_NEW_LWP,
68                  &new_thread_id) == 0)
69     thread_id = *(objc_thread_t *)&new_thread_id;
70   else
71     thread_id = NULL;
72   
73   return thread_id;
74 }
75
76 /* Set the current thread's priority. */
77 int
78 __objc_thread_set_priority(int priority)
79 {
80   int sys_priority = 0;
81
82   switch (priority)
83     {
84     case OBJC_THREAD_INTERACTIVE_PRIORITY:
85       sys_priority = 300;
86       break;
87     default:
88     case OBJC_THREAD_BACKGROUND_PRIORITY:
89       sys_priority = 200;
90       break;
91     case OBJC_THREAD_LOW_PRIORITY:
92       sys_priority = 1000;
93       break;
94     }
95
96   /* Change priority */
97   if (thr_setprio(thr_self(), sys_priority) == 0)
98     return 0;
99   else
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 (thr_getprio(thr_self(), &sys_priority) == 0)
110     {
111       if (sys_priority >= 250)
112         return OBJC_THREAD_INTERACTIVE_PRIORITY;
113       else if (sys_priority >= 150)
114         return OBJC_THREAD_BACKGROUND_PRIORITY;
115       return OBJC_THREAD_LOW_PRIORITY;
116     }
117
118   /* Couldn't get priority. */
119   return -1;
120 }
121
122 /* Yield our process time to another thread. */
123 void
124 __objc_thread_yield(void)
125 {
126   thr_yield();
127 }
128
129 /* Terminate the current thread. */
130 int
131 __objc_thread_exit(void)
132 {
133   /* exit the thread */
134   thr_exit(&__objc_thread_exit_status);
135
136   /* Failed if we reached here */
137   return -1;
138 }
139
140 /* Returns an integer value which uniquely describes a thread. */
141 objc_thread_t
142 __objc_thread_id(void)
143 {
144   return (objc_thread_t)thr_self();
145 }
146
147 /* Sets the thread's local storage pointer. */
148 int
149 __objc_thread_set_data(void *value)
150 {
151   if (thr_setspecific(__objc_thread_data_key, value) == 0)
152     return 0;
153   else
154     return -1;
155 }
156
157 /* Returns the thread's local storage pointer. */
158 void *
159 __objc_thread_get_data(void)
160 {
161   void *value = NULL;
162
163   if (thr_getspecific(__objc_thread_data_key, &value) == 0)
164     return value;
165
166   return NULL;
167 }
168
169 /* Backend mutex functions */
170
171 /* Allocate a mutex. */
172 int
173 __objc_mutex_allocate(objc_mutex_t mutex)
174 {
175   if (mutex_init( (mutex_t *)(&(mutex->backend)), USYNC_THREAD, 0))
176     return -1;
177   else
178     return 0;
179 }
180
181
182 /* Deallocate a mutex. */
183 int
184 __objc_mutex_deallocate(objc_mutex_t mutex)
185 {
186   mutex_destroy((mutex_t *)(&(mutex->backend)));
187   return 0;
188 }
189
190 /* Grab a lock on a mutex. */
191 int
192 __objc_mutex_lock(objc_mutex_t mutex)
193 {
194   if (mutex_lock((mutex_t *)(&(mutex->backend))) != 0)
195     return -1;
196   else
197     return 0;
198 }
199
200 /* Try to grab a lock on a mutex. */
201 int
202 __objc_mutex_trylock(objc_mutex_t mutex)
203 {
204   if (mutex_trylock((mutex_t *)(&(mutex->backend))) != 0)
205     return -1;
206   else
207     return 0;
208 }
209
210 /* Unlock the mutex */
211 int
212 __objc_mutex_unlock(objc_mutex_t mutex)
213 {
214   if (mutex_unlock((mutex_t *)(&(mutex->backend))) != 0)
215     return -1;
216   else
217     return 0;
218 }
219
220 /* Backend condition mutex functions */
221
222 /* Allocate a condition. */
223 int
224 __objc_condition_allocate(objc_condition_t condition)
225 {
226   return cond_init((cond_t *)(&(condition->backend)), USYNC_THREAD, NULL);
227 }
228
229 /* Deallocate a condition. */
230 int
231 __objc_condition_deallocate(objc_condition_t condition)
232 {
233   return cond_destroy((cond_t *)(&(condition->backend)));
234 }
235
236 /* Wait on the condition */
237 int
238 __objc_condition_wait(objc_condition_t condition, objc_mutex_t mutex)
239 {
240   return cond_wait((cond_t *)(&(condition->backend)),
241                    (mutex_t *)(&(mutex->backend)));
242 }
243
244 /* Wake up all threads waiting on this condition. */
245 int
246 __objc_condition_broadcast(objc_condition_t condition)
247 {
248   return cond_broadcast((cond_t *)(&(condition->backend)));
249 }
250
251 /* Wake up one thread waiting on this condition. */
252 int
253 __objc_condition_signal(objc_condition_t condition)
254 {
255   return cond_signal((cond_t *)(&(condition->backend)));
256 }
257
258 /* End of File */