2023-01-05 20:25:55 +08:00
|
|
|
/**************************************************************************/
|
|
|
|
/* rw_lock.h */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
2018-01-05 07:50:27 +08:00
|
|
|
|
2020-03-25 18:10:34 +08:00
|
|
|
#ifndef RW_LOCK_H
|
|
|
|
#define RW_LOCK_H
|
2017-01-08 05:25:37 +08:00
|
|
|
|
2023-01-27 19:36:01 +08:00
|
|
|
#include "core/typedefs.h"
|
|
|
|
|
2021-01-18 21:01:38 +08:00
|
|
|
#include <shared_mutex>
|
|
|
|
|
2017-01-08 05:25:37 +08:00
|
|
|
class RWLock {
|
2021-01-18 21:01:38 +08:00
|
|
|
mutable std::shared_timed_mutex mutex;
|
2017-01-08 05:25:37 +08:00
|
|
|
|
|
|
|
public:
|
2023-01-27 19:36:01 +08:00
|
|
|
// Lock the RWLock, block if locked by someone else.
|
|
|
|
_ALWAYS_INLINE_ void read_lock() const {
|
2021-01-18 21:01:38 +08:00
|
|
|
mutex.lock_shared();
|
|
|
|
}
|
2017-01-08 05:25:37 +08:00
|
|
|
|
2023-01-27 19:36:01 +08:00
|
|
|
// Unlock the RWLock, let other threads continue.
|
|
|
|
_ALWAYS_INLINE_ void read_unlock() const {
|
2021-01-18 21:01:38 +08:00
|
|
|
mutex.unlock_shared();
|
|
|
|
}
|
2017-01-08 05:25:37 +08:00
|
|
|
|
2023-01-27 18:04:41 +08:00
|
|
|
// Attempt to lock the RWLock for reading. True on success, false means it can't lock.
|
2023-01-27 19:36:01 +08:00
|
|
|
_ALWAYS_INLINE_ bool read_try_lock() const {
|
2023-01-27 18:04:41 +08:00
|
|
|
return mutex.try_lock_shared();
|
2021-01-18 21:01:38 +08:00
|
|
|
}
|
|
|
|
|
2023-01-27 19:36:01 +08:00
|
|
|
// Lock the RWLock, block if locked by someone else.
|
|
|
|
_ALWAYS_INLINE_ void write_lock() {
|
2021-01-18 21:01:38 +08:00
|
|
|
mutex.lock();
|
|
|
|
}
|
|
|
|
|
2023-01-27 19:36:01 +08:00
|
|
|
// Unlock the RWLock, let other threads continue.
|
|
|
|
_ALWAYS_INLINE_ void write_unlock() {
|
2021-01-18 21:01:38 +08:00
|
|
|
mutex.unlock();
|
|
|
|
}
|
2017-01-08 05:25:37 +08:00
|
|
|
|
2023-01-27 18:04:41 +08:00
|
|
|
// Attempt to lock the RWLock for writing. True on success, false means it can't lock.
|
2023-01-27 19:36:01 +08:00
|
|
|
_ALWAYS_INLINE_ bool write_try_lock() {
|
2023-01-27 18:04:41 +08:00
|
|
|
return mutex.try_lock();
|
2021-01-18 21:01:38 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-01-08 05:25:37 +08:00
|
|
|
class RWLockRead {
|
2021-01-18 21:01:38 +08:00
|
|
|
const RWLock &lock;
|
2017-01-08 05:25:37 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
public:
|
2023-01-27 19:36:01 +08:00
|
|
|
_ALWAYS_INLINE_ RWLockRead(const RWLock &p_lock) :
|
2021-01-18 21:01:38 +08:00
|
|
|
lock(p_lock) {
|
|
|
|
lock.read_lock();
|
2017-03-05 23:44:50 +08:00
|
|
|
}
|
2023-01-27 19:36:01 +08:00
|
|
|
_ALWAYS_INLINE_ ~RWLockRead() {
|
2021-01-18 21:01:38 +08:00
|
|
|
lock.read_unlock();
|
2017-03-05 23:44:50 +08:00
|
|
|
}
|
2017-01-08 05:25:37 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class RWLockWrite {
|
2021-01-18 21:01:38 +08:00
|
|
|
RWLock &lock;
|
2017-01-08 05:25:37 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
public:
|
2023-01-27 19:36:01 +08:00
|
|
|
_ALWAYS_INLINE_ RWLockWrite(RWLock &p_lock) :
|
2021-01-18 21:01:38 +08:00
|
|
|
lock(p_lock) {
|
|
|
|
lock.write_lock();
|
2017-03-05 23:44:50 +08:00
|
|
|
}
|
2023-01-27 19:36:01 +08:00
|
|
|
_ALWAYS_INLINE_ ~RWLockWrite() {
|
2021-01-18 21:01:38 +08:00
|
|
|
lock.write_unlock();
|
2017-03-05 23:44:50 +08:00
|
|
|
}
|
2017-01-08 05:25:37 +08:00
|
|
|
};
|
|
|
|
|
2020-03-25 18:10:34 +08:00
|
|
|
#endif // RW_LOCK_H
|