Nebula
Toggle main menu visibility
Loading...
Searching...
No Matches
spinlock.h
Go to the documentation of this file.
1
#pragma once
2
//------------------------------------------------------------------------------
14
//------------------------------------------------------------------------------
15
#include "
threading/interlocked.h
"
16
#include "
threading/thread.h
"
17
namespace
Threading
18
{
19
20
class
Spinlock
21
{
22
public
:
24
Spinlock
();
26
~Spinlock
();
27
29
void
operator=
(
Spinlock
&& rhs);
30
32
bool
Lock
();
34
void
Unlock
();
35
private
:
36
volatile
Threading::ThreadId
lock
;
37
};
38
39
//------------------------------------------------------------------------------
42
inline
43
Spinlock::Spinlock
()
44
:
lock
(
InvalidThreadId
)
45
{
46
}
47
48
//------------------------------------------------------------------------------
51
inline
52
Spinlock::~Spinlock
()
53
{
54
// Make sure this thread has the lock before we
55
this->
Lock
();
56
this->
lock
=
InvalidThreadId
;
57
}
58
59
//------------------------------------------------------------------------------
62
inline
void
63
Spinlock::operator=
(
Spinlock
&& rhs)
64
{
65
rhs.Lock();
66
rhs.lock =
InvalidThreadId
;
67
68
// If this wasn't locked when assigned, early out
69
if
(
InvalidThreadId
== this->
lock
)
70
return
;
71
72
// Lock this thread until whoever is holding it returns the lock
73
while
(
Interlocked::CompareExchange
((
volatile
ThreadIdStorage
*)&this->
lock
,
InvalidThreadId
,
InvalidThreadId
) !=
InvalidThreadId
)
74
{
75
Thread::YieldThread();
76
}
77
}
78
79
//------------------------------------------------------------------------------
82
inline
bool
83
Spinlock::Lock
()
84
{
85
// Attempt to set the lock, if already 1, yield the thread
86
ThreadId
threadId = Thread::GetMyThreadId();
87
88
// If this thread already owns the spinlock, return early
89
if
(threadId == this->
lock
)
90
return
false
;
91
92
// Otherwise, enter the spin to exchange the thread id to ours
93
while
(
Interlocked::CompareExchange
((
volatile
ThreadIdStorage
*) &this->
lock
, threadId,
InvalidThreadId
) !=
InvalidThreadId
)
94
{
95
Thread::YieldThread();
96
}
97
98
return
true
;
99
}
100
101
//------------------------------------------------------------------------------
104
inline
void
105
Spinlock::Unlock
()
106
{
107
n_assert
(this->
lock
!=
InvalidThreadId
);
108
Threading::Interlocked::Exchange
((
volatile
ThreadIdStorage
*) &this->
lock
,
InvalidThreadId
);
109
}
110
111
struct
SpinlockScope
112
{
113
SpinlockScope
(
Threading::Spinlock
*
lock
)
114
:
lock
(
lock
)
115
{
116
this->lock->
Lock
();
117
}
118
~SpinlockScope
()
119
{
120
this->
lock
->Unlock();
121
this->
lock
=
nullptr
;
122
}
123
124
Threading::Spinlock
*
lock
;
125
};
126
127
}
// namespace Threading
Threading::Spinlock
Definition
spinlock.h:21
Threading::Spinlock::Lock
bool Lock()
Lock, returns true if it locked.
Definition
spinlock.h:83
Threading::Spinlock::Spinlock
Spinlock()
Constructor.
Definition
spinlock.h:43
Threading::Spinlock::lock
volatile Threading::ThreadId lock
Definition
spinlock.h:36
Threading::Spinlock::~Spinlock
~Spinlock()
Destructor.
Definition
spinlock.h:52
Threading::Spinlock::Unlock
void Unlock()
Unlock.
Definition
spinlock.h:105
Threading::Spinlock::operator=
void operator=(Spinlock &&rhs)
Move operator.
Definition
spinlock.h:63
n_assert
#define n_assert(exp)
Definition
debug.h:50
interlocked.h
Threading::Interlocked::Exchange
int Exchange(int volatile *dest, int value)
interlocked exchange
Definition
gccinterlocked.cc:94
Threading::Interlocked::CompareExchange
int CompareExchange(int volatile *dest, int exchange, int comparand)
interlocked compare-exchange
Definition
gccinterlocked.cc:112
Threading
The Jobs2 system provides a set of threads and a pool of jobs from which threads can pickup work.
Definition
jobs2.h:16
Threading::InvalidThreadId
static const ThreadId InvalidThreadId
Definition
linuxthreadid.h:16
Threading::ThreadId
pthread_t ThreadId
Definition
linuxthreadid.h:15
Threading::ThreadIdStorage
int64_t ThreadIdStorage
Definition
linuxthreadid.h:17
Threading::SpinlockScope::SpinlockScope
SpinlockScope(Threading::Spinlock *lock)
Definition
spinlock.h:113
Threading::SpinlockScope::~SpinlockScope
~SpinlockScope()
Definition
spinlock.h:118
Threading::SpinlockScope::lock
Threading::Spinlock * lock
Definition
spinlock.h:124
thread.h
code
foundation
threading
spinlock.h
Generated on
for Nebula. Dark theme by
Tilen Majerle
. All rights reserved.