blob: 79a72a0752b27554265498799f7b4b0277a5a433 [file]
/*
* Copyright 2026 The WebRTC Project Authors. All rights reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#ifndef CALL_TASK_QUEUE_DRAINER_H_
#define CALL_TASK_QUEUE_DRAINER_H_
#include <memory>
#include "absl/container/flat_hash_map.h"
#include "api/scoped_refptr.h"
#include "api/task_queue/pending_task_safety_flag.h"
#include "api/task_queue/task_queue_base.h"
#include "rtc_base/thread_annotations.h"
namespace webrtc {
// Takes ownership of retired TaskQueueBase instances so callers can release
// them without blocking the owning sequence on in-flight tasks.
//
// Retire() posts a flush task to the retired queue; once that flush task
// completes, the queue is erased and deleted on the owning sequence. Tasks
// posted before Retire() are guaranteed to finish before deletion; tasks posted
// after Retire() are not guaranteed to run and are finalized or canceled by
// TaskQueueBase::Delete().
//
// ~TaskQueueDrainer() synchronously flushes and deletes any queues still
// draining before returning.
class TaskQueueDrainer {
public:
// `owner` is the sequence on which Retire(), async queue deletion, and
// ~TaskQueueDrainer() run.
explicit TaskQueueDrainer(TaskQueueBase& owner);
TaskQueueDrainer(const TaskQueueDrainer&) = delete;
TaskQueueDrainer& operator=(const TaskQueueDrainer&) = delete;
~TaskQueueDrainer();
// Takes ownership of `queue` and schedules its asynchronous deletion on the
// owning sequence after currently queued tasks on `queue` complete.
void Retire(std::unique_ptr<TaskQueueBase, TaskQueueDeleter> queue);
private:
TaskQueueBase& owner_;
const scoped_refptr<PendingTaskSafetyFlag> safety_;
absl::flat_hash_map<TaskQueueBase*,
std::unique_ptr<TaskQueueBase, TaskQueueDeleter>>
draining_ RTC_GUARDED_BY(owner_);
};
} // namespace webrtc
#endif // CALL_TASK_QUEUE_DRAINER_H_