grpc 1.22.0 → 1.23.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of grpc might be problematic. Click here for more details.

Files changed (176) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +487 -649
  3. data/include/grpc/grpc_security.h +25 -0
  4. data/include/grpc/impl/codegen/grpc_types.h +11 -2
  5. data/include/grpc/impl/codegen/port_platform.h +12 -0
  6. data/src/core/ext/filters/client_channel/backup_poller.cc +4 -2
  7. data/src/core/ext/filters/client_channel/client_channel.cc +477 -182
  8. data/src/core/ext/filters/client_channel/client_channel_channelz.cc +25 -16
  9. data/src/core/ext/filters/client_channel/client_channel_channelz.h +11 -6
  10. data/src/core/ext/filters/client_channel/connector.h +10 -2
  11. data/src/core/ext/filters/client_channel/health/health_check_client.cc +3 -3
  12. data/src/core/ext/filters/client_channel/http_proxy.cc +9 -10
  13. data/src/core/ext/filters/client_channel/lb_policy.cc +2 -17
  14. data/src/core/ext/filters/client_channel/lb_policy.h +36 -8
  15. data/src/core/ext/filters/client_channel/lb_policy/grpclb/client_load_reporting_filter.cc +22 -8
  16. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.cc +86 -52
  17. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.h +7 -0
  18. data/src/core/ext/filters/client_channel/lb_policy/pick_first/pick_first.cc +73 -72
  19. data/src/core/ext/filters/client_channel/lb_policy/round_robin/round_robin.cc +8 -12
  20. data/src/core/ext/filters/client_channel/lb_policy/subchannel_list.h +25 -101
  21. data/src/core/ext/filters/client_channel/lb_policy/xds/xds.cc +5 -5
  22. data/src/core/ext/filters/client_channel/parse_address.cc +29 -26
  23. data/src/core/ext/filters/client_channel/resolver.h +3 -11
  24. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/dns_resolver_ares.cc +5 -3
  25. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver_windows.cc +405 -82
  26. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.cc +44 -51
  27. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper_libuv.cc +0 -1
  28. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper_windows.cc +0 -1
  29. data/src/core/ext/filters/client_channel/resolver/dns/native/dns_resolver.cc +11 -6
  30. data/src/core/ext/filters/client_channel/resolver/fake/fake_resolver.cc +130 -65
  31. data/src/core/ext/filters/client_channel/resolver/fake/fake_resolver.h +8 -3
  32. data/src/core/ext/filters/client_channel/resolver/sockaddr/sockaddr_resolver.cc +31 -14
  33. data/src/core/ext/filters/client_channel/resolver_factory.h +4 -0
  34. data/src/core/ext/filters/client_channel/resolver_registry.cc +11 -0
  35. data/src/core/ext/filters/client_channel/resolver_registry.h +3 -0
  36. data/src/core/ext/filters/client_channel/resolving_lb_policy.cc +10 -49
  37. data/src/core/ext/filters/client_channel/resolving_lb_policy.h +1 -14
  38. data/src/core/ext/filters/client_channel/retry_throttle.h +2 -3
  39. data/src/core/ext/filters/client_channel/subchannel.cc +65 -58
  40. data/src/core/ext/filters/client_channel/subchannel.h +65 -45
  41. data/src/core/ext/filters/client_channel/subchannel_interface.h +15 -30
  42. data/src/core/ext/filters/client_idle/client_idle_filter.cc +262 -0
  43. data/src/core/ext/filters/http/client/http_client_filter.cc +4 -5
  44. data/src/core/ext/filters/http/client_authority_filter.cc +2 -2
  45. data/src/core/ext/filters/http/message_compress/message_compress_filter.cc +140 -152
  46. data/src/core/ext/filters/max_age/max_age_filter.cc +3 -3
  47. data/src/core/ext/transport/chttp2/client/chttp2_connector.cc +3 -4
  48. data/src/core/ext/transport/chttp2/server/chttp2_server.cc +7 -6
  49. data/src/core/ext/transport/chttp2/transport/chttp2_transport.cc +63 -38
  50. data/src/core/ext/transport/chttp2/transport/context_list.cc +3 -1
  51. data/src/core/ext/transport/chttp2/transport/frame_data.cc +4 -4
  52. data/src/core/ext/transport/chttp2/transport/frame_goaway.cc +1 -1
  53. data/src/core/ext/transport/chttp2/transport/frame_ping.cc +1 -0
  54. data/src/core/ext/transport/chttp2/transport/frame_rst_stream.cc +8 -0
  55. data/src/core/ext/transport/chttp2/transport/frame_rst_stream.h +7 -0
  56. data/src/core/ext/transport/chttp2/transport/frame_settings.cc +1 -0
  57. data/src/core/ext/transport/chttp2/transport/hpack_encoder.cc +37 -22
  58. data/src/core/ext/transport/chttp2/transport/hpack_parser.cc +136 -81
  59. data/src/core/ext/transport/chttp2/transport/hpack_parser.h +8 -0
  60. data/src/core/ext/transport/chttp2/transport/hpack_table.cc +7 -166
  61. data/src/core/ext/transport/chttp2/transport/hpack_table.h +41 -15
  62. data/src/core/ext/transport/chttp2/transport/internal.h +13 -2
  63. data/src/core/ext/transport/chttp2/transport/parsing.cc +35 -22
  64. data/src/core/ext/transport/chttp2/transport/stream_map.cc +28 -18
  65. data/src/core/ext/transport/chttp2/transport/writing.cc +1 -0
  66. data/src/core/ext/transport/inproc/inproc_transport.cc +1 -1
  67. data/src/core/lib/channel/channelz.cc +80 -33
  68. data/src/core/lib/channel/channelz.h +28 -13
  69. data/src/core/lib/compression/compression.cc +1 -2
  70. data/src/core/lib/compression/compression_args.cc +13 -6
  71. data/src/core/lib/compression/compression_args.h +3 -2
  72. data/src/core/lib/compression/compression_internal.cc +1 -1
  73. data/src/core/lib/gpr/env_linux.cc +10 -21
  74. data/src/core/lib/gpr/env_posix.cc +0 -5
  75. data/src/core/lib/gpr/string.cc +7 -2
  76. data/src/core/lib/gpr/string.h +1 -0
  77. data/src/core/lib/gpr/sync_posix.cc +0 -129
  78. data/src/core/lib/gprpp/debug_location.h +3 -2
  79. data/src/core/lib/gprpp/fork.cc +14 -21
  80. data/src/core/lib/gprpp/fork.h +15 -4
  81. data/src/core/lib/gprpp/host_port.cc +118 -0
  82. data/src/core/lib/{gpr → gprpp}/host_port.h +27 -11
  83. data/src/core/lib/gprpp/map.h +25 -0
  84. data/src/core/lib/gprpp/memory.h +26 -9
  85. data/src/core/lib/gprpp/ref_counted.h +63 -21
  86. data/src/core/lib/gprpp/string_view.h +143 -0
  87. data/src/core/lib/gprpp/thd.h +10 -1
  88. data/src/core/lib/gprpp/thd_posix.cc +25 -0
  89. data/src/core/lib/gprpp/thd_windows.cc +9 -1
  90. data/src/core/lib/http/httpcli_security_connector.cc +3 -1
  91. data/src/core/lib/iomgr/cfstream_handle.cc +6 -1
  92. data/src/core/lib/iomgr/cfstream_handle.h +8 -2
  93. data/src/core/lib/iomgr/combiner.cc +4 -4
  94. data/src/core/lib/iomgr/error.cc +18 -8
  95. data/src/core/lib/iomgr/error.h +2 -0
  96. data/src/core/lib/iomgr/ev_posix.cc +4 -2
  97. data/src/core/lib/iomgr/executor.cc +4 -1
  98. data/src/core/lib/iomgr/executor/mpmcqueue.cc +183 -0
  99. data/src/core/lib/iomgr/executor/mpmcqueue.h +178 -0
  100. data/src/core/lib/iomgr/executor/threadpool.cc +138 -0
  101. data/src/core/lib/iomgr/executor/threadpool.h +153 -0
  102. data/src/core/lib/iomgr/fork_posix.cc +4 -2
  103. data/src/core/lib/iomgr/iocp_windows.cc +2 -2
  104. data/src/core/lib/iomgr/iomgr_posix_cfstream.cc +14 -0
  105. data/src/core/lib/iomgr/iomgr_uv.cc +3 -0
  106. data/src/core/lib/iomgr/lockfree_event.cc +3 -3
  107. data/src/core/lib/iomgr/resolve_address_custom.cc +16 -20
  108. data/src/core/lib/iomgr/resolve_address_posix.cc +8 -10
  109. data/src/core/lib/iomgr/resolve_address_windows.cc +6 -8
  110. data/src/core/lib/iomgr/sockaddr_utils.cc +5 -3
  111. data/src/core/lib/iomgr/socket_utils_common_posix.cc +0 -1
  112. data/src/core/lib/iomgr/socket_windows.h +1 -1
  113. data/src/core/lib/iomgr/tcp_client_cfstream.cc +7 -6
  114. data/src/core/lib/iomgr/tcp_client_custom.cc +1 -0
  115. data/src/core/lib/iomgr/tcp_custom.cc +4 -0
  116. data/src/core/lib/iomgr/tcp_posix.cc +8 -2
  117. data/src/core/lib/iomgr/tcp_server_custom.cc +1 -0
  118. data/src/core/lib/iomgr/tcp_server_windows.cc +1 -1
  119. data/src/core/lib/iomgr/tcp_windows.cc +7 -7
  120. data/src/core/lib/iomgr/timer_custom.cc +1 -0
  121. data/src/core/lib/iomgr/timer_manager.cc +0 -29
  122. data/src/core/lib/security/credentials/credentials.cc +84 -0
  123. data/src/core/lib/security/credentials/credentials.h +58 -2
  124. data/src/core/lib/security/credentials/jwt/json_token.cc +6 -2
  125. data/src/core/lib/security/credentials/jwt/json_token.h +1 -1
  126. data/src/core/lib/security/credentials/oauth2/oauth2_credentials.cc +245 -24
  127. data/src/core/lib/security/credentials/oauth2/oauth2_credentials.h +16 -0
  128. data/src/core/lib/security/security_connector/alts/alts_security_connector.cc +3 -2
  129. data/src/core/lib/security/security_connector/fake/fake_security_connector.cc +21 -25
  130. data/src/core/lib/security/security_connector/local/local_security_connector.cc +3 -2
  131. data/src/core/lib/security/security_connector/security_connector.cc +1 -1
  132. data/src/core/lib/security/security_connector/security_connector.h +1 -1
  133. data/src/core/lib/security/security_connector/ssl/ssl_security_connector.cc +19 -19
  134. data/src/core/lib/security/security_connector/ssl_utils.cc +26 -31
  135. data/src/core/lib/security/security_connector/ssl_utils.h +11 -8
  136. data/src/core/lib/security/security_connector/tls/spiffe_security_connector.cc +16 -20
  137. data/src/core/lib/security/security_connector/tls/spiffe_security_connector.h +4 -3
  138. data/src/core/lib/security/transport/client_auth_filter.cc +1 -2
  139. data/src/core/lib/security/util/json_util.cc +19 -5
  140. data/src/core/lib/security/util/json_util.h +3 -1
  141. data/src/core/lib/slice/slice.cc +69 -50
  142. data/src/core/lib/slice/slice_buffer.cc +6 -5
  143. data/src/core/lib/slice/slice_hash_table.h +3 -7
  144. data/src/core/lib/slice/slice_intern.cc +130 -39
  145. data/src/core/lib/slice/slice_internal.h +8 -0
  146. data/src/core/lib/slice/slice_utils.h +120 -0
  147. data/src/core/lib/slice/slice_weak_hash_table.h +2 -7
  148. data/src/core/lib/surface/call.cc +8 -3
  149. data/src/core/lib/surface/channel.cc +31 -8
  150. data/src/core/lib/surface/completion_queue.cc +17 -7
  151. data/src/core/lib/surface/init_secure.cc +4 -1
  152. data/src/core/lib/surface/lame_client.cc +2 -2
  153. data/src/core/lib/surface/server.cc +34 -35
  154. data/src/core/lib/surface/server.h +8 -17
  155. data/src/core/lib/surface/version.cc +1 -1
  156. data/src/core/lib/transport/byte_stream.cc +3 -5
  157. data/src/core/lib/transport/byte_stream.h +1 -2
  158. data/src/core/lib/transport/error_utils.cc +10 -1
  159. data/src/core/lib/transport/metadata.cc +202 -35
  160. data/src/core/lib/transport/metadata.h +81 -6
  161. data/src/core/lib/transport/static_metadata.cc +1257 -465
  162. data/src/core/lib/transport/static_metadata.h +190 -347
  163. data/src/core/lib/transport/timeout_encoding.cc +7 -0
  164. data/src/core/lib/transport/timeout_encoding.h +3 -2
  165. data/src/core/plugin_registry/grpc_plugin_registry.cc +4 -0
  166. data/src/core/tsi/alts/handshaker/alts_tsi_handshaker.cc +0 -1
  167. data/src/core/tsi/ssl/session_cache/ssl_session_cache.h +2 -7
  168. data/src/core/tsi/ssl_transport_security.cc +35 -43
  169. data/src/core/tsi/ssl_transport_security.h +2 -1
  170. data/src/ruby/ext/grpc/rb_grpc_imports.generated.c +2 -0
  171. data/src/ruby/ext/grpc/rb_grpc_imports.generated.h +3 -0
  172. data/src/ruby/lib/grpc/generic/rpc_server.rb +1 -1
  173. data/src/ruby/lib/grpc/grpc.rb +1 -1
  174. data/src/ruby/lib/grpc/version.rb +1 -1
  175. metadata +39 -33
  176. data/src/core/lib/gpr/host_port.cc +0 -98
