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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-stable/] [newlib-1.18.0/] [newlib/] [libc/] [sys/] [linux/] [linuxthreads/] [mutex.c] - Blame information for rev 207

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

Line No. Rev Author Line
1 207 jeremybenn
/* Linuxthreads - a simple clone()-based implementation of Posix        */
2
/* threads for Linux.                                                   */
3
/* Copyright (C) 1996 Xavier Leroy (Xavier.Leroy@inria.fr)              */
4
/*                                                                      */
5
/* This program is free software; you can redistribute it and/or        */
6
/* modify it under the terms of the GNU Library General Public License  */
7
/* as published by the Free Software Foundation; either version 2       */
8
/* of the License, or (at your option) any later version.               */
9
/*                                                                      */
10
/* This program is distributed in the hope that it will be useful,      */
11
/* but WITHOUT ANY WARRANTY; without even the implied warranty of       */
12
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        */
13
/* GNU Library General Public License for more details.                 */
14
 
15
/* Mutexes */
16
 
17
#include <bits/libc-lock.h>
18
#include <errno.h>
19
#include <sched.h>
20
#include <stddef.h>
21
#include <limits.h>
22
#include "pthread.h"
23
#include "internals.h"
24
#include "spinlock.h"
25
#include "queue.h"
26
#include "restart.h"
27
 
28
int __pthread_mutex_init(pthread_mutex_t * mutex,
29
                       const pthread_mutexattr_t * mutex_attr)
30
{
31
  __pthread_init_lock(&mutex->__m_lock);
32
  mutex->__m_kind =
33
    mutex_attr == NULL ? PTHREAD_MUTEX_TIMED_NP : mutex_attr->__mutexkind;
34
  mutex->__m_count = 0;
35
  mutex->__m_owner = NULL;
36
  return 0;
37
}
38
strong_alias (__pthread_mutex_init, pthread_mutex_init)
39
 
40
int __pthread_mutex_destroy(pthread_mutex_t * mutex)
41
{
42
  switch (mutex->__m_kind) {
43
  case PTHREAD_MUTEX_ADAPTIVE_NP:
44
  case PTHREAD_MUTEX_RECURSIVE_NP:
45
    if ((mutex->__m_lock.__status & 1) != 0)
46
      return EBUSY;
47
    return 0;
48
  case PTHREAD_MUTEX_ERRORCHECK_NP:
49
  case PTHREAD_MUTEX_TIMED_NP:
50
    if (mutex->__m_lock.__status != 0)
51
      return EBUSY;
52
    return 0;
53
  default:
54
    return EINVAL;
55
  }
56
}
57
strong_alias (__pthread_mutex_destroy, pthread_mutex_destroy)
58
 
59
int __pthread_mutex_trylock(pthread_mutex_t * mutex)
60
{
61
  pthread_descr self;
62
  int retcode;
63
 
64
  switch(mutex->__m_kind) {
65
  case PTHREAD_MUTEX_ADAPTIVE_NP:
66
    retcode = __pthread_trylock(&mutex->__m_lock);
67
    return retcode;
68
  case PTHREAD_MUTEX_RECURSIVE_NP:
69
    self = thread_self();
70
    if (mutex->__m_owner == self) {
71
      mutex->__m_count++;
72
      return 0;
73
    }
74
    retcode = __pthread_trylock(&mutex->__m_lock);
75
    if (retcode == 0) {
76
      mutex->__m_owner = self;
77
      mutex->__m_count = 0;
78
    }
79
    return retcode;
80
  case PTHREAD_MUTEX_ERRORCHECK_NP:
81
    retcode = __pthread_alt_trylock(&mutex->__m_lock);
82
    if (retcode == 0) {
83
      mutex->__m_owner = thread_self();
84
    }
85
    return retcode;
86
  case PTHREAD_MUTEX_TIMED_NP:
87
    retcode = __pthread_alt_trylock(&mutex->__m_lock);
88
    return retcode;
89
  default:
90
    return EINVAL;
91
  }
92
}
93
strong_alias (__pthread_mutex_trylock, pthread_mutex_trylock)
94
 
