blob: 2a44c9a8e0a326fc90019782d8a817d56a5f00c8 [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"
Takashi Toyoshima88b4ac02019-02-12 11:25:56 +000012#include "base/compiler_specific.h"
toyoshimccb8ff92017-06-13 05:31:46 -070013#include "base/memory/ref_counted.h"
Robert Liaoa4512b62017-08-29 16:38:28 +000014#include "base/sequence_checker.h"
Robert Liaof66a9cf2017-08-26 01:40:35 +000015#include "base/synchronization/condition_variable.h"
toyoshimccb8ff92017-06-13 05:31:46 -070016#include "base/synchronization/lock.h"
Patrick Monette3f9156f2021-10-15 19:13:42 +000017#include "base/task/single_thread_task_runner.h"
Takashi Toyoshima88b4ac02019-02-12 11:25:56 +000018#include "base/thread_annotations.h"
toyoshimccb8ff92017-06-13 05:31:46 -070019#include "base/threading/thread.h"
20#include "base/time/time.h"
21#include "media/midi/midi_export.h"
22
23namespace midi {
24
25// TaskService manages TaskRunners that can be used in midi and provides
26// functionalities to ensure thread safety.
27class MIDI_EXPORT TaskService final {
28 public:
29 using RunnerId = size_t;
Takashi Toyoshima88b4ac02019-02-12 11:25:56 +000030 using InstanceId = int64_t;
toyoshimccb8ff92017-06-13 05:31:46 -070031
32 static constexpr RunnerId kDefaultRunnerId = 0;
33
34 TaskService();
Peter Boström53634032021-09-22 20:24:34 +000035
36 TaskService(const TaskService&) = delete;
37 TaskService& operator=(const TaskService&) = delete;
38
toyoshimccb8ff92017-06-13 05:31:46 -070039 ~TaskService();
40
41 // Issues an InstanceId internally to post tasks via PostBoundTask() and
42 // PostDelayedBoundTask() with the InstanceId. Once UnbindInstance() is
43 // called, tasks posted via these methods with unbind InstanceId won't be
44 // invoked any more.
45 // Returns true if call is bound or unbound correctly. Otherwise returns
46 // false, that happens when the BindInstance() is called twice without
Takashi Toyoshima88b4ac02019-02-12 11:25:56 +000047 // unbinding the previous instance, or the UnbindInstance() is called without
48 // any successful BindInstance() call.
49 bool BindInstance() WARN_UNUSED_RESULT;
50 bool UnbindInstance() WARN_UNUSED_RESULT;
toyoshimccb8ff92017-06-13 05:31:46 -070051
Takashi Toyoshima143f0fd2017-08-01 16:15:33 +000052 // Checks if the current thread belongs to the specified runner.
53 bool IsOnTaskRunner(RunnerId runner_id);
54
Takashi Toyoshima3f0ea8f2018-01-17 09:19:59 +000055 // Posts a task to run on a specified TaskRunner. |runner_id| should be a
56 // positive number that represents a dedicated thread on that |task| will run.
57 // |task| will run even without a bound instance.
toyoshimccb8ff92017-06-13 05:31:46 -070058 void PostStaticTask(RunnerId runner_id, base::OnceClosure task);
59
60 // Posts a task to run on a specified TaskRunner, and ensures that the bound
61 // instance should not quit UnbindInstance() while a bound task is running.
Takashi Toyoshima3f0ea8f2018-01-17 09:19:59 +000062 // |runner_id| should be |kDefaultRunnerId| or a positive number. If
63 // |kDefaultRunnerId| is specified, the task runs on the thread on which
64 // BindInstance() was called.
toyoshimccb8ff92017-06-13 05:31:46 -070065 void PostBoundTask(RunnerId runner, base::OnceClosure task);
66 void PostBoundDelayedTask(RunnerId runner_id,
67 base::OnceClosure task,
68 base::TimeDelta delay);
69
Takashi Toyoshima88b4ac02019-02-12 11:25:56 +000070 void OverflowInstanceIdForTesting();
71
toyoshimccb8ff92017-06-13 05:31:46 -070072 private:
Takashi Toyoshima88b4ac02019-02-12 11:25:56 +000073 static constexpr TaskService::InstanceId kInvalidInstanceId = -1;
74
toyoshimccb8ff92017-06-13 05:31:46 -070075 // Returns a SingleThreadTaskRunner reference. Each TaskRunner will be
76 // constructed on demand.
77 scoped_refptr<base::SingleThreadTaskRunner> GetTaskRunner(RunnerId runner_id);
78
79 // Helps to run a posted bound task on TaskRunner safely.
80 void RunTask(InstanceId instance_id,
81 RunnerId runner_id,
82 base::OnceClosure task);
83
Robert Liaof66a9cf2017-08-26 01:40:35 +000084 // Returns true if |instance_id| is equal to |bound_instance_id_|.
85 bool IsInstanceIdStillBound(InstanceId instance_id);
86
Takashi Toyoshima88b4ac02019-02-12 11:25:56 +000087 // Holds InstanceId for the next bound instance, accessed on the BindInstance
88 // call thread without any protection.
89 InstanceId next_instance_id_ = kInvalidInstanceId;
90
toyoshimccb8ff92017-06-13 05:31:46 -070091 // Keeps a TaskRunner for the thread that calls BindInstance() as a default
92 // task runner to run posted tasks.
Takashi Toyoshima88b4ac02019-02-12 11:25:56 +000093 scoped_refptr<base::SingleThreadTaskRunner> default_task_runner_
94 GUARDED_BY(lock_);
toyoshimccb8ff92017-06-13 05:31:46 -070095
96 // Holds threads to host SingleThreadTaskRunners.
Takashi Toyoshima88b4ac02019-02-12 11:25:56 +000097 std::vector<std::unique_ptr<base::Thread>> threads_ GUARDED_BY(lock_);
toyoshimccb8ff92017-06-13 05:31:46 -070098
Takashi Toyoshima88b4ac02019-02-12 11:25:56 +000099 // Holds InstanceId for the current bound instance.
100 InstanceId bound_instance_id_ GUARDED_BY(lock_) = kInvalidInstanceId;
101
102 base::Lock lock_;
Robert Liaof66a9cf2017-08-26 01:40:35 +0000103
104 // Signalled when the number of tasks in flight is 0 and ensures that
105 // UnbindInstance() does not return until all tasks have completed.
Takashi Toyoshima88b4ac02019-02-12 11:25:56 +0000106 base::ConditionVariable no_tasks_in_flight_cv_
107 GUARDED_BY(tasks_in_flight_lock_);
Robert Liaof66a9cf2017-08-26 01:40:35 +0000108
109 // Number of tasks in flight.
Takashi Toyoshima88b4ac02019-02-12 11:25:56 +0000110 int tasks_in_flight_ GUARDED_BY(tasks_in_flight_lock_) = 0;
toyoshimccb8ff92017-06-13 05:31:46 -0700111
Takashi Toyoshima88b4ac02019-02-12 11:25:56 +0000112 base::Lock tasks_in_flight_lock_;
toyoshimccb8ff92017-06-13 05:31:46 -0700113
Robert Liaoa4512b62017-08-29 16:38:28 +0000114 // Verifies all UnbindInstance() calls occur on the same sequence as
Takashi Toyoshimad2bdc592017-09-13 10:02:54 +0000115 // BindInstance().
Robert Liaoa4512b62017-08-29 16:38:28 +0000116 SEQUENCE_CHECKER(instance_binding_sequence_checker_);
toyoshimccb8ff92017-06-13 05:31:46 -0700117};
118
Nico Webera0faaf72019-02-07 19:07:54 +0000119} // namespace midi
toyoshimccb8ff92017-06-13 05:31:46 -0700120
121#endif // MEDIA_MIDI_TASK_SERVICE_H_