Skip to content

Commit

Permalink
Add thread locking/unlocking construct
Browse files Browse the repository at this point in the history
  • Loading branch information
dragorn committed Mar 20, 2017
1 parent 3e88985 commit 3c20e0b
Showing 1 changed file with 51 additions and 1 deletion.
52 changes: 51 additions & 1 deletion util.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,12 @@
#include <sstream>
#include <iomanip>
#include <stdexcept>
#include <functional>
#include <thread>
#include <mutex>
#include <condition_variable>

#include <pthread.h>
#include <pthread.h>

// ieee float struct for a 64bit float for serialization
typedef struct {
Expand Down Expand Up @@ -326,5 +330,51 @@ class local_eol_locker {
// Local copy of strerror_r because glibc did such an amazingly poor job of it
string kis_strerror_r(int errnum);

// Utility class for doing conditional thread locking; allows one thread to wait
// indefinitely and another thread to easily unlock it
template<class t>
class conditional_locker {
public:
conditional_locker() {
locked = false;
}

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

// Lock the conditional, does not block the caller
void lock() {
std::lock_guard<std::mutex> lk(m);
locked = true;
}

// Block this thread until another thread calls us and unlocks us, return
// whatever value we were unlocked with
t block_until() {
std::unique_lock<std::mutex> lk(m);
cv.wait(lk, [this] { return !locked; });
return data;
}

// Unlock the conditional, unblocking whatever thread was blocked
// waiting for us, and passing whatever data we'd like to pass
void unlock(t in_data) {
{
std::lock_guard<std::mutex> lg(m);
locked = false;
data = in_data;
}
cv.notify_one();
}

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

#endif

0 comments on commit 3c20e0b

Please sign in to comment.