OpenCores
URL https://opencores.org/ocsvn/scarts/scarts/trunk

Subversion Repositories scarts

[/] [scarts/] [trunk/] [toolchain/] [scarts-gcc/] [gcc-4.1.1/] [libobjc/] [thr-posix.c] - Blame information for rev 19

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 14 jlechner
/* GNU Objective C Runtime Thread Interface for POSIX compliant threads
2
   Copyright (C) 1996, 1997 Free Software Foundation, Inc.
3
   Contributed by Galen C. Hunt (gchunt@cs.rochester.edu)
4
   Modified for Linux/Pthreads by Kai-Uwe Sattler (kus@iti.cs.uni-magdeburg.de)
5
   Modified for posix compliance by Chris Ball (cball@fmco.com)
6
 
7
This file is part of GCC.
8
 
9
GCC is free software; you can redistribute it and/or modify it under the
10
terms of the GNU General Public License as published by the Free Software
11
Foundation; either version 2, or (at your option) any later version.
12
 
13
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15
FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
16
details.
17
 
18
You should have received a copy of the GNU General Public License
19
along with GCC; see the file COPYING.  If not, write to
20
the Free Software Foundation, 51 Franklin Street, Fifth Floor,
21
Boston, MA 02110-1301, USA.  */
22
 
23
/* As a special exception, if you link this library with files compiled with
24
   GCC to produce an executable, this does not cause the resulting executable
25
   to be covered by the GNU General Public License. This exception does not
26
   however invalidate any other reasons why the executable file might be
27
   covered by the GNU General Public License.  */
28
 
29
#include "objc/thr.h"
30
#include "objc/runtime.h"
31
#include <pthread.h>
32
 
33
/* Key structure for maintaining thread specific storage */
34
static pthread_key_t _objc_thread_storage;
35
static pthread_attr_t _objc_thread_attribs;
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 (pthread_key_create(&_objc_thread_storage, NULL) == 0)
45
    {
46
      /*
47
       * The normal default detach state for threads is PTHREAD_CREATE_JOINABLE
48
       * which causes threads to not die when you think they should.
49
           */
50
      if (pthread_attr_init(&_objc_thread_attribs) == 0)
51
        {
52
          if (pthread_attr_setdetachstate(&_objc_thread_attribs,
53
                                          PTHREAD_CREATE_DETACHED) == 0)
54
            return 0;
55
        }
56
    }
57
 
58
  return -1;
59
}
60
 
61
/* Close the threads subsystem. */
62
int
63
__objc_close_thread_system(void)
64
{
65
  if (pthread_key_delete(_objc_thread_storage) == 0)
66
    {
67
      if (pthread_attr_destroy(&_objc_thread_attribs) == 0)
68
        return 0;
69
    }
70
 
71
  return -1;
72
}
73
 
74
/* Backend thread functions */
75
 
76
/* Create a new thread of execution. */
77
objc_thread_t
78
__objc_thread_detach(void (*func)(void *arg), void *arg)
79
{
80
  objc_thread_t thread_id;
81
  pthread_t new_thread_handle;
82
 
83
  if (!(pthread_create(&new_thread_handle, &_objc_thread_attribs,
84
                       (void *)func, arg)))
85
    thread_id = *(objc_thread_t *)&new_thread_handle;
86
  else
87
    thread_id = NULL;
88
 
89
  return thread_id;
90
}
91
 
92
/* Set the current thread's priority.
93
 *
94
 * Be aware that the default schedpolicy often disallows thread priorities.
95
 */
96
int
97
__objc_thread_set_priority(int priority)
98
{
99
  pthread_t thread_id = pthread_self();
100
  int policy;
101
  struct sched_param params;
102
  int priority_min, priority_max;
103
 
104
  if (pthread_getschedparam(thread_id, &policy, &params) == 0)
105
    {
106
      if ((priority_max = sched_get_priority_max(policy)) != 0)
107
        return -1;
108
 
109
      if ((priority_min = sched_get_priority_min(policy)) != 0)
110
        return -1;
111
 
112
      if (priority > priority_max)
113
        priority = priority_max;
114
      else if (priority < priority_min)
115
        priority = priority_min;
116
      params.sched_priority = priority;
117
 
118
      /*
119
       * The solaris 7 and several other man pages incorrectly state that
120
       * this should be a pointer to policy but pthread.h is universally
121
       * at odds with this.
122
       */
123
      if (pthread_setschedparam(thread_id, policy, &params) == 0)
124
        return 0;
125
    }
126
  return -1;
127
}
128
 
129
/* Return the current thread's priority. */
130
int
131
__objc_thread_get_priority(void)
132
{
133
  int policy;
134
  struct sched_param params;
135
 
136
  if (pthread_getschedparam(pthread_self(), &policy, &params) == 0)
137
    return params.sched_priority;
138
  else
139
    return -1;
140
}
141
 
142
/* Yield our process time to another thread. */
143
void
144
__objc_thread_yield(void)
145
{
146
  sched_yield();
147
}
148
 
