1
0
mirror of https://git.zap.org.au/git/trader.git synced 2024-10-27 18:20:13 -04:00
trader/lib/windows-recmutex.c
John Zaitseff 91538aac7f Update all auto-generated files
These files have been created by running ./build-aux/bootstrap with
Gnulib as at commit b3b1e95ca1de55cef8fa04b165c48b2178bb545d (with a date
stamp of Mon Nov 11 13:04:02 2019 -0800), Autoconf 2.69, Automake 1.16.1,
Gettext 0.19.8.1 and pkg-config 0.29.
2019-11-13 08:58:46 +11:00

128 lines
3.3 KiB
C

/* Plain recursive mutexes (native Windows implementation).
Copyright (C) 2005-2019 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <https://www.gnu.org/licenses/>. */
/* Written by Bruno Haible <bruno@clisp.org>, 2005.
Based on GCC's gthr-win32.h. */
#include <config.h>
/* Specification. */
#include "windows-recmutex.h"
#include <errno.h>
void
glwthread_recmutex_init (glwthread_recmutex_t *mutex)
{
mutex->owner = 0;
mutex->depth = 0;
InitializeCriticalSection (&mutex->lock);
mutex->guard.done = 1;
}
int
glwthread_recmutex_lock (glwthread_recmutex_t *mutex)
{
if (!mutex->guard.done)
{
if (InterlockedIncrement (&mutex->guard.started) == 0)
/* This thread is the first one to need this mutex. Initialize it. */
glwthread_recmutex_init (mutex);
else
{
/* Don't let mutex->guard.started grow and wrap around. */
InterlockedDecrement (&mutex->guard.started);
/* Yield the CPU while waiting for another thread to finish
initializing this mutex. */
while (!mutex->guard.done)
Sleep (0);
}
}
{
DWORD self = GetCurrentThreadId ();
if (mutex->owner != self)
{
EnterCriticalSection (&mutex->lock);
mutex->owner = self;
}
if (++(mutex->depth) == 0) /* wraparound? */
{
mutex->depth--;
return EAGAIN;
}
}
return 0;
}
int
glwthread_recmutex_trylock (glwthread_recmutex_t *mutex)
{
if (!mutex->guard.done)
{
if (InterlockedIncrement (&mutex->guard.started) == 0)
/* This thread is the first one to need this mutex. Initialize it. */
glwthread_recmutex_init (mutex);
else
{
/* Don't let mutex->guard.started grow and wrap around. */
InterlockedDecrement (&mutex->guard.started);
/* Let another thread finish initializing this mutex, and let it also
lock this mutex. */
return EBUSY;
}
}
{
DWORD self = GetCurrentThreadId ();
if (mutex->owner != self)
{
if (!TryEnterCriticalSection (&mutex->lock))
return EBUSY;
mutex->owner = self;
}
if (++(mutex->depth) == 0) /* wraparound? */
{
mutex->depth--;
return EAGAIN;
}
}
return 0;
}
int
glwthread_recmutex_unlock (glwthread_recmutex_t *mutex)
{
if (mutex->owner != GetCurrentThreadId ())
return EPERM;
if (mutex->depth == 0)
return EINVAL;
if (--(mutex->depth) == 0)
{
mutex->owner = 0;
LeaveCriticalSection (&mutex->lock);
}
return 0;
}
int
glwthread_recmutex_destroy (glwthread_recmutex_t *mutex)
{
if (mutex->owner != 0)
return EBUSY;
DeleteCriticalSection (&mutex->lock);
mutex->guard.done = 0;
return 0;
}