@@ -62,6 +62,17 @@ namespace jbo::timers
6262 void
6363 stop ();
6464
65+ /* *
66+ * Change the interval of a periodic timer.
67+ *
68+ * @note This will fail (do nothing and return false) if the timer is not a constant interval type.
69+ *
70+ * @return Whether the interval was changed successfully.
71+ */
72+ template <typename Rep, typename Period>
73+ bool
74+ set_interval (const std::chrono::duration<Rep, Period>& interval);
75+
6576 private:
6677 data* m_data = nullptr ;
6778 };
@@ -71,6 +82,12 @@ namespace jbo::timers
7182 */
7283 struct data
7384 {
85+ private:
86+ struct periodic_constant ;
87+ struct periodic_uniform ;
88+ struct singleshot ;
89+
90+ public:
7491 /* *
7592 * The task type.
7693 *
@@ -94,6 +111,20 @@ namespace jbo::timers
94111 m_enabled = false ;
95112 }
96113
114+ template <typename Rep, typename Period>
115+ bool
116+ set_interval (const std::chrono::duration<Rep, Period>& interval)
117+ {
118+ std::scoped_lock lock (m_mutex);
119+
120+ if (auto pc = std::get_if<periodic_constant>(&m_data); pc) {
121+ pc->interval = std::chrono::duration_cast<decltype (pc->interval )>(interval);
122+ return true ;
123+ }
124+
125+ return false ;
126+ }
127+
97128 private:
98129 friend struct manager ;
99130
@@ -194,6 +225,17 @@ namespace jbo::timers
194225 m_data->stop ();
195226 }
196227
228+ template <typename Rep, typename Period>
229+ inline
230+ bool
231+ timer::set_interval (const std::chrono::duration<Rep, Period>& interval)
232+ {
233+ if (!m_data) [[unlikely]]
234+ return false ;
235+
236+ return m_data->set_interval (interval);
237+ }
238+
197239 /* *
198240 * A manager to manage timers.
199241 *
0 commit comments