summaryrefslogtreecommitdiffstats
path: root/ppapi/utility/threading
diff options
context:
space:
mode:
authorbrettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-01-26 05:15:48 +0000
committerbrettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-01-26 05:15:48 +0000
commit574c50aaf667d40981d81a046307f3057ab2eaee (patch)
tree4aa89f0e2558416ae9642c1126add05c7e6f3bc0 /ppapi/utility/threading
parentfd8f0fad7c0d359f2eb9a1ffd24e7861c45d0850 (diff)
downloadchromium_src-574c50aaf667d40981d81a046307f3057ab2eaee.zip
chromium_src-574c50aaf667d40981d81a046307f3057ab2eaee.tar.gz
chromium_src-574c50aaf667d40981d81a046307f3057ab2eaee.tar.bz2
First pass at implementing the MessageLoop interface. This includes a simple
example and a helper class. The current example just asserts due to thread checks we have in there now, but this should provide a good starting point. BUG= TEST= Review URL: https://chromiumcodereview.appspot.com/9097006 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@119198 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ppapi/utility/threading')
-rw-r--r--ppapi/utility/threading/simple_thread.cc96
-rw-r--r--ppapi/utility/threading/simple_thread.h65
2 files changed, 161 insertions, 0 deletions
diff --git a/ppapi/utility/threading/simple_thread.cc b/ppapi/utility/threading/simple_thread.cc
new file mode 100644
index 0000000..d0876e9
--- /dev/null
+++ b/ppapi/utility/threading/simple_thread.cc
@@ -0,0 +1,96 @@
+// Copyright (c) 2012 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 "ppapi/utility/threading/simple_thread.h"
+
+#ifdef WIN32
+#include <windows.h>
+#endif
+
+namespace pp {
+
+namespace {
+
+struct ThreadData {
+ MessageLoop_Dev message_loop;
+
+ SimpleThread::ThreadFunc func;
+ void* user_data;
+};
+
+#ifdef WIN32
+DWORD WINAPI RunThread(void* void_data) {
+#else
+void* RunThread(void* void_data) {
+#endif
+ ThreadData* data = static_cast<ThreadData*>(void_data);
+ data->message_loop.AttachToCurrentThread();
+
+ if (data->func)
+ data->func(data->message_loop, data->user_data);
+ else
+ data->message_loop.Run();
+
+ delete data;
+ return NULL;
+}
+
+} // namespace
+
+SimpleThread::SimpleThread(Instance* instance)
+ : instance_(instance),
+ message_loop_(instance),
+ thread_(0) {
+}
+
+SimpleThread::~SimpleThread() {
+ Join();
+}
+
+bool SimpleThread::Start() {
+ return StartWithFunction(NULL, NULL);
+}
+
+bool SimpleThread::Join() {
+ if (!thread_)
+ return false;
+
+ message_loop_.PostQuit(true);
+
+#ifdef WIN32
+ DWORD result = WaitForSingleObject(thread_, INFINITE);
+ CloseHandle(thread_);
+ thread_ = 0;
+ return result == WAIT_OBJECT_0;
+
+#else
+ void* retval;
+ int result = pthread_join(thread_, &retval);
+ thread_ = 0;
+ return result == 0;
+#endif
+}
+
+bool SimpleThread::StartWithFunction(ThreadFunc func, void* user_data) {
+ if (thread_)
+ return false;
+
+ ThreadData* data = new ThreadData;
+ data->message_loop = message_loop_;
+ data->func = func;
+ data->user_data = user_data;
+
+#ifdef WIN32
+ thread_ = CreateThread(NULL, 0, &RunThread, data, 0, NULL);
+ if (!thread_) {
+#else
+ if (pthread_create(&thread_, NULL, &RunThread, data) != 0) {
+#endif
+ delete data;
+ return false;
+ }
+ return true;
+}
+
+} // namespace pp
diff --git a/ppapi/utility/threading/simple_thread.h b/ppapi/utility/threading/simple_thread.h
new file mode 100644
index 0000000..a093961
--- /dev/null
+++ b/ppapi/utility/threading/simple_thread.h
@@ -0,0 +1,65 @@
+// Copyright (c) 2012 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.
+
+#ifndef PPAPI_UTILITY_THREADING_SIMPLE_THREAD_H_
+#define PPAPI_UTILITY_THREADING_SIMPLE_THREAD_H_
+
+#ifdef WIN32
+#include <windows.h>
+#else
+#include <pthread.h>
+#endif
+
+#include "ppapi/cpp/dev/message_loop_dev.h"
+
+namespace pp {
+
+// This class is a simple wrapper around a pthread/Windows thread that creates
+// and runs a PPAPI message loop on that thread.
+class SimpleThread {
+ public:
+#ifdef WIN32
+ typedef HANDLE ThreadHandle;
+#else
+ typedef pthread_t ThreadHandle;
+#endif
+
+ typedef void (*ThreadFunc)(MessageLoop_Dev&, void* user_data);
+
+ SimpleThread(Instance* instance);
+ ~SimpleThread();
+
+ // Starts a thread and runs a message loop in it. If you need control over
+ // how the message loop is run, use StartWithFunction. Returns true on
+ // success, false if the thread is already running or couldn't be started.
+ bool Start();
+
+ // Posts a quit message to the message loop and blocks until the thread
+ // exits. Returns true on success. If the thread is not running, returns
+ // false.
+ bool Join();
+
+ // Normally you can just use Start() to start a thread, and then post work to
+ // it. In some cases you will want control over the message. If ThreadFunc
+ // is NULL, this acts the same as Start().
+ bool StartWithFunction(ThreadFunc func, void* user_data);
+
+ MessageLoop_Dev& message_loop() { return message_loop_; }
+ ThreadHandle thread() const { return thread_; }
+
+ private:
+ Instance* instance_;
+ MessageLoop_Dev message_loop_;
+
+ ThreadHandle thread_;
+
+ // Disallow (not implemented).
+ SimpleThread(const SimpleThread&);
+ SimpleThread& operator=(const SimpleThread&);
+};
+
+} // namespace pp
+
+#endif // PPAPI_UTILITY_THREADING_SIMPLE_THREAD_H_
+