| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef BASE_TASK_SCHEDULER_SCHEDULER_WORKER_H_ | 5 #ifndef BASE_TASK_SCHEDULER_SCHEDULER_WORKER_H_ |
| 6 #define BASE_TASK_SCHEDULER_SCHEDULER_WORKER_H_ | 6 #define BASE_TASK_SCHEDULER_SCHEDULER_WORKER_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 | 9 |
| 10 #include "base/base_export.h" | 10 #include "base/base_export.h" |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 // | 35 // |
| 36 // The worker is free to release and reallocate the platform thread with | 36 // The worker is free to release and reallocate the platform thread with |
| 37 // guidance from the delegate. | 37 // guidance from the delegate. |
| 38 // | 38 // |
| 39 // This class is thread-safe. | 39 // This class is thread-safe. |
| 40 class BASE_EXPORT SchedulerWorker | 40 class BASE_EXPORT SchedulerWorker |
| 41 : public RefCountedThreadSafe<SchedulerWorker> { | 41 : public RefCountedThreadSafe<SchedulerWorker> { |
| 42 public: | 42 public: |
| 43 // Delegate interface for SchedulerWorker. The methods are always called from | 43 // Delegate interface for SchedulerWorker. The methods are always called from |
| 44 // the thread managed by the SchedulerWorker instance. | 44 // the thread managed by the SchedulerWorker instance. |
| 45 class Delegate { | 45 class BASE_EXPORT Delegate { |
| 46 public: | 46 public: |
| 47 virtual ~Delegate() = default; | 47 virtual ~Delegate() = default; |
| 48 | 48 |
| 49 // Called by a thread managed by |worker| when it enters its main function. | 49 // Called by a thread managed by |worker| when it enters its main function. |
| 50 // If a thread is recreated after detachment, |detach_duration| is the time | 50 // If a thread is recreated after detachment, |detach_duration| is the time |
| 51 // elapsed since detachment. Otherwise, if this is the first thread created | 51 // elapsed since detachment. Otherwise, if this is the first thread created |
| 52 // for |worker|, |detach_duration| is TimeDelta::Max(). | 52 // for |worker|, |detach_duration| is TimeDelta::Max(). |
| 53 virtual void OnMainEntry(SchedulerWorker* worker) = 0; | 53 virtual void OnMainEntry(SchedulerWorker* worker) = 0; |
| 54 | 54 |
| 55 // Called by a thread managed by |worker| to get a Sequence from which to | 55 // Called by a thread managed by |worker| to get a Sequence from which to |
| 56 // run a Task. | 56 // run a Task. |
| 57 virtual scoped_refptr<Sequence> GetWork(SchedulerWorker* worker) = 0; | 57 virtual scoped_refptr<Sequence> GetWork(SchedulerWorker* worker) = 0; |
| 58 | 58 |
| 59 // Called by the SchedulerWorker after it ran a task. | 59 // Called by the SchedulerWorker after it ran a task. |
| 60 virtual void DidRunTask() = 0; | 60 virtual void DidRunTask() = 0; |
| 61 | 61 |
| 62 // Called when |sequence| isn't empty after the SchedulerWorker pops a Task | 62 // Called when |sequence| isn't empty after the SchedulerWorker pops a Task |
| 63 // from it. |sequence| is the last Sequence returned by GetWork(). | 63 // from it. |sequence| is the last Sequence returned by GetWork(). |
| 64 virtual void ReEnqueueSequence(scoped_refptr<Sequence> sequence) = 0; | 64 virtual void ReEnqueueSequence(scoped_refptr<Sequence> sequence) = 0; |
| 65 | 65 |
| 66 // Called by a thread to determine how long to sleep before the next call to | 66 // Called by a thread to determine how long to sleep before the next call to |
| 67 // GetWork(). GetWork() may be called before this timeout expires if the | 67 // GetWork(). GetWork() may be called before this timeout expires if the |
| 68 // worker's WakeUp() method is called. | 68 // worker's WakeUp() method is called. |
| 69 virtual TimeDelta GetSleepTimeout() = 0; | 69 virtual TimeDelta GetSleepTimeout() = 0; |
| 70 | 70 |
| 71 // Called by a thread to wait for work. Override this method if the thread |
| 72 // in question needs special handling to go to sleep. |wake_up_event| is a |
| 73 // manually resettable event and is signaled on SchedulerWorker::WakeUp() |
| 74 virtual void WaitForWork(WaitableEvent* wake_up_event); |
| 75 |
| 71 // Called by a thread if it is allowed to detach if the last call to | 76 // Called by a thread if it is allowed to detach if the last call to |
| 72 // GetWork() returned nullptr. | 77 // GetWork() returned nullptr. |
| 73 // | 78 // |
| 74 // It is the responsibility of the delegate to determine if detachment is | 79 // It is the responsibility of the delegate to determine if detachment is |
| 75 // safe. If the delegate is responsible for thread-affine work, detachment | 80 // safe. If the delegate is responsible for thread-affine work, detachment |
| 76 // is generally not safe. | 81 // is generally not safe. |
| 77 // | 82 // |
| 78 // When true is returned: | 83 // When true is returned: |
| 79 // - The next WakeUp() could be more costly due to new thread creation. | 84 // - The next WakeUp() could be more costly due to new thread creation. |
| 80 // - The worker will take this as a signal that it can detach, but it is not | 85 // - The worker will take this as a signal that it can detach, but it is not |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 // Set once JoinForTesting() has been called. | 197 // Set once JoinForTesting() has been called. |
| 193 AtomicFlag join_called_for_testing_; | 198 AtomicFlag join_called_for_testing_; |
| 194 | 199 |
| 195 DISALLOW_COPY_AND_ASSIGN(SchedulerWorker); | 200 DISALLOW_COPY_AND_ASSIGN(SchedulerWorker); |
| 196 }; | 201 }; |
| 197 | 202 |
| 198 } // namespace internal | 203 } // namespace internal |
| 199 } // namespace base | 204 } // namespace base |
| 200 | 205 |
| 201 #endif // BASE_TASK_SCHEDULER_SCHEDULER_WORKER_H_ | 206 #endif // BASE_TASK_SCHEDULER_SCHEDULER_WORKER_H_ |
| OLD | NEW |