| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef PPAPI_UTILITY_THREADING_SIMPLE_THREAD_H_ | 5 #ifndef PPAPI_UTILITY_THREADING_SIMPLE_THREAD_H_ |
| 6 #define PPAPI_UTILITY_THREADING_SIMPLE_THREAD_H_ | 6 #define PPAPI_UTILITY_THREADING_SIMPLE_THREAD_H_ |
| 7 | 7 |
| 8 #ifdef WIN32 | 8 #ifdef WIN32 |
| 9 #include <windows.h> | 9 #include <windows.h> |
| 10 #else | 10 #else |
| 11 #include <pthread.h> | 11 #include <pthread.h> |
| 12 #endif | 12 #endif |
| 13 | 13 |
| 14 #include "ppapi/cpp/dev/message_loop_dev.h" | 14 #include "ppapi/cpp/dev/message_loop_dev.h" |
| 15 #include "ppapi/cpp/instance_handle.h" |
| 15 | 16 |
| 16 namespace pp { | 17 namespace pp { |
| 17 | 18 |
| 18 // This class is a simple wrapper around a pthread/Windows thread that creates | 19 // This class is a simple wrapper around a pthread/Windows thread that creates |
| 19 // and runs a PPAPI message loop on that thread. | 20 // and runs a PPAPI message loop on that thread. |
| 20 class SimpleThread { | 21 class SimpleThread { |
| 21 public: | 22 public: |
| 22 #ifdef WIN32 | 23 #ifdef WIN32 |
| 23 typedef HANDLE ThreadHandle; | 24 typedef HANDLE ThreadHandle; |
| 24 #else | 25 #else |
| 25 typedef pthread_t ThreadHandle; | 26 typedef pthread_t ThreadHandle; |
| 26 #endif | 27 #endif |
| 27 | 28 |
| 28 typedef void (*ThreadFunc)(MessageLoop_Dev&, void* user_data); | 29 typedef void (*ThreadFunc)(MessageLoop_Dev&, void* user_data); |
| 29 | 30 |
| 30 SimpleThread(Instance* instance); | 31 explicit SimpleThread(const InstanceHandle& instance); |
| 31 ~SimpleThread(); | 32 ~SimpleThread(); |
| 32 | 33 |
| 33 // Starts a thread and runs a message loop in it. If you need control over | 34 // Starts a thread and runs a message loop in it. If you need control over |
| 34 // how the message loop is run, use StartWithFunction. Returns true on | 35 // how the message loop is run, use StartWithFunction. Returns true on |
| 35 // success, false if the thread is already running or couldn't be started. | 36 // success, false if the thread is already running or couldn't be started. |
| 36 bool Start(); | 37 bool Start(); |
| 37 | 38 |
| 38 // Posts a quit message to the message loop and blocks until the thread | 39 // Posts a quit message to the message loop and blocks until the thread |
| 39 // exits. Returns true on success. If the thread is not running, returns | 40 // exits. Returns true on success. If the thread is not running, returns |
| 40 // false. | 41 // false. |
| 41 bool Join(); | 42 bool Join(); |
| 42 | 43 |
| 43 // Normally you can just use Start() to start a thread, and then post work to | 44 // Normally you can just use Start() to start a thread, and then post work to |
| 44 // it. In some cases you will want control over the message. If ThreadFunc | 45 // it. In some cases you will want control over the message. If ThreadFunc |
| 45 // is NULL, this acts the same as Start(). | 46 // is NULL, this acts the same as Start(). |
| 46 bool StartWithFunction(ThreadFunc func, void* user_data); | 47 bool StartWithFunction(ThreadFunc func, void* user_data); |
| 47 | 48 |
| 48 MessageLoop_Dev& message_loop() { return message_loop_; } | 49 MessageLoop_Dev& message_loop() { return message_loop_; } |
| 49 ThreadHandle thread() const { return thread_; } | 50 ThreadHandle thread() const { return thread_; } |
| 50 | 51 |
| 51 private: | 52 private: |
| 52 Instance* instance_; | 53 InstanceHandle instance_; |
| 53 MessageLoop_Dev message_loop_; | 54 MessageLoop_Dev message_loop_; |
| 54 | 55 |
| 55 ThreadHandle thread_; | 56 ThreadHandle thread_; |
| 56 | 57 |
| 57 // Disallow (not implemented). | 58 // Disallow (not implemented). |
| 58 SimpleThread(const SimpleThread&); | 59 SimpleThread(const SimpleThread&); |
| 59 SimpleThread& operator=(const SimpleThread&); | 60 SimpleThread& operator=(const SimpleThread&); |
| 60 }; | 61 }; |
| 61 | 62 |
| 62 } // namespace pp | 63 } // namespace pp |
| 63 | 64 |
| 64 #endif // PPAPI_UTILITY_THREADING_SIMPLE_THREAD_H_ | 65 #endif // PPAPI_UTILITY_THREADING_SIMPLE_THREAD_H_ |
| 65 | 66 |
| OLD | NEW |