blob: 3ac8f31f4c73e99c696ffa6bd6ed1065dfa20bc2 [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/macros.h"
14#include "base/memory/ref_counted.h"
Robert Liaoa4512b62017-08-29 16:38:28 +000015#include "base/sequence_checker.h"
toyoshimccb8ff92017-06-13 05:31:46 -070016#include "base/single_thread_task_runner.h"
Robert Liaof66a9cf2017-08-26 01:40:35 +000017#include "base/synchronization/condition_variable.h"
toyoshimccb8ff92017-06-13 05:31:46 -070018#include "base/synchronization/lock.h"
Takashi Toyoshima88b4ac02019-02-12 11:25:56 +000019#include "base/thread_annotations.h"
toyoshimccb8ff92017-06-13 05:31:46 -070020#include "base/threading/thread.h"
21#include "base/time/time.h"
22#include "media/midi/midi_export.h"
23
24namespace midi {
25
26// TaskService manages TaskRunners that can be used in midi and provides
27// functionalities to ensure thread safety.
28class MIDI_EXPORT TaskService final {
29 public:
30 using RunnerId = size_t;
Takashi Toyoshima88b4ac02019-02-12 11:25:56 +000031 using InstanceId = int64_t;
toyoshimccb8ff92017-06-13 05:31:46 -070032
33 static constexpr RunnerId kDefaultRunnerId = 0;
34
35 TaskService();
36 ~TaskService();
37
38 // Issues an InstanceId internally to post tasks via PostBoundTask() and
39 // PostDelayedBoundTask() with the InstanceId. Once UnbindInstance() is
40 // called, tasks posted via these methods with unbind InstanceId won't be
41 // invoked any more.
42 // Returns true if call is bound or unbound correctly. Otherwise returns
43 // false, that happens when the BindInstance() is called twice without
Takashi Toyoshima88b4ac02019-02-12 11:25:56 +000044 // unbinding the previous instance, or the UnbindInstance() is called without
45 // any successful BindInstance() call.
46 bool BindInstance() WARN_UNUSED_RESULT;
47 bool UnbindInstance() WARN_UNUSED_RESULT;
toyoshimccb8ff92017-06-13 05:31:46 -070048
Takashi Toyoshima143f0fd2017-08-01 16:15:33 +000049 // Checks if the current thread belongs to the specified runner.
50 bool IsOnTaskRunner(RunnerId runner_id);
51
Takashi Toyoshima3f0ea8f2018-01-17 09:19:59 +000052 // Posts a task to run on a specified TaskRunner. |runner_id| should be a
53 // positive number that represents a dedicated thread on that |task| will run.
54 // |task| will run even without a bound instance.
toyoshimccb8ff92017-06-13 05:31:46 -070055 void PostStaticTask(RunnerId runner_id, base::OnceClosure task);
56
57 // Posts a task to run on a specified TaskRunner, and ensures that the bound
58 // instance should not quit UnbindInstance() while a bound task is running.
Takashi Toyoshima3f0ea8f2018-01-17 09:19:59 +000059 // |runner_id| should be |kDefaultRunnerId| or a positive number. If
60 // |kDefaultRunnerId| is specified, the task runs on the thread on which
61 // BindInstance() was called.
toyoshimccb8ff92017-06-13 05:31:46 -070062 void PostBoundTask(RunnerId runner, base::OnceClosure task);
63 void PostBoundDelayedTask(RunnerId runner_id,
64 base::OnceClosure task,
65 base::TimeDelta delay);
66
Takashi Toyoshima88b4ac02019-02-12 11:25:56 +000067 void OverflowInstanceIdForTesting();
68
toyoshimccb8ff92017-06-13 05:31:46 -070069 private:
Takashi Toyoshima88b4ac02019-02-12 11:25:56 +000070 static constexpr TaskService::InstanceId kInvalidInstanceId = -1;
71
toyoshimccb8ff92017-06-13 05:31:46 -070072 // Returns a SingleThreadTaskRunner reference. Each TaskRunner will be
73 // constructed on demand.
74 scoped_refptr<base::SingleThreadTaskRunner> GetTaskRunner(RunnerId runner_id);
75
76 // Helps to run a posted bound task on TaskRunner safely.
77 void RunTask(InstanceId instance_id,
78 RunnerId runner_id,
79 base::OnceClosure task);
80
Robert Liaof66a9cf2017-08-26 01:40:35 +000081 // Returns true if |instance_id| is equal to |bound_instance_id_|.
82 bool IsInstanceIdStillBound(InstanceId instance_id);
83
Takashi Toyoshima88b4ac02019-02-12 11:25:56 +000084 // Holds InstanceId for the next bound instance, accessed on the BindInstance
85 // call thread without any protection.
86 InstanceId next_instance_id_ = kInvalidInstanceId;
87
toyoshimccb8ff92017-06-13 05:31:46 -070088 // Keeps a TaskRunner for the thread that calls BindInstance() as a default
89 // task runner to run posted tasks.
Takashi Toyoshima88b4ac02019-02-12 11:25:56 +000090 scoped_refptr<base::SingleThreadTaskRunner> default_task_runner_
91 GUARDED_BY(lock_);
toyoshimccb8ff92017-06-13 05:31:46 -070092
93 // Holds threads to host SingleThreadTaskRunners.
Takashi Toyoshima88b4ac02019-02-12 11:25:56 +000094 std::vector<std::unique_ptr<base::Thread>> threads_ GUARDED_BY(lock_);
toyoshimccb8ff92017-06-13 05:31:46 -070095
Takashi Toyoshima88b4ac02019-02-12 11:25:56 +000096 // Holds InstanceId for the current bound instance.
97 InstanceId bound_instance_id_ GUARDED_BY(lock_) = kInvalidInstanceId;
98
99 base::Lock lock_;
Robert Liaof66a9cf2017-08-26 01:40:35 +0000100
101 // Signalled when the number of tasks in flight is 0 and ensures that
102 // UnbindInstance() does not return until all tasks have completed.
Takashi Toyoshima88b4ac02019-02-12 11:25:56 +0000103 base::ConditionVariable no_tasks_in_flight_cv_
104 GUARDED_BY(tasks_in_flight_lock_);
Robert Liaof66a9cf2017-08-26 01:40:35 +0000105
106 // Number of tasks in flight.
Takashi Toyoshima88b4ac02019-02-12 11:25:56 +0000107 int tasks_in_flight_ GUARDED_BY(tasks_in_flight_lock_) = 0;
toyoshimccb8ff92017-06-13 05:31:46 -0700108
Takashi Toyoshima88b4ac02019-02-12 11:25:56 +0000109 base::Lock tasks_in_flight_lock_;
toyoshimccb8ff92017-06-13 05:31:46 -0700110
Robert Liaoa4512b62017-08-29 16:38:28 +0000111 // Verifies all UnbindInstance() calls occur on the same sequence as
Takashi Toyoshimad2bdc592017-09-13 10:02:54 +0000112 // BindInstance().
Robert Liaoa4512b62017-08-29 16:38:28 +0000113 SEQUENCE_CHECKER(instance_binding_sequence_checker_);
114
toyoshimccb8ff92017-06-13 05:31:46 -0700115 DISALLOW_COPY_AND_ASSIGN(TaskService);
116};
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_