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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [uClibc/] [libpthread/] [linuxthreads/] [mutex.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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