5 #ifndef __MONO_COOP_MUTEX_H__
6 #define __MONO_COOP_MUTEX_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
{
22 typedef struct _MonoCoopCond MonoCoopCond
;
23 struct _MonoCoopCond
{
28 mono_coop_mutex_init (MonoCoopMutex
*mutex
)
30 mono_os_mutex_init (&mutex
->m
);
34 mono_coop_mutex_init_recursive (MonoCoopMutex
*mutex
)
36 mono_os_mutex_init_recursive (&mutex
->m
);
40 mono_coop_mutex_destroy (MonoCoopMutex
*mutex
)
42 mono_os_mutex_destroy (&mutex
->m
);
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)
54 mono_os_mutex_lock (&mutex
->m
);
60 mono_coop_mutex_trylock (MonoCoopMutex
*mutex
)
62 return mono_os_mutex_trylock (&mutex
->m
);
66 mono_coop_mutex_unlock (MonoCoopMutex
*mutex
)
68 mono_os_mutex_unlock (&mutex
->m
);
72 mono_coop_cond_init (MonoCoopCond
*cond
)
74 mono_os_cond_init (&cond
->c
);
78 mono_coop_cond_destroy (MonoCoopCond
*cond
)
80 mono_os_cond_destroy (&cond
->c
);
84 mono_coop_cond_wait (MonoCoopCond
*cond
, MonoCoopMutex
*mutex
)
88 mono_os_cond_wait (&cond
->c
, &mutex
->m
);
94 mono_coop_cond_timedwait (MonoCoopCond
*cond
, MonoCoopMutex
*mutex
, guint32 timeout_ms
)
100 res
= mono_os_cond_timedwait (&cond
->c
, &mutex
->m
, timeout_ms
);
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.
116 mono_os_cond_signal (&cond
->c
);
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.
129 mono_os_cond_broadcast (&cond
->c
);
133 #endif /* __MONO_COOP_MUTEX_H__ */