1 /* Linuxthreads - a simple clone()-based implementation of Posix */
2 /* threads for Linux. */
3 /* Copyright (C) 1996 Xavier Leroy (Xavier.Leroy@inria.fr) */
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. */
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. */
21 #include "internals.h"
26 int __pthread_mutex_init(pthread_mutex_t * mutex,
27 const pthread_mutexattr_t * mutex_attr)
29 mutex->m_spinlock = 0;
31 mutex->m_owner = NULL;
33 mutex_attr == NULL ? PTHREAD_MUTEX_FAST_NP : mutex_attr->mutexkind;
34 queue_init(&mutex->m_waiting);
37 weak_alias (__pthread_mutex_init, pthread_mutex_init)
39 int __pthread_mutex_destroy(pthread_mutex_t * mutex)
42 acquire(&mutex->m_spinlock);
43 count = mutex->m_count;
44 release(&mutex->m_spinlock);
45 if (count > 0) return EBUSY;
48 weak_alias (__pthread_mutex_destroy, pthread_mutex_destroy)
50 int __pthread_mutex_trylock(pthread_mutex_t * mutex)
54 acquire(&mutex->m_spinlock);
55 switch(mutex->m_kind) {
56 case PTHREAD_MUTEX_FAST_NP:
57 if (mutex->m_count == 0) {
59 release(&mutex->m_spinlock);
63 case PTHREAD_MUTEX_RECURSIVE_NP:
65 if (mutex->m_count == 0 || mutex->m_owner == self) {
67 mutex->m_owner = self;
68 release(&mutex->m_spinlock);
72 case PTHREAD_MUTEX_ERRORCHECK_NP:
74 if (mutex->m_count == 0) {
76 mutex->m_owner = self;
77 release(&mutex->m_spinlock);
82 release(&mutex->m_spinlock);
85 release(&mutex->m_spinlock);
88 weak_alias (__pthread_mutex_trylock, pthread_mutex_trylock)
90 int __pthread_mutex_lock(pthread_mutex_t * mutex)
95 acquire(&mutex->m_spinlock);
96 switch(mutex->m_kind) {
97 case PTHREAD_MUTEX_FAST_NP:
98 if (mutex->m_count == 0) {
100 release(&mutex->m_spinlock);
103 self = thread_self();
105 case PTHREAD_MUTEX_RECURSIVE_NP:
106 self = thread_self();
107 if (mutex->m_count == 0 || mutex->m_owner == self) {
109 mutex->m_owner = self;
110 release(&mutex->m_spinlock);
114 case PTHREAD_MUTEX_ERRORCHECK_NP:
115 self = thread_self();
116 if (mutex->m_count == 0) {
118 mutex->m_owner = self;
119 release(&mutex->m_spinlock);
121 } else if (mutex->m_owner == self) {
122 release(&mutex->m_spinlock);
127 release(&mutex->m_spinlock);
130 /* Suspend ourselves, then try again */
131 enqueue(&mutex->m_waiting, self);
132 release(&mutex->m_spinlock);
133 suspend(self); /* This is not a cancellation point */
136 weak_alias (__pthread_mutex_lock, pthread_mutex_lock)
138 int __pthread_mutex_unlock(pthread_mutex_t * mutex)
142 acquire(&mutex->m_spinlock);
143 switch (mutex->m_kind) {
144 case PTHREAD_MUTEX_FAST_NP:
147 case PTHREAD_MUTEX_RECURSIVE_NP:
149 if (mutex->m_count > 0) {
150 release(&mutex->m_spinlock);
153 mutex->m_count = 0; /* so that excess unlocks do not break everything */
155 case PTHREAD_MUTEX_ERRORCHECK_NP:
156 if (mutex->m_count == 0 || mutex->m_owner != thread_self()) {
157 release(&mutex->m_spinlock);
163 release(&mutex->m_spinlock);
166 th = dequeue(&mutex->m_waiting);
167 release(&mutex->m_spinlock);
168 if (th != NULL) restart(th);
171 weak_alias (__pthread_mutex_unlock, pthread_mutex_unlock)
173 int __pthread_mutexattr_init(pthread_mutexattr_t *attr)
175 attr->mutexkind = PTHREAD_MUTEX_FAST_NP;
178 weak_alias (__pthread_mutexattr_init, pthread_mutexattr_init)
180 int __pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
184 weak_alias (__pthread_mutexattr_destroy, pthread_mutexattr_destroy)
186 int __pthread_mutexattr_setkind_np(pthread_mutexattr_t *attr, int kind)
188 if (kind != PTHREAD_MUTEX_FAST_NP
189 && kind != PTHREAD_MUTEX_RECURSIVE_NP
190 && kind != PTHREAD_MUTEX_ERRORCHECK_NP)
192 attr->mutexkind = kind;
195 weak_alias (__pthread_mutexattr_setkind_np, pthread_mutexattr_setkind_np)
197 int __pthread_mutexattr_getkind_np(const pthread_mutexattr_t *attr, int *kind)
199 *kind = attr->mutexkind;
202 weak_alias (__pthread_mutexattr_getkind_np, pthread_mutexattr_getkind_np)
204 /* Once-only execution */
206 static pthread_mutex_t once_masterlock = PTHREAD_MUTEX_INITIALIZER;
207 static pthread_cond_t once_finished = PTHREAD_COND_INITIALIZER;
209 enum { NEVER = 0, IN_PROGRESS = 1, DONE = 2 };
211 int __pthread_once(pthread_once_t * once_control, void (*init_routine)(void))
213 /* Test without locking first for speed */
214 if (*once_control == DONE) return 0;
215 /* Lock and test again */
216 pthread_mutex_lock(&once_masterlock);
217 /* If init_routine is being called from another routine, wait until
219 while (*once_control == IN_PROGRESS) {
220 pthread_cond_wait(&once_finished, &once_masterlock);
222 /* Here *once_control is stable and either NEVER or DONE. */
223 if (*once_control == NEVER) {
224 *once_control = IN_PROGRESS;
225 pthread_mutex_unlock(&once_masterlock);
227 pthread_mutex_lock(&once_masterlock);
228 *once_control = DONE;
229 pthread_cond_broadcast(&once_finished);
231 pthread_mutex_unlock(&once_masterlock);
234 weak_alias (__pthread_once, pthread_once)