Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
las3_pub
/
predictable_parallel_patterns
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
d38f584b
authored
Apr 18, 2019
by
FritzFlorian
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refacotor spinlocks to make it simpler to insert actions after CAS fails.
parent
2fece910
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
21 additions
and
8 deletions
+21
-8
lib/pls/src/internal/base/tas_spin_lock.cpp
+11
-3
lib/pls/src/internal/base/ttas_spin_lock.cpp
+10
-5
No files found.
lib/pls/src/internal/base/tas_spin_lock.cpp
View file @
d38f584b
...
...
@@ -8,26 +8,34 @@ namespace base {
void
tas_spin_lock
::
lock
()
{
PROFILE_LOCK
(
"Acquire Lock"
)
int
tries
=
0
;
while
(
flag_
.
test_and_set
(
std
::
memory_order_acquire
)
)
{
while
(
true
)
{
tries
++
;
if
(
tries
%
yield_at_tries_
==
0
)
{
this_thread
::
yield
();
}
if
(
flag_
.
test_and_set
(
std
::
memory_order_acquire
)
==
0
)
{
return
;
}
}
}
bool
tas_spin_lock
::
try_lock
(
unsigned
int
num_tries
)
{
PROFILE_LOCK
(
"Try Acquire Lock"
)
while
(
flag_
.
test_and_set
(
std
::
memory_order_acquire
)
)
{
while
(
true
)
{
num_tries
--
;
if
(
num_tries
<=
0
)
{
return
false
;
}
}
if
(
flag_
.
test_and_set
(
std
::
memory_order_acquire
)
==
0
)
{
return
true
;
}
}
}
void
tas_spin_lock
::
unlock
()
{
PROFILE_LOCK
(
"Unlock"
)
flag_
.
clear
(
std
::
memory_order_release
);
}
...
...
lib/pls/src/internal/base/ttas_spin_lock.cpp
View file @
d38f584b
...
...
@@ -10,7 +10,7 @@ void ttas_spin_lock::lock() {
int
tries
=
0
;
int
expected
=
0
;
do
{
while
(
true
)
{
while
(
flag_
.
load
(
std
::
memory_order_relaxed
)
==
1
)
{
tries
++
;
if
(
tries
%
yield_at_tries_
==
0
)
{
...
...
@@ -19,14 +19,17 @@ void ttas_spin_lock::lock() {
}
expected
=
0
;
}
while
(
!
flag_
.
compare_exchange_weak
(
expected
,
1
,
std
::
memory_order_acquire
));
if
(
flag_
.
compare_exchange_weak
(
expected
,
1
,
std
::
memory_order_acquire
))
{
return
;
}
}
}
bool
ttas_spin_lock
::
try_lock
(
unsigned
int
num_tries
)
{
PROFILE_LOCK
(
"Try Acquire Lock"
)
int
expected
=
0
;
do
{
while
(
true
)
{
while
(
flag_
.
load
(
std
::
memory_order_relaxed
)
==
1
)
{
num_tries
--
;
if
(
num_tries
<=
0
)
{
...
...
@@ -35,12 +38,14 @@ bool ttas_spin_lock::try_lock(unsigned int num_tries) {
}
expected
=
0
;
}
while
(
!
flag_
.
compare_exchange_weak
(
expected
,
1
,
std
::
memory_order_acquire
));
if
(
flag_
.
compare_exchange_weak
(
expected
,
1
,
std
::
memory_order_acquire
))
{
return
true
;
}
}
}
void
ttas_spin_lock
::
unlock
()
{
PROFILE_LOCK
(
"Unlock"
)
flag_
.
store
(
0
,
std
::
memory_order_release
);
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment