Bump external/ikdasm to master.
[mono-project.git] / mono / io-layer / critical-sections.c
blobd9ba58bd859b9cf27b54931dd8a1ae72dcd371ac
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>
13 #include <errno.h>
15 #include <mono/io-layer/wapi.h>
16 #include <mono/io-layer/critical-section-private.h>
18 #include <mono/utils/mono-mutex.h>
20 /* A critical section is really just like a lightweight mutex. It
21 * can't be waited for, and doesn't have a handle.
24 /**
25 * InitializeCriticalSection:
26 * @section: The critical section to initialise
28 * Initialises a critical section.
30 void InitializeCriticalSection(WapiCriticalSection *section)
32 int ret;
34 ret = mono_mutex_init_recursive (&section->mutex);
35 g_assert (ret == 0);
38 /**
39 * InitializeCriticalSectionAndSpinCount:
40 * @section: The critical section to initialise.
41 * @spincount: The spin count for this critical section. Not
42 * currently used.
44 * Initialises a critical section and sets the spin count. This
45 * implementation just calls InitializeCriticalSection().
47 * Return value: %TRUE on success, %FALSE otherwise. (%FALSE never
48 * happens).
50 gboolean InitializeCriticalSectionAndSpinCount(WapiCriticalSection *section,
51 guint32 spincount G_GNUC_UNUSED)
53 InitializeCriticalSection(section);
55 return(TRUE);
58 /**
59 * DeleteCriticalSection:
60 * @section: The critical section to delete.
62 * Releases all resources owned by critical section @section.
64 void DeleteCriticalSection(WapiCriticalSection *section)
66 int ret;
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);
73 /**
74 * SetCriticalSectionSpinCount:
75 * @section: The critical section to set
76 * @spincount: The new spin count for this critical section. Not
77 * currently used.
79 * Sets the spin count for the critical section @section. The spin
80 * count is currently ignored, and set to zero.
82 * Return value: The previous spin count. (Currently always zero).
84 guint32 SetCriticalSectionSpinCount(WapiCriticalSection *section G_GNUC_UNUSED, guint32 spincount G_GNUC_UNUSED)
86 return(0);
89 /**
90 * TryEnterCriticalSection:
91 * @section: The critical section to try and enter
93 * Attempts to enter a critical section without blocking. If
94 * successful the calling thread takes ownership of the critical
95 * section.
97 * A thread can recursively call EnterCriticalSection() and
98 * TryEnterCriticalSection(), but must call LeaveCriticalSection() an
99 * equal number of times.
101 * Return value: %TRUE if the thread successfully locked the critical
102 * section, %FALSE otherwise.
104 gboolean TryEnterCriticalSection(WapiCriticalSection *section)
106 int ret;
108 ret=mono_mutex_trylock(&section->mutex);
109 if(ret==0) {
110 return(TRUE);
111 } else {
112 return(FALSE);