MySQL 8.3.0
Source Code Documentation
monitor.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2020, 2023, 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 also distributed 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 included with MySQL.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23*/
24
25#ifndef MYSQL_HARNESS_STDX_MONITOR_H_INCLUDED
26#define MYSQL_HARNESS_STDX_MONITOR_H_INCLUDED
27
28#include <condition_variable>
29#include <mutex>
30#include <utility> // move
31
32/**
33 * Monitor pattern.
34 *
35 * implemented based on Herb Sutters example.
36 */
37template <class T>
38class Monitor {
39 public:
40 Monitor(T t) : t_{std::move(t)} {}
41
42 template <class F>
43 auto operator()(F f) const {
44 std::lock_guard<std::mutex> lk{mtx_};
45
46 return f(t_);
47 }
48
49 protected:
50 mutable T t_;
51
52 mutable std::mutex mtx_;
53};
54
55/**
56 * Monitor can be waited for.
57 *
58 * wraps T by with Notifyable to add '.notify_one' to T.
59 */
60template <class T>
61class WaitableMonitor : public Monitor<T> {
62 public:
64
65 template <class F>
66 auto serialize_with_cv(F f) const {
67 std::lock_guard<std::mutex> lk{this->mtx_};
68
69 return f(this->t_, cv_);
70 }
71
72 /**
73 * wait_for time or pred is true.
74 *
75 * @param rel_time time to wait max
76 * @param pred invocable that receives the monitored T
77 */
78 template <class Rep, class Period, class Pred>
79 auto wait_for(const std::chrono::duration<Rep, Period> &rel_time, Pred pred) {
80 std::unique_lock<std::mutex> lk{this->mtx_};
81
82 return cv_.wait_for(lk, rel_time,
83 [this, pred]() { return pred(this->t_); });
84 }
85
86 template <class Pred>
87 auto wait(Pred pred) const {
88 std::unique_lock<std::mutex> lk{this->mtx_};
89
90 return cv_.wait(lk, [this, pred]() { return pred(this->t_); });
91 }
92
93 private:
94 mutable std::condition_variable cv_;
95};
96
97#endif
Monitor pattern.
Definition: monitor.h:38
Monitor(T t)
Definition: monitor.h:40
std::mutex mtx_
Definition: monitor.h:52
T t_
Definition: monitor.h:50
auto operator()(F f) const
Definition: monitor.h:43
Monitor can be waited for.
Definition: monitor.h:61
auto serialize_with_cv(F f) const
Definition: monitor.h:66
std::condition_variable cv_
Definition: monitor.h:94
auto wait(Pred pred) const
Definition: monitor.h:87
auto wait_for(const std::chrono::duration< Rep, Period > &rel_time, Pred pred)
wait_for time or pred is true.
Definition: monitor.h:79
Definition: varlen_sort.h:174