Welcome to mirror list, hosted at ThFree Co, Russian Federation.

spinlock.h « cygwin « winsup - cygwin.com/git/newlib-cygwin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: abf723accd9bc6d8a23de5c3385f539e1599733b (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
/* spinlock.h: Header file for cygwin time-sensitive synchronization primitive.

   Copyright 2010 Red Hat, Inc.

This file is part of Cygwin.

This software is a copyrighted work licensed under the terms of the
Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
details. */

#ifndef _SPINLOCK_H
#define _SPINLOCK_H

#include "hires.h"

class spinlock
{
  LONG *locker;
  LONG val;
public:
  spinlock (LONG& locktest, LONGLONG timeout):
    locker (&locktest)
  {
    if ((val = locktest) == 1)
      return;
    LONGLONG then = gtod.msecs ();
    for (;;)
      {
	if ((val = InterlockedExchange (locker, -1)) != -1
	    || (gtod.msecs () - then) >= timeout)
	  break;
	yield ();
      }
  }
  ~spinlock () {InterlockedExchange (locker, 1);}
  operator LONG () const {return val;}
};

#endif /*_SPINLOCK_H*/