| 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 #include "ppapi/utility/threading/simple_thread.h" | 5 #include "ppapi/utility/threading/simple_thread.h" |
| 6 | 6 |
| 7 #ifdef WIN32 | 7 #ifdef WIN32 |
| 8 #include <windows.h> | 8 #include <windows.h> |
| 9 #endif | 9 #endif |
| 10 | 10 |
| 11 namespace pp { | 11 namespace pp { |
| 12 | 12 |
| 13 namespace { | 13 namespace { |
| 14 | 14 |
| 15 // Use 2MB default stack size for Native Client, otherwise use system default. |
| 16 #if defined(__native_client__) |
| 17 const size_t kDefaultStackSize = 2 * 1024 * 1024; |
| 18 #else |
| 19 const size_t kDefaultStackSize = 0; |
| 20 #endif |
| 21 |
| 22 |
| 15 struct ThreadData { | 23 struct ThreadData { |
| 16 MessageLoop message_loop; | 24 MessageLoop message_loop; |
| 17 | 25 |
| 18 SimpleThread::ThreadFunc func; | 26 SimpleThread::ThreadFunc func; |
| 19 void* user_data; | 27 void* user_data; |
| 20 }; | 28 }; |
| 21 | 29 |
| 22 #ifdef WIN32 | 30 #ifdef WIN32 |
| 23 DWORD WINAPI RunThread(void* void_data) { | 31 DWORD WINAPI RunThread(void* void_data) { |
| 24 #else | 32 #else |
| 25 void* RunThread(void* void_data) { | 33 void* RunThread(void* void_data) { |
| 26 #endif | 34 #endif |
| 27 ThreadData* data = static_cast<ThreadData*>(void_data); | 35 ThreadData* data = static_cast<ThreadData*>(void_data); |
| 28 data->message_loop.AttachToCurrentThread(); | 36 data->message_loop.AttachToCurrentThread(); |
| 29 | 37 |
| 30 if (data->func) | 38 if (data->func) |
| 31 data->func(data->message_loop, data->user_data); | 39 data->func(data->message_loop, data->user_data); |
| 32 else | 40 else |
| 33 data->message_loop.Run(); | 41 data->message_loop.Run(); |
| 34 | 42 |
| 35 delete data; | 43 delete data; |
| 36 return NULL; | 44 return NULL; |
| 37 } | 45 } |
| 38 | 46 |
| 39 } // namespace | 47 } // namespace |
| 40 | 48 |
| 41 SimpleThread::SimpleThread(const InstanceHandle& instance) | 49 SimpleThread::SimpleThread(const InstanceHandle& instance) |
| 42 : instance_(instance), | 50 : instance_(instance), |
| 43 message_loop_(instance), | 51 message_loop_(instance), |
| 52 stacksize_(kDefaultStackSize), |
| 53 thread_(0) { |
| 54 } |
| 55 |
| 56 SimpleThread::SimpleThread(const InstanceHandle& instance, |
| 57 const size_t stacksize) |
| 58 : instance_(instance), |
| 59 message_loop_(instance), |
| 60 stacksize_(stacksize), |
| 44 thread_(0) { | 61 thread_(0) { |
| 45 } | 62 } |
| 46 | 63 |
| 47 SimpleThread::~SimpleThread() { | 64 SimpleThread::~SimpleThread() { |
| 48 Join(); | 65 Join(); |
| 49 } | 66 } |
| 50 | 67 |
| 51 bool SimpleThread::Start() { | 68 bool SimpleThread::Start() { |
| 52 return StartWithFunction(NULL, NULL); | 69 return StartWithFunction(NULL, NULL); |
| 53 } | 70 } |
| (...skipping 21 matching lines...) Expand all Loading... |
| 75 bool SimpleThread::StartWithFunction(ThreadFunc func, void* user_data) { | 92 bool SimpleThread::StartWithFunction(ThreadFunc func, void* user_data) { |
| 76 if (thread_) | 93 if (thread_) |
| 77 return false; | 94 return false; |
| 78 | 95 |
| 79 ThreadData* data = new ThreadData; | 96 ThreadData* data = new ThreadData; |
| 80 data->message_loop = message_loop_; | 97 data->message_loop = message_loop_; |
| 81 data->func = func; | 98 data->func = func; |
| 82 data->user_data = user_data; | 99 data->user_data = user_data; |
| 83 | 100 |
| 84 #ifdef WIN32 | 101 #ifdef WIN32 |
| 85 thread_ = CreateThread(NULL, 0, &RunThread, data, 0, NULL); | 102 thread_ = CreateThread(NULL, stacksize_, &RunThread, data, 0, NULL); |
| 86 if (!thread_) { | 103 if (!thread_) { |
| 87 #else | 104 #else |
| 88 if (pthread_create(&thread_, NULL, &RunThread, data) != 0) { | 105 pthread_attr_t attr; |
| 106 pthread_attr_init(&attr); |
| 107 int setval = 0; |
| 108 if (stacksize_ > 0) |
| 109 setval = pthread_attr_setstacksize(&attr, stacksize_); |
| 110 if (setval != 0 || pthread_create(&thread_, &attr, &RunThread, data) != 0) { |
| 89 #endif | 111 #endif |
| 90 delete data; | 112 delete data; |
| 91 return false; | 113 return false; |
| 92 } | 114 } |
| 93 return true; | 115 return true; |
| 94 } | 116 } |
| 95 | 117 |
| 96 } // namespace pp | 118 } // namespace pp |
| OLD | NEW |