diff options
author | brettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-01-26 06:27:16 +0000 |
---|---|---|
committer | brettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-01-26 06:27:16 +0000 |
commit | 52f604edea346ddf298ce4b0828bf6dfd8e61eca (patch) | |
tree | ae4344f80e5d7099194317c927c4225607228c50 /ppapi/utility | |
parent | 1fdd73c241697f66cd05f1587137e000f90f3489 (diff) | |
download | chromium_src-52f604edea346ddf298ce4b0828bf6dfd8e61eca.zip chromium_src-52f604edea346ddf298ce4b0828bf6dfd8e61eca.tar.gz chromium_src-52f604edea346ddf298ce4b0828bf6dfd8e61eca.tar.bz2 |
Revert 119198 - 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
TBR=brettw@chromium.org
Review URL: https://chromiumcodereview.appspot.com/9290040
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@119200 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ppapi/utility')
-rw-r--r-- | ppapi/utility/threading/simple_thread.cc | 96 | ||||
-rw-r--r-- | ppapi/utility/threading/simple_thread.h | 65 |
2 files changed, 0 insertions, 161 deletions
diff --git a/ppapi/utility/threading/simple_thread.cc b/ppapi/utility/threading/simple_thread.cc deleted file mode 100644 index d0876e9..0000000 --- a/ppapi/utility/threading/simple_thread.cc +++ /dev/null @@ -1,96 +0,0 @@ -// 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 deleted file mode 100644 index a093961..0000000 --- a/ppapi/utility/threading/simple_thread.h +++ /dev/null @@ -1,65 +0,0 @@ -// 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_ - |