blob: 1e5c3fba37cd9c3597a1b2e6ae325839da76d3bc [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
Mark Sleef5f2be42006-09-05 21:05:31 +000020#ifndef _THRIFT_CONCURRENCY_MUTEX_H_
21#define _THRIFT_CONCURRENCY_MUTEX_H_ 1
Marc Slemko66949872006-07-15 01:52:39 +000022
cyy316723a2019-01-05 16:35:14 +080023#include <memory>
Mario Emmenlauerd270b352020-11-19 09:43:34 +010024#include <thrift/TNonCopyable.h>
boz19cee902007-09-22 23:08:11 +000025
Konrad Grochowski16a23a62014-11-13 15:33:38 +010026namespace apache {
27namespace thrift {
28namespace concurrency {
Marc Slemko66949872006-07-15 01:52:39 +000029
Mark Sleef5f2be42006-09-05 21:05:31 +000030/**
James E. King, III00d42522017-04-04 09:32:45 -040031 * NOTE: All mutex implementations throw an exception on failure. See each
32 * specific implementation to understand the exception type(s) used.
33 */
34
35/**
Mark Sleef5f2be42006-09-05 21:05:31 +000036 * A simple mutex class
37 *
Mark Sleef5f2be42006-09-05 21:05:31 +000038 * @version $Id:$
39 */
Marc Slemko66949872006-07-15 01:52:39 +000040class Mutex {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010041public:
cyyca8af9b2019-01-11 22:13:12 +080042 Mutex();
Sebastian Zenker042580f2019-01-29 15:48:12 +010043 virtual ~Mutex() = default;
James E. King, III00d42522017-04-04 09:32:45 -040044
Marc Slemko66949872006-07-15 01:52:39 +000045 virtual void lock() const;
boz5362e702007-08-15 20:55:36 +000046 virtual bool trylock() const;
David Reiss4e19f192010-03-09 05:19:59 +000047 virtual bool timedlock(int64_t milliseconds) const;
Marc Slemko66949872006-07-15 01:52:39 +000048 virtual void unlock() const;
49
David Reissb9db49c2010-03-09 05:19:30 +000050 void* getUnderlyingImpl() const;
51
Konrad Grochowski16a23a62014-11-13 15:33:38 +010052private:
Marc Slemko66949872006-07-15 01:52:39 +000053 class impl;
cyy316723a2019-01-05 16:35:14 +080054 std::shared_ptr<impl> impl_;
Marc Slemko66949872006-07-15 01:52:39 +000055};
56
Roger Meier3fc48192011-12-11 21:05:35 +000057
Mario Emmenlauerd270b352020-11-19 09:43:34 +010058class Guard : apache::thrift::TNonCopyable {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010059public:
David Reiss4e19f192010-03-09 05:19:59 +000060 Guard(const Mutex& value, int64_t timeout = 0) : mutex_(&value) {
61 if (timeout == 0) {
62 value.lock();
63 } else if (timeout < 0) {
64 if (!value.trylock()) {
Sebastian Zenker042580f2019-01-29 15:48:12 +010065 mutex_ = nullptr;
David Reiss4e19f192010-03-09 05:19:59 +000066 }
67 } else {
68 if (!value.timedlock(timeout)) {
Sebastian Zenker042580f2019-01-29 15:48:12 +010069 mutex_ = nullptr;
David Reiss4e19f192010-03-09 05:19:59 +000070 }
71 }
Marc Slemko66949872006-07-15 01:52:39 +000072 }
bozcce81842007-07-06 22:27:52 +000073 ~Guard() {
David Reiss4e19f192010-03-09 05:19:59 +000074 if (mutex_) {
75 mutex_->unlock();
76 }
77 }
78
Sebastian Zenker042580f2019-01-29 15:48:12 +010079 operator bool() const { return (mutex_ != nullptr); }
Marc Slemko66949872006-07-15 01:52:39 +000080
Konrad Grochowski16a23a62014-11-13 15:33:38 +010081private:
David Reiss4e19f192010-03-09 05:19:59 +000082 const Mutex* mutex_;
Marc Slemko66949872006-07-15 01:52:39 +000083};
84
Konrad Grochowski16a23a62014-11-13 15:33:38 +010085}
86}
87} // apache::thrift::concurrency
Marc Slemko66949872006-07-15 01:52:39 +000088
Mark Sleef5f2be42006-09-05 21:05:31 +000089#endif // #ifndef _THRIFT_CONCURRENCY_MUTEX_H_