forked from preda/gpuowl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMemlock.cpp
36 lines (28 loc) · 830 Bytes
/
Memlock.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Copyright Mihai Preda.
#include "Memlock.h"
#include "File.h"
#include "common.h"
#include "Signal.h"
#include <thread>
#include <chrono>
namespace {
error_code& noThrow() {
static error_code dummy;
return dummy;
}
}
Memlock::Memlock(fs::path base, u32 device) : lock{base / ("memlock-"s + to_string(device))} {
if (!fs::create_directory(lock, noThrow())) {
log("Waiting for memory lock '%s'\n", lock.string().c_str());
Signal signal;
do {
std::this_thread::sleep_for(std::chrono::seconds(5));
if (signal.stopRequested()) { throw "stop requested"; }
} while (!fs::create_directory(lock, noThrow()));
}
log("Acquired memory lock '%s'\n", lock.string().c_str());
}
Memlock::~Memlock() {
fs::remove(lock, noThrow());
log("Released memory lock '%s'\n", lock.string().c_str());
}