File indexing completed on 2026-04-17 08:35:01
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020 #ifndef _THRIFT_CONCURRENCY_MUTEX_H_
0021 #define _THRIFT_CONCURRENCY_MUTEX_H_ 1
0022
0023 #include <memory>
0024 #include <thrift/TNonCopyable.h>
0025
0026 namespace apache {
0027 namespace thrift {
0028 namespace concurrency {
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040 class Mutex {
0041 public:
0042 Mutex();
0043 virtual ~Mutex() = default;
0044
0045 virtual void lock() const;
0046 virtual bool trylock() const;
0047 virtual bool timedlock(int64_t milliseconds) const;
0048 virtual void unlock() const;
0049
0050 void* getUnderlyingImpl() const;
0051
0052 private:
0053 class impl;
0054 std::shared_ptr<impl> impl_;
0055 };
0056
0057
0058 class Guard : apache::thrift::TNonCopyable {
0059 public:
0060 Guard(const Mutex& value, int64_t timeout = 0) : mutex_(&value) {
0061 if (timeout == 0) {
0062 value.lock();
0063 } else if (timeout < 0) {
0064 if (!value.trylock()) {
0065 mutex_ = nullptr;
0066 }
0067 } else {
0068 if (!value.timedlock(timeout)) {
0069 mutex_ = nullptr;
0070 }
0071 }
0072 }
0073 ~Guard() {
0074 if (mutex_) {
0075 mutex_->unlock();
0076 }
0077 }
0078
0079 operator bool() const { return (mutex_ != nullptr); }
0080
0081 private:
0082 const Mutex* mutex_;
0083 };
0084
0085 }
0086 }
0087 }
0088
0089 #endif