blob: 12f1729d6bfe965341b6ea8a6dcc634811de2f74 [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
Sutou Kouhei947ad662024-12-23 12:33:22 +090023#include <cstdint>
cyy316723a2019-01-05 16:35:14 +080024#include <memory>
Mario Emmenlauerd270b352020-11-19 09:43:34 +010025#include <thrift/TNonCopyable.h>
boz19cee902007-09-22 23:08:11 +000026
Konrad Grochowski16a23a62014-11-13 15:33:38 +010027namespace apache {
28namespace thrift {
29namespace concurrency {
Marc Slemko66949872006-07-15 01:52:39 +000030
Mark Sleef5f2be42006-09-05 21:05:31 +000031/**
James E. King, III00d42522017-04-04 09:32:45 -040032 * 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 Sleef5f2be42006-09-05 21:05:31 +000037 * A simple mutex class
38 *
Mark Sleef5f2be42006-09-05 21:05:31 +000039 * @version $Id:$
40 */
Marc Slemko66949872006-07-15 01:52:39 +000041class Mutex {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010042public:
cyyca8af9b2019-01-11 22:13:12 +080043 Mutex();
Sebastian Zenker042580f2019-01-29 15:48:12 +010044 virtual ~Mutex() = default;
James E. King, III00d42522017-04-04 09:32:45 -040045
Marc Slemko66949872006-07-15 01:52:39 +000046 virtual void lock() const;
boz5362e702007-08-15 20:55:36 +000047 virtual bool trylock() const;
David Reiss4e19f192010-03-09 05:19:59 +000048 virtual bool timedlock(int64_t milliseconds) const;
Marc Slemko66949872006-07-15 01:52:39 +000049 virtual void unlock() const;
50
David Reissb9db49c2010-03-09 05:19:30 +000051 void* getUnderlyingImpl() const;
52
Konrad Grochowski16a23a62014-11-13 15:33:38 +010053private:
Marc Slemko66949872006-07-15 01:52:39 +000054 class impl;
cyy316723a2019-01-05 16:35:14 +080055 std::shared_ptr<impl> impl_;
Marc Slemko66949872006-07-15 01:52:39 +000056};
57
Roger Meier3fc48192011-12-11 21:05:35 +000058
Mario Emmenlauerd270b352020-11-19 09:43:34 +010059class Guard : apache::thrift::TNonCopyable {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010060public:
David Reiss4e19f192010-03-09 05:19:59 +000061 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 Zenker042580f2019-01-29 15:48:12 +010066 mutex_ = nullptr;
David Reiss4e19f192010-03-09 05:19:59 +000067 }
68 } else {
69 if (!value.timedlock(timeout)) {
Sebastian Zenker042580f2019-01-29 15:48:12 +010070 mutex_ = nullptr;
David Reiss4e19f192010-03-09 05:19:59 +000071 }
72 }
Marc Slemko66949872006-07-15 01:52:39 +000073 }
bozcce81842007-07-06 22:27:52 +000074 ~Guard() {
David Reiss4e19f192010-03-09 05:19:59 +000075 if (mutex_) {
76 mutex_->unlock();
77 }
78 }
79
Sebastian Zenker042580f2019-01-29 15:48:12 +010080 operator bool() const { return (mutex_ != nullptr); }
Marc Slemko66949872006-07-15 01:52:39 +000081
Konrad Grochowski16a23a62014-11-13 15:33:38 +010082private:
David Reiss4e19f192010-03-09 05:19:59 +000083 const Mutex* mutex_;
Marc Slemko66949872006-07-15 01:52:39 +000084};
85
Konrad Grochowski16a23a62014-11-13 15:33:38 +010086}
87}
88} // apache::thrift::concurrency
Marc Slemko66949872006-07-15 01:52:39 +000089
Mark Sleef5f2be42006-09-05 21:05:31 +000090#endif // #ifndef _THRIFT_CONCURRENCY_MUTEX_H_