MySQL 8.0.39
Source Code Documentation
monitor.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2020, 2024, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef MYSQL_HARNESS_STDX_MONITOR_H_INCLUDED
27#define MYSQL_HARNESS_STDX_MONITOR_H_INCLUDED
28
29#include <condition_variable>
30#include <mutex>
31#include <utility> // move
32
33/**
34 * Monitor pattern.
35 *
36 * implemented based on Herb Sutters example.
37 */
38template <class T>
39class Monitor {
40 public:
41 Monitor(T t) : t_{std::move(t)} {}
42
43 template <class F>
44 auto operator()(F f) const {
45 std::lock_guard<std::mutex> lk{mtx_};
46
47 return f(t_);
48 }
49
50 protected:
51 mutable T t_;
52
53 mutable std::mutex mtx_;
54};
55
56/**
57 * Monitor can be waited for.
58 *
59 * wraps T by with Notifyable to add '.notify_one' to T.
60 */
61template <class T>
62class WaitableMonitor : public Monitor<T> {
63 public:
65
66 template <class F>
67 auto serialize_with_cv(F f) const {
68 std::lock_guard<std::mutex> lk{this->mtx_};
69
70 return f(this->t_, cv_);
71 }
72
73 /**
74 * wait_for time or pred is true.
75 *
76 * @param rel_time time to wait max
77 * @param pred invocable that receives the monitored T
78 */
79 template <class Rep, class Period, class Pred>
80 auto wait_for(const std::chrono::duration<Rep, Period> &rel_time, Pred pred) {
81 std::unique_lock<std::mutex> lk{this->mtx_};
82
83 return cv_.wait_for(lk, rel_time,
84 [this, pred]() { return pred(this->t_); });
85 }
86
87 template <class Pred>
88 auto wait(Pred pred) const {
89 std::unique_lock<std::mutex> lk{this->mtx_};
90
91 return cv_.wait(lk, [this, pred]() { return pred(this->t_); });
92 }
93
94 private:
95 mutable std::condition_variable cv_;
96};
97
98#endif
Monitor pattern.
Definition: monitor.h:39
Monitor(T t)
Definition: monitor.h:41
std::mutex mtx_
Definition: monitor.h:53
T t_
Definition: monitor.h:51
auto operator()(F f) const
Definition: monitor.h:44
Monitor can be waited for.
Definition: monitor.h:62
auto serialize_with_cv(F f) const
Definition: monitor.h:67
std::condition_variable cv_
Definition: monitor.h:95
auto wait(Pred pred) const
Definition: monitor.h:88
auto wait_for(const std::chrono::duration< Rep, Period > &rel_time, Pred pred)
wait_for time or pred is true.
Definition: monitor.h:80
Definition: gcs_xcom_synode.h:64