149
/* Terminate the current thread. */
150
int
151
__objc_thread_exit(void)
152
{
153
  /* exit the thread */
154
  pthread_exit(&__objc_thread_exit_status);
155
 
156
  /* Failed if we reached here */
157
  return -1;
158
}
159
 
160
/* Returns an integer value which uniquely describes a thread. */
161
objc_thread_t
162
__objc_thread_id(void)
163
{
164
  pthread_t self = pthread_self();
165
 
166
  return *(objc_thread_t *)&self;
167
}
168
 
169
/* Sets the thread's local storage pointer. */
170
int
171
__objc_thread_set_data(void *value)
172
{
173
  if (pthread_setspecific(_objc_thread_storage, value) == 0)
174
    return 0;
175
  else
176
    return -1;
177
}
178
 
179
/* Returns the thread's local storage pointer. */
180
void *
181
__objc_thread_get_data(void)
182
{
183
  return pthread_getspecific(_objc_thread_storage);
184
}
185
 
186
/* Backend mutex functions */
187
 
188
/* Allocate a mutex. */
189
int
190
__objc_mutex_allocate(objc_mutex_t mutex)
191
{
192
  mutex->backend = objc_malloc(sizeof(pthread_mutex_t));
193
 
194
  if (pthread_mutex_init((pthread_mutex_t *)mutex->backend, NULL))
195
    {
196
      objc_free(mutex->backend);
197
      mutex->backend = NULL;
198
      return -1;
199
    }
200
 
201
  return 0;
202
}
203
 
204
/* Deallocate a mutex. */
205
int
206
__objc_mutex_deallocate(objc_mutex_t mutex)
207
{
208
  int count = 1;
209
 
210
  /*
211
   * Posix Threads specifically require that the thread be unlocked for
212
   * pthread_mutex_destroy to work.
213
   */
214
 
215
  while (count)
216
    {
217
      if ((count = pthread_mutex_unlock((pthread_mutex_t*)mutex->backend)) < 0)
218
        return -1;
219
    }
220
 
221
  if (pthread_mutex_destroy((pthread_mutex_t *)mutex->backend))
222
    return -1;
223
 
224
  objc_free(mutex->backend);
225
  mutex->backend = NULL;
226
  return 0;
227
}
228
 
229
/* Grab a lock on a mutex. */
230
int
231
__objc_mutex_lock(objc_mutex_t mutex)
232
{
233
  if (pthread_mutex_lock((pthread_mutex_t *)mutex->backend) == 0)
234
    return 0;
235
  else
236
    return -1;
237
}
238
 
239
/* Try to grab a lock on a mutex. */
240
int
241
__objc_mutex_trylock(objc_mutex_t mutex)
242
{
243
  if (pthread_mutex_trylock((pthread_mutex_t *)mutex->backend) == 0)
244
    return 0;
245
  else
246
    return -1;
247
}
248
 
249
/* Unlock the mutex */
250
int
251
__objc_mutex_unlock(objc_mutex_t mutex)
252
{
253
  if (pthread_mutex_unlock((pthread_mutex_t *)mutex->backend) == 0)
254
    return 0;
255
  else
256
    return -1;
257
}
258
 
259
/* Backend condition mutex functions */
260
 
261
/* Allocate a condition. */
262
int
263
__objc_condition_allocate(objc_condition_t condition)
264
{
265
  condition->backend = objc_malloc(sizeof(pthread_cond_t));
266
 
267
  if (pthread_cond_init((pthread_cond_t *)condition->backend, NULL))
268
    {
269
      objc_free(condition->backend);
270
      condition->backend = NULL;
271
      return -1;
272
    }
273
 
274
  return 0;
275
}
276
 
277
/* Deallocate a condition. */
278
int
279
__objc_condition_deallocate(objc_condition_t condition)
280
{
281
  if (pthread_cond_destroy((pthread_cond_t *)condition->backend))
282
    return -1;
283
 
284
  objc_free(condition->backend);
285
  condition->backend = NULL;
286
  return 0;
287
}
288
 
289
/* Wait on the condition */
290
int
291
__objc_condition_wait(objc_condition_t condition, objc_mutex_t mutex)
292
{
293
  if (pthread_cond_wait((pthread_cond_t *)condition->backend,
294
                        (pthread_mutex_t *)mutex->backend) == 0)
295
    return 0;
296
  else
297
    return -1;
298
}
299
 
300
/* Wake up all threads waiting on this condition. */
301
int
302
__objc_condition_broadcast(objc_condition_t condition)
303
{
304
  if (pthread_cond_broadcast((pthread_cond_t *)condition->backend) == 0)
305
    return 0;
306
  else
307
    return -1;
308
}
309
 
310
/* Wake up one thread waiting on this condition. */
311
int
312
__objc_condition_signal(objc_condition_t condition)
313
{
314
  if (pthread_cond_signal((pthread_cond_t *)condition->backend) == 0)
315
    return 0;
316
  else
317
    return -1;
318
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.