Seastar
High performance C++ framework for concurrent servers
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
closeable.hh
Go to the documentation of this file.
1/*
2 * This file is open source software, licensed to you under the terms
3 * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4 * distributed with this work for additional information regarding copyright
5 * ownership. You may not use this file except in compliance with the License.
6 *
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing,
12 * software distributed under the License is distributed on an
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 * KIND, either express or implied. See the License for the
15 * specific language governing permissions and limitations
16 * under the License.
17 */
18/*
19 * Copyright (C) 2021 Cloudius Systems, Ltd.
20 */
21
22#pragma once
23
24#include <concepts>
25#include <functional>
26#include <seastar/core/future.hh>
27#include <seastar/util/defer.hh>
28
30
32
33namespace seastar {
34
35template <typename Object>
36concept closeable = requires (Object o) {
37 { o.close() } SEASTAR_DEFERRED_ACTION_NOEXCEPT -> std::same_as<future<>>;
38};
39
47template <typename Object>
48requires closeable<Object>
49class [[nodiscard]] deferred_close {
50 std::reference_wrapper<Object> _obj;
51 bool _closed = false;
52
53 void do_close() noexcept {
54 if (!_closed) {
55 _closed = true;
56 _obj.get().close().get();
57 }
58 }
59public:
62 deferred_close(Object& obj) noexcept : _obj(obj) {}
65 deferred_close(deferred_close&& x) noexcept : _obj(x._obj), _closed(std::exchange(x._closed, true)) {}
66 deferred_close(const deferred_close&) = delete;
71 do_close();
72 _obj = x._obj;
73 _closed = std::exchange(x._closed, true);
74 return *this;
75 }
78 do_close();
79 }
81 void close_now() noexcept {
82 assert(!_closed);
83 do_close();
84 }
85
88 void cancel() noexcept {
89 _closed = true;
90 }
91};
92
93template <closeable Closeable, std::invocable<Closeable&> Func>
94requires std::is_nothrow_move_constructible_v<Closeable> && std::is_nothrow_move_constructible_v<Func>
95inline futurize_t<std::invoke_result_t<Func, Closeable&>>
96with_closeable(Closeable&& obj, Func func) noexcept {
97 return do_with(std::move(obj), [func = std::move(func)] (Closeable& obj) mutable {
98 return futurize_invoke(func, obj).finally([&obj] {
99 return obj.close();
100 });
101 });
102}
103
104template <typename Object>
105concept stoppable = requires (Object o) {
106 { o.stop() } SEASTAR_DEFERRED_ACTION_NOEXCEPT -> std::same_as<future<>>;
107};
108
116template <typename Object>
117requires stoppable<Object>
118class [[nodiscard]] deferred_stop {
119 std::reference_wrapper<Object> _obj;
120 bool _stopped = false;
121
122 void do_stop() noexcept {
123 if (!_stopped) {
124 _stopped = true;
125 _obj.get().stop().get();
126 }
127 }
128public:
131 deferred_stop(Object& obj) noexcept : _obj(obj) {}
134 deferred_stop(deferred_stop&& x) noexcept : _obj(x._obj), _stopped(std::exchange(x._stopped, true)) {}
135 deferred_stop(const deferred_stop&) = delete;
140 do_stop();
141 _obj = x._obj;
142 _stopped = std::exchange(x._stopped, true);
143 return *this;
144 }
147 do_stop();
148 }
150 void stop_now() noexcept {
151 assert(!_stopped);
152 do_stop();
153 }
154
157 void cancel() noexcept {
158 _stopped = true;
159 }
160};
161
162template <stoppable Stoppable, std::invocable<Stoppable&> Func>
163requires std::is_nothrow_move_constructible_v<Stoppable> && std::is_nothrow_move_constructible_v<Func>
164inline futurize_t<std::invoke_result_t<Func, Stoppable&>>
165with_stoppable(Stoppable&& obj, Func func) noexcept {
166 return do_with(std::move(obj), [func = std::move(func)] (Stoppable& obj) mutable {
167 return futurize_invoke(func, obj).finally([&obj] {
168 return obj.stop();
169 });
170 });
171}
172
173} // namespace seastar
Definition: closeable.hh:49
void close_now() noexcept
Close obj once now.
Definition: closeable.hh:81
deferred_close & operator=(deferred_close &&x) noexcept
Definition: closeable.hh:70
void cancel() noexcept
Definition: closeable.hh:88
deferred_close(deferred_close &&x) noexcept
Definition: closeable.hh:65
~deferred_close()
Destruct the deferred_close object and auto-close obj.
Definition: closeable.hh:77
deferred_close(Object &obj) noexcept
Definition: closeable.hh:62
Definition: closeable.hh:118
deferred_stop(Object &obj) noexcept
Definition: closeable.hh:131
void stop_now() noexcept
Stop obj once now.
Definition: closeable.hh:150
deferred_stop(deferred_stop &&x) noexcept
Definition: closeable.hh:134
~deferred_stop()
Destruct the deferred_stop object and auto-stop obj.
Definition: closeable.hh:146
void cancel() noexcept
Definition: closeable.hh:157
deferred_stop & operator=(deferred_stop &&x) noexcept
Definition: closeable.hh:139
auto do_with(T1 &&rv1, T2 &&rv2, More &&... more) noexcept
Definition: do_with.hh:135
Seastar API namespace.
Definition: abort_on_ebadf.hh:26