Debian Codegraph @

src/core/lib/gprpp/thd.h

raw
1 /*
2 *
3 * Copyright 2015 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 #ifndef GRPC_CORE_LIB_GPRPP_THD_H
20 #define GRPC_CORE_LIB_GPRPP_THD_H
21
22 /** Internal thread interface. */
23
24 #include <grpc/support/port_platform.h>
25
26 #include <stddef.h>
27
28 #include <grpc/support/log.h>
29
30 namespace grpc_core {
31 namespace internal {
32
33 /// Base class for platform-specific thread-state
34 class ThreadInternalsInterface {
35 public:
36 virtual ~ThreadInternalsInterface() {}
37 virtual void Start() = 0;
38 virtual void Join() = 0;
39 };
40
41 } // namespace internal
42
43 class Thread {
44 public:
45 class Options {
46 public:
47 Options() : joinable_(true), tracked_(true), stack_size_(0) {}
48 /// Set whether the thread is joinable or detached.
49 Options& set_joinable(bool joinable) {
50 joinable_ = joinable;
51 return *this;
52 }
53 bool joinable() const { return joinable_; }
54
55 /// Set whether the thread is tracked for fork support.
56 Options& set_tracked(bool tracked) {
57 tracked_ = tracked;
58 return *this;
59 }
60 bool tracked() const { return tracked_; }
61
62 /// Sets thread stack size (in bytes). Sets to 0 will use the default stack
63 /// size which is 64KB for Windows threads and 2MB for Posix(x86) threads.
64 Options& set_stack_size(size_t bytes) {
65 stack_size_ = bytes;
66 return *this;
67 }
68 size_t stack_size() const { return stack_size_; }
69
70 private:
71 bool joinable_;
72 bool tracked_;
73 size_t stack_size_;
74 };
75 /// Default constructor only to allow use in structs that lack constructors
76 /// Does not produce a validly-constructed thread; must later
77 /// use placement new to construct a real thread. Does not init mu_ and cv_
78 Thread() : state_(FAKE), impl_(nullptr) {}
79
80 /// Normal constructor to create a thread with name \a thd_name,
81 /// which will execute a thread based on function \a thd_body
82 /// with argument \a arg once it is started.
83 /// The optional \a success argument indicates whether the thread
84 /// is successfully created.
85 /// The optional \a options can be used to set the thread detachable.
86 Thread(const char* thd_name, void (*thd_body)(void* arg), void* arg,
87 bool* success = nullptr, const Options& options = Options());
88
89 /// Move constructor for thread. After this is called, the other thread
90 /// no longer represents a living thread object
91 Thread(Thread&& other) noexcept
92 : state_(other.state_), impl_(other.impl_), options_(other.options_) {
93 other.state_ = MOVED;
94 other.impl_ = nullptr;
95 other.options_ = Options();
96 }
97
98 /// Move assignment operator for thread. After this is called, the other
99 /// thread no longer represents a living thread object. Not allowed if this
100 /// thread actually exists
101 Thread& operator=(Thread&& other) noexcept {
102 if (this != &other) {
103 // TODO(vjpai): if we can be sure that all Thread's are actually
104 // constructed, then we should assert GPR_ASSERT(impl_ == nullptr) here.
105 // However, as long as threads come in structures that are
106 // allocated via gpr_malloc, this will not be the case, so we cannot
107 // assert it for the time being.
108 state_ = other.state_;
109 impl_ = other.impl_;
110 options_ = other.options_;
111 other.state_ = MOVED;
112 other.impl_ = nullptr;
113 other.options_ = Options();
114 }
115 return *this;
116 }
117
118 /// The destructor is strictly optional; either the thread never came to life
119 /// and the constructor itself killed it, or it has already been joined and
120 /// the Join function kills it, or it was detached (non-joinable) and it has
121 /// run to completion and is now killing itself. The destructor shouldn't have
122 /// to do anything.
123 ~Thread() { GPR_ASSERT(!options_.joinable() || impl_ == nullptr); }
124
125 void Start() {
126 if (impl_ != nullptr) {
127 GPR_ASSERT(state_ == ALIVE);
128 state_ = STARTED;
129 impl_->Start();
130 // If the Thread is not joinable, then the impl_ will cause the deletion
131 // of this Thread object when the thread function completes. Since no
132 // other operation is allowed to a detached thread after Start, there is
133 // no need to change the value of the impl_ or state_ . The next operation
134 // on this object will be the deletion, which will trigger the destructor.
135 } else {
136 GPR_ASSERT(state_ == FAILED);
137 }
138 }
139
140 // It is only legal to call Join if the Thread is created as joinable.
141 void Join() {
142 if (impl_ != nullptr) {
143 impl_->Join();
144 delete impl_;
145 state_ = DONE;
146 impl_ = nullptr;
147 } else {
148 GPR_ASSERT(state_ == FAILED);
149 }
150 }
151
152 private:
153 Thread(const Thread&) = delete;
154 Thread& operator=(const Thread&) = delete;
155
156 /// The thread states are as follows:
157 /// FAKE -- just a phony placeholder Thread created by the default constructor
158 /// ALIVE -- an actual thread of control exists associated with this thread
159 /// STARTED -- the thread of control has been started
160 /// DONE -- the thread of control has completed and been joined
161 /// FAILED -- the thread of control never came alive
162 /// MOVED -- contents were moved out and we're no longer tracking them
163 enum ThreadState { FAKE, ALIVE, STARTED, DONE, FAILED, MOVED };
164 ThreadState state_;
165 internal::ThreadInternalsInterface* impl_;
166 Options options_;
167 };
168
169 } // namespace grpc_core
170
171 #endif /* GRPC_CORE_LIB_GPRPP_THD_H */