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
3 // found in the LICENSE file.
5 #include "ppapi/utility/threading/simple_thread.h"
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;
19 const size_t kDefaultStackSize
= 0;
24 MessageLoop message_loop
;
26 SimpleThread::ThreadFunc func
;
31 DWORD WINAPI
RunThread(void* void_data
) {
33 void* RunThread(void* void_data
) {
35 ThreadData
* data
= static_cast<ThreadData
*>(void_data
);
36 data
->message_loop
.AttachToCurrentThread();
39 data
->func(data
->message_loop
, data
->user_data
);
41 data
->message_loop
.Run();
49 SimpleThread::SimpleThread(const InstanceHandle
& instance
)
50 : instance_(instance
),
51 message_loop_(instance
),
52 stacksize_(kDefaultStackSize
),
56 SimpleThread::SimpleThread(const InstanceHandle
& instance
,
58 : instance_(instance
),
59 message_loop_(instance
),
60 stacksize_(stacksize
),
64 SimpleThread::~SimpleThread() {
68 bool SimpleThread::Start() {
69 return StartWithFunction(NULL
, NULL
);
72 bool SimpleThread::Join() {
76 message_loop_
.PostQuit(true);
79 DWORD result
= WaitForSingleObject(thread_
, INFINITE
);
82 return result
== WAIT_OBJECT_0
;
86 int result
= pthread_join(thread_
, &retval
);
92 bool SimpleThread::StartWithFunction(ThreadFunc func
, void* user_data
) {
96 ThreadData
* data
= new ThreadData
;
97 data
->message_loop
= message_loop_
;
99 data
->user_data
= user_data
;
102 thread_
= CreateThread(NULL
, stacksize_
, &RunThread
, data
, 0, NULL
);
106 pthread_attr_init(&attr
);
109 setval
= pthread_attr_setstacksize(&attr
, stacksize_
);
110 if (setval
!= 0 || pthread_create(&thread_
, &attr
, &RunThread
, data
) != 0) {