95
int __pthread_mutex_lock(pthread_mutex_t * mutex)
96
{
97
  pthread_descr self;
98
 
99
  switch(mutex->__m_kind) {
100
  case PTHREAD_MUTEX_ADAPTIVE_NP:
101
    __pthread_lock(&mutex->__m_lock, NULL);
102
    return 0;
103
  case PTHREAD_MUTEX_RECURSIVE_NP:
104
    self = thread_self();
105
    if (mutex->__m_owner == self) {
106
      mutex->__m_count++;
107
      return 0;
108
    }
109
    __pthread_lock(&mutex->__m_lock, self);
110
    mutex->__m_owner = self;
111
    mutex->__m_count = 0;
112
    return 0;
113
  case PTHREAD_MUTEX_ERRORCHECK_NP:
114
    self = thread_self();
115
    if (mutex->__m_owner == self) return EDEADLK;
116
    __pthread_alt_lock(&mutex->__m_lock, self);
117
    mutex->__m_owner = self;
118
    return 0;
119
  case PTHREAD_MUTEX_TIMED_NP:
120
    __pthread_alt_lock(&mutex->__m_lock, NULL);
121
    return 0;
122
  default:
123
    return EINVAL;
124
  }
125
}
126
strong_alias (__pthread_mutex_lock, pthread_mutex_lock)
127
 
128
int __pthread_mutex_timedlock (pthread_mutex_t *mutex,
129
                               const struct timespec *abstime)
130
{
131
  pthread_descr self;
132
  int res;
133
 
134
  if (__builtin_expect (abstime->tv_nsec, 0) < 0
135
      || __builtin_expect (abstime->tv_nsec, 0) >= 1000000000)
136
    return EINVAL;
137
 
138
  switch(mutex->__m_kind) {
139
  case PTHREAD_MUTEX_ADAPTIVE_NP:
140
    __pthread_lock(&mutex->__m_lock, NULL);
141
    return 0;
142
  case PTHREAD_MUTEX_RECURSIVE_NP:
143
    self = thread_self();
144
    if (mutex->__m_owner == self) {
145
      mutex->__m_count++;
146
      return 0;
147
    }
148
    __pthread_lock(&mutex->__m_lock, self);
149
    mutex->__m_owner = self;
150
    mutex->__m_count = 0;
151
    return 0;
152
  case PTHREAD_MUTEX_ERRORCHECK_NP:
153
    self = thread_self();
154
    if (mutex->__m_owner == self) return EDEADLK;
155
    res = __pthread_alt_timedlock(&mutex->__m_lock, self, abstime);
156
    if (res != 0)
157
      {
158
        mutex->__m_owner = self;
159
        return 0;
160
      }
161
    return ETIMEDOUT;
162
  case PTHREAD_MUTEX_TIMED_NP:
163
    /* Only this type supports timed out lock. */
164
    return (__pthread_alt_timedlock(&mutex->__m_lock, NULL, abstime)
165
            ? 0 : ETIMEDOUT);
166
  default:
167
    return EINVAL;
168
  }
169
}
170
strong_alias (__pthread_mutex_timedlock, pthread_mutex_timedlock)
171
 
