-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #101 from redboltz/refine_close
Refine close
- Loading branch information
Showing
3 changed files
with
113 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright Takatoshi Kondo 2023 | ||
// | ||
// Distributed under the Boost Software License, Version 1.0. | ||
// (See accompanying file LICENSE_1_0.txt or copy at | ||
// http://www.boost.org/LICENSE_1_0.txt) | ||
|
||
#if !defined(ASYNC_MQTT_UTIL_IOC_QUEUE_HPP) | ||
#define ASYNC_MQTT_UTIL_IOC_QUEUE_HPP | ||
|
||
#include <boost/asio.hpp> | ||
|
||
#include <async_mqtt/util/optional.hpp> | ||
|
||
namespace async_mqtt { | ||
|
||
namespace as = boost::asio; | ||
|
||
class ioc_queue { | ||
public: | ||
ioc_queue() { | ||
queue_.stop(); | ||
} | ||
|
||
void start_work() { | ||
working_ = true; | ||
guard_.emplace(queue_.get_executor()); | ||
} | ||
|
||
void stop_work() { | ||
guard_.reset(); | ||
} | ||
|
||
template <typename CompletionToken> | ||
void post(CompletionToken&& token) { | ||
as::post( | ||
queue_, | ||
std::forward<CompletionToken>(token) | ||
); | ||
if (!working_ && queue_.stopped()) { | ||
queue_.restart(); | ||
queue_.poll_one(); | ||
} | ||
} | ||
|
||
bool stopped() const { | ||
return queue_.stopped(); | ||
} | ||
|
||
void poll_one() { | ||
working_ = false; | ||
if (queue_.stopped()) queue_.restart(); | ||
queue_.poll_one(); | ||
} | ||
|
||
private: | ||
as::io_context queue_; | ||
bool working_ = false; | ||
optional<as::executor_work_guard<as::io_context::executor_type>> guard_; | ||
}; | ||
|
||
} // namespace async_mqtt | ||
|
||
#endif // ASYNC_MQTT_UTIL_IOC_QUEUE_HPP |