-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
1 changed file
with
68 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#include <algorithm> | ||
#include <chrono> | ||
#include <cstdlib> | ||
#include <cstring> | ||
#include <seqlock/seqlock.hpp> | ||
#include <seqlock/util.hpp> | ||
#include <thread> | ||
|
||
using namespace seqlock; // NOLINT | ||
using namespace std::chrono_literals; // NOLINT | ||
|
||
int main() { // NOLINT | ||
const char* filename = "/shmfiletest42"; | ||
const size_t filesize = util::GetPageSize(); | ||
|
||
util::SharedMemory shm{filename, filesize}; | ||
auto* region = util::Region<mode::SingleWriter, 4096>::Create(shm); | ||
|
||
std::atomic<int> writer_state{0}; // 0: preparing 1: working 2: done | ||
|
||
std::thread writer{[&] { | ||
char from[4096]; | ||
memset(from, 0, 4096); | ||
|
||
writer_state = 1; | ||
for (int i = 0; i < 1024; i++) { | ||
memset(from, i & 127, 4096); // NOLINT | ||
region->Store(from, 4096); | ||
std::this_thread::sleep_for(1ms); | ||
} | ||
|
||
writer_state = 2; | ||
}}; | ||
|
||
std::atomic_flag ok{}; | ||
ok.clear(); | ||
std::thread reader{[&] { | ||
while (writer_state == 0) { | ||
std::this_thread::sleep_for(10ms); | ||
} | ||
|
||
char to[4096]; | ||
memset(to, 0, 4096); | ||
while (writer_state == 1) { | ||
region->Load(to, 4096); | ||
if (to[0] > 0) { | ||
ok.test_and_set(); | ||
for (int i = 0; i < 4095; i++) { | ||
if (to[i] != to[i + 1]) { | ||
std::cout << "invalid load" << std::endl; | ||
std::terminate(); | ||
} | ||
} | ||
} | ||
} | ||
}}; | ||
|
||
writer.join(); | ||
reader.join(); | ||
|
||
if (not ok.test()) { | ||
std::cout << "invalid load" << std::endl; | ||
return EXIT_FAILURE; | ||
} | ||
|
||
std::cout << "load successful" << std::endl; | ||
return EXIT_SUCCESS; | ||
} |