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 = 10000; /* 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 */
69 mono_mutexattr_init (mono_mutexattr_t *attr)
71 memset (attr, 0, sizeof (mono_mutexattr_t));
76 mono_mutexattr_settype (mono_mutexattr_t *attr, int type)
83 mono_mutexattr_gettype (mono_mutexattr_t *attr, int *type)
90 mono_mutexattr_setpshared (mono_mutexattr_t *attr, int pshared)
92 attr->shared = pshared;
97 mono_mutexattr_getpshared (mono_mutexattr_t *attr, int *pshared)
99 *pshared = attr->shared;
104 mono_mutexattr_setprotocol (mono_mutexattr_t *attr, int protocol)
106 attr->protocol = protocol;
111 mono_mutexattr_getprotocol (mono_mutexattr_t *attr, int *protocol)
113 *protocol = attr->protocol;
118 mono_mutexattr_setprioceiling (mono_mutexattr_t *attr, int prioceiling)
120 attr->priority = prioceiling;
125 mono_mutexattr_getprioceiling (mono_mutexattr_t *attr, int *prioceiling)
127 *prioceiling = attr->priority;
132 mono_mutexattr_destroy (mono_mutexattr_t *attr)
139 mono_mutex_init (mono_mutex_t *mutex, const mono_mutexattr_t *attr)
143 mutex->owner = MONO_THREAD_NONE;
145 if (!attr || attr->type == MONO_MUTEX_NORMAL) {
146 mutex->type = MONO_MUTEX_NORMAL;
147 pthread_mutex_init (&mutex->mutex, NULL);
149 mutex->type = MONO_MUTEX_RECURSIVE;
150 pthread_mutex_init (&mutex->mutex, NULL);
151 pthread_cond_init (&mutex->cond, NULL);
158 mono_mutex_lock (mono_mutex_t *mutex)
162 switch (mutex->type) {
163 case MONO_MUTEX_NORMAL:
164 return pthread_mutex_lock (&mutex->mutex);
165 case MONO_MUTEX_RECURSIVE:
166 id = pthread_self ();
167 if (pthread_mutex_lock (&mutex->mutex) != 0)
171 if (mutex->owner == MONO_THREAD_NONE) {
175 } else if (mutex->owner == id) {
180 if (pthread_cond_wait (&mutex->cond, &mutex->mutex) == -1)
186 return pthread_mutex_unlock (&mutex->mutex);
193 mono_mutex_trylock (mono_mutex_t *mutex)
197 switch (mutex->type) {
198 case MONO_MUTEX_NORMAL:
199 return pthread_mutex_trylock (&mutex->mutex);
200 case MONO_MUTEX_RECURSIVE:
201 id = pthread_self ();
203 if (pthread_mutex_lock (&mutex->mutex) != 0)
206 if (mutex->owner != MONO_THREAD_NONE && mutex->owner != id) {
207 pthread_mutex_unlock (&mutex->mutex);
212 if (mutex->owner == MONO_THREAD_NONE) {
222 return pthread_mutex_unlock (&mutex->mutex);
229 mono_mutex_timedlock (mono_mutex_t *mutex, const struct timespec *timeout)
233 switch (mutex->type) {
234 case MONO_MUTEX_NORMAL:
235 return pthread_mutex_timedlock (&mutex->mutex, timeout);
236 case MONO_MUTEX_RECURSIVE:
237 id = pthread_self ();
239 if (pthread_mutex_timedlock (&mutex->mutex, timeout) != 0)
243 if (mutex->owner == MONO_THREAD_NONE) {
247 } else if (mutex->owner == id) {
252 if (pthread_cond_timedwait (&mutex->cond, &mutex->mutex, timeout) != 0)
258 return pthread_mutex_unlock (&mutex->mutex);
265 mono_mutex_unlock (mono_mutex_t *mutex)
267 switch (mutex->type) {
268 case MONO_MUTEX_NORMAL:
269 return pthread_mutex_unlock (&mutex->mutex);
270 case MONO_MUTEX_RECURSIVE:
271 if (pthread_mutex_lock (&mutex->mutex) != 0)
274 assert (mutex->owner == pthread_self ());
277 if (mutex->depth == 0) {
278 mutex->owner = MONO_THREAD_NONE;
279 if (mutex->waiters > 0)
280 pthread_cond_signal (&mutex->cond);
283 return pthread_mutex_unlock (&mutex->mutex);
290 mono_mutex_destroy (mono_mutex_t *mutex)
294 switch (mutex->type) {
295 case MONO_MUTEX_NORMAL:
296 ret = pthread_mutex_destroy (&mutex->mutex);
298 case MONO_MUTEX_RECURSIVE:
299 if ((ret = pthread_mutex_destroy (&mutex->mutex)) == 0) {
300 pthread_cond_destroy (&mutex->cond);
309 mono_cond_wait (pthread_cond_t *cond, mono_mutex_t *mutex)
311 return pthread_cond_wait (cond, &mutex->mutex);
315 mono_cond_timedwait (pthread_cond_t *cond, mono_mutex_t *mutex, const struct timespec *timeout)
317 return pthread_cond_timedwait (cond, &mutex->mutex, timeout);
320 #endif /* USE_MONO_MUTEX */