summaryrefslogtreecommitdiffstats
path: root/base/simple_thread.cc
diff options
context:
space:
mode:
authordeanm@google.com <deanm@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-08-29 07:54:54 +0000
committerdeanm@google.com <deanm@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-08-29 07:54:54 +0000
commit8a2cebb5dc1393c74c1921852369cc45e291914e (patch)
treefd6bf76b0f7d6b3469873adb2983bb9fdb80451c /base/simple_thread.cc
parent7a8475c6da19434a3bae6783279fd9e9ad79fc47 (diff)
downloadchromium_src-8a2cebb5dc1393c74c1921852369cc45e291914e.zip
chromium_src-8a2cebb5dc1393c74c1921852369cc45e291914e.tar.gz
chromium_src-8a2cebb5dc1393c74c1921852369cc45e291914e.tar.bz2
Bring back SimpleThread, but with a Delegate interface.
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@1519 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/simple_thread.cc')
-rw-r--r--base/simple_thread.cc52
1 files changed, 52 insertions, 0 deletions
diff --git a/base/simple_thread.cc b/base/simple_thread.cc
new file mode 100644
index 0000000..fa9a723
--- /dev/null
+++ b/base/simple_thread.cc
@@ -0,0 +1,52 @@
+// 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/simple_thread.h"
+
+#include "base/waitable_event.h"
+#include "base/logging.h"
+#include "base/platform_thread.h"
+#include "base/string_util.h"
+
+namespace base {
+
+void SimpleThread::Start() {
+ DCHECK(!HasBeenStarted()) << "Tried to Start a thread multiple times.";
+ bool success = PlatformThread::Create(options_.stack_size(), this, &thread_);
+ CHECK(success);
+ event_.Wait(); // Wait for the thread to complete initialization.
+}
+
+void SimpleThread::Join() {
+ DCHECK(HasBeenStarted()) << "Tried to Join a never-started thread.";
+ DCHECK(!HasBeenJoined()) << "Tried to Join a thread multiple times.";
+ PlatformThread::Join(thread_);
+ joined_ = true;
+}
+
+void SimpleThread::ThreadMain() {
+ tid_ = PlatformThread::CurrentId();
+ // Construct our full name of the form "name_prefix_/TID".
+ name_.push_back('/');
+ name_.append(IntToString(tid_));
+ PlatformThread::SetName(name_.c_str());
+
+ // We've initialized our new thread, signal that we're done to Start().
+ event_.Signal();
+
+ Run();
+}
+
+SimpleThread::~SimpleThread() {
+ DCHECK(HasBeenStarted()) << "SimpleThread was never started.";
+ DCHECK(HasBeenJoined()) << "SimpleThread destroyed without being Join()ed.";
+}
+
+void DelegateSimpleThread::Run() {
+ DCHECK(delegate_) << "Tried to call Run without a delegate (called twice?)";
+ delegate_->Run();
+ delegate_ = NULL;
+}
+
+} // namespace base