summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
authormmentovai@google.com <mmentovai@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-08-28 17:40:59 +0000
committermmentovai@google.com <mmentovai@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-08-28 17:40:59 +0000
commitb060bd427af7ea65e6afc74d9aef3f055894154e (patch)
treecfbc2a1a7fb30dd82033ada58a6575582b9571c8 /base
parent7ec93dfb306b62e48cdf2498f4a2b5074074969a (diff)
downloadchromium_src-b060bd427af7ea65e6afc74d9aef3f055894154e.zip
chromium_src-b060bd427af7ea65e6afc74d9aef3f055894154e.tar.gz
chromium_src-b060bd427af7ea65e6afc74d9aef3f055894154e.tar.bz2
WorkerPool implementation for Mac, using NSOperationQueue
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@1493 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r--base/base.xcodeproj/project.pbxproj12
-rw-r--r--base/singleton_objc.h60
-rw-r--r--base/worker_pool_mac.mm72
-rw-r--r--base/worker_pool_unittest.cc5
4 files changed, 148 insertions, 1 deletions
diff --git a/base/base.xcodeproj/project.pbxproj b/base/base.xcodeproj/project.pbxproj
index 8ff872e..fc5ccfa 100644
--- a/base/base.xcodeproj/project.pbxproj
+++ b/base/base.xcodeproj/project.pbxproj
@@ -84,6 +84,8 @@
7BD8F6D30E65DB0200034DE9 /* bzip2_error_handler.cc in Sources */ = {isa = PBXBuildFile; fileRef = 7BD8F6D20E65DB0200034DE9 /* bzip2_error_handler.cc */; };
7BD8F7740E65E89800034DE9 /* string16.cc in Sources */ = {isa = PBXBuildFile; fileRef = 7BD8F7730E65E89800034DE9 /* string16.cc */; };
7BF164F30E660CA500AA999E /* platform_thread_mac.mm in Sources */ = {isa = PBXBuildFile; fileRef = 7BF164F20E660CA500AA999E /* platform_thread_mac.mm */; };
+ 7BF1658D0E663B4800AA999E /* worker_pool_mac.mm in Sources */ = {isa = PBXBuildFile; fileRef = 7BF1658B0E663B3500AA999E /* worker_pool_mac.mm */; };
+ 7BF1658E0E663B4A00AA999E /* worker_pool_unittest.cc in Sources */ = {isa = PBXBuildFile; fileRef = 7BF1658C0E663B3500AA999E /* worker_pool_unittest.cc */; };
820EB4F70E3A613F009668FC /* string_piece.cc in Sources */ = {isa = PBXBuildFile; fileRef = 820EB4F50E3A613F009668FC /* string_piece.cc */; };
820EB4FA0E3A6178009668FC /* string_util_icu.cc in Sources */ = {isa = PBXBuildFile; fileRef = 820EB4F90E3A6178009668FC /* string_util_icu.cc */; };
820EB5020E3A618B009668FC /* tracked.cc in Sources */ = {isa = PBXBuildFile; fileRef = 820EB4FE0E3A618B009668FC /* tracked.cc */; };
@@ -362,6 +364,10 @@
7BEFC29C0D99832D000829AD /* lock_impl.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = lock_impl.h; sourceTree = "<group>"; };
7BEFC29D0D99832D000829AD /* lock_impl_posix.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = lock_impl_posix.cc; sourceTree = "<group>"; };
7BF164F20E660CA500AA999E /* platform_thread_mac.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = platform_thread_mac.mm; sourceTree = "<group>"; };
+ 7BF165810E663AA800AA999E /* singleton_objc.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = singleton_objc.h; sourceTree = "<group>"; };
+ 7BF1658A0E663B3500AA999E /* worker_pool.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = worker_pool.h; sourceTree = "<group>"; };
+ 7BF1658B0E663B3500AA999E /* worker_pool_mac.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = worker_pool_mac.mm; sourceTree = "<group>"; };
+ 7BF1658C0E663B3500AA999E /* worker_pool_unittest.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = worker_pool_unittest.cc; sourceTree = "<group>"; };
820EB4EB0E3A60FE009668FC /* idle_timer.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = idle_timer.cc; sourceTree = "<group>"; };
820EB4EC0E3A60FE009668FC /* idle_timer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = idle_timer.h; sourceTree = "<group>"; };
820EB4EF0E3A610A009668FC /* linked_ptr.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = linked_ptr.h; sourceTree = "<group>"; };
@@ -831,6 +837,7 @@
825403760D92D2CF0006B936 /* shared_memory.h */,
824653730DC12D0E007C2BAA /* shared_memory_posix.cc */,
7BD9E84E0DA447F800FC7A01 /* singleton.h */,
+ 7BF165810E663AA800AA999E /* singleton_objc.h */,
E4AFA4BE0E50DE7400201347 /* singleton_unittest.cc */,
BA73AA420E5F62F400A20026 /* spin_wait.h */,
825403770D92D2CF0006B936 /* stack_container.h */,
@@ -888,6 +895,9 @@
8254038E0D92D2CF0006B936 /* word_iterator.cc */,
8254038F0D92D2CF0006B936 /* word_iterator.h */,
E4AFA4CE0E50E0C500201347 /* word_iterator_unittest.cc */,
+ 7BF1658A0E663B3500AA999E /* worker_pool.h */,
+ 7BF1658B0E663B3500AA999E /* worker_pool_mac.mm */,
+ 7BF1658C0E663B3500AA999E /* worker_pool_unittest.cc */,
);
name = base;
sourceTree = "<group>";
@@ -1218,6 +1228,7 @@
ABF4B9C30DC2BD6C00A6E319 /* values.cc in Sources */,
9301C03B0E54C839001EF103 /* waitable_event_generic.cc in Sources */,
824655DD0DC659B8007C2BAA /* word_iterator.cc in Sources */,
+ 7BF1658D0E663B4800AA999E /* worker_pool_mac.mm in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -1279,6 +1290,7 @@
7B78D3A30E54FE0100609465 /* values_unittest.cc in Sources */,
7B78D3A40E54FE0100609465 /* waitable_event_unittest.cc in Sources */,
7B78D3A50E54FE0100609465 /* word_iterator_unittest.cc in Sources */,
+ 7BF1658E0E663B4A00AA999E /* worker_pool_unittest.cc in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
diff --git a/base/singleton_objc.h b/base/singleton_objc.h
new file mode 100644
index 0000000..ba9fc78
--- /dev/null
+++ b/base/singleton_objc.h
@@ -0,0 +1,60 @@
+// Copyright (c) 2008 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Support for using the Singleton<T> pattern with Objective-C objects. A
+// SingletonObjC is the same as a Singleton, except the default traits are
+// appropriate for Objective-C objects. A typical Objective-C object of type
+// NSExampleType can be maintained as a singleton and accessed with:
+//
+// NSExampleType* exampleSingleton = SingletonObjC<NSExampleType>::get();
+//
+// The first time this is used, it will create exampleSingleton as the result
+// of [[NSExampleType alloc] init]. Subsequent calls will return the same
+// NSExampleType* object. The object will be released by calling
+// -[NSExampleType release] when Singleton's atexit routines run
+// (see singleton.h).
+//
+// For Objective-C objects initialized through means other than the
+// no-parameter -init selector, DefaultSingletonObjCTraits may be extended
+// as needed:
+//
+// struct FooSingletonTraits : public DefaultSingletonObjCTraits<Foo> {
+// static Foo* New() {
+// return [[Foo alloc] initWithName:@"selecty"];
+// }
+// }
+// ...
+// Foo* widgetSingleton = SingletonObjC<Foo, FooSingletonTraits>::get();
+
+#ifndef BASE_SINGLETON_OBJC_H_
+#define BASE_SINGLETON_OBJC_H_
+
+#import <Foundation/Foundation.h>
+#include "base/singleton.h"
+
+// Singleton traits usable to manage traditional Objective-C objects, which
+// are instantiated by sending |alloc| and |init| messages, and are deallocated
+// in a memory-managed environment when their retain counts drop to 0 by
+// sending |release| messages.
+template<typename Type>
+struct DefaultSingletonObjCTraits : public DefaultSingletonTraits<Type> {
+ static Type* New() {
+ return [[Type alloc] init];
+ }
+
+ static void Delete(Type* object) {
+ [object release];
+ }
+};
+
+// Exactly like Singleton, but without the DefaultSingletonObjCTraits as the
+// default trait class. This makes it straightforward for Objective-C++ code
+// to hold Objective-C objects as singletons.
+template<typename Type,
+ typename Traits = DefaultSingletonObjCTraits<Type>,
+ typename DifferentiatingType = Type>
+class SingletonObjC : public Singleton<Type, Traits, DifferentiatingType> {
+};
+
+#endif // BASE_SINGLETON_OBJC_H_
diff --git a/base/worker_pool_mac.mm b/base/worker_pool_mac.mm
new file mode 100644
index 0000000..1147110
--- /dev/null
+++ b/base/worker_pool_mac.mm
@@ -0,0 +1,72 @@
+// Copyright (c) 2008 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/worker_pool.h"
+
+#import <Foundation/Foundation.h>
+
+#include "base/logging.h"
+#import "base/singleton_objc.h"
+#include "base/task.h"
+
+// TaskOperation adapts Task->Run() for use in an NSOperationQueue.
+@interface TaskOperation : NSOperation {
+ @private
+ Task* task_; // (strong)
+}
+
+// Returns an autoreleased instance of TaskOperation. See -initWithTask: for
+// details.
++ (id)taskOperationWithTask:(Task*)task;
+
+// Designated initializer. |task| is adopted as the Task* whose Run method
+// this operation will call when executed.
+- (id)initWithTask:(Task*)task;
+
+@end
+
+@implementation TaskOperation
+
++ (id)taskOperationWithTask:(Task*)task {
+ return [[[TaskOperation alloc] initWithTask:task] autorelease];
+}
+
+- (id)init {
+ return [self initWithTask:NULL];
+}
+
+- (id)initWithTask:(Task*)task {
+ if ((self = [super init])) {
+ task_ = task;
+ }
+ return self;
+}
+
+- (void)main {
+ DCHECK(task_) << "-[TaskOperation main] called with no task";
+ task_->Run();
+ delete task_;
+ task_ = NULL;
+}
+
+- (void)dealloc {
+ DCHECK(!task_) << "-[TaskOperation dealloc] called on unused TaskOperation";
+ delete task_;
+ [super dealloc];
+}
+
+@end
+
+bool WorkerPool::PostTask(const tracked_objects::Location& from_here,
+ Task* task, bool task_is_slow) {
+ // Ignore |task_is_slow|, it doesn't map directly to any tunable aspect of
+ // an NSOperation.
+
+ task->SetBirthPlace(from_here);
+
+ NSOperationQueue* operation_queue = SingletonObjC<NSOperationQueue>::get();
+ [operation_queue addOperation:[TaskOperation taskOperationWithTask:task]];
+
+ return true;
+}
diff --git a/base/worker_pool_unittest.cc b/base/worker_pool_unittest.cc
index ba79766..aea78e6 100644
--- a/base/worker_pool_unittest.cc
+++ b/base/worker_pool_unittest.cc
@@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+#include "base/platform_test.h"
#include "base/task.h"
#include "base/waitable_event.h"
#include "base/worker_pool.h"
@@ -9,6 +10,8 @@
using base::WaitableEvent;
+typedef PlatformTest WorkerPoolTest;
+
namespace {
class PostTaskTestTask : public Task {
@@ -24,7 +27,7 @@ class PostTaskTestTask : public Task {
WaitableEvent* event_;
};
-TEST(WorkerPoolTest, PostTask) {
+TEST_F(WorkerPoolTest, PostTask) {
WaitableEvent test_event(false, false);
WaitableEvent long_test_event(false, false);
bool signaled;