Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-17 08:35:01

0001 /*
0002  * Licensed to the Apache Software Foundation (ASF) under one
0003  * or more contributor license agreements. See the NOTICE file
0004  * distributed with this work for additional information
0005  * regarding copyright ownership. The ASF licenses this file
0006  * to you under the Apache License, Version 2.0 (the
0007  * "License"); you may not use this file except in compliance
0008  * with the License. You may obtain a copy of the License at
0009  *
0010  *   http://www.apache.org/licenses/LICENSE-2.0
0011  *
0012  * Unless required by applicable law or agreed to in writing,
0013  * software distributed under the License is distributed on an
0014  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
0015  * KIND, either express or implied. See the License for the
0016  * specific language governing permissions and limitations
0017  * under the License.
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  * NOTE: All mutex implementations throw an exception on failure.  See each
0032  *       specific implementation to understand the exception type(s) used.
0033  */
0034 
0035 /**
0036  * A simple mutex class
0037  *
0038  * @version $Id:$
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 } // apache::thrift::concurrency
0088 
0089 #endif // #ifndef _THRIFT_CONCURRENCY_MUTEX_H_