root/maint/gnulib/lib/windows-spin.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. glwthread_spin_init
  2. glwthread_spin_lock
  3. glwthread_spin_trylock
  4. glwthread_spin_unlock
  5. glwthread_spin_destroy

   1 /* Spin locks (native Windows implementation).
   2    Copyright (C) 2019-2021 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>, 2019.  */
  18 
  19 #include <config.h>
  20 
  21 /* Specification.  */
  22 #include "windows-spin.h"
  23 
  24 #include <errno.h>
  25 
  26 void
  27 glwthread_spin_init (glwthread_spinlock_t *lock)
     /* [previous][next][first][last][top][bottom][index][help] */
  28 {
  29   lock->word = 0;
  30   MemoryBarrier ();
  31 }
  32 
  33 int
  34 glwthread_spin_lock (glwthread_spinlock_t *lock)
     /* [previous][next][first][last][top][bottom][index][help] */
  35 {
  36   /* Wait until lock->word becomes 0, then replace it with 1.  */
  37   /* InterlockedCompareExchange
  38      <https://docs.microsoft.com/en-us/windows/win32/api/winnt/nf-winnt-interlockedcompareexchange> */
  39   while (InterlockedCompareExchange (&lock->word, 1, 0))
  40     ;
  41   return 0;
  42 }
  43 
  44 int
  45 glwthread_spin_trylock (glwthread_spinlock_t *lock)
     /* [previous][next][first][last][top][bottom][index][help] */
  46 {
  47   /* If lock->word is 0, then replace it with 1.  */
  48   /* InterlockedCompareExchange
  49      <https://docs.microsoft.com/en-us/windows/win32/api/winnt/nf-winnt-interlockedcompareexchange> */
  50   if (InterlockedCompareExchange (&lock->word, 1, 0))
  51     return EBUSY;
  52   return 0;
  53 }
  54 
  55 int
  56 glwthread_spin_unlock (glwthread_spinlock_t *lock)
     /* [previous][next][first][last][top][bottom][index][help] */
  57 {
  58   /* If lock->word is 1, then replace it with 0.  */
  59   /* InterlockedCompareExchange
  60      <https://docs.microsoft.com/en-us/windows/win32/api/winnt/nf-winnt-interlockedcompareexchange> */
  61   if (!InterlockedCompareExchange (&lock->word, 0, 1))
  62     return EINVAL;
  63   return 0;
  64 }
  65 
  66 int
  67 glwthread_spin_destroy (glwthread_spinlock_t *lock)
     /* [previous][next][first][last][top][bottom][index][help] */
  68 {
  69   return 0;
  70 }

/* [previous][next][first][last][top][bottom][index][help] */