1 // Copyright 2014 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 "mojo/public/cpp/utility/thread.h"
11 Thread::Thread() : options_(), thread_(), started_(false), joined_(false) {
14 Thread::Thread(const Options& options)
15 : options_(options), thread_(), started_(false), joined_(false) {
19 // If it was started, it must have been joined.
20 assert(!started_ || joined_);
23 void Thread::Start() {
28 int rv = pthread_attr_init(&attr);
29 MOJO_ALLOW_UNUSED_LOCAL(rv);
32 // Non-default stack size?
33 if (options_.stack_size() != 0) {
34 rv = pthread_attr_setstacksize(&attr, options_.stack_size());
39 rv = pthread_create(&thread_, &attr, &ThreadRunTrampoline, this);
42 rv = pthread_attr_destroy(&attr);
47 // Must have been started but not yet joined.
52 int rv = pthread_join(thread_, NULL);
53 MOJO_ALLOW_UNUSED_LOCAL(rv);
58 void* Thread::ThreadRunTrampoline(void* arg) {
59 Thread* self = static_cast<Thread*>(arg);