1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Authors: Jeffrey Stedfast <fejj@ximian.com>
5 * Copyright 2002 Ximain, Inc. (www.ximian.com)
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
34 #include "mono-mutex.h"
37 #ifndef HAVE_PTHREAD_MUTEX_TIMEDLOCK
39 pthread_mutex_timedlock (pthread_mutex_t *mutex, const struct timespec *timeout)
41 struct timeval timenow;
42 struct timespec sleepytime;
45 /* This is just to avoid a completely busy wait */
46 sleepytime.tv_sec = 0;
47 sleepytime.tv_nsec = 10000000; /* 10ms */
49 while ((retcode = pthread_mutex_trylock (mutex)) == EBUSY) {
50 gettimeofday (&timenow, NULL);
52 if (timenow.tv_sec >= timeout->tv_sec &&
53 (timenow.tv_usec * 1000) >= timeout->tv_nsec) {
57 nanosleep (&sleepytime, NULL);
62 #endif /* HAVE_PTHREAD_MUTEX_TIMEDLOCK */
66 mono_once (mono_once_t *once, void (*once_init) (void))
68 if (!once->complete) {
69 pthread_mutex_lock (&once->mutex);
70 if (!once->complete) {
72 once->complete = TRUE;
74 pthread_mutex_unlock (&once->mutex);
84 mono_mutexattr_init (mono_mutexattr_t *attr)
86 memset (attr, 0, sizeof (mono_mutexattr_t));
91 mono_mutexattr_settype (mono_mutexattr_t *attr, int type)
98 mono_mutexattr_gettype (mono_mutexattr_t *attr, int *type)
105 mono_mutexattr_setpshared (mono_mutexattr_t *attr, int pshared)
107 attr->shared = pshared;
112 mono_mutexattr_getpshared (mono_mutexattr_t *attr, int *pshared)
114 *pshared = attr->shared;
119 mono_mutexattr_setprotocol (mono_mutexattr_t *attr, int protocol)
121 attr->protocol = protocol;
126 mono_mutexattr_getprotocol (mono_mutexattr_t *attr, int *protocol)
128 *protocol = attr->protocol;
133 mono_mutexattr_setprioceiling (mono_mutexattr_t *attr, int prioceiling)
135 attr->priority = prioceiling;
140 mono_mutexattr_getprioceiling (mono_mutexattr_t *attr, int *prioceiling)
142 *prioceiling = attr->priority;
147 mono_mutexattr_destroy (mono_mutexattr_t *attr)
154 mono_mutex_init (mono_mutex_t *mutex, const mono_mutexattr_t *attr)
158 mutex->owner = MONO_THREAD_NONE;
160 if (!attr || attr->type == MONO_MUTEX_NORMAL) {
161 mutex->type = MONO_MUTEX_NORMAL;
162 pthread_mutex_init (&mutex->mutex, NULL);
164 mutex->type = MONO_MUTEX_RECURSIVE;
165 pthread_mutex_init (&mutex->mutex, NULL);
166 pthread_cond_init (&mutex->cond, NULL);
173 mono_mutex_lock (mono_mutex_t *mutex)
177 switch (mutex->type) {
178 case MONO_MUTEX_NORMAL:
179 return pthread_mutex_lock (&mutex->mutex);
180 case MONO_MUTEX_RECURSIVE:
181 id = pthread_self ();
182 if (pthread_mutex_lock (&mutex->mutex) != 0)
186 if (mutex->owner == MONO_THREAD_NONE) {
190 } else if (mutex->owner == id) {
195 if (pthread_cond_wait (&mutex->cond, &mutex->mutex) == -1)
201 return pthread_mutex_unlock (&mutex->mutex);
208 mono_mutex_trylock (mono_mutex_t *mutex)
212 switch (mutex->type) {
213 case MONO_MUTEX_NORMAL:
214 return pthread_mutex_trylock (&mutex->mutex);
215 case MONO_MUTEX_RECURSIVE:
216 id = pthread_self ();
218 if (pthread_mutex_lock (&mutex->mutex) != 0)
221 if (mutex->owner != MONO_THREAD_NONE && mutex->owner != id) {
222 pthread_mutex_unlock (&mutex->mutex);
227 if (mutex->owner == MONO_THREAD_NONE) {
237 return pthread_mutex_unlock (&mutex->mutex);
244 mono_mutex_timedlock (mono_mutex_t *mutex, const struct timespec *timeout)
248 switch (mutex->type) {
249 case MONO_MUTEX_NORMAL:
250 return pthread_mutex_timedlock (&mutex->mutex, timeout);
251 case MONO_MUTEX_RECURSIVE:
252 id = pthread_self ();
254 if (pthread_mutex_timedlock (&mutex->mutex, timeout) != 0)
258 if (mutex->owner == MONO_THREAD_NONE) {
262 } else if (mutex->owner == id) {
267 if (pthread_cond_timedwait (&mutex->cond, &mutex->mutex, timeout) != 0)
273 return pthread_mutex_unlock (&mutex->mutex);
280 mono_mutex_unlock (mono_mutex_t *mutex)
282 switch (mutex->type) {
283 case MONO_MUTEX_NORMAL:
284 return pthread_mutex_unlock (&mutex->mutex);
285 case MONO_MUTEX_RECURSIVE:
286 if (pthread_mutex_lock (&mutex->mutex) != 0)
289 assert (mutex->owner == pthread_self ());
292 if (mutex->depth == 0) {
293 mutex->owner = MONO_THREAD_NONE;
294 if (mutex->waiters > 0)
295 pthread_cond_signal (&mutex->cond);
298 return pthread_mutex_unlock (&mutex->mutex);
305 mono_mutex_destroy (mono_mutex_t *mutex)
309 switch (mutex->type) {
310 case MONO_MUTEX_NORMAL:
311 ret = pthread_mutex_destroy (&mutex->mutex);
313 case MONO_MUTEX_RECURSIVE:
314 if ((ret = pthread_mutex_destroy (&mutex->mutex)) == 0) {
315 pthread_cond_destroy (&mutex->cond);
324 mono_cond_wait (pthread_cond_t *cond, mono_mutex_t *mutex)
326 return pthread_cond_wait (cond, &mutex->mutex);
330 mono_cond_timedwait (pthread_cond_t *cond, mono_mutex_t *mutex, const struct timespec *timeout)
332 return pthread_cond_timedwait (cond, &mutex->mutex, timeout);
335 #endif /* USE_MONO_MUTEX */