10 #include <condition_variable> 14 #include <torch/torch.h> 19 template <
typename Mutex_t>
54 queueCount_.resize(maxPrio_ + 1, 0);
61 if (prio < 0 || prio > maxPrio_) {
62 throw std::runtime_error(
"Invalid priority level");
65 std::lock_guard
lock(queueMutex_);
69 queueCV_.wait(lock, [
this, prio]() {
return canGo(prio); });
72 std::lock_guard
lock(queueMutex_);
78 if (prio < 0 || prio > maxPrio_) {
79 throw std::runtime_error(
"Invalid priority level");
83 return dataMutex_.try_lock();
88 queueCV_.notify_all();
93 std::lock_guard
lock(queueMutex_);
94 for (
int i = prio + 1; i <= maxPrio_; ++i) {
95 if (queueCount_[i] != 0)
113 int default_prio_ = 0;
118 : m(&m), locked(false), default_prio_(default_prio) {}
164 template <
typename Mutex_t>
174 template <
typename Mutex_t>
180 template <
typename Mutex_t>
186 template <
typename Mutex_t>
This is exactly an unique_lock without automatic lock, except that the lock functions accepts a prior...
Definition: prioritymutex.h:110
~permanent_lock()
Definition: prioritymutex.h:31
std::vector< int > queueCount_
Definition: prioritymutex.h:103
void unlock()
Definition: prioritymutex.h:146
bool owns_lock()
Definition: prioritymutex.h:159
bool try_lock(int prio)
Definition: prioritymutex.h:151
permanent_lock & operator=(const permanent_lock &)=delete
std::condition_variable_any queueCV_
Definition: prioritymutex.h:102
This class implements a mutex that offers some control over the priority of the waiting threads...
Definition: prioritymutex.h:49
priority_mutex(int maxPrio)
Constructs a mutex.
Definition: prioritymutex.h:53
void lock(int prio=0)
Definition: prioritymutex.h:60
void operator=(priority_lock &&o)
Definition: prioritymutex.h:128
permanent_lock(Mutex_t &m_)
Definition: prioritymutex.h:26
std::mutex queueMutex_
Definition: prioritymutex.h:101
~priority_lock()
Definition: prioritymutex.h:122
bool canGo(int prio)
Definition: prioritymutex.h:92
int maxPrio_
Definition: prioritymutex.h:105
void lock()
Definition: prioritymutex.h:142
bool try_lock(int prio=0)
Definition: prioritymutex.h:77
void lock(int prio)
Definition: prioritymutex.h:137
bool owns_lock()
Definition: prioritymutex.h:187
priority_lock(priority_mutex &m, int default_prio=0)
Definition: prioritymutex.h:117
The TorchCraftAI training library.
Definition: batcher.cpp:15
void unlock()
Definition: prioritymutex.h:86
This exactly a unique lock that doesn't unlock on delete.
Definition: prioritymutex.h:20
void unlock()
Definition: prioritymutex.h:181
void lock()
Definition: prioritymutex.h:175