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
51cc6572
authored
6 years ago
by
FritzFlorian
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add spinlock and tests for it.
parent
7dfa2a34
Pipeline
#1099
failed with stages
in 1 minute 47 seconds
Changes
4
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
65 additions
and
35 deletions
+65
-35
lib/pls/include/pls/internal/base/spin_lock.h
+4
-0
test/CMakeLists.txt
+2
-3
test/base_tests.cpp
+59
-0
test/thread_tests.cpp
+0
-32
No files found.
lib/pls/include/pls/internal/base/spin_lock.h
View file @
51cc6572
...
...
@@ -26,6 +26,10 @@ namespace pls {
}
}
}
void
unlock
()
{
flag_
.
clear
(
std
::
memory_order_release
);
}
};
}
}
...
...
This diff is collapsed.
Click to expand it.
test/CMakeLists.txt
View file @
51cc6572
add_executable
(
tests
main.cpp
example_tests.cpp
thread_tests.cpp
)
target_link_libraries
(
tests catch2 pls
)
\ No newline at end of file
base_tests.cpp
)
target_link_libraries
(
tests catch2 pls
)
This diff is collapsed.
Click to expand it.
test/base_tests.cpp
0 → 100644
View file @
51cc6572
#include <catch.hpp>
#include <pls/internal/base/thread.h>
#include <pls/internal/base/spin_lock.h>
#include <vector>
using
namespace
pls
::
internal
::
base
;
using
namespace
std
;
static
bool
base_tests_visited
;
static
int
base_tests_local_value_one
;
static
vector
<
int
>
base_tests_local_value_two
;
TEST_CASE
(
"thread creation and joining"
,
"[internal/base/thread.h]"
)
{
base_tests_visited
=
false
;
auto
t1
=
start_thread
([]()
{
base_tests_visited
=
true
;
});
t1
.
join
();
REQUIRE
(
base_tests_visited
);
}
TEST_CASE
(
"thread state"
,
"[internal/base/thread.h]"
)
{
int
state_one
=
1
;
vector
<
int
>
state_two
{
1
,
2
};
auto
t1
=
start_thread
([]()
{
base_tests_local_value_one
=
*
this_thread
::
state
<
int
>
();
},
&
state_one
);
auto
t2
=
start_thread
([]()
{
base_tests_local_value_two
=
*
this_thread
::
state
<
vector
<
int
>>
();
},
&
state_two
);
t1
.
join
();
t2
.
join
();
REQUIRE
(
base_tests_local_value_one
==
1
);
REQUIRE
(
base_tests_local_value_two
==
vector
<
int
>
{
1
,
2
});
}
TEST_CASE
(
"spinlock protects concurrent counter"
,
"[internal/base/spinlock.h]"
)
{
constexpr
int
num_iterations
=
1000000
;
int
shared_counter
=
0
;
spin_lock
lock
{};
auto
t1
=
start_thread
([
&
]
()
{
for
(
int
i
=
0
;
i
<
num_iterations
;
i
++
)
{
lock
.
lock
();
shared_counter
++
;
lock
.
unlock
();
}
});
auto
t2
=
start_thread
([
&
]
()
{
for
(
int
i
=
0
;
i
<
num_iterations
;
i
++
)
{
lock
.
lock
();
shared_counter
--
;
lock
.
unlock
();
}
});
t1
.
join
();
t2
.
join
();
REQUIRE
(
shared_counter
==
0
);
}
This diff is collapsed.
Click to expand it.
test/thread_tests.cpp
deleted
100644 → 0
View file @
7dfa2a34
#include <catch.hpp>
#include <pls/internal/base/thread.h>
#include <vector>
using
namespace
pls
::
internal
::
base
;
using
namespace
std
;
static
bool
visited
;
static
int
local_value_1
;
static
vector
<
int
>
local_value_two
;
TEST_CASE
(
"thread creation and joining"
,
"[internal/base/thread.h]"
)
{
visited
=
false
;
auto
t1
=
start_thread
([]()
{
visited
=
true
;
});
t1
.
join
();
REQUIRE
(
visited
);
}
TEST_CASE
(
"thread state"
,
"[internal/base/thread.h]"
)
{
int
state_one
=
1
;
vector
<
int
>
state_two
{
1
,
2
};
auto
t1
=
start_thread
([]()
{
local_value_1
=
*
this_thread
::
state
<
int
>
();
},
&
state_one
);
auto
t2
=
start_thread
([]()
{
local_value_two
=
*
this_thread
::
state
<
vector
<
int
>>
();
},
&
state_two
);
t1
.
join
();
t2
.
join
();
REQUIRE
(
local_value_1
==
1
);
REQUIRE
(
local_value_two
==
vector
<
int
>
{
1
,
2
});
}
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