-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsync-file-buffer.cpp
More file actions
33 lines (27 loc) · 1.04 KB
/
Copy pathsync-file-buffer.cpp
File metadata and controls
33 lines (27 loc) · 1.04 KB
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
#include <chrono>
#include <thread>
#include <utility>
#include "sync-file-buffer.hpp"
SyncFileBuffer::SyncFileBuffer() : bytesAvailable(0), bufferConsumed_(true), currentFilepath("") {
this->consumerLock_ = std::unique_lock<std::mutex>(this->bufferMutex_, std::defer_lock);
this->producerLock_ = std::unique_lock<std::mutex>(this->bufferMutex_, std::defer_lock);
}
void SyncFileBuffer::setBufferConsumed() {
this->bufferConsumed_ = true;
this->consumerLock_.unlock();
this->cond_.notify_one();
}
void SyncFileBuffer::waitForConsumption() {
this->producerLock_.lock();
this->cond_.wait(this->producerLock_, [this]() -> bool { return this->bufferConsumed_; });
}
void SyncFileBuffer::setBufferReady(std::string filepath, int bytesAvailable) {
this->bytesAvailable = bytesAvailable;
this->currentFilepath = filepath;
this->bufferConsumed_ = false;
this->producerLock_.unlock();
}
bool SyncFileBuffer::lockIfBufferReady() {
if (this->bufferConsumed_ || !this->consumerLock_.try_lock()) return false;
return true;
}