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.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
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
19 * Copyright (C) 2020 Cloudius Systems, Ltd.
22 #include <fmt/format.h>
23 #include <seastar/core/semaphore.hh>
27 // Exception Factory for standard semaphore
29 // constructs standard semaphore exceptions
30 // \see semaphore_timed_out and broken_semaphore
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
>);
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
>);
40 const char* broken_semaphore::what() const noexcept
{
41 return "Semaphore broken";
44 const char* semaphore_timed_out::what() const noexcept
{
45 return "Semaphore timedout";
48 const char* semaphore_aborted::what() const noexcept
{
49 return "Semaphore aborted";
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();
57 broken_semaphore
semaphore_default_exception_factory::broken() noexcept
{
58 static_assert(std::is_nothrow_default_constructible_v
<broken_semaphore
>);
59 return broken_semaphore();
62 semaphore_aborted
semaphore_default_exception_factory::aborted() noexcept
{
63 static_assert(std::is_nothrow_default_constructible_v
<semaphore_aborted
>);
64 return semaphore_aborted();
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"});
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
>);
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
>);
77 named_semaphore_timed_out::named_semaphore_timed_out(std::string_view msg
) noexcept
: _msg() {
79 _msg
= format("Semaphore timed out: {}", msg
);
81 // ignore, empty _msg will generate a static message in what().
85 broken_named_semaphore::broken_named_semaphore(std::string_view msg
) noexcept
: _msg() {
87 _msg
= format("Semaphore broken: {}", msg
);
89 // ignore, empty _msg will generate a static message in what().
93 named_semaphore_aborted::named_semaphore_aborted(std::string_view msg
) noexcept
: _msg() {
95 _msg
= format("Semaphore aborted: {}", msg
);
97 // ignore, empty _msg will generate a static message in what().
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();
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();
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();
116 named_semaphore_timed_out
named_semaphore_exception_factory::timeout() const noexcept
{
117 return named_semaphore_timed_out(name
);
120 broken_named_semaphore
named_semaphore_exception_factory::broken() const noexcept
{
121 return broken_named_semaphore(name
);
124 named_semaphore_aborted
named_semaphore_exception_factory::aborted() const noexcept
{
125 return named_semaphore_aborted(name
);
128 } // namespace seastar