File: sync_task_manager.h

package info (click to toggle)
chromium 135.0.7049.95-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,959,392 kB
  • sloc: cpp: 34,198,526; ansic: 7,100,035; javascript: 3,985,800; python: 1,395,489; asm: 896,754; xml: 722,891; pascal: 180,504; sh: 94,909; perl: 88,388; objc: 79,739; sql: 53,020; cs: 41,358; fortran: 24,137; makefile: 22,501; php: 13,699; tcl: 10,142; yacc: 8,822; ruby: 7,350; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; awk: 197; sed: 36
file content (218 lines) | stat: -rw-r--r-- 7,999 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_SYNC_TASK_MANAGER_H_
#define CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_SYNC_TASK_MANAGER_H_

#include <stddef.h>
#include <stdint.h>

#include <memory>
#include <queue>
#include <unordered_map>
#include <vector>

#include "base/functional/callback.h"
#include "base/memory/weak_ptr.h"
#include "base/sequence_checker.h"
#include "chrome/browser/sync_file_system/drive_backend/task_dependency_manager.h"
#include "chrome/browser/sync_file_system/sync_callbacks.h"
#include "chrome/browser/sync_file_system/sync_status_code.h"
#include "chrome/browser/sync_file_system/task_logger.h"

namespace base {
class Location;
class SequencedTaskRunner;
}

namespace sync_file_system {
namespace drive_backend {

class SyncTask;
class SyncTaskToken;
struct TaskBlocker;

// This class manages asynchronous tasks for Sync FileSystem.  Each task must be
// either a Task or a SyncTask.
// The instance runs single task as the foreground task, and multiple tasks as
// background tasks.  Running background task has a TaskBlocker that
// describes which task can run in parallel.  When a task start running as a
// background task, SyncTaskManager checks if any running background task
// doesn't block the new background task, and queues it up if it can't run.
class SyncTaskManager {
 public:
  using Task = base::OnceCallback<void(SyncStatusCallback callback)>;
  using Continuation =
      base::OnceCallback<void(std::unique_ptr<SyncTaskToken> token)>;

  enum Priority {
    PRIORITY_LOW,
    PRIORITY_MED,
    PRIORITY_HIGH,
  };

  class Client {
   public:
    virtual ~Client() = default;

    // Called when the manager is idle.
    virtual void MaybeScheduleNextTask() = 0;

    // Called when the manager is notified a task is done.
    virtual void NotifyLastOperationStatus(
        SyncStatusCode last_operation_status,
        bool last_operation_used_network) = 0;

    virtual void RecordTaskLog(
        std::unique_ptr<TaskLogger::TaskLog> task_log) = 0;
  };

  // Runs at most |maximum_background_tasks| parallel as background tasks.
  // If |maximum_background_tasks| is zero, all task runs as foreground task.
  SyncTaskManager(base::WeakPtr<Client> client,
                  size_t maximum_background_task,
                  const scoped_refptr<base::SequencedTaskRunner>& task_runner);

  SyncTaskManager(const SyncTaskManager&) = delete;
  SyncTaskManager& operator=(const SyncTaskManager&) = delete;

  virtual ~SyncTaskManager();

  // This needs to be called to start task scheduling.
  // If |status| is not SYNC_STATUS_OK calling this may change the
  // service status. This should not be called more than once.
  void Initialize(SyncStatusCode status);

  // Schedules a task at the given priority.
  void ScheduleTask(const base::Location& from_here,
                    Task task,
                    Priority priority,
                    SyncStatusCallback callback);
  void ScheduleSyncTask(const base::Location& from_here,
                        std::unique_ptr<SyncTask> task,
                        Priority priority,
                        SyncStatusCallback callback);

  // Runs the posted task only when we're idle.  Returns true if that task is
  // scheduled.
  bool ScheduleTaskIfIdle(const base::Location& from_here,
                          Task task,
                          SyncStatusCallback callback);
  bool ScheduleSyncTaskIfIdle(const base::Location& from_here,
                              std::unique_ptr<SyncTask> task,
                              SyncStatusCallback callback);

  // Notifies SyncTaskManager that the task associated to |token| has finished
  // with |status|.
  static void NotifyTaskDone(std::unique_ptr<SyncTaskToken> token,
                             SyncStatusCode status);

