Roger Meier | 3faaedf | 2011-10-02 10:51:45 +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 | */ |
| 19 | |
Konrad Grochowski | 9be4e68 | 2013-06-22 22:03:31 +0200 | [diff] [blame] | 20 | #include <thrift/thrift-config.h> |
| 21 | |
Roger Meier | 4285ba2 | 2013-06-10 21:17:23 +0200 | [diff] [blame] | 22 | #include <thrift/concurrency/Mutex.h> |
| 23 | #include <thrift/concurrency/Util.h> |
Ben Craig | cd54ec6 | 2013-08-29 10:45:21 -0500 | [diff] [blame] | 24 | #include <thrift/Thrift.h> |
Roger Meier | 3faaedf | 2011-10-02 10:51:45 +0000 | [diff] [blame] | 25 | |
| 26 | #include <cassert> |
| 27 | #include <boost/thread.hpp> |
Roger Meier | 3831578 | 2011-11-06 11:29:41 +0000 | [diff] [blame] | 28 | #include <boost/thread/mutex.hpp> |
Roger Meier | 3faaedf | 2011-10-02 10:51:45 +0000 | [diff] [blame] | 29 | #include <boost/date_time/posix_time/posix_time.hpp> |
Roger Meier | 3faaedf | 2011-10-02 10:51:45 +0000 | [diff] [blame] | 30 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 31 | namespace apache { |
| 32 | namespace thrift { |
| 33 | namespace concurrency { |
Roger Meier | 3faaedf | 2011-10-02 10:51:45 +0000 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * Implementation of Mutex class using boost interprocess mutex |
| 37 | * |
| 38 | * @version $Id:$ |
| 39 | */ |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 40 | class Mutex::impl : public boost::timed_mutex {}; |
Roger Meier | 3faaedf | 2011-10-02 10:51:45 +0000 | [diff] [blame] | 41 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 42 | Mutex::Mutex(Initializer init) : impl_(new Mutex::impl()) { |
| 43 | THRIFT_UNUSED_VARIABLE(init); |
| 44 | } |
Roger Meier | 3faaedf | 2011-10-02 10:51:45 +0000 | [diff] [blame] | 45 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 46 | void* Mutex::getUnderlyingImpl() const { |
| 47 | return impl_.get(); |
| 48 | } |
Roger Meier | 3faaedf | 2011-10-02 10:51:45 +0000 | [diff] [blame] | 49 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 50 | void Mutex::lock() const { |
| 51 | impl_->lock(); |
| 52 | } |
Roger Meier | 3faaedf | 2011-10-02 10:51:45 +0000 | [diff] [blame] | 53 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 54 | bool Mutex::trylock() const { |
| 55 | return impl_->try_lock(); |
| 56 | } |
Roger Meier | 3faaedf | 2011-10-02 10:51:45 +0000 | [diff] [blame] | 57 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 58 | bool Mutex::timedlock(int64_t ms) const { |
| 59 | return impl_->timed_lock(boost::get_system_time() + boost::posix_time::milliseconds(ms)); |
| 60 | } |
Roger Meier | 3faaedf | 2011-10-02 10:51:45 +0000 | [diff] [blame] | 61 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 62 | void Mutex::unlock() const { |
| 63 | impl_->unlock(); |
| 64 | } |
Roger Meier | 3faaedf | 2011-10-02 10:51:45 +0000 | [diff] [blame] | 65 | |
| 66 | void Mutex::DEFAULT_INITIALIZER(void* arg) { |
Ben Craig | cd54ec6 | 2013-08-29 10:45:21 -0500 | [diff] [blame] | 67 | THRIFT_UNUSED_VARIABLE(arg); |
Roger Meier | 3faaedf | 2011-10-02 10:51:45 +0000 | [diff] [blame] | 68 | } |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | } // apache::thrift::concurrency |