[runtime] Add condition variable definitions that are missing on MinGW.
[mono.git] / mono / utils / mono-mutex.h
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * mono-mutex.h: Portability wrappers around POSIX Mutexes
4  *
5  * Authors: Jeffrey Stedfast <fejj@ximian.com>
6  *
7  * Copyright 2002 Ximian, Inc. (www.ximian.com)
8  */
9
10 #ifndef __MONO_MUTEX_H__
11 #define __MONO_MUTEX_H__
12
13 #include <config.h>
14
15 #include <glib.h>
16 #ifdef HAVE_PTHREAD_H
17 #include <pthread.h>
18 #endif
19 #include <time.h>
20
21 #ifdef HOST_WIN32
22 #include <winsock2.h>
23 #include <windows.h>
24
25 /* Vanilla MinGW is missing some defs, loan them from MinGW-w64. */
26 #if defined __MINGW32__ && !defined __MINGW64_VERSION_MAJOR
27
28 #if (_WIN32_WINNT >= 0x0600)
29 /* Fixme: Opaque structs */
30 typedef PVOID RTL_CONDITION_VARIABLE;
31 typedef PVOID RTL_SRWLOCK;
32
33 #ifndef _RTL_RUN_ONCE_DEF
34 #define _RTL_RUN_ONCE_DEF 1
35 typedef PVOID RTL_RUN_ONCE, *PRTL_RUN_ONCE;
36 typedef DWORD (WINAPI *PRTL_RUN_ONCE_INIT_FN)(PRTL_RUN_ONCE, PVOID, PVOID *);
37 #define RTL_RUN_ONCE_INIT 0
38 #define RTL_RUN_ONCE_CHECK_ONLY 1UL
39 #define RTL_RUN_ONCE_ASYNC 2UL
40 #define RTL_RUN_ONCE_INIT_FAILED 4UL
41 #define RTL_RUN_ONCE_CTX_RESERVED_BITS 2
42 #endif
43 #define RTL_SRWLOCK_INIT 0
44 #define RTL_CONDITION_VARIABLE_INIT 0
45 #define RTL_CONDITION_VARIABLE_LOCKMODE_SHARED 1
46
47 #define CONDITION_VARIABLE_INIT RTL_CONDITION_VARIABLE_INIT
48 #define CONDITION_VARIABLE_LOCKMODE_SHARED RTL_CONDITION_VARIABLE_LOCKMODE_SHARED
49 #define SRWLOCK_INIT RTL_SRWLOCK_INIT
50 #endif
51
52 #if (_WIN32_WINNT >= 0x0600)
53 /*Condition Variables http://msdn.microsoft.com/en-us/library/ms682052%28VS.85%29.aspx*/
54 typedef RTL_CONDITION_VARIABLE CONDITION_VARIABLE, *PCONDITION_VARIABLE;
55 typedef RTL_SRWLOCK SRWLOCK, *PSRWLOCK;
56
57 WINBASEAPI VOID WINAPI InitializeConditionVariable(PCONDITION_VARIABLE ConditionVariable);
58 WINBASEAPI WINBOOL WINAPI SleepConditionVariableCS(PCONDITION_VARIABLE ConditionVariable, PCRITICAL_SECTION CriticalSection, DWORD dwMilliseconds);
59 WINBASEAPI WINBOOL WINAPI SleepConditionVariableSRW(PCONDITION_VARIABLE ConditionVariable, PSRWLOCK SRWLock, DWORD dwMilliseconds, ULONG Flags);
60 WINBASEAPI VOID WINAPI WakeAllConditionVariable(PCONDITION_VARIABLE ConditionVariable);
61 WINBASEAPI VOID WINAPI WakeConditionVariable(PCONDITION_VARIABLE ConditionVariable);
62
63 /*Slim Reader/Writer (SRW) Locks http://msdn.microsoft.com/en-us/library/aa904937%28VS.85%29.aspx*/
64 WINBASEAPI VOID WINAPI AcquireSRWLockExclusive(PSRWLOCK SRWLock);
65 WINBASEAPI VOID WINAPI AcquireSRWLockShared(PSRWLOCK SRWLock);
66 WINBASEAPI VOID WINAPI InitializeSRWLock(PSRWLOCK SRWLock);
67 WINBASEAPI VOID WINAPI ReleaseSRWLockExclusive(PSRWLOCK SRWLock);
68 WINBASEAPI VOID WINAPI ReleaseSRWLockShared(PSRWLOCK SRWLock);
69
70 WINBASEAPI BOOLEAN TryAcquireSRWLockExclusive(PSRWLOCK SRWLock);
71 WINBASEAPI BOOLEAN TryAcquireSRWLockShared(PSRWLOCK SRWLock);
72
73 /*One-Time Initialization http://msdn.microsoft.com/en-us/library/aa363808(VS.85).aspx*/
74 #define INIT_ONCE_ASYNC 0x00000002UL
75 #define INIT_ONCE_INIT_FAILED 0x00000004UL
76
77 typedef PRTL_RUN_ONCE PINIT_ONCE;
78 typedef PRTL_RUN_ONCE LPINIT_ONCE;
79 typedef WINBOOL CALLBACK (*PINIT_ONCE_FN) (PINIT_ONCE InitOnce, PVOID Parameter, PVOID *Context);
80
81 WINBASEAPI WINBOOL WINAPI InitOnceBeginInitialize(LPINIT_ONCE lpInitOnce, DWORD dwFlags, PBOOL fPending, LPVOID *lpContext);
82 WINBASEAPI WINBOOL WINAPI InitOnceComplete(LPINIT_ONCE lpInitOnce, DWORD dwFlags, LPVOID lpContext);
83 WINBASEAPI WINBOOL WINAPI InitOnceExecuteOnce(PINIT_ONCE InitOnce, PINIT_ONCE_FN InitFn, PVOID Parameter, LPVOID *Context);
84 #endif
85
86 #endif /* defined __MINGW32__ && !defined __MINGW64_VERSION_MAJOR */
87 #endif /* HOST_WIN32 */
88
89 G_BEGIN_DECLS
90
91 #ifndef HOST_WIN32
92
93 typedef struct {
94         pthread_mutex_t mutex;
95         gboolean complete;
96 } mono_once_t;
97
98 #define MONO_ONCE_INIT { PTHREAD_MUTEX_INITIALIZER, FALSE }
99
100 int mono_once (mono_once_t *once, void (*once_init) (void));
101
102 typedef pthread_mutex_t mono_mutex_t;
103 typedef pthread_cond_t mono_cond_t;
104
105 #define mono_mutex_init(mutex) pthread_mutex_init (mutex, NULL)
106 #define mono_mutex_lock(mutex) pthread_mutex_lock (mutex)
107 #define mono_mutex_trylock(mutex) pthread_mutex_trylock (mutex)
108 #define mono_mutex_timedlock(mutex,timeout) pthread_mutex_timedlock (mutex, timeout)
109 #define mono_mutex_unlock(mutex) pthread_mutex_unlock (mutex)
110 #define mono_mutex_destroy(mutex) pthread_mutex_destroy (mutex)
111
112 #define mono_cond_init(cond,attr) pthread_cond_init (cond,attr)
113 #define mono_cond_wait(cond,mutex) pthread_cond_wait (cond, mutex)
114 #define mono_cond_timedwait(cond,mutex,timeout) pthread_cond_timedwait (cond, mutex, timeout)
115 #define mono_cond_signal(cond) pthread_cond_signal (cond)
116 #define mono_cond_broadcast(cond) pthread_cond_broadcast (cond)
117 #define mono_cond_destroy(cond)
118
119 /*
120  * This should be used instead of mono_cond_timedwait, since that function is not implemented on windows.
121  */
122 int mono_cond_timedwait_ms (mono_cond_t *cond, mono_mutex_t *mutex, int timeout_ms);
123
124 /* This is a function so it can be passed to pthread_cleanup_push -
125  * that is a macro and giving it a macro as a parameter breaks.
126  */
127 G_GNUC_UNUSED
128 static inline int mono_mutex_unlock_in_cleanup (mono_mutex_t *mutex)
129 {
130         return(mono_mutex_unlock (mutex));
131 }
132
133 /* Returns zero on success. */
134 static inline int
135 mono_mutex_init_recursive (mono_mutex_t *mutex)
136 {
137         int res;
138         pthread_mutexattr_t attr;
139
140         pthread_mutexattr_init (&attr);
141         pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE);
142         res = pthread_mutex_init (mutex, &attr);
143         pthread_mutexattr_destroy (&attr);
144
145         return res;
146 }
147
148 #else
149
150 typedef CRITICAL_SECTION mono_mutex_t;
151 typedef CONDITION_VARIABLE mono_cond_t;
152
153 #define mono_mutex_init(mutex) (InitializeCriticalSection((mutex)), 0)
154 #define mono_mutex_init_recursive(mutex) (InitializeCriticalSection((mutex)), 0)
155 #define mono_mutex_lock(mutex) EnterCriticalSection((mutex))
156 #define mono_mutex_trylock(mutex) (!TryEnterCriticalSection((mutex)))
157 #define mono_mutex_unlock(mutex)  LeaveCriticalSection((mutex))
158 #define mono_mutex_destroy(mutex) DeleteCriticalSection((mutex))
159
160 static inline int
161 mono_cond_init (mono_cond_t *cond, int attr)
162 {
163         InitializeConditionVariable (cond);
164         return 0;
165 }
166
167 static inline int
168 mono_cond_wait (mono_cond_t *cond, mono_mutex_t *mutex)
169 {
170         return SleepConditionVariableCS (cond, mutex, INFINITE) ? 0 : 1;
171 }
172
173 static inline int
174 mono_cond_timedwait (mono_cond_t *cond, mono_mutex_t *mutex, struct timespec *timeout)
175 {
176         // FIXME:
177         g_assert_not_reached ();
178         return 0;
179 }
180
181 static inline int
182 mono_cond_signal (mono_cond_t *cond)
183 {
184         WakeConditionVariable (cond);
185         return 0;
186 }
187
188 static inline int
189 mono_cond_broadcast (mono_cond_t *cond)
190 {
191         WakeAllConditionVariable (cond);
192         return 0;
193 }
194
195 static inline int
196 mono_cond_destroy (mono_cond_t *cond)
197 {
198         return 0;
199 }
200
201 static inline int
202 mono_cond_timedwait_ms (mono_cond_t *cond, mono_mutex_t *mutex, int timeout_ms)
203 {
204         return SleepConditionVariableCS (cond, mutex, timeout_ms) ? 0 : 1;
205 }
206
207 #endif
208
209 int mono_mutex_init_suspend_safe (mono_mutex_t *mutex);
210
211 G_END_DECLS
212
213 #endif /* __MONO_MUTEX_H__ */