172
int __pthread_mutex_unlock(pthread_mutex_t * mutex)
173
{
174
  switch (mutex->__m_kind) {
175
  case PTHREAD_MUTEX_ADAPTIVE_NP:
176
    __pthread_unlock(&mutex->__m_lock);
177
    return 0;
178
  case PTHREAD_MUTEX_RECURSIVE_NP:
179
    if (mutex->__m_owner != thread_self())
180
      return EPERM;
181
    if (mutex->__m_count > 0) {
182
      mutex->__m_count--;
183
      return 0;
184
    }
185
    mutex->__m_owner = NULL;
186
    __pthread_unlock(&mutex->__m_lock);
187
    return 0;
188
  case PTHREAD_MUTEX_ERRORCHECK_NP:
189
    if (mutex->__m_owner != thread_self() || mutex->__m_lock.__status == 0)
190
      return EPERM;
191
    mutex->__m_owner = NULL;
192
    __pthread_alt_unlock(&mutex->__m_lock);
193
    return 0;
194
  case PTHREAD_MUTEX_TIMED_NP:
195
    __pthread_alt_unlock(&mutex->__m_lock);
196
    return 0;
197
  default:
198
    return EINVAL;
199
  }
200
}
201
strong_alias (__pthread_mutex_unlock, pthread_mutex_unlock)
202
 
203
int __pthread_mutexattr_init(pthread_mutexattr_t *attr)
204
{
205
  attr->__mutexkind = PTHREAD_MUTEX_TIMED_NP;
206
  return 0;
207
}
208
strong_alias (__pthread_mutexattr_init, pthread_mutexattr_init)
209
 
210
int __pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
211
{
212
  return 0;
213
}
214
strong_alias (__pthread_mutexattr_destroy, pthread_mutexattr_destroy)
215
 
216
int __pthread_mutexattr_settype(pthread_mutexattr_t *attr, int kind)
217
{
218
  if (kind != PTHREAD_MUTEX_ADAPTIVE_NP
219
      && kind != PTHREAD_MUTEX_RECURSIVE_NP
220
      && kind != PTHREAD_MUTEX_ERRORCHECK_NP
221
      && kind != PTHREAD_MUTEX_TIMED_NP)
222
    return EINVAL;
223
  attr->__mutexkind = kind;
224
  return 0;
225
}
226
weak_alias (__pthread_mutexattr_settype, pthread_mutexattr_settype)
227
#if !defined(_ELIX_LEVEL) || _ELIX_LEVEL >= 2
228
strong_alias ( __pthread_mutexattr_settype, __pthread_mutexattr_setkind_np)
229
weak_alias (__pthread_mutexattr_setkind_np, pthread_mutexattr_setkind_np)
230
#endif /* !_ELIX_LEVEL || _ELIX_LEVEL >= 2 */
231
 
232
int __pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *kind)
233
{
234
  *kind = attr->__mutexkind;
235
  return 0;
236
}
237
weak_alias (__pthread_mutexattr_gettype, pthread_mutexattr_gettype)
238
#if !defined(_ELIX_LEVEL) || _ELIX_LEVEL >= 2
239
strong_alias (__pthread_mutexattr_gettype, __pthread_mutexattr_getkind_np)
240
weak_alias (__pthread_mutexattr_getkind_np, pthread_mutexattr_getkind_np)
241
#endif /* !_ELIX_LEVEL || _ELIX_LEVEL >= 2 */
242
 
243
 
244
#if !defined(_ELIX_LEVEL) || _ELIX_LEVEL >= 3
245
 
246
int __pthread_mutexattr_getpshared (const pthread_mutexattr_t *attr,
247
                                   int *pshared)
248
{
249
  *pshared = PTHREAD_PROCESS_PRIVATE;
250
  return 0;
251
}
252
weak_alias (__pthread_mutexattr_getpshared, pthread_mutexattr_getpshared)
253
 
254
int __pthread_mutexattr_setpshared (pthread_mutexattr_t *attr, int pshared)
255
{
256
  if (pshared != PTHREAD_PROCESS_PRIVATE && pshared != PTHREAD_PROCESS_SHARED)
257
    return EINVAL;
258
 
259
  /* For now it is not possible to shared a conditional variable.  */
260
  if (pshared != PTHREAD_PROCESS_PRIVATE)
261
    return ENOSYS;
262
 
263
  return 0;
264
}
265
weak_alias (__pthread_mutexattr_setpshared, pthread_mutexattr_setpshared)
266
 
267
#endif /* !_ELIX_LEVEL || _ELIX_LEVEL >= 3 */
268
 
