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 | |
Roger Meier | 4285ba2 | 2013-06-10 21:17:23 +0200 | [diff] [blame] | 20 | #include <thrift/concurrency/Mutex.h> |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 21 | |
cyy | ca8af9b | 2019-01-11 22:13:12 +0800 | [diff] [blame] | 22 | #include <chrono> |
| 23 | #include <mutex> |
yunfang | 1454296 | 2007-10-03 22:59:41 +0000 | [diff] [blame] | 24 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 25 | namespace apache { |
| 26 | namespace thrift { |
| 27 | namespace concurrency { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 28 | |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 29 | /** |
cyy | ca8af9b | 2019-01-11 22:13:12 +0800 | [diff] [blame] | 30 | * Implementation of Mutex class using C++11 std::timed_mutex |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 31 | * |
cyy | ca8af9b | 2019-01-11 22:13:12 +0800 | [diff] [blame] | 32 | * Methods throw std::system_error on error. |
James E. King, III | 00d4252 | 2017-04-04 09:32:45 -0400 | [diff] [blame] | 33 | * |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 34 | * @version $Id:$ |
| 35 | */ |
cyy | ca8af9b | 2019-01-11 22:13:12 +0800 | [diff] [blame] | 36 | class Mutex::impl : public std::timed_mutex {}; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 37 | |
cyy | ca8af9b | 2019-01-11 22:13:12 +0800 | [diff] [blame] | 38 | Mutex::Mutex() : impl_(new Mutex::impl()) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 39 | } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 40 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 41 | void* Mutex::getUnderlyingImpl() const { |
cyy | ca8af9b | 2019-01-11 22:13:12 +0800 | [diff] [blame] | 42 | return impl_.get(); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 43 | } |
David Reiss | b9db49c | 2010-03-09 05:19:30 +0000 | [diff] [blame] | 44 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 45 | void Mutex::lock() const { |
| 46 | impl_->lock(); |
| 47 | } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 48 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 49 | bool Mutex::trylock() const { |
cyy | ca8af9b | 2019-01-11 22:13:12 +0800 | [diff] [blame] | 50 | return impl_->try_lock(); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 51 | } |
boz | 5362e70 | 2007-08-15 20:55:36 +0000 | [diff] [blame] | 52 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 53 | bool Mutex::timedlock(int64_t ms) const { |
cyy | ca8af9b | 2019-01-11 22:13:12 +0800 | [diff] [blame] | 54 | return impl_->try_lock_for(std::chrono::milliseconds(ms)); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 55 | } |
David Reiss | 4e19f19 | 2010-03-09 05:19:59 +0000 | [diff] [blame] | 56 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 57 | void Mutex::unlock() const { |
| 58 | impl_->unlock(); |
| 59 | } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 60 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | } // apache::thrift::concurrency |