michael@0: // Copyright (c) 2008 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #include "base/worker_pool.h" michael@0: michael@0: #import michael@0: michael@0: #include "base/logging.h" michael@0: #import "base/singleton_objc.h" michael@0: #include "base/task.h" michael@0: michael@0: // TaskOperation adapts Task->Run() for use in an NSOperationQueue. michael@0: @interface TaskOperation : NSOperation { michael@0: @private michael@0: Task* task_; // (strong) michael@0: } michael@0: michael@0: // Returns an autoreleased instance of TaskOperation. See -initWithTask: for michael@0: // details. michael@0: + (id)taskOperationWithTask:(Task*)task; michael@0: michael@0: // Designated initializer. |task| is adopted as the Task* whose Run method michael@0: // this operation will call when executed. michael@0: - (id)initWithTask:(Task*)task; michael@0: michael@0: @end michael@0: michael@0: @implementation TaskOperation michael@0: michael@0: + (id)taskOperationWithTask:(Task*)task { michael@0: return [[[TaskOperation alloc] initWithTask:task] autorelease]; michael@0: } michael@0: michael@0: - (id)init { michael@0: return [self initWithTask:NULL]; michael@0: } michael@0: michael@0: - (id)initWithTask:(Task*)task { michael@0: if ((self = [super init])) { michael@0: task_ = task; michael@0: } michael@0: return self; michael@0: } michael@0: michael@0: - (void)main { michael@0: DCHECK(task_) << "-[TaskOperation main] called with no task"; michael@0: task_->Run(); michael@0: delete task_; michael@0: task_ = NULL; michael@0: } michael@0: michael@0: - (void)dealloc { michael@0: DCHECK(!task_) << "-[TaskOperation dealloc] called on unused TaskOperation"; michael@0: delete task_; michael@0: [super dealloc]; michael@0: } michael@0: michael@0: @end michael@0: michael@0: bool WorkerPool::PostTask(const tracked_objects::Location& from_here, michael@0: Task* task, bool task_is_slow) { michael@0: // Ignore |task_is_slow|, it doesn't map directly to any tunable aspect of michael@0: // an NSOperation. michael@0: michael@0: task->SetBirthPlace(from_here); michael@0: michael@0: NSOperationQueue* operation_queue = SingletonObjC::get(); michael@0: [operation_queue addOperation:[TaskOperation taskOperationWithTask:task]]; michael@0: michael@0: return true; michael@0: }