-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9aa4101
commit 837ea31
Showing
2 changed files
with
47 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
|
||
#include "thread_local.h" | ||
#include <atomic> | ||
#include <doctest/doctest.h> | ||
#include <memory> | ||
|
||
namespace securefs | ||
{ | ||
namespace | ||
{ | ||
struct A | ||
{ | ||
int value = 1; | ||
static inline std::atomic<int> destroy_count = 0; | ||
|
||
~A() { ++destroy_count; } | ||
}; | ||
|
||
TEST_CASE("Test custom ThreadLocal") | ||
{ | ||
ThreadLocal<A> a1([]() { return std::make_unique<A>(); }); | ||
{ | ||
ThreadLocal<A> a2( | ||
[]() | ||
{ | ||
auto result = std::make_unique<A>(); | ||
result->value = 2; | ||
return result; | ||
}); | ||
CHECK(a1.get().value == 1); | ||
CHECK(a2.get().value == 2); | ||
} | ||
// Now a2 is destroyed, and a3 will take over its slot. | ||
ThreadLocal<A> a3( | ||
[]() | ||
{ | ||
auto result = std::make_unique<A>(); | ||
result->value = 3; | ||
return result; | ||
}); | ||
CHECK(a1.get().value == 1); | ||
CHECK(a3.get().value == 3); | ||
CHECK(A::destroy_count.load() == 1); | ||
} | ||
} // namespace | ||
} // namespace securefs |