Revert "update rx to the latest rx-oss-v1.1 build."
[mono-project.git] / mono / io-layer / critical-sections.c
blob479b419b79d58f87397de4be33dcb7f6eb6bad08
1 /*
2 * critical-sections.c: Critical sections
4 * Author:
5 * Dick Porter (dick@ximian.com)
7 * (C) 2002 Ximian, Inc.
8 */
10 #include <config.h>
11 #include <glib.h>
12 #include <pthread.h>
14 #include <mono/io-layer/wapi.h>
15 #include <mono/io-layer/critical-section-private.h>
17 #include "mono-mutex.h"
19 /* A critical section is really just like a lightweight mutex. It
20 * can't be waited for, and doesn't have a handle.
23 /* According to the MSDN docs, the Microsoft implementation spins a
24 * number of times then waits for a semaphore. I could implement that
25 * here but I'd need a mutex around the critical section structure
26 * anyway. So I may as well just use a pthread mutex.
28 static mono_once_t attr_key_once=MONO_ONCE_INIT;
29 static mono_mutexattr_t attr;
31 static void attr_init(void)
33 int ret;
35 ret = mono_mutexattr_init(&attr);
36 g_assert (ret == 0);
38 ret = mono_mutexattr_settype(&attr, MONO_MUTEX_RECURSIVE);
39 g_assert (ret == 0);
42 void _wapi_critical_section_cleanup (void)
44 mono_mutexattr_destroy (&attr);
47 /**
48 * InitializeCriticalSection:
49 * @section: The critical section to initialise
51 * Initialises a critical section.
53 void InitializeCriticalSection(WapiCriticalSection *section)
55 int ret;
57 mono_once(&attr_key_once, attr_init);
58 ret = mono_mutex_init(&section->mutex, &attr);
59 g_assert (ret == 0);
62 /**
63 * InitializeCriticalSectionAndSpinCount:
64 * @section: The critical section to initialise.
65 * @spincount: The spin count for this critical section. Not
66 * currently used.
68 * Initialises a critical section and sets the spin count. This
69 * implementation just calls InitializeCriticalSection().
71 * Return value: %TRUE on success, %FALSE otherwise. (%FALSE never
72 * happens).
74 gboolean InitializeCriticalSectionAndSpinCount(WapiCriticalSection *section,
75 guint32 spincount G_GNUC_UNUSED)
77 InitializeCriticalSection(section);
79 return(TRUE);
82 /**
83 * DeleteCriticalSection:
84 * @section: The critical section to delete.
86 * Releases all resources owned by critical section @section.
88 void DeleteCriticalSection(WapiCriticalSection *section)
90 int ret;
92 ret = mono_mutex_destroy(&section->mutex);
93 g_assert (ret == 0);
96 /**
97 * SetCriticalSectionSpinCount:
98 * @section: The critical section to set
99 * @spincount: The new spin count for this critical section. Not
100 * currently used.
102 * Sets the spin count for the critical section @section. The spin
103 * count is currently ignored, and set to zero.
105 * Return value: The previous spin count. (Currently always zero).
107 guint32 SetCriticalSectionSpinCount(WapiCriticalSection *section G_GNUC_UNUSED, guint32 spincount G_GNUC_UNUSED)
109 return(0);
113 * TryEnterCriticalSection:
114 * @section: The critical section to try and enter
116 * Attempts to enter a critical section without blocking. If
117 * successful the calling thread takes ownership of the critical
118 * section.
120 * A thread can recursively call EnterCriticalSection() and
121 * TryEnterCriticalSection(), but must call LeaveCriticalSection() an
122 * equal number of times.
124 * Return value: %TRUE if the thread successfully locked the critical
125 * section, %FALSE otherwise.
127 gboolean TryEnterCriticalSection(WapiCriticalSection *section)
129 int ret;
131 ret=mono_mutex_trylock(&section->mutex);
132 if(ret==0) {
133 return(TRUE);
134 } else {
135 return(FALSE);