@@ -0,0 +1,178 @@
1
+ /*
2
+ *
3
+ * Copyright 2019 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_IOMGR_EXECUTOR_MPMCQUEUE_H
20
+ #define GRPC_CORE_LIB_IOMGR_EXECUTOR_MPMCQUEUE_H
21
+
22
+ #include <grpc/support/port_platform.h>
23
+
24
+ #include "src/core/lib/debug/stats.h"
25
+ #include "src/core/lib/gprpp/abstract.h"
26
+ #include "src/core/lib/gprpp/atomic.h"
27
+ #include "src/core/lib/gprpp/sync.h"
28
+
29
+ namespace grpc_core {
30
+
31
+ extern DebugOnlyTraceFlag grpc_thread_pool_trace;
32
+
33
+ // Abstract base class of a Multiple-Producer-Multiple-Consumer(MPMC) queue
34
+ // interface
35
+ class MPMCQueueInterface {
36
+ public:
37
+ virtual ~MPMCQueueInterface() {}
38
+
39
+ // Puts elem into queue immediately at the end of queue.
40
+ // This might cause to block on full queue depending on implementation.
41
+ virtual void Put(void* elem) GRPC_ABSTRACT;
42
+
43
+ // Removes the oldest element from the queue and return it.
44
+ // This might cause to block on empty queue depending on implementation.
45
+ // Optional argument for collecting stats purpose.
46
+ virtual void* Get(gpr_timespec* wait_time = nullptr) GRPC_ABSTRACT;
47
+
48
+ // Returns number of elements in the queue currently
49
+ virtual int count() const GRPC_ABSTRACT;
50
+
51
+ GRPC_ABSTRACT_BASE_CLASS
52
+ };
53
+
54
+ class InfLenFIFOQueue : public MPMCQueueInterface {
55
+ public:
56
+ // Creates a new MPMC Queue. The queue created will have infinite length.
57
+ InfLenFIFOQueue();
58
+
59
+ // Releases all resources held by the queue. The queue must be empty, and no
60
+ // one waits on conditional variables.
61
+ ~InfLenFIFOQueue();
62
+
63
+ // Puts elem into queue immediately at the end of queue. Since the queue has
64
+ // infinite length, this routine will never block and should never fail.
65
+ void Put(void* elem);
66
+
67
+ // Removes the oldest element from the queue and returns it.
68
+ // This routine will cause the thread to block if queue is currently empty.
69
+ // Argument wait_time should be passed in when trace flag turning on (for
70
+ // collecting stats info purpose.)
71
+ void* Get(gpr_timespec* wait_time = nullptr);
72
+
73
+ // Returns number of elements in queue currently.
74
+ // There might be concurrently add/remove on queue, so count might change
75
+ // quickly.
76
+ int count() const { return count_.Load(MemoryOrder::RELAXED); }
77
+
78
+ struct Node {
79
+ Node* next; // Linking
80
+ Node* prev;
81
+ void* content; // Points to actual element
82
+ gpr_timespec insert_time; // Time for stats
83
+
84
+ Node() {
85
+ next = prev = nullptr;
86
+ content = nullptr;
87
+ }
88
+ };
89
+
90
+ // For test purpose only. Returns number of nodes allocated in queue.
91
+ // Any allocated node will be alive until the destruction of the queue.
92
+ int num_nodes() const { return num_nodes_; }
93
+
94
+ // For test purpose only. Returns the initial number of nodes in queue.
95
+ int init_num_nodes() const { return kQueueInitNumNodes; }
96
+
97
+ private:
98
+ // For Internal Use Only.
99
+ // Removes the oldest element from the queue and returns it. This routine
100
+ // will NOT check whether queue is empty, and it will NOT acquire mutex.
101
+ // Caller MUST check that queue is not empty and must acquire mutex before
102
+ // callling.
103
+ void* PopFront();
104
+
105
+ // Stats of queue. This will only be collect when debug trace mode is on.
106
+ // All printed stats info will have time measurement in microsecond.
107
+ struct Stats {
108
+ uint64_t num_started; // Number of elements have been added to queue
109
+ uint64_t num_completed; // Number of elements have been removed from
110
+ // the queue
111
+ gpr_timespec total_queue_time; // Total waiting time that all the
112
+ // removed elements have spent in queue
113
+ gpr_timespec max_queue_time; // Max waiting time among all removed
114
+ // elements
115
+ gpr_timespec busy_queue_time; // Accumulated amount of time that queue
116
+ // was not empty
117
+
118
+ Stats() {
119
+ num_started = 0;
120
+ num_completed = 0;
121
+ total_queue_time = gpr_time_0(GPR_TIMESPAN);
122
+ max_queue_time = gpr_time_0(GPR_TIMESPAN);
123
+ busy_queue_time = gpr_time_0(GPR_TIMESPAN);
124
+ }
125
+ };
126
+
127
+ // Node for waiting thread queue. Stands for one waiting thread, should have
128
+ // exact one thread waiting on its CondVar.
129
+ // Using a doubly linked list for waiting thread queue to wake up waiting
130
+ // threads in LIFO order to reduce cache misses.
131
+ struct Waiter {
132
+ CondVar cv;
133
+ Waiter* next;
134
+ Waiter* prev;
135
+ };
136
+
137
+ // Pushs waiter to the front of queue, require caller held mutex
138
+ void PushWaiter(Waiter* waiter);
139
+
140
+ // Removes waiter from queue, require caller held mutex
141
+ void RemoveWaiter(Waiter* waiter);
142
+
143
+ // Returns pointer to the waiter that should be waken up next, should be the
144
+ // last added waiter.
145
+ Waiter* TopWaiter();
146
+
147
+ Mutex mu_; // Protecting lock
148
+ Waiter waiters_; // Head of waiting thread queue
149
+
150
+ // Initial size for delete list
151
+ static const int kDeleteListInitSize = 1024;
152
+ // Initial number of nodes allocated
153
+ static const int kQueueInitNumNodes = 1024;
154
+
155
+ Node** delete_list_ = nullptr; // Keeps track of all allocated array entries
156
+ // for deleting on destruction
157
+ size_t delete_list_count_ = 0; // Number of entries in list
158
+ size_t delete_list_size_ = 0; // Size of the list. List will be expanded to
159
+ // double size on full
160
+
161
+ Node* queue_head_ = nullptr; // Head of the queue, remove position
162
+ Node* queue_tail_ = nullptr; // End of queue, insert position
163
+ Atomic<int> count_{0}; // Number of elements in queue
164
+ int num_nodes_ = 0; // Number of nodes allocated
165
+
166
+ Stats stats_; // Stats info
167
+ gpr_timespec busy_time; // Start time of busy queue
168
+
169
+ // Internal Helper.
170
+ // Allocates an array of nodes of size "num", links all nodes together except
171
+ // the first node's prev and last node's next. They should be set by caller
172
+ // manually afterward.
173
+ Node* AllocateNodes(int num);
174
+ };
175
+
176
+ } // namespace grpc_core
177
+
178
+ #endif /* GRPC_CORE_LIB_IOMGR_EXECUTOR_MPMCQUEUE_H */
@@ -0,0 +1,138 @@
1
+ /*
2
+ *
3
+ * Copyright 2019 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
+ #include <grpc/support/port_platform.h>
20
+
21
+ #include "src/core/lib/iomgr/executor/threadpool.h"
22
+
23
+ namespace grpc_core {
24
+
25
+ void ThreadPoolWorker::Run() {
26
+ while (true) {
27
+ void* elem;
28
+
29
+ if (GRPC_TRACE_FLAG_ENABLED(grpc_thread_pool_trace)) {
30
+ // Updates stats and print
31
+ gpr_timespec wait_time = gpr_time_0(GPR_TIMESPAN);
32
+ elem = queue_->Get(&wait_time);
33
+ stats_.sleep_time = gpr_time_add(stats_.sleep_time, wait_time);
34
+ gpr_log(GPR_INFO,
35
+ "ThreadPool Worker [%s %d] Stats: sleep_time %f",
36
+ thd_name_, index_, gpr_timespec_to_micros(stats_.sleep_time));
37
+ } else {
38
+ elem = queue_->Get(nullptr);
39
+ }
40
+ if (elem == nullptr) {
41
+ break;
42
+ }
43
+ // Runs closure
44
+ auto* closure =
45
+ static_cast<grpc_experimental_completion_queue_functor*>(elem);
46
+ closure->functor_run(closure, closure->internal_success);
47
+ }
48
+ }
49
+
50
+ void ThreadPool::SharedThreadPoolConstructor() {
51
+ // All worker threads in thread pool must be joinable.
52
+ thread_options_.set_joinable(true);
53
+
54
+ // Create at least 1 worker thread.
55
+ if (num_threads_ <= 0) num_threads_ = 1;
56
+
57
+ queue_ = New<InfLenFIFOQueue>();
58
+ threads_ = static_cast<ThreadPoolWorker**>(
59
+ gpr_zalloc(num_threads_ * sizeof(ThreadPoolWorker*)));
60
+ for (int i = 0; i < num_threads_; ++i) {
61
+ threads_[i] =
62
+ New<ThreadPoolWorker>(thd_name_, this, queue_, thread_options_, i);
63
+ threads_[i]->Start();
64
+ }
65
+ }
66
+
67
+ size_t ThreadPool::DefaultStackSize() {
68
+ #if defined(__ANDROID__) || defined(__APPLE__)
69
+ return 1952 * 1024;
70
+ #else
71
+ return 64 * 1024;
72
+ #endif
73
+ }
74
+
75
+ void ThreadPool::AssertHasNotBeenShutDown() {
76
+ // For debug checking purpose, using RELAXED order is sufficient.
77
+ GPR_DEBUG_ASSERT(!shut_down_.Load(MemoryOrder::RELAXED));
78
+ }
79
+
80
+ ThreadPool::ThreadPool(int num_threads) : num_threads_(num_threads) {
81
+ thd_name_ = "ThreadPoolWorker";
82
+ thread_options_ = Thread::Options();
83
+ thread_options_.set_stack_size(DefaultStackSize());
84
+ SharedThreadPoolConstructor();
85
+ }
86
+
87
+ ThreadPool::ThreadPool(int num_threads, const char* thd_name)
88
+ : num_threads_(num_threads), thd_name_(thd_name) {
89
+ thread_options_ = Thread::Options();
90
+ thread_options_.set_stack_size(DefaultStackSize());
91
+ SharedThreadPoolConstructor();
92
+ }
93
+
94
+ ThreadPool::ThreadPool(int num_threads, const char* thd_name,
95
+ const Thread::Options& thread_options)
96
+ : num_threads_(num_threads),
97
+ thd_name_(thd_name),
98
+ thread_options_(thread_options) {
99
+ if (thread_options_.stack_size() == 0) {
100
+ thread_options_.set_stack_size(DefaultStackSize());
101
+ }
102
+ SharedThreadPoolConstructor();
103
+ }
104
+
105
+ ThreadPool::~ThreadPool() {
106
+ // For debug checking purpose, using RELAXED order is sufficient.
107
+ shut_down_.Store(true, MemoryOrder::RELAXED);
108
+
109
+ for (int i = 0; i < num_threads_; ++i) {
110
+ queue_->Put(nullptr);
111
+ }
112
+
113
+ for (int i = 0; i < num_threads_; ++i) {
114
+ threads_[i]->Join();
115
+ }
116
+
117
+ for (int i = 0; i < num_threads_; ++i) {
118
+ Delete(threads_[i]);
119
+ }
120
+ gpr_free(threads_);
121
+ Delete(queue_);
122
+ }
123
+
124
+ void ThreadPool::Add(grpc_experimental_completion_queue_functor* closure) {
125
+ AssertHasNotBeenShutDown();
126
+ queue_->Put(static_cast<void*>(closure));
127
+ }
128
+
129
+ int ThreadPool::num_pending_closures() const { return queue_->count(); }
130
+
131
+ int ThreadPool::pool_capacity() const { return num_threads_; }
132
+
133
+ const Thread::Options& ThreadPool::thread_options() const {
134
+ return thread_options_;
135
+ }
136
+
137
+ const char* ThreadPool::thread_name() const { return thd_name_; }
138
+ } // namespace grpc_core
@@ -0,0 +1,153 @@
1
+ /*
2
+ *
3
+ * Copyright 2019 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_IOMGR_EXECUTOR_THREADPOOL_H
20
+ #define GRPC_CORE_LIB_IOMGR_EXECUTOR_THREADPOOL_H
21
+
22
+ #include <grpc/support/port_platform.h>
23
+
24
+ #include <grpc/grpc.h>
25
+
26
+ #include "src/core/lib/gprpp/thd.h"
27
+ #include "src/core/lib/iomgr/executor/mpmcqueue.h"
28
+
29
+ namespace grpc_core {
30
+
31
+ // A base abstract base class for threadpool.
32
+ // Threadpool is an executor that maintains a pool of threads sitting around
33
+ // and waiting for closures. A threadpool also maintains a queue of pending
34
+ // closures, when closures appearing in the queue, the threads in pool will
35
+ // pull them out and execute them.
36
+ class ThreadPoolInterface {
37
+ public:
38
+ // Waits for all pending closures to complete, then shuts down thread pool.
39
+ virtual ~ThreadPoolInterface() {}
40
+
41
+ // Schedules a given closure for execution later.
42
+ // Depending on specific subclass implementation, this routine might cause
43
+ // current thread to be blocked (in case of unable to schedule).
44
+ // Closure should contain a function pointer and arguments it will take, more
45
+ // details for closure struct at /grpc/include/grpc/impl/codegen/grpc_types.h
46
+ virtual void Add(grpc_experimental_completion_queue_functor* closure)
47
+ GRPC_ABSTRACT;
48
+
49
+ // Returns the current number of pending closures
50
+ virtual int num_pending_closures() const GRPC_ABSTRACT;
51
+
52
+ // Returns the capacity of pool (number of worker threads in pool)
53
+ virtual int pool_capacity() const GRPC_ABSTRACT;
54
+
55
+ // Thread option accessor
56
+ virtual const Thread::Options& thread_options() const GRPC_ABSTRACT;
57
+
58
+ // Returns the thread name for threads in this ThreadPool.
59
+ virtual const char* thread_name() const GRPC_ABSTRACT;
60
+
61
+ GRPC_ABSTRACT_BASE_CLASS
62
+ };
63
+
64
+ // Worker thread for threadpool. Executes closures in the queue, until getting a
65
+ // NULL closure.
66
+ class ThreadPoolWorker {
67
+ public:
68
+ ThreadPoolWorker(const char* thd_name, ThreadPoolInterface* pool,
69
+ MPMCQueueInterface* queue, Thread::Options& options,
70
+ int index)
71
+ : queue_(queue), thd_name_(thd_name), index_(index) {
72
+ thd_ = Thread(thd_name,
73
+ [](void* th) { static_cast<ThreadPoolWorker*>(th)->Run(); },
74
+ this, nullptr, options);
75
+ }
76
+
77
+ ~ThreadPoolWorker() {}
78
+
79
+ void Start() { thd_.Start(); }
80
+ void Join() { thd_.Join(); }
81
+
82
+ private:
83
+ // struct for tracking stats of thread
84
+ struct Stats {
85
+ gpr_timespec sleep_time;
86
+ Stats() { sleep_time = gpr_time_0(GPR_TIMESPAN); }
87
+ };
88
+
89
+ void Run(); // Pulls closures from queue and executes them
90
+
91
+ MPMCQueueInterface* queue_; // Queue in thread pool to pull closures from
92
+ Thread thd_; // Thread wrapped in
93
+ Stats stats_; // Stats to be collected in run time
94
+ const char* thd_name_; // Name of thread
95
+ int index_; // Index in thread pool
96
+ };
97
+
98
+ // A fixed size thread pool implementation of abstract thread pool interface.
99
+ // In this implementation, the number of threads in pool is fixed, but the
100
+ // capacity of closure queue is unlimited.
101
+ class ThreadPool : public ThreadPoolInterface {
102
+ public:
103
+ // Creates a thread pool with size of "num_threads", with default thread name
104
+ // "ThreadPoolWorker" and all thread options set to default. If the given size
105
+ // is 0 or less, there will be 1 worker thread created inside pool.
106
+ ThreadPool(int num_threads);
107
+
108
+ // Same as ThreadPool(int num_threads) constructor, except
109
+ // that it also sets "thd_name" as the name of all threads in the thread pool.
110
+ ThreadPool(int num_threads, const char* thd_name);
111
+
112
+ // Same as ThreadPool(const char *thd_name, int num_threads) constructor,
113
+ // except that is also set thread_options for threads.
114
+ // Notes for stack size:
115
+ // If the stack size field of the passed in Thread::Options is set to default
116
+ // value 0, default ThreadPool stack size will be used. The current default
117
+ // stack size of this implementation is 1952K for mobile platform and 64K for
118
+ // all others.
119
+ ThreadPool(int num_threads, const char* thd_name,
120
+ const Thread::Options& thread_options);
121
+
122
+ // Waits for all pending closures to complete, then shuts down thread pool.
123
+ ~ThreadPool() override;
124
+
125
+ // Adds given closure into pending queue immediately. Since closure queue has
126
+ // infinite length, this routine will not block.
127
+ void Add(grpc_experimental_completion_queue_functor* closure) override;
128
+
129
+ int num_pending_closures() const override;
130
+ int pool_capacity() const override;
131
+ const Thread::Options& thread_options() const override;
132
+ const char* thread_name() const override;
133
+
134
+ private:
135
+ int num_threads_ = 0;
136
+ const char* thd_name_ = nullptr;
137
+ Thread::Options thread_options_;
138
+ ThreadPoolWorker** threads_ = nullptr; // Array of worker threads
139
+ MPMCQueueInterface* queue_ = nullptr; // Closure queue
140
+
141
+ Atomic<bool> shut_down_{false}; // Destructor has been called if set to true
142
+
143
+ void SharedThreadPoolConstructor();
144
+ // For ThreadPool, default stack size for mobile platform is 1952K. for other
145
+ // platforms is 64K.
146
+ size_t DefaultStackSize();
147
+ // Internal Use Only for debug checking.
148
+ void AssertHasNotBeenShutDown();
149
+ };
150
+
151
+ } // namespace grpc_core
152
+
153
+ #endif /* GRPC_CORE_LIB_IOMGR_EXECUTOR_THREADPOOL_H */