| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "mojo/public/cpp/utility/thread.h" | |
| 6 | |
| 7 #include <assert.h> | |
| 8 | |
| 9 #include "base/compiler_specific.h" | |
| 10 | |
| 11 namespace mojo { | |
| 12 | |
| 13 Thread::Thread() : options_(), thread_(), started_(false), joined_(false) { | |
| 14 } | |
| 15 | |
| 16 Thread::Thread(const Options& options) | |
| 17 : options_(options), thread_(), started_(false), joined_(false) { | |
| 18 } | |
| 19 | |
| 20 Thread::~Thread() { | |
| 21 // If it was started, it must have been joined. | |
| 22 assert(!started_ || joined_); | |
| 23 } | |
| 24 | |
| 25 void Thread::Start() { | |
| 26 assert(!started_); | |
| 27 assert(!joined_); | |
| 28 | |
| 29 pthread_attr_t attr; | |
| 30 int rv = pthread_attr_init(&attr); | |
| 31 ALLOW_UNUSED_LOCAL(rv); | |
| 32 assert(rv == 0); | |
| 33 | |
| 34 // Non-default stack size? | |
| 35 if (options_.stack_size() != 0) { | |
| 36 rv = pthread_attr_setstacksize(&attr, options_.stack_size()); | |
| 37 assert(rv == 0); | |
| 38 } | |
| 39 | |
| 40 started_ = true; | |
| 41 rv = pthread_create(&thread_, &attr, &ThreadRunTrampoline, this); | |
| 42 assert(rv == 0); | |
| 43 | |
| 44 rv = pthread_attr_destroy(&attr); | |
| 45 assert(rv == 0); | |
| 46 } | |
| 47 | |
| 48 void Thread::Join() { | |
| 49 // Must have been started but not yet joined. | |
| 50 assert(started_); | |
| 51 assert(!joined_); | |
| 52 | |
| 53 joined_ = true; | |
| 54 int rv = pthread_join(thread_, nullptr); | |
| 55 ALLOW_UNUSED_LOCAL(rv); | |
| 56 assert(rv == 0); | |
| 57 } | |
| 58 | |
| 59 // static | |
| 60 void* Thread::ThreadRunTrampoline(void* arg) { | |
| 61 Thread* self = static_cast<Thread*>(arg); | |
| 62 self->Run(); | |
| 63 return nullptr; | |
| 64 } | |
| 65 | |
| 66 } // namespace mojo | |
| OLD | NEW |