vasnprintf: Don't use %n on modern, ISO C 99 compliant platforms.
[gnulib.git] / lib / windows-recmutex.c
blobb0eae0f1699af7a286b3f6fdfd4ea9ba410249a8
1 /* Plain recursive mutexes (native Windows implementation).
2 Copyright (C) 2005-2020 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
9 This program 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 General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2005.
18 Based on GCC's gthr-win32.h. */
20 #include <config.h>
22 /* Specification. */
23 #include "windows-recmutex.h"
25 #include <errno.h>
27 void
28 glwthread_recmutex_init (glwthread_recmutex_t *mutex)
30 mutex->owner = 0;
31 mutex->depth = 0;
32 InitializeCriticalSection (&mutex->lock);
33 mutex->guard.done = 1;
36 int
37 glwthread_recmutex_lock (glwthread_recmutex_t *mutex)
39 if (!mutex->guard.done)
41 if (InterlockedIncrement (&mutex->guard.started) == 0)
42 /* This thread is the first one to need this mutex. Initialize it. */
43 glwthread_recmutex_init (mutex);
44 else
46 /* Don't let mutex->guard.started grow and wrap around. */
47 InterlockedDecrement (&mutex->guard.started);
48 /* Yield the CPU while waiting for another thread to finish
49 initializing this mutex. */
50 while (!mutex->guard.done)
51 Sleep (0);
55 DWORD self = GetCurrentThreadId ();
56 if (mutex->owner != self)
58 EnterCriticalSection (&mutex->lock);
59 mutex->owner = self;
61 if (++(mutex->depth) == 0) /* wraparound? */
63 mutex->depth--;
64 return EAGAIN;
67 return 0;
70 int
71 glwthread_recmutex_trylock (glwthread_recmutex_t *mutex)
73 if (!mutex->guard.done)
75 if (InterlockedIncrement (&mutex->guard.started) == 0)
76 /* This thread is the first one to need this mutex. Initialize it. */
77 glwthread_recmutex_init (mutex);
78 else
80 /* Don't let mutex->guard.started grow and wrap around. */
81 InterlockedDecrement (&mutex->guard.started);
82 /* Let another thread finish initializing this mutex, and let it also
83 lock this mutex. */
84 return EBUSY;
88 DWORD self = GetCurrentThreadId ();
89 if (mutex->owner != self)
91 if (!TryEnterCriticalSection (&mutex->lock))
92 return EBUSY;
93 mutex->owner = self;
95 if (++(mutex->depth) == 0) /* wraparound? */
97 mutex->depth--;
98 return EAGAIN;
101 return 0;
105 glwthread_recmutex_unlock (glwthread_recmutex_t *mutex)
107 if (mutex->owner != GetCurrentThreadId ())
108 return EPERM;
109 if (mutex->depth == 0)
110 return EINVAL;
111 if (--(mutex->depth) == 0)
113 mutex->owner = 0;
114 LeaveCriticalSection (&mutex->lock);
116 return 0;
120 glwthread_recmutex_destroy (glwthread_recmutex_t *mutex)
122 if (mutex->owner != 0)
123 return EBUSY;
124 DeleteCriticalSection (&mutex->lock);
125 mutex->guard.done = 0;
126 return 0;