OSDN Git Service

* doc/install.texi (Specific, i?86-*-solaris2.10): Fix grammar.
[pf3gnuchains/gcc-fork.git] / libobjc / thr-pthreads.c
1 /* GNU Objective C Runtime Thread Implementation for PCThreads under GNU/Linux.
2    Copyright (C) 1996, 1997, 2009 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 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 <pcthread.h>
28 #include "objc/thr.h"
29 #include "objc/runtime.h"
30
31 /* Key structure for maintaining thread specific storage */
32 static pthread_key_t _objc_thread_storage;
33
34 /* Backend initialization functions */
35
36 /* Initialize the threads subsystem. */
37 int
38 __objc_init_thread_system(void)
39 {
40   /* Initialize the thread storage key */
41   return pthread_key_create(&_objc_thread_storage, NULL);
42 }
43
44 /* Close the threads subsystem. */
45 int
46 __objc_close_thread_system(void)
47 {
48   /* Destroy the thread storage key */
49   /* Not implemented yet */
50   /* return pthread_key_delete(&_objc_thread_storage); */
51   return 0;
52 }
53
54 /* Backend thread functions */
55
56 /* Create a new thread of execution. */
57 objc_thread_t
58 __objc_thread_detach(void (*func)(void *arg), void *arg)
59 {
60   objc_thread_t thread_id;
61   pthread_t new_thread_handle;
62
63   if ( !(pthread_create(&new_thread_handle, NULL, (void *)func, arg)) )
64       thread_id = *(objc_thread_t *)&new_thread_handle;
65   else
66     thread_id = NULL;
67   
68   return thread_id;
69 }
70
71 /* Set the current thread's priority. */
72 int
73 __objc_thread_set_priority(int priority)
74 {
75   /* Not implemented yet */
76   return -1;
77 }
78
79 /* Return the current thread's priority. */
80 int
81 __objc_thread_get_priority(void)
82 {
83   /* Not implemented yet */
84   return OBJC_THREAD_INTERACTIVE_PRIORITY;
85 }
86
87 /* Yield our process time to another thread. */
88 void
89 __objc_thread_yield(void)
90 {
91   pthread_yield(NULL);
92 }
93
94 /* Terminate the current thread. */
95 int
96 __objc_thread_exit(void)
97 {
98   /* exit the thread */
99   pthread_exit(&__objc_thread_exit_status);
100
101   /* Failed if we reached here */
102   return -1;
103 }
104
105 /* Returns an integer value which uniquely describes a thread. */
106 objc_thread_t
107 __objc_thread_id(void)
108 {
109   pthread_t self = pthread_self();
110
111   return *(objc_thread_t *)&self;
112 }
113
114 /* Sets the thread's local storage pointer. */
115 int
116 __objc_thread_set_data(void *value)
117 {
118   return pthread_setspecific(_objc_thread_storage, value);
119 }
120
121 /* Returns the thread's local storage pointer. */
122 void *
123 __objc_thread_get_data(void)
124 {
125   void *value = NULL;
126
127   if ( !(pthread_getspecific(_objc_thread_storage, &value)) )
128     return value;
129
130   return NULL;
131 }
132
133 /* Backend mutex functions */
134
135 /* Allocate a mutex. */
136 int
137 __objc_mutex_allocate(objc_mutex_t mutex)
138 {
139   if (pthread_mutex_init((pthread_mutex_t *)(&(mutex->backend)), NULL))
140     return -1;
141   else
142     return 0;
143 }
144
145 /* Deallocate a mutex. */
146 int
147 __objc_mutex_deallocate(objc_mutex_t mutex)
148 {
149   if (pthread_mutex_destroy((pthread_mutex_t *)(&(mutex->backend))))
150     return -1;
151   else
152     return 0;
153 }
154
155 /* Grab a lock on a mutex. */
156 int
157 __objc_mutex_lock(objc_mutex_t mutex)
158 {
159   return pthread_mutex_lock((pthread_mutex_t *)(&(mutex->backend)));
160 }
161
162 /* Try to grab a lock on a mutex. */
163 int
164 __objc_mutex_trylock(objc_mutex_t mutex)
165 {
166   return pthread_mutex_trylock((pthread_mutex_t *)(&(mutex->backend)));
167 }
168
169 /* Unlock the mutex */
170 int
171 __objc_mutex_unlock(objc_mutex_t mutex)
172 {
173   return pthread_mutex_unlock((pthread_mutex_t *)(&(mutex->backend)));
174 }
175
176 /* Backend condition mutex functions */
177
178 /* Allocate a condition. */
179 int
180 __objc_condition_allocate(objc_condition_t condition)
181 {
182   if (pthread_cond_init((pthread_cond_t *)(&(condition->backend)), NULL))
183     return -1;
184   else
185     return 0;
186 }
187
188 /* Deallocate a condition. */
189 int
190 __objc_condition_deallocate(objc_condition_t condition)
191 {
192   return pthread_cond_destroy((pthread_cond_t *)(&(condition->backend)));
193 }
194
195 /* Wait on the condition */
196 int
197 __objc_condition_wait(objc_condition_t condition, objc_mutex_t mutex)
198 {
199   return pthread_cond_wait((pthread_cond_t *)(&(condition->backend)),
200                            (pthread_mutex_t *)(&(mutex->backend)));
201 }
202
203 /* Wake up all threads waiting on this condition. */
204 int
205 __objc_condition_broadcast(objc_condition_t condition)
206 {
207   return pthread_cond_broadcast((pthread_cond_t *)(&(condition->backend)));
208 }
209
210 /* Wake up one thread waiting on this condition. */
211 int
212 __objc_condition_signal(objc_condition_t condition)
213 {
214   return pthread_cond_signal((pthread_cond_t *)(&(condition->backend)));
215 }
216
217 /* End of File */