thd.h
Go to the documentation of this file.
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 
25 
26 #include <stddef.h>
27 
28 #include <grpc/support/log.h>
29 
30 namespace grpc_core {
31 namespace internal {
32 
35  public:
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:
51  return *this;
52  }
53  bool joinable() const { return joinable_; }
54 
57  tracked_ = tracked;
58  return *this;
59  }
60  bool tracked() const { return tracked_; }
61 
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  };
78  Thread() : state_(FAKE), impl_(nullptr) {}
79 
86  Thread(const char* thd_name, void (*thd_body)(void* arg), void* arg,
87  bool* success = nullptr, const Options& options = Options());
88 
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 
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 
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 {
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 {
149  }
150  }
151 
152  private:
153  Thread(const Thread&) = delete;
154  Thread& operator=(const Thread&) = delete;
155 
167 };
168 
169 } // namespace grpc_core
170 
171 #endif /* GRPC_CORE_LIB_GPRPP_THD_H */
grpc_core::Thread::FAILED
@ FAILED
Definition: thd.h:163
log.h
grpc_core::Thread::ThreadState
ThreadState
Definition: thd.h:163
grpc_core::Thread::operator=
Thread & operator=(Thread &&other) noexcept
Definition: thd.h:101
grpc_core::Thread::options_
Options options_
Definition: thd.h:166
grpc_core::Thread::state_
ThreadState state_
Definition: thd.h:164
grpc_core
Definition: call_metric_recorder.h:31
options
double_dict options[]
Definition: capstone_test.c:55
grpc_core::internal::ThreadInternalsInterface
Base class for platform-specific thread-state.
Definition: thd.h:34
grpc_core::Thread::Options::tracked_
bool tracked_
Definition: thd.h:72
grpc_core::Thread::Thread
Thread(Thread &&other) noexcept
Definition: thd.h:91
grpc_core::Thread::Options::joinable
bool joinable() const
Definition: thd.h:53
grpc_core::Thread::Options::Options
Options()
Definition: thd.h:47
true
#define true
Definition: setup_once.h:324
grpc_core::Thread::Options::tracked
bool tracked() const
Definition: thd.h:60
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
grpc_core::Thread::~Thread
~Thread()
Definition: thd.h:123
grpc_core::Thread::DONE
@ DONE
Definition: thd.h:163
arg
Definition: cmdline.cc:40
grpc_core::Thread::Thread
Thread()
Definition: thd.h:78
grpc_core::Thread::Join
void Join()
Definition: thd.h:141
grpc_core::Thread::Options::set_stack_size
Options & set_stack_size(size_t bytes)
Definition: thd.h:64
grpc_core::Thread::ALIVE
@ ALIVE
Definition: thd.h:163
grpc_core::Thread::Options::stack_size
size_t stack_size() const
Definition: thd.h:68
grpc_core::Thread::Options
Definition: thd.h:45
grpc_core::Thread::Start
void Start()
Definition: thd.h:125
grpc_core::Thread::FAKE
@ FAKE
Definition: thd.h:163
grpc_core::internal::ThreadInternalsInterface::Start
virtual void Start()=0
grpc_core::Thread::Options::set_joinable
Options & set_joinable(bool joinable)
Set whether the thread is joinable or detached.
Definition: thd.h:49
grpc_core::Thread::MOVED
@ MOVED
Definition: thd.h:163
grpc_core::Thread::impl_
internal::ThreadInternalsInterface * impl_
Definition: thd.h:165
grpc_core::internal::ThreadInternalsInterface::~ThreadInternalsInterface
virtual ~ThreadInternalsInterface()
Definition: thd.h:36
bytes
uint8 bytes[10]
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/coded_stream_unittest.cc:153
grpc_core::Thread::Options::stack_size_
size_t stack_size_
Definition: thd.h:73
grpc_core::Thread
Definition: thd.h:43
grpc_core::Thread::STARTED
@ STARTED
Definition: thd.h:163
grpc_core::Thread::Options::set_tracked
Options & set_tracked(bool tracked)
Set whether the thread is tracked for fork support.
Definition: thd.h:56
internal
Definition: benchmark/test/output_test_helper.cc:20
grpc_core::internal::ThreadInternalsInterface::Join
virtual void Join()=0
grpc_core::Thread::Options::joinable_
bool joinable_
Definition: thd.h:71
port_platform.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:36