SW Task Event Loop Framework v1.0.0
High-performance C++ asynchronous event loop framework with timer management and promise-based programming
Loading...
Searching...
No Matches
Timer.cpp
Go to the documentation of this file.
1#include "Timer.h"
2#include "SLLooper.h"
3#include <iostream>
4
5namespace swt {
6
7Timer::Timer(TimerId id, std::weak_ptr<SLLooper> looper)
8 : mId(id), mLooper(looper), mCancelled(false), mMoved(false) {
9 // std::cout << "[Timer] Created timer " << mId << std::endl;
10}
11
12Timer::Timer(Timer&& other) noexcept {
13 moveFrom(std::move(other));
14 // std::cout << "[Timer] Move constructed timer " << mId << " (from "
15 // << other.mId << ")" << std::endl;
16}
17
18Timer& Timer::operator=(Timer&& other) noexcept {
19 if (this != &other) {
20 // Cancel current timer trước khi move
21 if (!mMoved.load() && !mCancelled.load()) {
22 cancel();
23 }
24
25 moveFrom(std::move(other));
26 // std::cout << "[Timer] Move assigned timer " << mId << " (from "
27 // << other.mId << ")" << std::endl;
28 }
29 return *this;
30}
31
32void Timer::moveFrom(Timer&& other) noexcept {
33 mId = other.mId;
34 mLooper = other.mLooper;
35 mCancelled.store(other.mCancelled.load());
36 mMoved.store(false); // This object is not moved
37
38
39 if (auto looper = mLooper.lock()) {
40 looper->updateTimerCancelledPtr(mId, &mCancelled);
41 // std::cout << "[Timer] Updated TimerManager pointer for moved timer " << mId << std::endl;
42 }
43
44 // Mark source as moved
45 other.mMoved.store(true);
46 other.mCancelled.store(true); // Prevent double cancellation
47}
48
50 // Chỉ cancel nếu object này chưa bị moved
51 if (!mMoved.load() && !mCancelled.load()) {
52 // std::cout << "[Timer] Destructor cancelling timer " << mId << std::endl;
53 cancel();
54 } else if (mMoved.load()) {
55 // std::cout << "[Timer] Destructor skipping moved timer " << mId << std::endl;
56 }
57}
58
60 if (mMoved.load()) {
61 std::cout << "[Timer] Cannot cancel moved timer " << mId << std::endl;
62 return;
63 }
64
65 if (mCancelled.exchange(true)) {
66 return; // Already cancelled
67 }
68
69 if (auto looper = mLooper.lock()) {
70 std::cout << "[Timer] Cancelling timer " << mId << std::endl;
71 looper->cancelTimerInternal(mId);
72 }
73}
74
75bool Timer::isActive() const {
76 if (mMoved.load() || mCancelled.load()) {
77 return false;
78 }
79
80 if (auto looper = mLooper.lock()) {
81 return looper->hasTimerInternal(mId);
82 }
83 return false;
84}
85
86void Timer::restart(uint64_t delay_ms) {
87 if (mMoved.load()) {
88 // std::cout << "[Timer] Cannot restart moved timer " << mId << std::endl;
89 return;
90 }
91
92 if (auto looper = mLooper.lock()) {
93 // std::cout << "[Timer] Restarting timer " << mId << " with delay "
94 // << delay_ms << "ms" << std::endl;
95
96 if (looper->restartTimerInternal(mId, delay_ms)) {
97 mCancelled.store(false);
98 }
99 }
100}
101
102} // namespace swt
Main event loop coordinator for asynchronous task management and timer operations.
RAII timer wrapper with move semantics for safe timer management.
RAII timer wrapper with boost-style API and move semantics.
Definition Timer.h:68
bool isActive() const
Check if timer is currently active.
Definition Timer.cpp:75
void restart(uint64_t delay_ms)
Restart timer with new delay (one-shot timers only)
Definition Timer.cpp:86
void cancel()
Cancel the timer.
Definition Timer.cpp:59
Timer & operator=(const Timer &)=delete
Copy assignment - deleted (move-only semantics)
Timer(TimerId id, std::weak_ptr< SLLooper > looper)
Constructor - creates timer handle.
Definition Timer.cpp:7
~Timer()
Destructor - automatic timer cleanup.
Definition Timer.cpp:49
Software Timer namespace containing all timer-related classes.
Definition Awaitable.h:21
uint64_t TimerId
Unique identifier type for timer instances.
Definition Timer.h:25