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
0599cfdc
authored
6 years ago
by
FritzFlorian
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add std::lock_guard test for spin_lock.
parent
51cc6572
Pipeline
#1100
failed with stages
in 1 minute 35 seconds
Changes
3
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
56 additions
and
32 deletions
+56
-32
lib/pls/include/pls/internal/base/spin_lock.h
+2
-13
lib/pls/src/internal/base/spin_lock.cpp
+13
-1
test/base_tests.cpp
+41
-18
No files found.
lib/pls/include/pls/internal/base/spin_lock.h
View file @
0599cfdc
...
@@ -17,19 +17,8 @@ namespace pls {
...
@@ -17,19 +17,8 @@ namespace pls {
public
:
public
:
spin_lock
()
:
flag_
{
ATOMIC_FLAG_INIT
},
yield_at_tries_
{
1024
}
{};
spin_lock
()
:
flag_
{
ATOMIC_FLAG_INIT
},
yield_at_tries_
{
1024
}
{};
void
lock
()
{
void
lock
();
int
tries
=
0
;
void
unlock
();
while
(
flag_
.
test_and_set
(
std
::
memory_order_acquire
))
{
tries
++
;
if
(
tries
%
yield_at_tries_
==
0
)
{
this_thread
::
yield
();
}
}
}
void
unlock
()
{
flag_
.
clear
(
std
::
memory_order_release
);
}
};
};
}
}
}
}
...
...
This diff is collapsed.
Click to expand it.
lib/pls/src/internal/base/spin_lock.cpp
View file @
0599cfdc
...
@@ -3,7 +3,19 @@
...
@@ -3,7 +3,19 @@
namespace
pls
{
namespace
pls
{
namespace
internal
{
namespace
internal
{
namespace
base
{
namespace
base
{
// implementation in header (inlining)
void
spin_lock
::
lock
()
{
int
tries
=
0
;
while
(
flag_
.
test_and_set
(
std
::
memory_order_acquire
))
{
tries
++
;
if
(
tries
%
yield_at_tries_
==
0
)
{
this_thread
::
yield
();
}
}
}
void
spin_lock
::
unlock
()
{
flag_
.
clear
(
std
::
memory_order_release
);
}
}
}
}
}
}
}
This diff is collapsed.
Click to expand it.
test/base_tests.cpp
View file @
0599cfdc
...
@@ -3,6 +3,7 @@
...
@@ -3,6 +3,7 @@
#include <pls/internal/base/spin_lock.h>
#include <pls/internal/base/spin_lock.h>
#include <vector>
#include <vector>
#include <mutex>
using
namespace
pls
::
internal
::
base
;
using
namespace
pls
::
internal
::
base
;
using
namespace
std
;
using
namespace
std
;
...
@@ -33,27 +34,49 @@ TEST_CASE( "thread state", "[internal/base/thread.h]") {
...
@@ -33,27 +34,49 @@ TEST_CASE( "thread state", "[internal/base/thread.h]") {
}
}
TEST_CASE
(
"spinlock protects concurrent counter"
,
"[internal/base/spinlock.h]"
)
{
TEST_CASE
(
"spinlock protects concurrent counter"
,
"[internal/base/spinlock.h]"
)
{
constexpr
int
num_iterations
=
100000
0
;
constexpr
int
num_iterations
=
100000
;
int
shared_counter
=
0
;
int
shared_counter
=
0
;
spin_lock
lock
{};
spin_lock
lock
{};
auto
t1
=
start_thread
([
&
]
()
{
SECTION
(
"lock can be used by itself"
)
{
for
(
int
i
=
0
;
i
<
num_iterations
;
i
++
)
{
auto
t1
=
start_thread
([
&
]()
{
lock
.
lock
();
for
(
int
i
=
0
;
i
<
num_iterations
;
i
++
)
{
shared_counter
++
;
lock
.
lock
();
lock
.
unlock
();
shared_counter
++
;
}
lock
.
unlock
();
});
}
auto
t2
=
start_thread
([
&
]
()
{
});
for
(
int
i
=
0
;
i
<
num_iterations
;
i
++
)
{
auto
t2
=
start_thread
([
&
]()
{
lock
.
lock
();
for
(
int
i
=
0
;
i
<
num_iterations
;
i
++
)
{
shared_counter
--
;
lock
.
lock
();
lock
.
unlock
();
shared_counter
--
;
}
lock
.
unlock
();
});
}
});
t1
.
join
();
t1
.
join
();
t2
.
join
();
t2
.
join
();
REQUIRE
(
shared_counter
==
0
);
}
SECTION
(
"lock can be used with std::lock_guard"
)
{
auto
t1
=
start_thread
([
&
]()
{
for
(
int
i
=
0
;
i
<
num_iterations
;
i
++
)
{
std
::
lock_guard
<
spin_lock
>
my_lock
{
lock
};
shared_counter
++
;
}
});
auto
t2
=
start_thread
([
&
]()
{
for
(
int
i
=
0
;
i
<
num_iterations
;
i
++
)
{
std
::
lock_guard
<
spin_lock
>
my_lock
{
lock
};
shared_counter
--
;
}
});
t1
.
join
();
t2
.
join
();
REQUIRE
(
shared_counter
==
0
);
REQUIRE
(
shared_counter
==
0
);
}
}
}
This diff is collapsed.
Click to expand it.
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