summaryrefslogtreecommitdiffstats
path: root/ppapi
diff options
context:
space:
mode:
authorbrettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-01-26 19:40:58 +0000
committerbrettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-01-26 19:40:58 +0000
commitcefc3f13fd0616eb2aeae1c4fdb63b8bb5476761 (patch)
tree5de41b0f2d14535afa5e49cc9bae0526a8b16f3f /ppapi
parent50fa6f92efd0aceae9dbe689b3b216d003b095a7 (diff)
downloadchromium_src-cefc3f13fd0616eb2aeae1c4fdb63b8bb5476761.zip
chromium_src-cefc3f13fd0616eb2aeae1c4fdb63b8bb5476761.tar.gz
chromium_src-cefc3f13fd0616eb2aeae1c4fdb63b8bb5476761.tar.bz2
Revert 119200, the rest of the patch.
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. git-svn-id: svn://svn.chromium.org/chrome/trunk/src@119270 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ppapi')
-rw-r--r--ppapi/examples/threading/threading.cc60
-rw-r--r--ppapi/utility/threading/simple_thread.cc96
-rw-r--r--ppapi/utility/threading/simple_thread.h65
3 files changed, 221 insertions, 0 deletions
diff --git a/ppapi/examples/threading/threading.cc b/ppapi/examples/threading/threading.cc
new file mode 100644
index 0000000..bfd21a9
--- /dev/null
+++ b/ppapi/examples/threading/threading.cc
@@ -0,0 +1,60 @@
+// 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/c/pp_errors.h"
+#include "ppapi/cpp/input_event.h"
+#include "ppapi/cpp/instance.h"
+#include "ppapi/cpp/module.h"
+#include "ppapi/utility/completion_callback_factory.h"
+#include "ppapi/utility/threading/simple_thread.h"
+
+class MyInstance : public pp::Instance {
+ public:
+ MyInstance(PP_Instance instance) : pp::Instance(instance) {
+ thread_ = new pp::SimpleThread(this);
+ factory_.Initialize(this);
+ }
+
+ virtual ~MyInstance() {
+ delete thread_;
+ }
+
+ virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]) {
+ thread_->Start();
+ thread_->message_loop().PostWork(
+ factory_.NewCallback(&MyInstance::CallOnBackground));
+ return true;
+ }
+
+ virtual void DidChangeView(const pp::View& view) {
+ }
+
+ private:
+ void CallOnBackground(int32_t result) {
+ }
+
+ pp::CompletionCallbackFactory<MyInstance> factory_;
+
+ pp::SimpleThread* thread_;
+};
+
+
+class MyModule : public pp::Module {
+ public:
+ MyModule() : pp::Module() {}
+ virtual ~MyModule() {}
+
+ virtual pp::Instance* CreateInstance(PP_Instance instance) {
+ return new MyInstance(instance);
+ }
+};
+
+namespace pp {
+
+// Factory function for your specialization of the Module object.
+Module* CreateModule() {
+ return new MyModule();
+}
+
+} // namespace pp
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_
+