Delete FakeIceTransport usage of AsyncInvoker
Bug: webrtc:12339
Change-Id: I1656bef5178dd150f5069d743ea7236dc0cb0300
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/212500
Commit-Queue: Niels Moller <nisse@webrtc.org>
Reviewed-by: Tommi <tommi@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#33509}
diff --git a/p2p/BUILD.gn b/p2p/BUILD.gn
index 070a30e..7ccad67 100644
--- a/p2p/BUILD.gn
+++ b/p2p/BUILD.gn
@@ -144,6 +144,8 @@
"../api:libjingle_peerconnection_api",
"../rtc_base",
"../rtc_base:rtc_base_approved",
+ "../rtc_base/task_utils:pending_task_safety_flag",
+ "../rtc_base/task_utils:to_queued_task",
]
absl_deps = [
"//third_party/abseil-cpp/absl/algorithm:container",
diff --git a/p2p/base/fake_ice_transport.h b/p2p/base/fake_ice_transport.h
index 58d83d7..f8be8a9 100644
--- a/p2p/base/fake_ice_transport.h
+++ b/p2p/base/fake_ice_transport.h
@@ -20,8 +20,9 @@
#include "absl/types/optional.h"
#include "api/ice_transport_interface.h"
#include "p2p/base/ice_transport_internal.h"
-#include "rtc_base/async_invoker.h"
#include "rtc_base/copy_on_write_buffer.h"
+#include "rtc_base/task_utils/pending_task_safety_flag.h"
+#include "rtc_base/task_utils/to_queued_task.h"
namespace cricket {
@@ -305,12 +306,12 @@
if (!combine_outgoing_packets_ || send_packet_.size() > len) {
rtc::CopyOnWriteBuffer packet(std::move(send_packet_));
if (async_) {
- invoker_.AsyncInvokeDelayed<void>(
- RTC_FROM_HERE, network_thread_,
- [this, packet] {
- RTC_DCHECK_RUN_ON(network_thread_);
- FakeIceTransport::SendPacketInternal(packet);
- },
+ network_thread_->PostDelayedTask(
+ ToQueuedTask(task_safety_.flag(),
+ [this, packet] {
+ RTC_DCHECK_RUN_ON(network_thread_);
+ FakeIceTransport::SendPacketInternal(packet);
+ }),
async_delay_ms_);
} else {
SendPacketInternal(packet);
@@ -389,7 +390,6 @@
}
}
- rtc::AsyncInvoker invoker_;
const std::string name_;
const int component_;
FakeIceTransport* dest_ RTC_GUARDED_BY(network_thread_) = nullptr;
@@ -420,6 +420,7 @@
RTC_GUARDED_BY(network_thread_);
rtc::CopyOnWriteBuffer last_sent_packet_ RTC_GUARDED_BY(network_thread_);
rtc::Thread* const network_thread_;
+ webrtc::ScopedTaskSafetyDetached task_safety_;
};
class FakeIceTransportWrapper : public webrtc::IceTransportInterface {
diff --git a/rtc_base/task_utils/pending_task_safety_flag.cc b/rtc_base/task_utils/pending_task_safety_flag.cc
index acdbc00..595457d 100644
--- a/rtc_base/task_utils/pending_task_safety_flag.cc
+++ b/rtc_base/task_utils/pending_task_safety_flag.cc
@@ -19,6 +19,13 @@
return new rtc::RefCountedObject<PendingTaskSafetyFlag>();
}
+rtc::scoped_refptr<PendingTaskSafetyFlag>
+PendingTaskSafetyFlag::CreateDetached() {
+ auto safety_flag = Create();
+ safety_flag->main_sequence_.Detach();
+ return safety_flag;
+}
+
void PendingTaskSafetyFlag::SetNotAlive() {
RTC_DCHECK_RUN_ON(&main_sequence_);
alive_ = false;
diff --git a/rtc_base/task_utils/pending_task_safety_flag.h b/rtc_base/task_utils/pending_task_safety_flag.h
index 4c9d348..abfce26 100644
--- a/rtc_base/task_utils/pending_task_safety_flag.h
+++ b/rtc_base/task_utils/pending_task_safety_flag.h
@@ -58,10 +58,28 @@
class PendingTaskSafetyFlag : public rtc::RefCountInterface {
public:
static rtc::scoped_refptr<PendingTaskSafetyFlag> Create();
+ // Creates a flag, but with its SequenceChecker initially detached. Hence, it
+ // may be created on a different thread than the flag will be used on.
+ static rtc::scoped_refptr<PendingTaskSafetyFlag> CreateDetached();
~PendingTaskSafetyFlag() = default;
void SetNotAlive();
+ // The SetAlive method is intended to support Start/Stop/Restart usecases.
+ // When a class has called SetNotAlive on a flag used for posted tasks, and
+ // decides it wants to post new tasks and have them run, there are two
+ // reasonable ways to do that:
+ //
+ // (i) Use the below SetAlive method. One subtlety is that any task posted
+ // prior to SetNotAlive, and still in the queue, is resurrected and will
+ // run.
+ //
+ // (ii) Create a fresh flag, and just drop the reference to the old one. This
+ // avoids the above problem, and ensures that tasks poster prior to
+ // SetNotAlive stay cancelled. Instead, there's a potential data race on
+ // the flag pointer itself. Some synchronization is required between the
+ // thread overwriting the flag pointer, and the threads that want to post
+ // tasks and therefore read that same pointer.
void SetAlive();
bool alive() const;
@@ -103,6 +121,21 @@
PendingTaskSafetyFlag::Create();
};
+// Like ScopedTaskSafety, but allows construction on a different thread than
+// where the flag will be used.
+class ScopedTaskSafetyDetached {
+ public:
+ ScopedTaskSafetyDetached() = default;
+ ~ScopedTaskSafetyDetached() { flag_->SetNotAlive(); }
+
+ // Returns a new reference to the safety flag.
+ rtc::scoped_refptr<PendingTaskSafetyFlag> flag() const { return flag_; }
+
+ private:
+ rtc::scoped_refptr<PendingTaskSafetyFlag> flag_ =
+ PendingTaskSafetyFlag::CreateDetached();
+};
+
} // namespace webrtc
#endif // RTC_BASE_TASK_UTILS_PENDING_TASK_SAFETY_FLAG_H_