1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * mono-mutex.h: Portability wrappers around POSIX Mutexes
5 * Authors: Jeffrey Stedfast <fejj@ximian.com>
7 * Copyright 2002 Ximian, Inc. (www.ximian.com)
20 #include <mono/utils/mono-memory-model.h>
22 #include "mono-mutex.h"
26 #if defined(__APPLE__)
27 #define _DARWIN_C_SOURCE
28 #include <pthread_spis.h>
32 #ifndef HAVE_PTHREAD_MUTEX_TIMEDLOCK
33 /* Android does not implement pthread_mutex_timedlock(), but does provide an
34 * unusual declaration: http://code.google.com/p/android/issues/detail?id=7807
36 #ifdef PLATFORM_ANDROID
39 #define CONST_NEEDED const
42 int pthread_mutex_timedlock (pthread_mutex_t *mutex,
43 CONST_NEEDED struct timespec *timeout);
45 pthread_mutex_timedlock (pthread_mutex_t *mutex, CONST_NEEDED struct timespec *timeout)
47 struct timeval timenow;
48 struct timespec sleepytime;
51 /* This is just to avoid a completely busy wait */
52 sleepytime.tv_sec = 0;
53 sleepytime.tv_nsec = 10000000; /* 10ms */
55 while ((retcode = pthread_mutex_trylock (mutex)) == EBUSY) {
56 gettimeofday (&timenow, NULL);
58 if (timenow.tv_sec >= timeout->tv_sec &&
59 (timenow.tv_usec * 1000) >= timeout->tv_nsec) {
63 nanosleep (&sleepytime, NULL);
68 #endif /* HAVE_PTHREAD_MUTEX_TIMEDLOCK */
72 mono_once (mono_once_t *once, void (*once_init) (void))
76 if (!once->complete) {
77 pthread_cleanup_push ((void(*)(void *))pthread_mutex_unlock,
78 (void *)&once->mutex);
79 thr_ret = pthread_mutex_lock (&once->mutex);
80 g_assert (thr_ret == 0);
82 if (!once->complete) {
84 once->complete = TRUE;
86 thr_ret = pthread_mutex_unlock (&once->mutex);
87 g_assert (thr_ret == 0);
89 pthread_cleanup_pop (0);
98 Returns a recursive mutex that is safe under suspension.
100 A suspension safe mutex means one that can handle this scenario:
113 Say (1) happens before (5) and (5) happens before (2).
114 This means that thread 2 was suspended by the kernel because
115 it's waiting on mutext M.
117 Thread 1 then proceed to suspend thread 2 and unlock/lock the
120 If the kernel implements mutexes with FIFO wait lists, this means
121 that thread 1 will be blocked waiting for thread 2 acquire the lock.
122 Since thread 2 is suspended, we have a deadlock.
124 A suspend safe mutex is an unfair lock but will schedule any runable
125 thread that is waiting for a the lock.
127 This problem was witnessed on OSX in mono/tests/thread-exit.cs.
131 mono_mutex_init_suspend_safe (mono_mutex_t *mutex)
133 #if defined(__APPLE__)
135 pthread_mutexattr_t attr;
136 static gboolean inited;
137 static int (*setpolicy_np) (pthread_mutexattr_t *, int);
140 setpolicy_np = dlsym (RTLD_NEXT, "pthread_mutexattr_setpolicy_np");
141 mono_atomic_store_release (&inited, TRUE);
144 pthread_mutexattr_init (&attr);
145 pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE);
147 setpolicy_np (&attr, _PTHREAD_MUTEX_POLICY_FIRSTFIT);
148 res = pthread_mutex_init (mutex, &attr);
149 pthread_mutexattr_destroy (&attr);
153 return mono_mutex_init (mutex);