blob: bf59cdb2658c31d728c249813d7e79420a46ed9f [file] [log] [blame]
toyoshimccb8ff92017-06-13 05:31:46 -07001// Copyright 2017 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef MEDIA_MIDI_TASK_SERVICE_H_
6#define MEDIA_MIDI_TASK_SERVICE_H_
7
Takashi Toyoshima88b4ac02019-02-12 11:25:56 +00008#include <memory>
9#include <vector>
10
toyoshimccb8ff92017-06-13 05:31:46 -070011#include "base/callback_forward.h"
toyoshimccb8ff92017-06-13 05:31:46 -070012#include "base/memory/ref_counted.h"
Robert Liaoa4512b62017-08-29 16:38:28 +000013#include "base/sequence_checker.h"
Robert Liaof66a9cf2017-08-26 01:40:35 +000014#include "base/synchronization/condition_variable.h"
toyoshimccb8ff92017-06-13 05:31:46 -070015#include "base/synchronization/lock.h"
Patrick Monette3f9156f2021-10-15 19:13:42 +000016#include "base/task/single_thread_task_runner.h"
Takashi Toyoshima88b4ac02019-02-12 11:25:56 +000017#include "base/thread_annotations.h"
toyoshimccb8ff92017-06-13 05:31:46 -070018#include "base/threading/thread.h"
19#include "base/time/time.h"
20#include "media/midi/midi_export.h"
21
22namespace midi {
23
24// TaskService manages TaskRunners that can be used in midi and provides
25// functionalities to ensure thread safety.
26class MIDI_EXPORT TaskService final {
27 public:
28 using RunnerId = size_t;
Takashi Toyoshima88b4ac02019-02-12 11:25:56 +000029 using InstanceId = int64_t;
toyoshimccb8ff92017-06-13 05:31:46 -070030
31 static constexpr RunnerId kDefaultRunnerId = 0;
32
33 TaskService();
Peter Boström53634032021-09-22 20:24:34 +000034
35 TaskService(const TaskService&) = delete;
36 TaskService& operator=(const TaskService&) = delete;
37
toyoshimccb8ff92017-06-13 05:31:46 -070038 ~TaskService();
39
40 // Issues an InstanceId internally to post tasks via PostBoundTask() and
41 // PostDelayedBoundTask() with the InstanceId. Once UnbindInstance() is
42 // called, tasks posted via these methods with unbind InstanceId won't be
43 // invoked any more.
44 // Returns true if call is bound or unbound correctly. Otherwise returns
45 // false, that happens when the BindInstance() is called twice without
Takashi Toyoshima88b4ac02019-02-12 11:25:56 +000046 // unbinding the previous instance, or the UnbindInstance() is called without
47 // any successful BindInstance() call.
Daniel Chengba689ba2022-01-14 00:14:33 +000048 [[nodiscard]] bool BindInstance();
49 [[nodiscard]] bool UnbindInstance();
toyoshimccb8ff92017-06-13 05:31:46 -070050
Takashi Toyoshima143f0fd2017-08-01 16:15:33 +000051 // Checks if the current thread belongs to the specified runner.
52 bool IsOnTaskRunner(RunnerId runner_id);
53
Takashi Toyoshima3f0ea8f2018-01-17 09:19:59 +000054 // Posts a task to run on a specified TaskRunner. |runner_id| should be a
55 // positive number that represents a dedicated thread on that |task| will run.
56 // |task| will run even without a bound instance.
toyoshimccb8ff92017-06-13 05:31:46 -070057 void PostStaticTask(RunnerId runner_id, base::OnceClosure task);
58
59 // Posts a task to run on a specified TaskRunner, and ensures that the bound
60 // instance should not quit UnbindInstance() while a bound task is running.
Takashi Toyoshima3f0ea8f2018-01-17 09:19:59 +000061 // |runner_id| should be |kDefaultRunnerId| or a positive number. If
62 // |kDefaultRunnerId| is specified, the task runs on the thread on which
63 // BindInstance() was called.
toyoshimccb8ff92017-06-13 05:31:46 -070064 void PostBoundTask(RunnerId runner, base::OnceClosure task);
65 void PostBoundDelayedTask(RunnerId runner_id,
66 base::OnceClosure task,
67 base::TimeDelta delay);
68
Takashi Toyoshima88b4ac02019-02-12 11:25:56 +000069 void OverflowInstanceIdForTesting();
70
toyoshimccb8ff92017-06-13 05:31:46 -070071 private:
Takashi Toyoshima88b4ac02019-02-12 11:25:56 +000072 static constexpr TaskService::InstanceId kInvalidInstanceId = -1;
73
toyoshimccb8ff92017-06-13 05:31:46 -070074 // Returns a SingleThreadTaskRunner reference. Each TaskRunner will be
75 // constructed on demand.
76 scoped_refptr<base::SingleThreadTaskRunner> GetTaskRunner(RunnerId runner_id);
77
78 // Helps to run a posted bound task on TaskRunner safely.
79 void RunTask(InstanceId instance_id,
80 RunnerId runner_id,
81 base::OnceClosure task);
82
Robert Liaof66a9cf2017-08-26 01:40:35 +000083 // Returns true if |instance_id| is equal to |bound_instance_id_|.
84 bool IsInstanceIdStillBound(InstanceId instance_id);
85
Takashi Toyoshima88b4ac02019-02-12 11:25:56 +000086 // Holds InstanceId for the next bound instance, accessed on the BindInstance
87 // call thread without any protection.
88 InstanceId next_instance_id_ = kInvalidInstanceId;
89
toyoshimccb8ff92017-06-13 05:31:46 -070090 // Keeps a TaskRunner for the thread that calls BindInstance() as a default
91 // task runner to run posted tasks.
Takashi Toyoshima88b4ac02019-02-12 11:25:56 +000092 scoped_refptr<base::SingleThreadTaskRunner> default_task_runner_
93 GUARDED_BY(lock_);
toyoshimccb8ff92017-06-13 05:31:46 -070094
95 // Holds threads to host SingleThreadTaskRunners.
Takashi Toyoshima88b4ac02019-02-12 11:25:56 +000096 std::vector<std::unique_ptr<base::Thread>> threads_ GUARDED_BY(lock_);
toyoshimccb8ff92017-06-13 05:31:46 -070097
Takashi Toyoshima88b4ac02019-02-12 11:25:56 +000098 // Holds InstanceId for the current bound instance.
99 InstanceId bound_instance_id_ GUARDED_BY(lock_) = kInvalidInstanceId;
100
101 base::Lock lock_;
Robert Liaof66a9cf2017-08-26 01:40:35 +0000102
103 // Signalled when the number of tasks in flight is 0 and ensures that
104 // UnbindInstance() does not return until all tasks have completed.
Takashi Toyoshima88b4ac02019-02-12 11:25:56 +0000105 base::ConditionVariable no_tasks_in_flight_cv_
106 GUARDED_BY(tasks_in_flight_lock_);
Robert Liaof66a9cf2017-08-26 01:40:35 +0000107
108 // Number of tasks in flight.
Takashi Toyoshima88b4ac02019-02-12 11:25:56 +0000109 int tasks_in_flight_ GUARDED_BY(tasks_in_flight_lock_) = 0;
toyoshimccb8ff92017-06-13 05:31:46 -0700110
Takashi Toyoshima88b4ac02019-02-12 11:25:56 +0000111 base::Lock tasks_in_flight_lock_;
toyoshimccb8ff92017-06-13 05:31:46 -0700112
Robert Liaoa4512b62017-08-29 16:38:28 +0000113 // Verifies all UnbindInstance() calls occur on the same sequence as
Takashi Toyoshimad2bdc592017-09-13 10:02:54 +0000114 // BindInstance().
Robert Liaoa4512b62017-08-29 16:38:28 +0000115 SEQUENCE_CHECKER(instance_binding_sequence_checker_);
toyoshimccb8ff92017-06-13 05:31:46 -0700116};
117
Nico Webera0faaf72019-02-07 19:07:54 +0000118} // namespace midi
toyoshimccb8ff92017-06-13 05:31:46 -0700119
120#endif // MEDIA_MIDI_TASK_SERVICE_H_