Honeycomb  0.1
Component-Model Framework
Spin.h
Go to the documentation of this file.
1 // Honeycomb, Copyright (C) 2015 NewGamePlus Inc. Distributed under the Boost Software License v1.0.
2 #pragma once
3 
5 #include "Honey/Thread/Atomic.h"
6 #include "Honey/Thread/Thread.h"
7 
8 namespace honey
9 {
10 
12 
17 class SpinLock
18 {
19 public:
21 
22  SpinLock() : _owner(Thread::threadIdInvalid), _holdCount(0) {}
24  SpinLock(const SpinLock&) : _owner(Thread::threadIdInvalid), _holdCount(0) {}
25 
26  ~SpinLock() {}
27 
29  SpinLock& operator=(const SpinLock&) { return *this; }
30 
32  void lock();
34  void unlock();
35 
37  bool tryLock();
39  bool tryLock(MonoClock::Duration time) { return tryLock(time == time.max() ? MonoClock::TimePoint::max() : MonoClock::now() + time); }
41  bool tryLock(MonoClock::TimePoint time);
42 
43 private:
44  bool tryLock_priv(Thread::ThreadId threadId);
45 
47  int _holdCount;
48 };
49 
50 }
static TimePoint now()
Get current time.
Definition: Clock.h:48
Thread class.
Definition: Thread.h:128
A thread lock where the lock is acquired through a busy wait loop.
Definition: Spin.h:17
bool tryLock(MonoClock::Duration time)
Attempt to acquire the lock for an amount of time. Returns true if the lock was acquired, false otherwise.
Definition: Spin.h:39
~SpinLock()
Definition: Spin.h:26
Super::TimePoint TimePoint
Definition: Clock.h:40
Definition: Atomic.h:119
bool tryLock()
Attempt to acquire the lock, returns immediately. Returns true if the lock was acquired, false otherwise.
Definition: Spin.cpp:34
SpinLock()
Definition: Spin.h:22
void lock()
Acquire the lock. Thread waits in a busy loop until lock becomes available.
Definition: Spin.cpp:9
Super::ThreadId ThreadId
Definition: Thread.h:140
SpinLock & operator=(const SpinLock &)
Can't copy, silently does nothing.
Definition: Spin.h:29
SpinLock(const SpinLock &)
Can't copy, silently inits to default.
Definition: Spin.h:24
UniqueLock< SpinLock > Scoped
Definition: Spin.h:20
void unlock()
Release the lock.
Definition: Spin.cpp:19
TimePoint::Duration Duration
Definition: Clock.h:41
Global Honeycomb namespace.