blob: 75802835d0bdc41561ee2491a08a08ca27329f4b [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
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 Slee9f0c6512007-02-28 23:58:26 +000019
Roger Meier4285ba22013-06-10 21:17:23 +020020#include <thrift/concurrency/Mutex.h>
Marc Slemko66949872006-07-15 01:52:39 +000021
cyyca8af9b2019-01-11 22:13:12 +080022#include <chrono>
23#include <mutex>
yunfang14542962007-10-03 22:59:41 +000024
Konrad Grochowski16a23a62014-11-13 15:33:38 +010025namespace apache {
26namespace thrift {
27namespace concurrency {
Marc Slemko66949872006-07-15 01:52:39 +000028
David Reiss0c90f6f2008-02-06 22:18:40 +000029/**
cyyca8af9b2019-01-11 22:13:12 +080030 * Implementation of Mutex class using C++11 std::timed_mutex
Mark Sleef5f2be42006-09-05 21:05:31 +000031 *
cyyca8af9b2019-01-11 22:13:12 +080032 * Methods throw std::system_error on error.
James E. King, III00d42522017-04-04 09:32:45 -040033 *
Mark Sleef5f2be42006-09-05 21:05:31 +000034 * @version $Id:$
35 */
cyyca8af9b2019-01-11 22:13:12 +080036class Mutex::impl : public std::timed_mutex {};
Marc Slemko66949872006-07-15 01:52:39 +000037
cyyca8af9b2019-01-11 22:13:12 +080038Mutex::Mutex() : impl_(new Mutex::impl()) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010039}
Marc Slemko66949872006-07-15 01:52:39 +000040
Konrad Grochowski16a23a62014-11-13 15:33:38 +010041void* Mutex::getUnderlyingImpl() const {
cyyca8af9b2019-01-11 22:13:12 +080042 return impl_.get();
Konrad Grochowski16a23a62014-11-13 15:33:38 +010043}
David Reissb9db49c2010-03-09 05:19:30 +000044
Konrad Grochowski16a23a62014-11-13 15:33:38 +010045void Mutex::lock() const {
46 impl_->lock();
47}
Marc Slemko66949872006-07-15 01:52:39 +000048
Konrad Grochowski16a23a62014-11-13 15:33:38 +010049bool Mutex::trylock() const {
cyyca8af9b2019-01-11 22:13:12 +080050 return impl_->try_lock();
Konrad Grochowski16a23a62014-11-13 15:33:38 +010051}
boz5362e702007-08-15 20:55:36 +000052
Konrad Grochowski16a23a62014-11-13 15:33:38 +010053bool Mutex::timedlock(int64_t ms) const {
cyyca8af9b2019-01-11 22:13:12 +080054 return impl_->try_lock_for(std::chrono::milliseconds(ms));
Konrad Grochowski16a23a62014-11-13 15:33:38 +010055}
David Reiss4e19f192010-03-09 05:19:59 +000056
Konrad Grochowski16a23a62014-11-13 15:33:38 +010057void Mutex::unlock() const {
58 impl_->unlock();
59}
Marc Slemko66949872006-07-15 01:52:39 +000060
Konrad Grochowski16a23a62014-11-13 15:33:38 +010061}
62}
63} // apache::thrift::concurrency