Skip to content

Commit

Permalink
Provide alternate constructor for specifying a simple thread's stacks…
Browse files Browse the repository at this point in the history
…ize.

For Native Client builds, make default stack size 2MB regardless of
newlib or glibc.

TEST=try bots
BUG=none

Review URL: https://chromiumcodereview.appspot.com/12259018

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@183618 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
nfullagar@chromium.org committed Feb 20, 2013
1 parent 2eb12c2 commit 86bd4e7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
26 changes: 24 additions & 2 deletions ppapi/utility/threading/simple_thread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ namespace pp {

namespace {

// Use 2MB default stack size for Native Client, otherwise use system default.
#if defined(__native_client__)
const size_t kDefaultStackSize = 2 * 1024 * 1024;
#else
const size_t kDefaultStackSize = 0;
#endif


struct ThreadData {
MessageLoop message_loop;

Expand Down Expand Up @@ -41,6 +49,15 @@ void* RunThread(void* void_data) {
SimpleThread::SimpleThread(const InstanceHandle& instance)
: instance_(instance),
message_loop_(instance),
stacksize_(kDefaultStackSize),
thread_(0) {
}

SimpleThread::SimpleThread(const InstanceHandle& instance,
size_t stacksize)
: instance_(instance),
message_loop_(instance),
stacksize_(stacksize),
thread_(0) {
}

Expand Down Expand Up @@ -82,10 +99,15 @@ bool SimpleThread::StartWithFunction(ThreadFunc func, void* user_data) {
data->user_data = user_data;

#ifdef WIN32
thread_ = CreateThread(NULL, 0, &RunThread, data, 0, NULL);
thread_ = CreateThread(NULL, stacksize_, &RunThread, data, 0, NULL);
if (!thread_) {
#else
if (pthread_create(&thread_, NULL, &RunThread, data) != 0) {
pthread_attr_t attr;
pthread_attr_init(&attr);
int setval = 0;
if (stacksize_ > 0)
setval = pthread_attr_setstacksize(&attr, stacksize_);
if (setval != 0 || pthread_create(&thread_, &attr, &RunThread, data) != 0) {
#endif
delete data;
return false;
Expand Down
4 changes: 3 additions & 1 deletion ppapi/utility/threading/simple_thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class SimpleThread {
typedef void (*ThreadFunc)(MessageLoop&, void* user_data);

explicit SimpleThread(const InstanceHandle& instance);
explicit SimpleThread(const InstanceHandle& instance, size_t stacksize);
~SimpleThread();

// Starts a thread and runs a message loop in it. If you need control over
Expand All @@ -52,11 +53,12 @@ class SimpleThread {
private:
InstanceHandle instance_;
MessageLoop message_loop_;

const size_t stacksize_;
ThreadHandle thread_;

// Disallow (not implemented).
SimpleThread(const SimpleThread&);
SimpleThread(const SimpleThread&, size_t stacksize);
SimpleThread& operator=(const SimpleThread&);
};

Expand Down

0 comments on commit 86bd4e7

Please sign in to comment.