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