cpp-msgpack-rpc 0.2.0
An RPC library implementing MessagePack RPC.
Loading...
Searching...
No Matches
background_task_state_machine.h
Go to the documentation of this file.
1/*
2 * Copyright 2023 MusicScience37 (Kenta Kabashima)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
20#pragma once
21
22#include <atomic>
23
26
27namespace msgpack_rpc::transport {
28
34public:
36 enum class State {
39
42
45
48 };
49
52
57 auto expected_state = State::INIT;
58 const auto next_state = State::STARTING;
59 if (!state_.compare_exchange_strong(
60 expected_state, next_state, std::memory_order_relaxed)) {
62 StatusCode::PRECONDITION_NOT_MET, "Already started.");
63 }
64 }
65
70 state_.store(State::PROCESSING, std::memory_order_release);
71 }
72
80 return state_.exchange(State::STOPPED, std::memory_order_relaxed) !=
82 }
83
88 state_.store(State::STOPPED, std::memory_order_relaxed);
89 }
90
97 [[nodiscard]] bool is_processing() const noexcept {
98 return state_.load(std::memory_order_acquire) == State::PROCESSING;
99 }
100
101private:
103 std::atomic<State> state_{State::INIT};
104};
105
106} // namespace msgpack_rpc::transport
Class of exceptions in cpp-msgpack-rpc library.
void handle_processing_stopped()
Handle the condition that processing has been stopped.
std::atomic< State > state_
Whether the process of this connection is started.
bool is_processing() const noexcept
Check whether processing.
void handle_start_request()
Handle the condition that starting processing is requested.
void handle_processing_started()
Handle the condition that processing is started.
bool handle_stop_requested()
Handle the condition that stopping processing is requested.
Definition of MsgpackRPCException class.
Namespace of transport of messages.
Definition backends.h:31
Definition of StatusCode enumeration.