Ensure Automake does not drop ~~gnulib.m4.
[gnulib.git] / lib / windows-spin.c
blobb90e7ae9dcd101f90f24640fd49848e77cdf7adc
1 /* Spin locks (native Windows implementation).
2 Copyright (C) 2019-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>, 2019. */
19 #include <config.h>
21 /* Specification. */
22 #include "windows-spin.h"
24 #include <errno.h>
26 void
27 glwthread_spin_init (glwthread_spinlock_t *lock)
29 lock->word = 0;
32 int
33 glwthread_spin_lock (glwthread_spinlock_t *lock)
35 /* Wait until lock->word becomes 0, then replace it with 1. */
36 /* InterlockedCompareExchange
37 <https://docs.microsoft.com/en-us/windows/win32/api/winnt/nf-winnt-interlockedcompareexchange> */
38 while (InterlockedCompareExchange (&lock->word, 1, 0))
40 return 0;
43 int
44 glwthread_spin_trylock (glwthread_spinlock_t *lock)
46 /* If lock->word is 0, then replace it with 1. */
47 /* InterlockedCompareExchange
48 <https://docs.microsoft.com/en-us/windows/win32/api/winnt/nf-winnt-interlockedcompareexchange> */
49 if (InterlockedCompareExchange (&lock->word, 1, 0))
50 return EBUSY;
51 return 0;
54 int
55 glwthread_spin_unlock (glwthread_spinlock_t *lock)
57 /* If lock->word is 1, then replace it with 0. */
58 /* InterlockedCompareExchange
59 <https://docs.microsoft.com/en-us/windows/win32/api/winnt/nf-winnt-interlockedcompareexchange> */
60 if (!InterlockedCompareExchange (&lock->word, 0, 1))
61 return EINVAL;
62 return 0;
65 int
66 glwthread_spin_destroy (glwthread_spinlock_t *lock)
68 return 0;