summaryrefslogtreecommitdiffstats
path: root/gl/windows-mutex.c
diff options
context:
space:
mode:
authorLorenz <12514511+RincewindsHat@users.noreply.github.com>2023-02-10 10:43:20 (GMT)
committerGitHub <noreply@github.com>2023-02-10 10:43:20 (GMT)
commitc07206f2ccc2356aa74bc6813a94c2190017d44e (patch)
tree1677e5fdca5c3ce544e92c5484e16a0b68df1cc9 /gl/windows-mutex.c
parente92046f810658016b7c373677efefda3b21400e4 (diff)
parentd2fcf49ad4afc15f37d082d7bc96a808369ec2d4 (diff)
downloadmonitoring-plugins-c07206f.tar.gz
Merge pull request #1832 from RincewindsHat/gnulib_update_2023
Gnulib update 2023 and attendant fixes
Diffstat (limited to 'gl/windows-mutex.c')
-rw-r--r--gl/windows-mutex.c95
1 files changed, 95 insertions, 0 deletions
diff --git a/gl/windows-mutex.c b/gl/windows-mutex.c
new file mode 100644
index 0000000..ab7258c
--- /dev/null
+++ b/gl/windows-mutex.c
@@ -0,0 +1,95 @@
1/* Plain mutexes (native Windows implementation).
2 Copyright (C) 2005-2023 Free Software Foundation, Inc.
3
4 This file is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
8
9 This file is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
16
17/* Written by Bruno Haible <bruno@clisp.org>, 2005.
18 Based on GCC's gthr-win32.h. */
19
20#include <config.h>
21
22/* Specification. */
23#include "windows-mutex.h"
24
25#include <errno.h>
26
27void
28glwthread_mutex_init (glwthread_mutex_t *mutex)
29{
30 InitializeCriticalSection (&mutex->lock);
31 mutex->guard.done = 1;
32}
33
34int
35glwthread_mutex_lock (glwthread_mutex_t *mutex)
36{
37 if (!mutex->guard.done)
38 {
39 if (InterlockedIncrement (&mutex->guard.started) == 0)
40 /* This thread is the first one to need this mutex. Initialize it. */
41 glwthread_mutex_init (mutex);
42 else
43 {
44 /* Don't let mutex->guard.started grow and wrap around. */
45 InterlockedDecrement (&mutex->guard.started);
46 /* Yield the CPU while waiting for another thread to finish
47 initializing this mutex. */
48 while (!mutex->guard.done)
49 Sleep (0);
50 }
51 }
52 EnterCriticalSection (&mutex->lock);
53 return 0;
54}
55
56int
57glwthread_mutex_trylock (glwthread_mutex_t *mutex)
58{
59 if (!mutex->guard.done)
60 {
61 if (InterlockedIncrement (&mutex->guard.started) == 0)
62 /* This thread is the first one to need this mutex. Initialize it. */
63 glwthread_mutex_init (mutex);
64 else
65 {
66 /* Don't let mutex->guard.started grow and wrap around. */
67 InterlockedDecrement (&mutex->guard.started);
68 /* Let another thread finish initializing this mutex, and let it also
69 lock this mutex. */
70 return EBUSY;
71 }
72 }
73 if (!TryEnterCriticalSection (&mutex->lock))
74 return EBUSY;
75 return 0;
76}
77
78int
79glwthread_mutex_unlock (glwthread_mutex_t *mutex)
80{
81 if (!mutex->guard.done)
82 return EINVAL;
83 LeaveCriticalSection (&mutex->lock);
84 return 0;
85}
86
87int
88glwthread_mutex_destroy (glwthread_mutex_t *mutex)
89{
90 if (!mutex->guard.done)
91 return EINVAL;
92 DeleteCriticalSection (&mutex->lock);
93 mutex->guard.done = 0;
94 return 0;
95}