add Mutex wrapper around std::mutex and use it instead of SpinLock
This commit is contained in:
parent
7ddc3099f6
commit
f4c97b25ec
@ -138,6 +138,7 @@ set(TDUTILS_SOURCE
|
||||
td/utils/port/IPAddress.h
|
||||
td/utils/port/IoSlice.h
|
||||
td/utils/port/MemoryMapping.h
|
||||
td/utils/port/Mutex.h
|
||||
td/utils/port/path.h
|
||||
td/utils/port/platform.h
|
||||
td/utils/port/Poll.h
|
||||
|
@ -12,7 +12,7 @@
|
||||
|
||||
#if !TD_EVENTFD_UNSUPPORTED
|
||||
|
||||
#include "td/utils/SpinLock.h"
|
||||
#include "td/utils/port/Mutex.h"
|
||||
|
||||
#include <utility>
|
||||
|
||||
@ -91,7 +91,7 @@ class MpscPollableQueue {
|
||||
}
|
||||
|
||||
private:
|
||||
SpinLock lock_;
|
||||
Mutex lock_;
|
||||
bool wait_event_fd_{false};
|
||||
EventFd event_fd_;
|
||||
std::vector<ValueType> writer_vector_;
|
||||
|
20
tdutils/td/utils/port/Mutex.h
Normal file
20
tdutils/td/utils/port/Mutex.h
Normal 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
|
@ -29,7 +29,7 @@
|
||||
|
||||
#if TD_PORT_WINDOWS
|
||||
#include "td/utils/port/detail/Iocp.h"
|
||||
#include "td/utils/SpinLock.h"
|
||||
#include "td/utils/port/Mutex.h"
|
||||
#include "td/utils/VectorQueue.h"
|
||||
#endif
|
||||
|
||||
@ -87,7 +87,7 @@ class ServerSocketFdImpl final : private Iocp::Callback {
|
||||
private:
|
||||
PollableFdInfo info_;
|
||||
|
||||
SpinLock lock_;
|
||||
Mutex lock_;
|
||||
VectorQueue<SocketFd> accepted_;
|
||||
VectorQueue<Status> pending_errors_;
|
||||
static constexpr size_t MAX_ADDR_SIZE = sizeof(sockaddr_in6) + 16;
|
||||
|
@ -17,7 +17,7 @@
|
||||
#if TD_PORT_WINDOWS
|
||||
#include "td/utils/buffer.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 <limits>
|
||||
@ -164,7 +164,7 @@ class SocketFdImpl final : private Iocp::Callback {
|
||||
|
||||
private:
|
||||
PollableFdInfo info_;
|
||||
SpinLock lock_;
|
||||
Mutex lock_;
|
||||
|
||||
std::atomic<int> refcnt_{1};
|
||||
bool close_flag_{false};
|
||||
|
@ -18,7 +18,7 @@
|
||||
|
||||
#if TD_PORT_WINDOWS
|
||||
#include "td/utils/port/detail/Iocp.h"
|
||||
#include "td/utils/SpinLock.h"
|
||||
#include "td/utils/port/Mutex.h"
|
||||
#endif
|
||||
|
||||
#if TD_PORT_POSIX
|
||||
@ -154,7 +154,7 @@ class UdpSocketFdImpl final : private Iocp::Callback {
|
||||
|
||||
private:
|
||||
PollableFdInfo info_;
|
||||
SpinLock lock_;
|
||||
Mutex lock_;
|
||||
|
||||
std::atomic<int> refcnt_{1};
|
||||
bool is_connected_{false};
|
||||
|
@ -12,8 +12,8 @@
|
||||
#include "td/utils/logging.h"
|
||||
#include "td/utils/Observer.h"
|
||||
#include "td/utils/port/detail/NativeFd.h"
|
||||
#include "td/utils/port/Mutex.h"
|
||||
#include "td/utils/port/PollFlags.h"
|
||||
#include "td/utils/SpinLock.h"
|
||||
|
||||
#include <atomic>
|
||||
#include <memory>
|
||||
@ -140,7 +140,7 @@ class PollableFdInfo final : private ListNode {
|
||||
std::atomic_flag lock_ = ATOMIC_FLAG_INIT;
|
||||
PollFlagsSet flags_;
|
||||
#if TD_PORT_WINDOWS
|
||||
SpinLock observer_lock_;
|
||||
Mutex observer_lock_;
|
||||
#endif
|
||||
ObserverBase *observer_{nullptr};
|
||||
|
||||
|
@ -7,6 +7,7 @@
|
||||
#include "td/utils/benchmark.h"
|
||||
#include "td/utils/ConcurrentHashTable.h"
|
||||
#include "td/utils/misc.h"
|
||||
#include "td/utils/port/Mutex.h"
|
||||
#include "td/utils/port/thread.h"
|
||||
#include "td/utils/SpinLock.h"
|
||||
#include "td/utils/tests.h"
|
||||
@ -72,11 +73,11 @@ class ConcurrentHashMapMutex {
|
||||
return "ConcurrentHashMapMutex";
|
||||
}
|
||||
void insert(KeyT key, ValueT value) {
|
||||
std::unique_lock<std::mutex> lock(mutex_);
|
||||
auto guard = mutex_.lock();
|
||||
hash_map_.emplace(key, 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);
|
||||
if (it == hash_map_.end()) {
|
||||
return default_value;
|
||||
@ -85,7 +86,7 @@ class ConcurrentHashMapMutex {
|
||||
}
|
||||
|
||||
private:
|
||||
std::mutex mutex_;
|
||||
Mutex mutex_;
|
||||
#if TD_HAVE_ABSL
|
||||
absl::flat_hash_map<KeyT, ValueT> hash_map_;
|
||||
#else
|
||||
|
Loading…
Reference in New Issue
Block a user