]> git.proxmox.com Git - ceph.git/blob - ceph/src/seastar/src/core/semaphore.cc
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / seastar / src / core / semaphore.cc
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) 2020 Cloudius Systems, Ltd.
20 */
21
22 #include <fmt/format.h>
23 #include <seastar/core/semaphore.hh>
24
25 namespace seastar {
26
27 // Exception Factory for standard semaphore
28 //
29 // constructs standard semaphore exceptions
30 // \see semaphore_timed_out and broken_semaphore
31
32 static_assert(std::is_nothrow_default_constructible_v<semaphore_default_exception_factory>);
33 static_assert(std::is_nothrow_move_constructible_v<semaphore_default_exception_factory>);
34
35 static_assert(std::is_nothrow_constructible_v<semaphore, size_t>);
36 static_assert(std::is_nothrow_constructible_v<semaphore, size_t, semaphore_default_exception_factory&&>);
37 static_assert(std::is_nothrow_move_constructible_v<semaphore>);
38
39
40 const char* broken_semaphore::what() const noexcept {
41 return "Semaphore broken";
42 }
43
44 const char* semaphore_timed_out::what() const noexcept {
45 return "Semaphore timedout";
46 }
47
48 const char* semaphore_aborted::what() const noexcept {
49 return "Semaphore aborted";
50 }
51
52 semaphore_timed_out semaphore_default_exception_factory::timeout() noexcept {
53 static_assert(std::is_nothrow_default_constructible_v<semaphore_timed_out>);
54 return semaphore_timed_out();
55 }
56
57 broken_semaphore semaphore_default_exception_factory::broken() noexcept {
58 static_assert(std::is_nothrow_default_constructible_v<broken_semaphore>);
59 return broken_semaphore();
60 }
61
62 semaphore_aborted semaphore_default_exception_factory::aborted() noexcept {
63 static_assert(std::is_nothrow_default_constructible_v<semaphore_aborted>);
64 return semaphore_aborted();
65 }
66
67 // A factory of semaphore exceptions that contain additional context: the semaphore name
68 // auto sem = named_semaphore(0, named_semaphore_exception_factory{"file_opening_limit_semaphore"});
69
70 static_assert(std::is_nothrow_default_constructible_v<named_semaphore_exception_factory>);
71 static_assert(std::is_nothrow_move_constructible_v<named_semaphore_exception_factory>);
72
73 static_assert(std::is_nothrow_constructible_v<named_semaphore, size_t>);
74 static_assert(std::is_nothrow_constructible_v<named_semaphore, size_t, named_semaphore_exception_factory&&>);
75 static_assert(std::is_nothrow_move_constructible_v<named_semaphore>);
76
77 named_semaphore_timed_out::named_semaphore_timed_out(std::string_view msg) noexcept : _msg() {
78 try {
79 _msg = format("Semaphore timed out: {}", msg);
80 } catch (...) {
81 // ignore, empty _msg will generate a static message in what().
82 }
83 }
84
85 broken_named_semaphore::broken_named_semaphore(std::string_view msg) noexcept : _msg() {
86 try {
87 _msg = format("Semaphore broken: {}", msg);
88 } catch (...) {
89 // ignore, empty _msg will generate a static message in what().
90 }
91 }
92
93 named_semaphore_aborted::named_semaphore_aborted(std::string_view msg) noexcept : _msg() {
94 try {
95 _msg = format("Semaphore aborted: {}", msg);
96 } catch (...) {
97 // ignore, empty _msg will generate a static message in what().
98 }
99 }
100
101 const char* named_semaphore_timed_out::what() const noexcept {
102 // return a static message if generating the dynamic message failed.
103 return _msg.empty() ? "Named semaphore timed out" : _msg.c_str();
104 }
105
106 const char* broken_named_semaphore::what() const noexcept {
107 // return a static message if generating the dynamic message failed.
108 return _msg.empty() ? "Broken named semaphore" : _msg.c_str();
109 }
110
111 const char* named_semaphore_aborted::what() const noexcept {
112 // return a static message if generating the dynamic message failed.
113 return _msg.empty() ? "Named semaphore aborted" : _msg.c_str();
114 }
115
116 named_semaphore_timed_out named_semaphore_exception_factory::timeout() const noexcept {
117 return named_semaphore_timed_out(name);
118 }
119
120 broken_named_semaphore named_semaphore_exception_factory::broken() const noexcept {
121 return broken_named_semaphore(name);
122 }
123
124 named_semaphore_aborted named_semaphore_exception_factory::aborted() const noexcept {
125 return named_semaphore_aborted(name);
126 }
127
128 } // namespace seastar