Skip to content

Commit

Permalink
Clean up some of the core conditional locker core
Browse files Browse the repository at this point in the history
  • Loading branch information
dragorn committed Jul 25, 2018
1 parent 3a6c4b9 commit 15410f8
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions util.h
Expand Up @@ -38,6 +38,7 @@
#include <math.h>
#include <string.h>

#include <atomic>
#include <string>
#include <map>
#include <vector>
Expand Down Expand Up @@ -256,14 +257,12 @@ int GetLengthTagOffsets(unsigned int init_offset,
template<class t>
class conditional_locker {
public:
conditional_locker() {
locked = false;
}
conditional_locker() :
locked(false) { }

conditional_locker(t in_data) {
locked = false;
data = in_data;
}
conditional_locker(t in_data) :
locked(false),
data(in_data) { }

~conditional_locker() {
unlock();
Expand All @@ -279,14 +278,12 @@ class conditional_locker {
// whatever value we were unlocked with
t block_until() {
std::unique_lock<std::mutex> lk(m);

// Return false if waiting is to be continued, so the inverse of the
// lock state
cv.wait(lk, [this](){ return !locked; });

return data;
}

// Block for a given number of milliseconds, returning false if it did not
// successfully unlock
bool block_for_ms(const std::chrono::milliseconds& rel_time) {
std::unique_lock<std::mutex> lk(m);
return cv.wait_for(lk, rel_time, [this](){ return !locked; });
Expand All @@ -296,24 +293,28 @@ class conditional_locker {
// waiting for us, and passing whatever data we'd like to pass
void unlock(t in_data) {
std::unique_lock<std::mutex> lk(m);

locked = false;
data = in_data;

lk.unlock();
cv.notify_all();
}

void unlock() {
std::unique_lock<std::mutex> lk(m);

locked = false;
lk.unlock();

cv.notify_all();
}


protected:
std::mutex m;
std::condition_variable cv;
bool locked;
std::atomic<bool> locked;
t data;
};

Expand Down

0 comments on commit 15410f8

Please sign in to comment.