diff options
author | dsh@google.com <dsh@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-08-27 21:07:24 +0000 |
---|---|---|
committer | dsh@google.com <dsh@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-08-27 21:07:24 +0000 |
commit | 0c3e3ed3f85afe7483ed476d2b185d7b21c31a84 (patch) | |
tree | 31269374aa45ec5de0a92a5ad1a22d1528bd60d8 /base | |
parent | e1a64ca263731e00a8629e97f9bbc8027cafdb5b (diff) | |
download | chromium_src-0c3e3ed3f85afe7483ed476d2b185d7b21c31a84.zip chromium_src-0c3e3ed3f85afe7483ed476d2b185d7b21c31a84.tar.gz chromium_src-0c3e3ed3f85afe7483ed476d2b185d7b21c31a84.tar.bz2 |
Add unit tests for WorkerPool and add a working but poor implementation for Linux.
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@1466 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/SConscript | 3 | ||||
-rw-r--r-- | base/worker_pool.cc | 40 | ||||
-rw-r--r-- | base/worker_pool_unittest.cc | 41 |
3 files changed, 83 insertions, 1 deletions
diff --git a/base/SConscript b/base/SConscript index 2114560..27dcab6 100644 --- a/base/SConscript +++ b/base/SConscript @@ -90,6 +90,7 @@ input_files = [ 'tracked_objects.cc', 'values.cc', 'word_iterator.cc', + 'worker_pool.cc', 'third_party/nspr/prtime.cc', 'third_party/nss/sha512.cc', ] @@ -116,7 +117,6 @@ if env['PLATFORM'] == 'win32': 'resource_util.cc', # Uses HMODULE, but may be abstractable. 'stats_table.cc', # Amanda is working on this(?). - 'worker_pool.cc', # Maybe not necessary for test shell. ]) if env['PLATFORM'] == 'win32': @@ -276,6 +276,7 @@ test_files = [ 'values_unittest.cc', 'waitable_event_unittest.cc', 'word_iterator_unittest.cc', + 'worker_pool_unittest.cc', 'gfx/convolver_unittest.cc', 'gfx/image_operations_unittest.cc', 'gfx/png_codec_unittest.cc', diff --git a/base/worker_pool.cc b/base/worker_pool.cc index fb64c85..b2c8c2b 100644 --- a/base/worker_pool.cc +++ b/base/worker_pool.cc @@ -6,6 +6,9 @@ #include "base/task.h" +// TODO(dsh): Split this file into worker_pool_win.cc and worker_pool_posix.cc. +#if defined(OS_WIN) + namespace { DWORD CALLBACK WorkItemCallback(void* param) { @@ -34,3 +37,40 @@ bool WorkerPool::PostTask(const tracked_objects::Location& from_here, return true; } +#elif defined(OS_LINUX) + +namespace { + +void* PThreadCallback(void* param) { + Task* task = static_cast<Task*>(param); + task->Run(); + delete task; + return 0; +} + +} // namespace + +bool WorkerPool::PostTask(const tracked_objects::Location& from_here, + Task* task, bool task_is_slow) { + task->SetBirthPlace(from_here); + pthread_t thread; + pthread_attr_t attr; + + // POSIX does not have a worker thread pool implementation. For now we just + // create a thread for each task, and ignore |task_is_slow|. + // TODO(dsh): Implement thread reuse. + pthread_attr_init(&attr); + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); + + int err = pthread_create(&thread, &attr, PThreadCallback, task); + pthread_attr_destroy(&attr); + if (err) { + DLOG(ERROR) << "pthread_create failed: " << err; + delete task; + return false; + } + + return true; +} + +#endif // OS_LINUX diff --git a/base/worker_pool_unittest.cc b/base/worker_pool_unittest.cc new file mode 100644 index 0000000..ba79766 --- /dev/null +++ b/base/worker_pool_unittest.cc @@ -0,0 +1,41 @@ +// Copyright (c) 2006-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/task.h" +#include "base/waitable_event.h" +#include "base/worker_pool.h" +#include "testing/gtest/include/gtest/gtest.h" + +using base::WaitableEvent; + +namespace { + +class PostTaskTestTask : public Task { + public: + PostTaskTestTask(WaitableEvent* event) : event_(event) { + } + + void Run() { + event_->Signal(); + } + + private: + WaitableEvent* event_; +}; + +TEST(WorkerPoolTest, PostTask) { + WaitableEvent test_event(false, false); + WaitableEvent long_test_event(false, false); + bool signaled; + + WorkerPool::PostTask(FROM_HERE, new PostTaskTestTask(&test_event), false); + WorkerPool::PostTask(FROM_HERE, new PostTaskTestTask(&long_test_event), true); + + signaled = test_event.Wait(); + EXPECT_TRUE(signaled); + signaled = long_test_event.Wait(); + EXPECT_TRUE(signaled); +} + +} // namespace |