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
3ff10baa
authored
Apr 16, 2019
by
FritzFlorian
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add try_lock method to spin_lock implementations.
parent
b9bb90a4
Pipeline
#1154
passed with stages
in 3 minutes 35 seconds
Changes
5
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
32 additions
and
9 deletions
+32
-9
lib/pls/include/pls/internal/base/tas_spin_lock.h
+1
-1
lib/pls/include/pls/internal/base/ttas_spin_lock.h
+2
-4
lib/pls/include/pls/internal/helpers/profiler.h
+5
-0
lib/pls/src/internal/base/tas_spin_lock.cpp
+9
-2
lib/pls/src/internal/base/ttas_spin_lock.cpp
+15
-2
No files found.
lib/pls/include/pls/internal/base/tas_spin_lock.h
View file @
3ff10baa
...
...
@@ -28,7 +28,7 @@ namespace pls {
tas_spin_lock
(
const
tas_spin_lock
&
other
)
:
flag_
{
ATOMIC_FLAG_INIT
},
yield_at_tries_
{
other
.
yield_at_tries_
}
{}
void
lock
();
bool
try_lock
();
bool
try_lock
(
unsigned
int
num_tries
=
1
);
void
unlock
();
};
}
...
...
lib/pls/include/pls/internal/base/ttas_spin_lock.h
View file @
3ff10baa
...
...
@@ -2,8 +2,6 @@
#ifndef PLS_TTAS_SPIN_LOCK_H
#define PLS_TTAS_SPIN_LOCK_H
#include "tas_spin_lock.h"
#include <atomic>
#include <iostream>
...
...
@@ -20,7 +18,7 @@ namespace pls {
*/
class
ttas_spin_lock
{
std
::
atomic
<
int
>
flag_
;
unsigned
int
yield_at_tries_
;
const
unsigned
int
yield_at_tries_
;
public
:
...
...
@@ -28,7 +26,7 @@ namespace pls {
ttas_spin_lock
(
const
ttas_spin_lock
&
other
)
:
flag_
{
0
},
yield_at_tries_
{
other
.
yield_at_tries_
}
{}
void
lock
();
bool
try_lock
();
bool
try_lock
(
unsigned
int
num_tries
=
1
);
void
unlock
();
};
}
...
...
lib/pls/include/pls/internal/helpers/profiler.h
View file @
3ff10baa
...
...
@@ -4,6 +4,7 @@
#ifdef ENABLE_EASY_PROFILER
#include <easy/profiler.h>
#include <easy/arbitrary_value.h>
#define PROFILE_WORK_BLOCK(msg) EASY_BLOCK(msg, profiler::colors::LightGreen)
#define PROFILE_FORK_JOIN_STEALING(msg) EASY_BLOCK(msg, profiler::colors::LightBlue)
...
...
@@ -16,6 +17,8 @@
#define PROFILE_ENABLE EASY_PROFILER_ENABLE
#define PROFILE_MAIN_THREAD EASY_MAIN_THREAD
#define PROFILE_VALUE(name, value) EASY_VALUE(name, value)
#else //ENABLE_EASY_PROFILER
#define PROFILE_WORK_BLOCK(msg)
...
...
@@ -29,5 +32,7 @@
#define PROFILE_ENABLE
#define PROFILE_MAIN_THREAD
#define PROFILE_VALUE(name, value)
#endif //ENABLE_EASY_PROFILER
#endif //PLS_PROFILER_H
lib/pls/src/internal/base/tas_spin_lock.cpp
View file @
3ff10baa
...
...
@@ -15,8 +15,15 @@ namespace pls {
}
}
bool
tas_spin_lock
::
try_lock
()
{
return
flag_
.
test_and_set
(
std
::
memory_order_acquire
)
==
0
;
bool
tas_spin_lock
::
try_lock
(
unsigned
int
num_tries
)
{
PROFILE_LOCK
(
"Try Acquire Lock"
)
while
(
flag_
.
test_and_set
(
std
::
memory_order_acquire
))
{
num_tries
--
;
if
(
num_tries
<=
0
)
{
return
false
;
}
}
return
true
;
}
void
tas_spin_lock
::
unlock
()
{
...
...
lib/pls/src/internal/base/ttas_spin_lock.cpp
View file @
3ff10baa
...
...
@@ -21,9 +21,22 @@ namespace pls {
}
while
(
!
flag_
.
compare_exchange_weak
(
expected
,
1
,
std
::
memory_order_acquire
));
}
bool
ttas_spin_lock
::
try_lock
()
{
bool
ttas_spin_lock
::
try_lock
(
unsigned
int
num_tries
)
{
PROFILE_LOCK
(
"Try Acquire Lock"
)
int
expected
=
0
;
return
flag_
.
load
(
std
::
memory_order_relaxed
)
==
0
&&
flag_
.
compare_exchange_weak
(
expected
,
1
,
std
::
memory_order_acquire
);
do
{
while
(
flag_
.
load
(
std
::
memory_order_relaxed
)
==
1
)
{
num_tries
--
;
if
(
num_tries
<=
0
)
{
return
false
;
}
}
expected
=
0
;
}
while
(
!
flag_
.
compare_exchange_weak
(
expected
,
1
,
std
::
memory_order_acquire
));
return
true
;
}
void
ttas_spin_lock
::
unlock
()
{
...
...
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