summaryrefslogtreecommitdiff
path: root/lib/windows-spin.c
blob: b90e7ae9dcd101f90f24640fd49848e77cdf7adc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* Spin locks (native Windows implementation).
   Copyright (C) 2019-2020 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 2, 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>, 2019.  */

#include <config.h>

/* Specification.  */
#include "windows-spin.h"

#include <errno.h>

void
glwthread_spin_init (glwthread_spinlock_t *lock)
{
  lock->word = 0;
}

int
glwthread_spin_lock (glwthread_spinlock_t *lock)
{
  /* Wait until lock->word becomes 0, then replace it with 1.  */
  /* InterlockedCompareExchange
     <https://docs.microsoft.com/en-us/windows/win32/api/winnt/nf-winnt-interlockedcompareexchange> */
  while (InterlockedCompareExchange (&lock->word, 1, 0))
    ;
  return 0;
}

int
glwthread_spin_trylock (glwthread_spinlock_t *lock)
{
  /* If lock->word is 0, then replace it with 1.  */
  /* InterlockedCompareExchange
     <https://docs.microsoft.com/en-us/windows/win32/api/winnt/nf-winnt-interlockedcompareexchange> */
  if (InterlockedCompareExchange (&lock->word, 1, 0))
    return EBUSY;
  return 0;
}

int
glwthread_spin_unlock (glwthread_spinlock_t *lock)
{
  /* If lock->word is 1, then replace it with 0.  */
  /* InterlockedCompareExchange
     <https://docs.microsoft.com/en-us/windows/win32/api/winnt/nf-winnt-interlockedcompareexchange> */
  if (!InterlockedCompareExchange (&lock->word, 0, 1))
    return EINVAL;
  return 0;
}

int
glwthread_spin_destroy (glwthread_spinlock_t *lock)
{
  return 0;
}

Return to:

Send suggestions and report system problems to the System administrator.