1+ /* *
2+ * MIT License
3+ *
4+ * Copyright (c) 2025 Huawei Technologies Co., Ltd. All rights reserved.
5+ *
6+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7+ * of this software and associated documentation files (the "Software"), to deal
8+ * in the Software without restriction, including without limitation the rights
9+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+ * copies of the Software, and to permit persons to whom the Software is
11+ * furnished to do so, subject to the following conditions:
12+ *
13+ * The above copyright notice and this permission notice shall be included in all
14+ * copies or substantial portions of the Software.
15+ *
16+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+ * SOFTWARE.
23+ * */
24+ #ifndef UNIFIEDCACHE_TIMER_H
25+ #define UNIFIEDCACHE_TIMER_H
26+
27+ #include < chrono>
28+ #include < thread>
29+ #include < mutex>
30+ #include < atomic>
31+ #include < condition_variable>
32+ #include " logger/logger.h"
33+ #include " status/status.h"
34+
35+ namespace UC {
36+
37+ template <typename Callable>
38+ class Timer {
39+ public:
40+ Timer (const std::chrono::seconds& interval, Callable&& callable)
41+ : interval_(interval), callable_(callable), running_(false ) {}
42+ ~Timer () {
43+ {
44+ std::lock_guard<std::mutex> lg (this ->mutex_ );
45+ this ->running_ = false ;
46+ }
47+
48+ this ->cv_ .notify_one ();
49+ if (this ->thread_ .joinable ()) { this ->thread_ .join (); }
50+ }
51+ Status Start ()
52+ {
53+ {
54+ std::lock_guard<std::mutex> lg (this ->mutex_ );
55+ if (this ->running_ ) { return Status::OK (); }
56+ }
57+ try {
58+ this ->running_ = true ;
59+ this ->thread_ = std::thread (&Timer::Runner, this );
60+ return Status::OK ();
61+ } catch (const std::exception& e) {
62+ UC_ERROR (" Failed({}) to start timer." , e.what ());
63+ return Status::OutOfMemory ();
64+ }
65+ }
66+
67+ private:
68+ void Runner ()
69+ {
70+ while (this ->running_ ) {
71+ try {
72+ {
73+ std::unique_lock<std::mutex> lg (this ->mutex_ );
74+ this ->cv_ .wait_for (lg, this ->interval_ , [this ] { return !this ->running_ ; });
75+ if (!this ->running_ ) { break ; }
76+ }
77+ this ->callable_ ();
78+ } catch (const std::exception& e) { UC_ERROR (" Failed({}) to run timer." , e.what ()); }
79+ }
80+ }
81+
82+ private:
83+ std::chrono::seconds interval_;
84+ Callable callable_;
85+ std::thread thread_;
86+ std::mutex mutex_;
87+ std::condition_variable cv_;
88+ std::atomic<bool > running_;
89+ };
90+
91+ } // namespace UC
92+
93+ #endif
0 commit comments