mono_native_tls_get_value that does not change LastError. (#15568)
[mono-project.git] / mono / utils / mono-coop-mutex.h
blob2aa9bc6c37b6392b8db815218be3029a7325f3cf
1 /**
2 * \file
3 */
5 #ifndef __MONO_COOP_MUTEX_H__
6 #define __MONO_COOP_MUTEX_H__
8 #include <config.h>
9 #include <glib.h>
11 #include "mono-os-mutex.h"
12 #include "mono-threads-api.h"
14 /* We put the OS sync primitives in struct, so the compiler will warn us if
15 * we use mono_os_(mutex|cond|sem)_... on MonoCoop(Mutex|Cond|Sem) structures */
17 typedef struct _MonoCoopMutex MonoCoopMutex;
18 struct _MonoCoopMutex {
19 mono_mutex_t m;
22 typedef struct _MonoCoopCond MonoCoopCond;
23 struct _MonoCoopCond {
24 mono_cond_t c;
27 static inline void
28 mono_coop_mutex_init (MonoCoopMutex *mutex)
30 mono_os_mutex_init (&mutex->m);
33 static inline void
34 mono_coop_mutex_init_recursive (MonoCoopMutex *mutex)
36 mono_os_mutex_init_recursive (&mutex->m);
39 static inline void
40 mono_coop_mutex_destroy (MonoCoopMutex *mutex)
42 mono_os_mutex_destroy (&mutex->m);
45 static inline void
46 mono_coop_mutex_lock (MonoCoopMutex *mutex)
48 /* Avoid thread state switch if lock is not contended */
49 if (mono_os_mutex_trylock (&mutex->m) == 0)
50 return;
52 MONO_ENTER_GC_SAFE;
54 mono_os_mutex_lock (&mutex->m);
56 MONO_EXIT_GC_SAFE;
59 static inline gint
60 mono_coop_mutex_trylock (MonoCoopMutex *mutex)
62 return mono_os_mutex_trylock (&mutex->m);
65 static inline void
66 mono_coop_mutex_unlock (MonoCoopMutex *mutex)
68 mono_os_mutex_unlock (&mutex->m);
71 static inline void
72 mono_coop_cond_init (MonoCoopCond *cond)
74 mono_os_cond_init (&cond->c);
77 static inline void
78 mono_coop_cond_destroy (MonoCoopCond *cond)
80 mono_os_cond_destroy (&cond->c);
83 static inline void
84 mono_coop_cond_wait (MonoCoopCond *cond, MonoCoopMutex *mutex)
86 MONO_ENTER_GC_SAFE;
88 mono_os_cond_wait (&cond->c, &mutex->m);
90 MONO_EXIT_GC_SAFE;
93 static inline gint
94 mono_coop_cond_timedwait (MonoCoopCond *cond, MonoCoopMutex *mutex, guint32 timeout_ms)
96 gint res;
98 MONO_ENTER_GC_SAFE;
100 res = mono_os_cond_timedwait (&cond->c, &mutex->m, timeout_ms);
102 MONO_EXIT_GC_SAFE;
104 return res;
107 static inline void
108 mono_coop_cond_signal (MonoCoopCond *cond)
111 * On glibc using NTPL (ie Linux with an underlying futex), signaling a
112 * condition variable can block in the __condvar_quiesce_and_switch_g1
113 * operation. So switch to GC Safe mode here.
115 MONO_ENTER_GC_SAFE;
116 mono_os_cond_signal (&cond->c);
117 MONO_EXIT_GC_SAFE;
120 static inline void
121 mono_coop_cond_broadcast (MonoCoopCond *cond)
124 * On glibc using NTPL (ie Linux with an underlying futex), signaling a
125 * condition variable can block in the __condvar_quiesce_and_switch_g1
126 * operation. So switch to GC Safe mode here.
128 MONO_ENTER_GC_SAFE;
129 mono_os_cond_broadcast (&cond->c);
130 MONO_EXIT_GC_SAFE;
133 #endif /* __MONO_COOP_MUTEX_H__ */