xref: /aosp_15_r20/external/webrtc/net/dcsctp/timer/task_queue_timeout.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2021 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 #ifndef NET_DCSCTP_TIMER_TASK_QUEUE_TIMEOUT_H_
11 #define NET_DCSCTP_TIMER_TASK_QUEUE_TIMEOUT_H_
12 
13 #include <memory>
14 #include <utility>
15 
16 #include "api/task_queue/pending_task_safety_flag.h"
17 #include "api/task_queue/task_queue_base.h"
18 #include "net/dcsctp/public/timeout.h"
19 
20 namespace dcsctp {
21 
22 // The TaskQueueTimeoutFactory creates `Timeout` instances, which schedules
23 // itself to be triggered on the provided `task_queue`, which may be a thread,
24 // an actual TaskQueue or something else which supports posting a delayed task.
25 //
26 // Note that each `DcSctpSocket` must have its own `TaskQueueTimeoutFactory`,
27 // as the `TimeoutID` are not unique among sockets.
28 //
29 // This class must outlive any created Timeout that it has created. Note that
30 // the `DcSctpSocket` will ensure that all Timeouts are deleted when the socket
31 // is destructed, so this means that this class must outlive the `DcSctpSocket`.
32 //
33 // This class, and the timeouts created it, are not thread safe.
34 class TaskQueueTimeoutFactory {
35  public:
36   // The `get_time` function must return the current time, relative to any
37   // epoch. Whenever a timeout expires, the `on_expired` callback will be
38   // triggered, and then the client should provided `timeout_id` to
39   // `DcSctpSocketInterface::HandleTimeout`.
TaskQueueTimeoutFactory(webrtc::TaskQueueBase & task_queue,std::function<TimeMs ()> get_time,std::function<void (TimeoutID timeout_id)> on_expired)40   TaskQueueTimeoutFactory(webrtc::TaskQueueBase& task_queue,
41                           std::function<TimeMs()> get_time,
42                           std::function<void(TimeoutID timeout_id)> on_expired)
43       : task_queue_(task_queue),
44         get_time_(std::move(get_time)),
45         on_expired_(std::move(on_expired)) {}
46 
47   // Creates an implementation of `Timeout`.
48   std::unique_ptr<Timeout> CreateTimeout(
49       webrtc::TaskQueueBase::DelayPrecision precision =
50           webrtc::TaskQueueBase::DelayPrecision::kLow) {
51     return std::make_unique<TaskQueueTimeout>(*this, precision);
52   }
53 
54  private:
55   class TaskQueueTimeout : public Timeout {
56    public:
57     TaskQueueTimeout(TaskQueueTimeoutFactory& parent,
58                      webrtc::TaskQueueBase::DelayPrecision precision);
59     ~TaskQueueTimeout();
60 
61     void Start(DurationMs duration_ms, TimeoutID timeout_id) override;
62     void Stop() override;
63 
64    private:
65     TaskQueueTimeoutFactory& parent_;
66     const webrtc::TaskQueueBase::DelayPrecision precision_;
67     // A safety flag to ensure that posted tasks to the task queue don't
68     // reference these object when they go out of scope. Note that this safety
69     // flag will be re-created if the scheduled-but-not-yet-expired task is not
70     // to be run. This happens when there is a posted delayed task with an
71     // expiration time _further away_ than what is now the expected expiration
72     // time. In this scenario, a new delayed task has to be posted with a
73     // shorter duration and the old task has to be forgotten.
74     rtc::scoped_refptr<webrtc::PendingTaskSafetyFlag> pending_task_safety_flag_;
75     // The time when the posted delayed task is set to expire. Will be set to
76     // the infinite future if there is no such task running.
77     TimeMs posted_task_expiration_ = TimeMs::InfiniteFuture();
78     // The time when the timeout expires. It will be set to the infinite future
79     // if the timeout is not running/not started.
80     TimeMs timeout_expiration_ = TimeMs::InfiniteFuture();
81     // The current timeout ID that will be reported when expired.
82     TimeoutID timeout_id_ = TimeoutID(0);
83   };
84 
85   RTC_NO_UNIQUE_ADDRESS webrtc::SequenceChecker thread_checker_;
86   webrtc::TaskQueueBase& task_queue_;
87   const std::function<TimeMs()> get_time_;
88   const std::function<void(TimeoutID)> on_expired_;
89 };
90 }  // namespace dcsctp
91 
92 #endif  // NET_DCSCTP_TIMER_TASK_QUEUE_TIMEOUT_H_
93