add Mutex wrapper around std::mutex and use it instead of SpinLock

This commit is contained in:
Arseny Smirnov 2022-07-14 16:27:06 +04:00
parent 7ddc3099f6
commit f4c97b25ec
8 changed files with 35 additions and 13 deletions

View File

@ -138,6 +138,7 @@ set(TDUTILS_SOURCE
td/utils/port/IPAddress.h td/utils/port/IPAddress.h
td/utils/port/IoSlice.h td/utils/port/IoSlice.h
td/utils/port/MemoryMapping.h td/utils/port/MemoryMapping.h
td/utils/port/Mutex.h
td/utils/port/path.h td/utils/port/path.h
td/utils/port/platform.h td/utils/port/platform.h
td/utils/port/Poll.h td/utils/port/Poll.h

View File

@ -12,7 +12,7 @@
#if !TD_EVENTFD_UNSUPPORTED #if !TD_EVENTFD_UNSUPPORTED
#include "td/utils/SpinLock.h" #include "td/utils/port/Mutex.h"
#include <utility> #include <utility>
@ -91,7 +91,7 @@ class MpscPollableQueue {
} }
private: private:
SpinLock lock_; Mutex lock_;
bool wait_event_fd_{false}; bool wait_event_fd_{false};
EventFd event_fd_; EventFd event_fd_;
std::vector<ValueType> writer_vector_; std::vector<ValueType> writer_vector_;

View File

@ -0,0 +1,20 @@
#include <mutex>
namespace td {
class Mutex {
public:
struct Guard {
std::unique_lock<std::mutex> guard;
void reset() {
guard.unlock();
}
};
Guard lock() {
return {std::unique_lock<std::mutex>(mutex_)};
}
private:
std::mutex mutex_;
};
} // namespace td

View File

@ -29,7 +29,7 @@
#if TD_PORT_WINDOWS #if TD_PORT_WINDOWS
#include "td/utils/port/detail/Iocp.h" #include "td/utils/port/detail/Iocp.h"
#include "td/utils/SpinLock.h" #include "td/utils/port/Mutex.h"
#include "td/utils/VectorQueue.h" #include "td/utils/VectorQueue.h"
#endif #endif
@ -87,7 +87,7 @@ class ServerSocketFdImpl final : private Iocp::Callback {
private: private:
PollableFdInfo info_; PollableFdInfo info_;
SpinLock lock_; Mutex lock_;
VectorQueue<SocketFd> accepted_; VectorQueue<SocketFd> accepted_;
VectorQueue<Status> pending_errors_; VectorQueue<Status> pending_errors_;
static constexpr size_t MAX_ADDR_SIZE = sizeof(sockaddr_in6) + 16; static constexpr size_t MAX_ADDR_SIZE = sizeof(sockaddr_in6) + 16;

View File

@ -17,7 +17,7 @@
#if TD_PORT_WINDOWS #if TD_PORT_WINDOWS
#include "td/utils/buffer.h" #include "td/utils/buffer.h"
#include "td/utils/port/detail/Iocp.h" #include "td/utils/port/detail/Iocp.h"
#include "td/utils/SpinLock.h" #include "td/utils/port/Mutex.h"
#include "td/utils/VectorQueue.h" #include "td/utils/VectorQueue.h"
#include <limits> #include <limits>
@ -164,7 +164,7 @@ class SocketFdImpl final : private Iocp::Callback {
private: private:
PollableFdInfo info_; PollableFdInfo info_;
SpinLock lock_; Mutex lock_;
std::atomic<int> refcnt_{1}; std::atomic<int> refcnt_{1};
bool close_flag_{false}; bool close_flag_{false};

View File

@ -18,7 +18,7 @@
#if TD_PORT_WINDOWS #if TD_PORT_WINDOWS
#include "td/utils/port/detail/Iocp.h" #include "td/utils/port/detail/Iocp.h"
#include "td/utils/SpinLock.h" #include "td/utils/port/Mutex.h"
#endif #endif
#if TD_PORT_POSIX #if TD_PORT_POSIX
@ -154,7 +154,7 @@ class UdpSocketFdImpl final : private Iocp::Callback {
private: private:
PollableFdInfo info_; PollableFdInfo info_;
SpinLock lock_; Mutex lock_;
std::atomic<int> refcnt_{1}; std::atomic<int> refcnt_{1};
bool is_connected_{false}; bool is_connected_{false};

View File

@ -12,8 +12,8 @@
#include "td/utils/logging.h" #include "td/utils/logging.h"
#include "td/utils/Observer.h" #include "td/utils/Observer.h"
#include "td/utils/port/detail/NativeFd.h" #include "td/utils/port/detail/NativeFd.h"
#include "td/utils/port/Mutex.h"
#include "td/utils/port/PollFlags.h" #include "td/utils/port/PollFlags.h"
#include "td/utils/SpinLock.h"
#include <atomic> #include <atomic>
#include <memory> #include <memory>
@ -140,7 +140,7 @@ class PollableFdInfo final : private ListNode {
std::atomic_flag lock_ = ATOMIC_FLAG_INIT; std::atomic_flag lock_ = ATOMIC_FLAG_INIT;
PollFlagsSet flags_; PollFlagsSet flags_;
#if TD_PORT_WINDOWS #if TD_PORT_WINDOWS
SpinLock observer_lock_; Mutex observer_lock_;
#endif #endif
ObserverBase *observer_{nullptr}; ObserverBase *observer_{nullptr};

View File

@ -7,6 +7,7 @@
#include "td/utils/benchmark.h" #include "td/utils/benchmark.h"
#include "td/utils/ConcurrentHashTable.h" #include "td/utils/ConcurrentHashTable.h"
#include "td/utils/misc.h" #include "td/utils/misc.h"
#include "td/utils/port/Mutex.h"
#include "td/utils/port/thread.h" #include "td/utils/port/thread.h"
#include "td/utils/SpinLock.h" #include "td/utils/SpinLock.h"
#include "td/utils/tests.h" #include "td/utils/tests.h"
@ -72,11 +73,11 @@ class ConcurrentHashMapMutex {
return "ConcurrentHashMapMutex"; return "ConcurrentHashMapMutex";
} }
void insert(KeyT key, ValueT value) { void insert(KeyT key, ValueT value) {
std::unique_lock<std::mutex> lock(mutex_); auto guard = mutex_.lock();
hash_map_.emplace(key, value); hash_map_.emplace(key, value);
} }
ValueT find(KeyT key, ValueT default_value) { ValueT find(KeyT key, ValueT default_value) {
std::unique_lock<std::mutex> lock(mutex_); auto guard = mutex_.lock();
auto it = hash_map_.find(key); auto it = hash_map_.find(key);
if (it == hash_map_.end()) { if (it == hash_map_.end()) {
return default_value; return default_value;
@ -85,7 +86,7 @@ class ConcurrentHashMapMutex {
} }
private: private:
std::mutex mutex_; Mutex mutex_;
#if TD_HAVE_ABSL #if TD_HAVE_ABSL
absl::flat_hash_map<KeyT, ValueT> hash_map_; absl::flat_hash_map<KeyT, ValueT> hash_map_;
#else #else