Merge pull request #615 from nealef/master
[mono.git] / mono / io-layer / critical-sections.c
1 /*
2  * critical-sections.c:  Critical sections
3  *
4  * Author:
5  *      Dick Porter (dick@ximian.com)
6  *
7  * (C) 2002 Ximian, Inc.
8  */
9
10 #include <config.h>
11 #include <glib.h>
12 #include <pthread.h>
13 #include <errno.h>
14
15 #include <mono/io-layer/wapi.h>
16 #include <mono/io-layer/critical-section-private.h>
17
18 #include <mono/utils/mono-mutex.h>
19
20 /* A critical section is really just like a lightweight mutex. It
21  * can't be waited for, and doesn't have a handle.
22  */
23
24 /**
25  * InitializeCriticalSection:
26  * @section: The critical section to initialise
27  *
28  * Initialises a critical section.
29  */
30 void InitializeCriticalSection(WapiCriticalSection *section)
31 {
32         int ret;
33         
34         ret = mono_mutex_init_recursive (&section->mutex);
35         g_assert (ret == 0);
36 }
37
38 /**
39  * InitializeCriticalSectionAndSpinCount:
40  * @section: The critical section to initialise.
41  * @spincount: The spin count for this critical section.  Not
42  * currently used.
43  *
44  * Initialises a critical section and sets the spin count.  This
45  * implementation just calls InitializeCriticalSection().
46  *
47  * Return value: %TRUE on success, %FALSE otherwise.  (%FALSE never
48  * happens).
49  */
50 gboolean InitializeCriticalSectionAndSpinCount(WapiCriticalSection *section,
51                                                guint32 spincount G_GNUC_UNUSED)
52 {
53         InitializeCriticalSection(section);
54         
55         return(TRUE);
56 }
57
58 /**
59  * DeleteCriticalSection:
60  * @section: The critical section to delete.
61  *
62  * Releases all resources owned by critical section @section.
63  */
64 void DeleteCriticalSection(WapiCriticalSection *section)
65 {
66         int ret;
67         
68         ret = mono_mutex_destroy(&section->mutex);
69         if (ret)
70                 g_error ("Failed to destroy mutex %p error code %d errno %d", &section->mutex, ret, errno);
71 }
72
73 /**
74  * SetCriticalSectionSpinCount:
75  * @section: The critical section to set
76  * @spincount: The new spin count for this critical section.  Not
77  * currently used.
78  *
79  * Sets the spin count for the critical section @section.  The spin
80  * count is currently ignored, and set to zero.
81  *
82  * Return value: The previous spin count.  (Currently always zero).
83  */
84 guint32 SetCriticalSectionSpinCount(WapiCriticalSection *section G_GNUC_UNUSED, guint32 spincount G_GNUC_UNUSED)
85 {
86         return(0);
87 }
88
89 /**
90  * TryEnterCriticalSection:
91  * @section: The critical section to try and enter
92  *
93  * Attempts to enter a critical section without blocking.  If
94  * successful the calling thread takes ownership of the critical
95  * section.
96  *
97  * A thread can recursively call EnterCriticalSection() and
98  * TryEnterCriticalSection(), but must call LeaveCriticalSection() an
99  * equal number of times.
100  *
101  * Return value: %TRUE if the thread successfully locked the critical
102  * section, %FALSE otherwise.
103  */
104 gboolean TryEnterCriticalSection(WapiCriticalSection *section)
105 {
106         int ret;
107         
108         ret=mono_mutex_trylock(&section->mutex);
109         if(ret==0) {
110                 return(TRUE);
111         } else {
112                 return(FALSE);
113         }
114 }
115