269
/* Once-only execution */
270
 
271
static pthread_mutex_t once_masterlock = PTHREAD_MUTEX_INITIALIZER;
272
static pthread_cond_t once_finished = PTHREAD_COND_INITIALIZER;
273
static int fork_generation = 0;  /* Child process increments this after fork. */
274
 
275
enum { NEVER = 0, IN_PROGRESS = 1, DONE = 2 };
276
 
277
/* If a thread is canceled while calling the init_routine out of
278
   pthread once, this handler will reset the once_control variable
279
   to the NEVER state. */
280
 
281
static void pthread_once_cancelhandler(void *arg)
282
{
283
    pthread_once_t *once_control = arg;
284
 
285
    pthread_mutex_lock(&once_masterlock);
286
    *once_control = NEVER;
287
    pthread_mutex_unlock(&once_masterlock);
288
    pthread_cond_broadcast(&once_finished);
289
}
290
 
291
int __pthread_once(pthread_once_t * once_control, void (*init_routine)(void))
292
{
293
  /* flag for doing the condition broadcast outside of mutex */
294
  int state_changed;
295
 
296
  /* Test without locking first for speed */
297
  if (*once_control == DONE) {
298
    READ_MEMORY_BARRIER();
299
    return 0;
300
  }
301
  /* Lock and test again */
302
 
303
  state_changed = 0;
304
 
305
  pthread_mutex_lock(&once_masterlock);
306
 
307
  /* If this object was left in an IN_PROGRESS state in a parent
308
     process (indicated by stale generation field), reset it to NEVER. */
309
  if ((*once_control & 3) == IN_PROGRESS && (*once_control & ~3) != fork_generation)
310
    *once_control = NEVER;
311
 
312
  /* If init_routine is being called from another routine, wait until
313
     it completes. */
314
  while ((*once_control & 3) == IN_PROGRESS) {
315
    pthread_cond_wait(&once_finished, &once_masterlock);
316
  }
317
  /* Here *once_control is stable and either NEVER or DONE. */
318
  if (*once_control == NEVER) {
319
    *once_control = IN_PROGRESS | fork_generation;
320
    pthread_mutex_unlock(&once_masterlock);
321
    pthread_cleanup_push(pthread_once_cancelhandler, once_control);
322
    init_routine();
323
    pthread_cleanup_pop(0);
324
    pthread_mutex_lock(&once_masterlock);
325
    WRITE_MEMORY_BARRIER();
326
    *once_control = DONE;
327
    state_changed = 1;
328
  }
329
  pthread_mutex_unlock(&once_masterlock);
330
 
331
  if (state_changed)
332
    pthread_cond_broadcast(&once_finished);
333
 
334
  return 0;
335
}
336
strong_alias (__pthread_once, pthread_once)
337
 
338
/*
339
 * Handle the state of the pthread_once mechanism across forks.  The
340
 * once_masterlock is acquired in the parent process prior to a fork to ensure
341
 * that no thread is in the critical region protected by the lock.  After the
342
 * fork, the lock is released. In the child, the lock and the condition
343
 * variable are simply reset.  The child also increments its generation
344
 * counter which lets pthread_once calls detect stale IN_PROGRESS states
345
 * and reset them back to NEVER.
346
 */
347
 
348
void __pthread_once_fork_prepare(void)
349
{
350
  pthread_mutex_lock(&once_masterlock);
351
}
352
 
353
void __pthread_once_fork_parent(void)
354
{
355
  pthread_mutex_unlock(&once_masterlock);
356
}
357
 
358
void __pthread_once_fork_child(void)
359
{
360
  pthread_mutex_init(&once_masterlock, NULL);
361
  pthread_cond_init(&once_finished, NULL);
362
  if (fork_generation <= INT_MAX - 4)
363
    fork_generation += 4;       /* leave least significant two bits zero */
364
  else
365
    fork_generation = 0;
366
}

powered by: WebSVN 2.1.0

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