  // Updates |task_blocker| associated to the current task by specified
  // |task_blocker| and turns the current task to a background task if
  // the current task is running as a foreground task.
  // If specified |task_blocker| is blocked by any other blocking factor
  // associated to an existing background task, this function waits for the
  // existing background task to finish.
  // Upon the task is ready to run as a background task, calls |continuation|
  // with new SyncTaskToken.
  // Note that this function once releases previous |task_blocker| before
  // applying new |task_blocker|.  So, any other task may be run before
  // invocation of |continuation|.
  static void UpdateTaskBlocker(
      std::unique_ptr<SyncTaskToken> current_task_token,
      std::unique_ptr<TaskBlocker> task_blocker,
      Continuation continuation);

  bool IsRunningTask(int64_t task_token_id) const;

  void DetachFromSequence();

 private:
  struct PendingTask {
    base::OnceClosure closure;
    Priority priority;
    int64_t seq;

    PendingTask();
    PendingTask(base::OnceClosure task, Priority pri, int seq);
    ~PendingTask();

    PendingTask(PendingTask&& other);
    PendingTask& operator=(PendingTask&& other);
  };

  struct PendingTaskComparator {
    bool operator()(const PendingTask& left,
                    const PendingTask& right) const;
  };

  // Non-static version of NotifyTaskDone.
  void NotifyTaskDoneBody(std::unique_ptr<SyncTaskToken> token,
                          SyncStatusCode status);

  // Non-static version of UpdateTaskBlocker.
  void UpdateTaskBlockerBody(
      std::unique_ptr<SyncTaskToken> foreground_task_token,
      std::unique_ptr<SyncTaskToken> background_task_token,
      std::unique_ptr<TaskLogger::TaskLog> task_log,
      std::unique_ptr<TaskBlocker> task_blocker,
      Continuation continuation);

  // This should be called when an async task needs to get a task token.
  // SyncTasksToken::UpdateTask must be called manually on the returned token.
  std::unique_ptr<SyncTaskToken> GetUnupdatedToken();

  std::unique_ptr<SyncTaskToken> GetTokenForBackgroundTask(
      const base::Location& from_here,
      SyncStatusCallback callback,
      std::unique_ptr<TaskBlocker> task_blocker);

  void PushPendingTask(base::OnceClosure closure, Priority priority);

  void RunTask(std::unique_ptr<SyncTaskToken> token,
               std::unique_ptr<SyncTask> task);

  // Runs a pending task as a foreground task if possible.
  // If |token| is non-nullptr, put |token| back to |token_| beforehand.
  void MaybeStartNextForegroundTask(std::unique_ptr<SyncTaskToken> token);

  base::WeakPtr<Client> client_;

  // Owns running SyncTask to cancel the task on SyncTaskManager deletion.
  std::unique_ptr<SyncTask> running_foreground_task_;

  // Owns running backgrounded SyncTask to cancel the task on SyncTaskManager
  // deletion.
  std::unordered_map<int64_t, std::unique_ptr<SyncTask>>
      running_background_tasks_;

  size_t maximum_background_task_;

  // Holds pending continuation to move task to background.
  base::OnceClosure pending_backgrounding_task_;

  std::priority_queue<PendingTask, std::vector<PendingTask>,
                      PendingTaskComparator> pending_tasks_;
  int64_t pending_task_seq_;
  int64_t task_token_seq_;

  // Absence of |token_| implies a task is running. Incoming tasks should
  // wait for the task to finish in |pending_tasks_| if |token_| is null.
  // Each task must take TaskToken instance from |token_| and must hold it
  // until it finished. And the task must return the instance through
  // NotifyTaskDone when the task finished.
  std::unique_ptr<SyncTaskToken> token_;

  TaskDependencyManager dependency_manager_;

  scoped_refptr<base::SequencedTaskRunner> task_runner_;
  SEQUENCE_CHECKER(sequence_checker_);

  base::WeakPtrFactory<SyncTaskManager> weak_ptr_factory_{this};
};

}  // namespace drive_backend
}  // namespace sync_file_system

#endif  // CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_SYNC_TASK_MANAGER_H_