Fixed deadlock on unexpected connection loss
This commit is contained in:
+26
-53
@@ -11,99 +11,72 @@
|
||||
|
||||
namespace util {
|
||||
|
||||
template<class T, std::size_t N = 1>
|
||||
template<class T, class M = std::mutex>
|
||||
class sync_t {
|
||||
public:
|
||||
static_assert(N > 0, "sync_t should have more than zero mutexes");
|
||||
using value_type = T;
|
||||
using value_t = T;
|
||||
using mutex_t = M;
|
||||
|
||||
template<std::size_t I = 0>
|
||||
std::lock_guard<std::mutex> lock() {
|
||||
return std::lock_guard { std::get<I>(_lock) };
|
||||
std::lock_guard<mutex_t> lock() {
|
||||
return std::lock_guard { _lock };
|
||||
}
|
||||
|
||||
template<class ...Args>
|
||||
sync_t(Args&&... args) : raw {std::forward<Args>(args)... } {}
|
||||
|
||||
sync_t &operator=(sync_t &&other) noexcept {
|
||||
for(auto &l : _lock) {
|
||||
l.lock();
|
||||
}
|
||||
|
||||
for(auto &l : other._lock) {
|
||||
l.lock();
|
||||
}
|
||||
std::lock(_lock, other._lock);
|
||||
|
||||
raw = std::move(other.raw);
|
||||
|
||||
for(auto &l : _lock) {
|
||||
l.unlock();
|
||||
}
|
||||
|
||||
for(auto &l : other._lock) {
|
||||
l.unlock();
|
||||
}
|
||||
_lock.unlock();
|
||||
other._lock.unlock();
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
sync_t &operator=(sync_t &other) noexcept {
|
||||
for(auto &l : _lock) {
|
||||
l.lock();
|
||||
}
|
||||
|
||||
for(auto &l : other._lock) {
|
||||
l.lock();
|
||||
}
|
||||
std::lock(_lock, other._lock);
|
||||
|
||||
raw = other.raw;
|
||||
|
||||
for(auto &l : _lock) {
|
||||
l.unlock();
|
||||
}
|
||||
|
||||
for(auto &l : other._lock) {
|
||||
l.unlock();
|
||||
}
|
||||
_lock.unlock();
|
||||
other._lock.unlock();
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
sync_t &operator=(const value_type &val) noexcept {
|
||||
for(auto &l : _lock) {
|
||||
l.lock();
|
||||
}
|
||||
sync_t &operator=(const value_t &val) noexcept {
|
||||
auto lg = lock();
|
||||
|
||||
raw = val;
|
||||
|
||||
for(auto &l : _lock) {
|
||||
l.unlock();
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
sync_t &operator=(value_type &&val) noexcept {
|
||||
for(auto &l : _lock) {
|
||||
l.lock();
|
||||
}
|
||||
sync_t &operator=(value_t &&val) noexcept {
|
||||
auto lg = lock();
|
||||
|
||||
raw = std::move(val);
|
||||
|
||||
for(auto &l : _lock) {
|
||||
l.unlock();
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
value_type *operator->() {
|
||||
value_t *operator->() {
|
||||
return &raw;
|
||||
}
|
||||
|
||||
value_type raw;
|
||||
value_t &operator*() {
|
||||
return raw;
|
||||
}
|
||||
|
||||
const value_t &operator*() const {
|
||||
return raw;
|
||||
}
|
||||
|
||||
value_t raw;
|
||||
private:
|
||||
std::array<std::mutex, N> _lock;
|
||||
mutex_t _lock;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user