[utils] Add mutex and semaphore API to support coop suspend
authorLudovic Henry <ludovic@xamarin.com>
Fri, 13 Nov 2015 21:01:59 +0000 (21:01 +0000)
committerLudovic Henry <ludovic@xamarin.com>
Thu, 19 Nov 2015 18:30:14 +0000 (18:30 +0000)
Instead of having to use MONO_PREPARE_BLOCKING/MONO_TRY_BLOCKING in the
user code, we do it directly in this new mutex and semaphore coop API.

Using the mono_os_* locking functions should not be the default case, as
that could lead to longer than necessary time to suspend, and even in
certain cases to deadlocks.

mono/utils/Makefile.am
mono/utils/mono-coop-mutex.h [new file with mode: 0644]
mono/utils/mono-coop-semaphore.h [new file with mode: 0644]

index 73747f8042f56a48ececcf075acd919e0cc44313..c608c582b555f2b6041d2f8afee13ba3dbe2201e 100644 (file)
@@ -38,6 +38,7 @@ monoutils_sources = \
        mono-mmap.h             \
        mono-mmap-internals.h   \
        mono-os-mutex.h         \
+       mono-coop-mutex.h               \
        mono-once.h             \
        mono-lazy-init.h                \
        mono-networkinterfaces.c                \
@@ -54,6 +55,7 @@ monoutils_sources = \
        mono-poll.c             \
        mono-path.c             \
        mono-os-semaphore.h     \
+       mono-coop-semaphore.h           \
        mono-sigcontext.h       \
        mono-stdlib.c           \
        mono-property-hash.h    \
diff --git a/mono/utils/mono-coop-mutex.h b/mono/utils/mono-coop-mutex.h
new file mode 100644 (file)
index 0000000..95856fe
--- /dev/null
@@ -0,0 +1,128 @@
+
+#ifndef __MONO_COOP_MUTEX_H__
+#define __MONO_COOP_MUTEX_H__
+
+#include <config.h>
+#include <glib.h>
+
+#include "mono-os-mutex.h"
+#include "mono-threads.h"
+
+G_BEGIN_DECLS
+
+/* We put the OS sync primitives in struct, so the compiler will warn us if
+ * we use mono_os_(mutex|cond|sem)_... on MonoCoop(Mutex|Cond|Sem) structures */
+
+typedef struct _MonoCoopMutex MonoCoopMutex;
+struct _MonoCoopMutex {
+       mono_mutex_t m;
+};
+
+typedef struct _MonoCoopCond MonoCoopCond;
+struct _MonoCoopCond {
+       mono_cond_t c;
+};
+
+static inline gint
+mono_coop_mutex_init (MonoCoopMutex *mutex)
+{
+       return mono_os_mutex_init (&mutex->m);
+}
+
+static inline gint
+mono_coop_mutex_init_recursive (MonoCoopMutex *mutex)
+{
+       return mono_os_mutex_init_recursive (&mutex->m);
+}
+
+static inline gint
+mono_coop_mutex_destroy (MonoCoopMutex *mutex)
+{
+       return mono_os_mutex_destroy (&mutex->m);
+}
+
+static inline gint
+mono_coop_mutex_lock (MonoCoopMutex *mutex)
+{
+       gint res;
+
+       /* Avoid thread state switch if lock is not contended */
+       if (mono_os_mutex_trylock (&mutex->m) == 0)
+               return 0;
+
+       MONO_PREPARE_BLOCKING;
+
+       res = mono_os_mutex_lock (&mutex->m);
+
+       MONO_FINISH_BLOCKING;
+
+       return res;
+}
+
+static inline gint
+mono_coop_mutex_trylock (MonoCoopMutex *mutex)
+{
+       return mono_os_mutex_trylock (&mutex->m);
+}
+
+static inline gint
+mono_coop_mutex_unlock (MonoCoopMutex *mutex)
+{
+       return mono_os_mutex_unlock (&mutex->m);
+}
+
+static inline gint
+mono_coop_cond_init (MonoCoopCond *cond)
+{
+       return mono_os_cond_init (&cond->c);
+}
+
+static inline gint
+mono_coop_cond_destroy (MonoCoopCond *cond)
+{
+       return mono_os_cond_destroy (&cond->c);
+}
+
+static inline gint
+mono_coop_cond_wait (MonoCoopCond *cond, MonoCoopMutex *mutex)
+{
+       gint res;
+
+       MONO_PREPARE_BLOCKING;
+
+       res = mono_os_cond_wait (&cond->c, &mutex->m);
+
+       MONO_FINISH_BLOCKING;
+
+       return res;
+}
+
+static inline gint
+mono_coop_cond_timedwait (MonoCoopCond *cond, MonoCoopMutex *mutex, guint32 timeout_ms)
+{
+       gint res;
+
+       MONO_PREPARE_BLOCKING;
+
+       res = mono_os_cond_timedwait_ms (&cond->c, &mutex->m, timeout_ms);
+
+       MONO_FINISH_BLOCKING;
+
+       return res;
+}
+
+static inline gint
+mono_coop_cond_signal (MonoCoopCond *cond)
+{
+       return mono_os_cond_signal (&cond->c);
+}
+
+static inline gint
+mono_coop_cond_broadcast (MonoCoopCond *cond)
+{
+       return mono_os_cond_broadcast (&cond->c);
+}
+
+G_END_DECLS
+
+#endif /* __MONO_COOP_MUTEX_H__ */
diff --git a/mono/utils/mono-coop-semaphore.h b/mono/utils/mono-coop-semaphore.h
new file mode 100644 (file)
index 0000000..bcf45a2
--- /dev/null
@@ -0,0 +1,69 @@
+
+#ifndef __MONO_COOP_SEMAPHORE_H__
+#define __MONO_COOP_SEMAPHORE_H__
+
+#include <config.h>
+#include <glib.h>
+
+#include "mono-os-semaphore.h"
+#include "mono-threads.h"
+
+G_BEGIN_DECLS
+
+/* We put the OS sync primitives in struct, so the compiler will warn us if
+ * we use mono_os_(mutex|cond|sem)_... on MonoCoop(Mutex|Cond|Sem) structures */
+
+typedef struct _MonoCoopSem MonoCoopSem;
+struct _MonoCoopSem {
+       MonoSemType s;
+};
+
+static inline gint
+mono_coop_sem_init (MonoCoopSem *sem, int value)
+{
+       return mono_os_sem_init (&sem->s, value);
+}
+
+static inline gint
+mono_coop_sem_destroy (MonoCoopSem *sem)
+{
+       return mono_os_sem_destroy (&sem->s);
+}
+
+static inline gint
+mono_coop_sem_wait (MonoCoopSem *sem, MonoSemFlags flags)
+{
+       gint res;
+
+       MONO_PREPARE_BLOCKING;
+
+       res = mono_os_sem_wait (&sem->s, flags);
+
+       MONO_FINISH_BLOCKING;
+
+       return res;
+}
+
+static inline gint
+mono_coop_sem_timedwait (MonoCoopSem *sem, guint timeout_ms, MonoSemFlags flags)
+{
+       gint res;
+
+       MONO_PREPARE_BLOCKING;
+
+       res = mono_os_sem_timedwait (&sem->s, timeout_ms, flags);
+
+       MONO_FINISH_BLOCKING;
+
+       return res;
+}
+
+static inline gint
+mono_coop_sem_post (MonoCoopSem *sem)
+{
+       return mono_os_sem_post (&sem->s);
+}
+
+G_END_DECLS
+
+#endif /* __MONO_COOP_SEMAPHORE_H__ */