Merge remote-tracking branch 'upstream/master'
[mono.git] / mono / io-layer / critical-sections.h
1 /*
2  * critical-sections.h:  Critical sections
3  *
4  * Author:
5  *      Dick Porter (dick@ximian.com)
6  *
7  * (C) 2002 Ximian, Inc.
8  */
9
10 #ifndef _WAPI_CRITICAL_SECTIONS_H_
11 #define _WAPI_CRITICAL_SECTIONS_H_
12
13 #include <glib.h>
14 #include <pthread.h>
15
16 #include <mono/utils/mono-mutex.h>
17
18 G_BEGIN_DECLS
19
20 typedef struct _WapiCriticalSection WapiCriticalSection;
21
22 struct _WapiCriticalSection
23 {
24         guint32 depth;
25         mono_mutex_t mutex;
26 };
27
28 extern void InitializeCriticalSection(WapiCriticalSection *section);
29 extern gboolean InitializeCriticalSectionAndSpinCount(WapiCriticalSection *section, guint32 spincount);
30 extern void DeleteCriticalSection(WapiCriticalSection *section);
31 extern guint32 SetCriticalSectionSpinCount(WapiCriticalSection *section, guint32 spincount);
32 extern gboolean TryEnterCriticalSection(WapiCriticalSection *section);
33
34 /* These two are perf critical so avoid the wrapper function */
35
36 #define EnterCriticalSection(section) do { \
37         int ret = mono_mutex_lock(&(section)->mutex);    \
38         if (ret != 0) \
39                 g_warning ("Bad call to mono_mutex_lock result %d", ret); \
40         g_assert (ret == 0);                             \
41 } while (0)
42
43 #define LeaveCriticalSection(section) do { \
44         int ret = mono_mutex_unlock(&(section)->mutex);      \
45         if (ret != 0) \
46                 g_warning ("Bad call to mono_mutex_unlock result %d", ret); \
47         g_assert (ret == 0);    \
48 } while (0)
49
50 G_END_DECLS
51
52 #endif /* _WAPI_CRITICAL_SECTIONS_H_ */