|
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 |
|
3 // found in the LICENSE file. |
|
4 |
|
5 #include "base/worker_pool.h" |
|
6 |
|
7 #import <Foundation/Foundation.h> |
|
8 |
|
9 #include "base/logging.h" |
|
10 #import "base/singleton_objc.h" |
|
11 #include "base/task.h" |
|
12 |
|
13 // TaskOperation adapts Task->Run() for use in an NSOperationQueue. |
|
14 @interface TaskOperation : NSOperation { |
|
15 @private |
|
16 Task* task_; // (strong) |
|
17 } |
|
18 |
|
19 // Returns an autoreleased instance of TaskOperation. See -initWithTask: for |
|
20 // details. |
|
21 + (id)taskOperationWithTask:(Task*)task; |
|
22 |
|
23 // Designated initializer. |task| is adopted as the Task* whose Run method |
|
24 // this operation will call when executed. |
|
25 - (id)initWithTask:(Task*)task; |
|
26 |
|
27 @end |
|
28 |
|
29 @implementation TaskOperation |
|
30 |
|
31 + (id)taskOperationWithTask:(Task*)task { |
|
32 return [[[TaskOperation alloc] initWithTask:task] autorelease]; |
|
33 } |
|
34 |
|
35 - (id)init { |
|
36 return [self initWithTask:NULL]; |
|
37 } |
|
38 |
|
39 - (id)initWithTask:(Task*)task { |
|
40 if ((self = [super init])) { |
|
41 task_ = task; |
|
42 } |
|
43 return self; |
|
44 } |
|
45 |
|
46 - (void)main { |
|
47 DCHECK(task_) << "-[TaskOperation main] called with no task"; |
|
48 task_->Run(); |
|
49 delete task_; |
|
50 task_ = NULL; |
|
51 } |
|
52 |
|
53 - (void)dealloc { |
|
54 DCHECK(!task_) << "-[TaskOperation dealloc] called on unused TaskOperation"; |
|
55 delete task_; |
|
56 [super dealloc]; |
|
57 } |
|
58 |
|
59 @end |
|
60 |
|
61 bool WorkerPool::PostTask(const tracked_objects::Location& from_here, |
|
62 Task* task, bool task_is_slow) { |
|
63 // Ignore |task_is_slow|, it doesn't map directly to any tunable aspect of |
|
64 // an NSOperation. |
|
65 |
|
66 task->SetBirthPlace(from_here); |
|
67 |
|
68 NSOperationQueue* operation_queue = SingletonObjC<NSOperationQueue>::get(); |
|
69 [operation_queue addOperation:[TaskOperation taskOperationWithTask:task]]; |
|
70 |
|
71 return true; |
|
72 } |