David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
Mark Slee | 9f0c651 | 2007-02-28 23:58:26 +0000 | [diff] [blame] | 19 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 20 | #ifndef _THRIFT_CONCURRENCY_MUTEX_H_ |
| 21 | #define _THRIFT_CONCURRENCY_MUTEX_H_ 1 |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 22 | |
Sutou Kouhei | 947ad66 | 2024-12-23 12:33:22 +0900 | [diff] [blame] | 23 | #include <cstdint> |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 24 | #include <memory> |
Mario Emmenlauer | d270b35 | 2020-11-19 09:43:34 +0100 | [diff] [blame] | 25 | #include <thrift/TNonCopyable.h> |
boz | 19cee90 | 2007-09-22 23:08:11 +0000 | [diff] [blame] | 26 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 27 | namespace apache { |
| 28 | namespace thrift { |
| 29 | namespace concurrency { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 30 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 31 | /** |
James E. King, III | 00d4252 | 2017-04-04 09:32:45 -0400 | [diff] [blame] | 32 | * NOTE: All mutex implementations throw an exception on failure. See each |
| 33 | * specific implementation to understand the exception type(s) used. |
| 34 | */ |
| 35 | |
| 36 | /** |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 37 | * A simple mutex class |
| 38 | * |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 39 | * @version $Id:$ |
| 40 | */ |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 41 | class Mutex { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 42 | public: |
cyy | ca8af9b | 2019-01-11 22:13:12 +0800 | [diff] [blame] | 43 | Mutex(); |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 44 | virtual ~Mutex() = default; |
James E. King, III | 00d4252 | 2017-04-04 09:32:45 -0400 | [diff] [blame] | 45 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 46 | virtual void lock() const; |
boz | 5362e70 | 2007-08-15 20:55:36 +0000 | [diff] [blame] | 47 | virtual bool trylock() const; |
David Reiss | 4e19f19 | 2010-03-09 05:19:59 +0000 | [diff] [blame] | 48 | virtual bool timedlock(int64_t milliseconds) const; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 49 | virtual void unlock() const; |
| 50 | |
David Reiss | b9db49c | 2010-03-09 05:19:30 +0000 | [diff] [blame] | 51 | void* getUnderlyingImpl() const; |
| 52 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 53 | private: |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 54 | class impl; |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 55 | std::shared_ptr<impl> impl_; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 56 | }; |
| 57 | |
Roger Meier | 3fc4819 | 2011-12-11 21:05:35 +0000 | [diff] [blame] | 58 | |
Mario Emmenlauer | d270b35 | 2020-11-19 09:43:34 +0100 | [diff] [blame] | 59 | class Guard : apache::thrift::TNonCopyable { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 60 | public: |
David Reiss | 4e19f19 | 2010-03-09 05:19:59 +0000 | [diff] [blame] | 61 | Guard(const Mutex& value, int64_t timeout = 0) : mutex_(&value) { |
| 62 | if (timeout == 0) { |
| 63 | value.lock(); |
| 64 | } else if (timeout < 0) { |
| 65 | if (!value.trylock()) { |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 66 | mutex_ = nullptr; |
David Reiss | 4e19f19 | 2010-03-09 05:19:59 +0000 | [diff] [blame] | 67 | } |
| 68 | } else { |
| 69 | if (!value.timedlock(timeout)) { |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 70 | mutex_ = nullptr; |
David Reiss | 4e19f19 | 2010-03-09 05:19:59 +0000 | [diff] [blame] | 71 | } |
| 72 | } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 73 | } |
boz | cce8184 | 2007-07-06 22:27:52 +0000 | [diff] [blame] | 74 | ~Guard() { |
David Reiss | 4e19f19 | 2010-03-09 05:19:59 +0000 | [diff] [blame] | 75 | if (mutex_) { |
| 76 | mutex_->unlock(); |
| 77 | } |
| 78 | } |
| 79 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 80 | operator bool() const { return (mutex_ != nullptr); } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 81 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 82 | private: |
David Reiss | 4e19f19 | 2010-03-09 05:19:59 +0000 | [diff] [blame] | 83 | const Mutex* mutex_; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 84 | }; |
| 85 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | } // apache::thrift::concurrency |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 89 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 90 | #endif // #ifndef _THRIFT_CONCURRENCY_MUTEX_H_ |