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