1 // Copyright 2016 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 BASE_TASK_SCHEDULER_TASK_TRACKER_POSIX_H_
6 #define BASE_TASK_SCHEDULER_TASK_TRACKER_POSIX_H_
7 
8 #include <memory>
9 
10 #include "base/base_export.h"
11 #include "base/logging.h"
12 #include "base/macros.h"
13 #include "base/task_scheduler/task_tracker.h"
14 #include "base/threading/platform_thread.h"
15 
16 namespace base {
17 
18 class MessageLoopForIO;
19 
20 namespace internal {
21 
22 struct Task;
23 
24 // A TaskTracker that instantiates a FileDescriptorWatcher in the scope in which
25 // a task runs. Used on all POSIX platforms except NaCl SFI.
26 // set_watch_file_descriptor_message_loop() must be called before the
27 // TaskTracker can run tasks.
28 class BASE_EXPORT TaskTrackerPosix : public TaskTracker {
29  public:
30   TaskTrackerPosix(StringPiece name);
31   ~TaskTrackerPosix() override;
32 
33   // Sets the MessageLoopForIO with which to setup FileDescriptorWatcher in the
34   // scope in which tasks run. Must be called before starting to run tasks.
35   // External synchronization is required between a call to this and a call to
36   // RunTask().
set_watch_file_descriptor_message_loop(MessageLoopForIO * watch_file_descriptor_message_loop)37   void set_watch_file_descriptor_message_loop(
38       MessageLoopForIO* watch_file_descriptor_message_loop) {
39     watch_file_descriptor_message_loop_ = watch_file_descriptor_message_loop;
40   }
41 
42 #if DCHECK_IS_ON()
43   // TODO(robliao): http://crbug.com/698140. This addresses service thread tasks
44   // that could run after the task scheduler has shut down. Anything from the
45   // service thread is exempted from the task scheduler shutdown DCHECKs.
set_service_thread_handle(const PlatformThreadHandle & service_thread_handle)46   void set_service_thread_handle(
47       const PlatformThreadHandle& service_thread_handle) {
48     DCHECK(!service_thread_handle.is_null());
49     service_thread_handle_ = service_thread_handle;
50   }
51 #endif
52 
53  protected:
54   // TaskTracker:
55   void RunOrSkipTask(Task task, Sequence* sequence, bool can_run_task) override;
56 
57  private:
58 #if DCHECK_IS_ON()
59   bool IsPostingBlockShutdownTaskAfterShutdownAllowed() override;
60 #endif
61 
62   MessageLoopForIO* watch_file_descriptor_message_loop_ = nullptr;
63 
64 #if DCHECK_IS_ON()
65   PlatformThreadHandle service_thread_handle_;
66 #endif
67 
68   DISALLOW_COPY_AND_ASSIGN(TaskTrackerPosix);
69 };
70 
71 }  // namespace internal
72 }  // namespace base
73 
74 #endif  // BASE_TASK_SCHEDULER_TASK_TRACKER_POSIX_H_
75