cxxmcp 1.1.6
C++ MCP SDK
Loading...
Searching...
No Matches
timer_handle.hpp
1// Copyright (c) 2025 [caomengxuan666]
2
3#pragma once
4
5#include <atomic>
6#include <chrono>
7#include <cstdint>
8#include <functional>
9#include <memory>
10
11namespace mcp::core {
12
14struct TimerEntry {
15 std::chrono::steady_clock::time_point when;
16 std::function<void()> task;
17 std::atomic<bool> cancelled{false};
18 std::uint64_t id = 0;
19 int priority = 1;
20};
21
28 public:
29 TimerHandle() = default;
30
31 explicit TimerHandle(std::shared_ptr<TimerEntry> entry)
32 : entry_(std::move(entry)) {}
33
35 bool valid() const noexcept { return entry_ != nullptr; }
36
38 void cancel() {
39 if (entry_) {
40 entry_->cancelled.store(true, std::memory_order_release);
41 }
42 }
43
45 bool cancelled() const noexcept {
46 return !entry_ || entry_->cancelled.load(std::memory_order_acquire);
47 }
48
49 private:
50 std::shared_ptr<TimerEntry> entry_;
51};
52
53} // namespace mcp::core
Handle to a scheduled timer that can be cancelled before it fires.
Definition timer_handle.hpp:27
void cancel()
Cancels the timer. The associated task will not execute.
Definition timer_handle.hpp:38
bool cancelled() const noexcept
Returns true if the timer has been cancelled.
Definition timer_handle.hpp:45
bool valid() const noexcept
Returns true if this handle references a valid timer entry.
Definition timer_handle.hpp:35
Internal entry for a scheduled timer task.
Definition timer_handle.hpp:14