OLD | NEW |
1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2008 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 #include "base/worker_pool.h" | 5 #include "base/worker_pool_mac.h" |
6 | |
7 #import <Foundation/Foundation.h> | |
8 | 6 |
9 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #import "base/scoped_nsautorelease_pool.h" |
| 9 #include "base/scoped_ptr.h" |
10 #import "base/singleton_objc.h" | 10 #import "base/singleton_objc.h" |
11 #include "base/task.h" | 11 #include "base/task.h" |
12 | 12 |
| 13 @implementation WorkerPoolObjC |
| 14 |
| 15 + (NSOperationQueue*)sharedOperationQueue { |
| 16 return SingletonObjC<NSOperationQueue>::get(); |
| 17 } |
| 18 |
| 19 @end // @implementation WorkerPoolObjC |
| 20 |
13 // TaskOperation adapts Task->Run() for use in an NSOperationQueue. | 21 // TaskOperation adapts Task->Run() for use in an NSOperationQueue. |
14 @interface TaskOperation : NSOperation { | 22 @interface TaskOperation : NSOperation { |
15 @private | 23 @private |
16 Task* task_; // (strong) | 24 scoped_ptr<Task> task_; |
17 } | 25 } |
18 | 26 |
19 // Returns an autoreleased instance of TaskOperation. See -initWithTask: for | 27 // Returns an autoreleased instance of TaskOperation. See -initWithTask: for |
20 // details. | 28 // details. |
21 + (id)taskOperationWithTask:(Task*)task; | 29 + (id)taskOperationWithTask:(Task*)task; |
22 | 30 |
23 // Designated initializer. |task| is adopted as the Task* whose Run method | 31 // Designated initializer. |task| is adopted as the Task* whose Run method |
24 // this operation will call when executed. | 32 // this operation will call when executed. |
25 - (id)initWithTask:(Task*)task; | 33 - (id)initWithTask:(Task*)task; |
26 | 34 |
27 @end | 35 @end // @interface TaskOperation |
28 | 36 |
29 @implementation TaskOperation | 37 @implementation TaskOperation |
30 | 38 |
31 + (id)taskOperationWithTask:(Task*)task { | 39 + (id)taskOperationWithTask:(Task*)task { |
32 return [[[TaskOperation alloc] initWithTask:task] autorelease]; | 40 return [[[TaskOperation alloc] initWithTask:task] autorelease]; |
33 } | 41 } |
34 | 42 |
35 - (id)init { | 43 - (id)init { |
36 return [self initWithTask:NULL]; | 44 return [self initWithTask:NULL]; |
37 } | 45 } |
38 | 46 |
39 - (id)initWithTask:(Task*)task { | 47 - (id)initWithTask:(Task*)task { |
40 if ((self = [super init])) { | 48 if ((self = [super init])) { |
41 task_ = task; | 49 task_.reset(task); |
42 } | 50 } |
43 return self; | 51 return self; |
44 } | 52 } |
45 | 53 |
46 - (void)main { | 54 - (void)main { |
47 DCHECK(task_) << "-[TaskOperation main] called with no task"; | 55 DCHECK(task_.get()) << "-[TaskOperation main] called with no task"; |
| 56 if (!task_.get()) { |
| 57 return; |
| 58 } |
| 59 |
| 60 base::ScopedNSAutoreleasePool autoreleasePool; |
| 61 |
48 task_->Run(); | 62 task_->Run(); |
49 delete task_; | 63 task_.reset(NULL); |
50 task_ = NULL; | |
51 } | 64 } |
52 | 65 |
53 - (void)dealloc { | 66 - (void)dealloc { |
54 DCHECK(!task_) << "-[TaskOperation dealloc] called on unused TaskOperation"; | 67 DCHECK(!task_.get()) |
55 delete task_; | 68 << "-[TaskOperation dealloc] called without running task"; |
| 69 |
56 [super dealloc]; | 70 [super dealloc]; |
57 } | 71 } |
58 | 72 |
59 @end | 73 @end // @implementation TaskOperation |
60 | 74 |
61 bool WorkerPool::PostTask(const tracked_objects::Location& from_here, | 75 bool WorkerPool::PostTask(const tracked_objects::Location& from_here, |
62 Task* task, bool task_is_slow) { | 76 Task* task, bool task_is_slow) { |
63 // Ignore |task_is_slow|, it doesn't map directly to any tunable aspect of | 77 // Ignore |task_is_slow|, it doesn't map directly to any tunable aspect of |
64 // an NSOperation. | 78 // an NSOperation. |
65 | 79 |
| 80 DCHECK(task) << "WorkerPool::PostTask called with no task"; |
| 81 if (!task) { |
| 82 return false; |
| 83 } |
| 84 |
66 task->SetBirthPlace(from_here); | 85 task->SetBirthPlace(from_here); |
67 | 86 |
68 NSOperationQueue* operation_queue = SingletonObjC<NSOperationQueue>::get(); | 87 NSOperationQueue* operation_queue = [WorkerPoolObjC sharedOperationQueue]; |
69 [operation_queue addOperation:[TaskOperation taskOperationWithTask:task]]; | 88 [operation_queue addOperation:[TaskOperation taskOperationWithTask:task]]; |
70 | 89 |
71 return true; | 90 return true; |
72 } | 91 } |
OLD | NEW |