OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_MAC_LIBDISPATCH_SEQUENCED_TASK_RUNNER_H_ | 5 #ifndef BASE_MAC_LIBDISPATCH_SEQUENCED_TASK_RUNNER_H_ |
6 #define BASE_MAC_LIBDISPATCH_SEQUENCED_TASK_RUNNER_H_ | 6 #define BASE_MAC_LIBDISPATCH_SEQUENCED_TASK_RUNNER_H_ |
7 | 7 |
8 #include <dispatch/dispatch.h> | 8 #include <dispatch/dispatch.h> |
9 | 9 |
10 #include "base/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
| 11 #include "base/synchronization/waitable_event.h" |
11 | 12 |
12 namespace base { | 13 namespace base { |
13 namespace mac { | 14 namespace mac { |
14 | 15 |
15 // This is an implementation of the TaskRunner interface that runs closures on | 16 // This is an implementation of the TaskRunner interface that runs closures on |
16 // a thread managed by Apple's libdispatch. This has the benefit of being able | 17 // a thread managed by Apple's libdispatch. This has the benefit of being able |
17 // to PostTask() and friends to a dispatch queue, while being reusable as a | 18 // to PostTask() and friends to a dispatch queue, while being reusable as a |
18 // dispatch_queue_t. | 19 // dispatch_queue_t. |
19 // | 20 // |
20 // One would use this class if an object lives exclusively on one thread but | 21 // One would use this class if an object lives exclusively on one thread but |
(...skipping 20 matching lines...) Expand all Loading... |
41 const Closure& task, | 42 const Closure& task, |
42 base::TimeDelta delay) OVERRIDE; | 43 base::TimeDelta delay) OVERRIDE; |
43 virtual bool RunsTasksOnCurrentThread() const OVERRIDE; | 44 virtual bool RunsTasksOnCurrentThread() const OVERRIDE; |
44 | 45 |
45 // base::SequencedTaskRunner: | 46 // base::SequencedTaskRunner: |
46 virtual bool PostNonNestableDelayedTask( | 47 virtual bool PostNonNestableDelayedTask( |
47 const tracked_objects::Location& from_here, | 48 const tracked_objects::Location& from_here, |
48 const Closure& task, | 49 const Closure& task, |
49 base::TimeDelta delay) OVERRIDE; | 50 base::TimeDelta delay) OVERRIDE; |
50 | 51 |
| 52 // This blocks the calling thread until all work on the dispatch queue has |
| 53 // been run and the queue has been destroyed. Destroying a queue requires |
| 54 // ALL retained references to it to be released. Any new tasks posted to |
| 55 // this thread after shutdown are dropped. |
| 56 void Shutdown(); |
| 57 |
51 // Returns the dispatch queue associated with this task runner, for use with | 58 // Returns the dispatch queue associated with this task runner, for use with |
52 // system APIs that take dispatch queues. The caller is responsible for | 59 // system APIs that take dispatch queues. The caller is responsible for |
53 // retaining the result. | 60 // retaining the result. |
| 61 // |
| 62 // All properties (context, finalizer, etc.) are managed by this class, and |
| 63 // clients should only use the result of this for dispatch_async(). |
54 dispatch_queue_t GetDispatchQueue() const; | 64 dispatch_queue_t GetDispatchQueue() const; |
55 | 65 |
56 protected: | 66 protected: |
57 virtual ~LibDispatchTaskRunner(); | 67 virtual ~LibDispatchTaskRunner(); |
58 | 68 |
59 private: | 69 private: |
| 70 static void Finalizer(void* context); |
| 71 |
60 dispatch_queue_t queue_; | 72 dispatch_queue_t queue_; |
| 73 |
| 74 // The event on which Shutdown waits until Finalizer runs. |
| 75 base::WaitableEvent queue_finalized_; |
61 }; | 76 }; |
62 | 77 |
63 } // namespace mac | 78 } // namespace mac |
64 } // namespace base | 79 } // namespace base |
65 | 80 |
66 #endif // BASE_MAC_LIBDISPATCH_SEQUENCED_TASK_RUNNER_H_ | 81 #endif // BASE_MAC_LIBDISPATCH_SEQUENCED_TASK_RUNNER_H_ |
OLD | NEW |