grpc 1.26.0 → 1.27.0.pre1

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 (209) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +1654 -1519
  3. data/etc/roots.pem +44 -0
  4. data/include/grpc/grpc_security.h +37 -15
  5. data/include/grpc/grpc_security_constants.h +27 -0
  6. data/include/grpc/impl/codegen/grpc_types.h +14 -0
  7. data/include/grpc/impl/codegen/port_platform.h +1 -1
  8. data/src/core/ext/filters/client_channel/client_channel.cc +0 -20
  9. data/src/core/ext/filters/client_channel/http_proxy.cc +4 -4
  10. data/src/core/ext/filters/client_channel/lb_policy.cc +4 -3
  11. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.cc +191 -201
  12. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_balancer_addresses.cc +89 -0
  13. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_balancer_addresses.h +40 -0
  14. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_channel_secure.cc +3 -2
  15. data/src/core/ext/filters/client_channel/lb_policy/grpclb/load_balancer_api.cc +88 -121
  16. data/src/core/ext/filters/client_channel/lb_policy/grpclb/load_balancer_api.h +28 -57
  17. data/src/core/ext/filters/client_channel/lb_policy/subchannel_list.h +0 -7
  18. data/src/core/ext/filters/client_channel/lb_policy/xds/cds.cc +8 -9
  19. data/src/core/ext/filters/client_channel/lb_policy/xds/xds.cc +53 -34
  20. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/dns_resolver_ares.cc +18 -5
  21. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.cc +24 -19
  22. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.h +2 -1
  23. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper_fallback.cc +4 -2
  24. data/src/core/ext/filters/client_channel/server_address.cc +6 -9
  25. data/src/core/ext/filters/client_channel/server_address.h +3 -10
  26. data/src/core/ext/filters/client_channel/xds/xds_api.cc +394 -150
  27. data/src/core/ext/filters/client_channel/xds/xds_api.h +75 -35
  28. data/src/core/ext/filters/client_channel/xds/xds_bootstrap.cc +59 -22
  29. data/src/core/ext/filters/client_channel/xds/xds_bootstrap.h +13 -9
  30. data/src/core/ext/filters/client_channel/xds/xds_channel_secure.cc +8 -6
  31. data/src/core/ext/filters/client_channel/xds/xds_client.cc +456 -175
  32. data/src/core/ext/filters/client_channel/xds/xds_client.h +33 -21
  33. data/src/core/ext/filters/client_channel/xds/xds_client_stats.cc +5 -8
  34. data/src/core/ext/filters/client_channel/xds/xds_client_stats.h +18 -24
  35. data/src/core/ext/transport/chttp2/transport/chttp2_transport.cc +2 -2
  36. data/src/core/ext/upb-generated/src/proto/grpc/lb/v1/load_balancer.upb.c +13 -5
  37. data/src/core/ext/upb-generated/src/proto/grpc/lb/v1/load_balancer.upb.h +34 -0
  38. data/src/core/lib/channel/channelz.h +11 -1
  39. data/src/core/lib/gpr/time_precise.cc +1 -1
  40. data/src/core/lib/gprpp/optional.h +26 -0
  41. data/src/core/lib/gprpp/string_view.h +14 -10
  42. data/src/core/lib/iomgr/executor.cc +1 -1
  43. data/src/core/lib/iomgr/fork_posix.cc +4 -0
  44. data/src/core/lib/iomgr/poller/eventmanager_libuv.cc +87 -0
  45. data/src/core/lib/iomgr/poller/eventmanager_libuv.h +88 -0
  46. data/src/core/lib/iomgr/socket_utils_common_posix.cc +14 -0
  47. data/src/core/lib/iomgr/socket_utils_posix.h +12 -0
  48. data/src/core/lib/iomgr/tcp_custom.h +3 -0
  49. data/src/core/lib/iomgr/tcp_posix.cc +607 -56
  50. data/src/core/lib/iomgr/tcp_server_custom.cc +15 -2
  51. data/src/core/lib/iomgr/tcp_server_utils_posix_common.cc +8 -0
  52. data/src/core/lib/json/json.h +11 -1
  53. data/src/core/lib/json/json_reader.cc +206 -28
  54. data/src/core/lib/json/json_writer.cc +111 -24
  55. data/src/core/lib/security/credentials/composite/composite_credentials.cc +7 -0
  56. data/src/core/lib/security/credentials/composite/composite_credentials.h +5 -1
  57. data/src/core/lib/security/credentials/credentials.h +10 -1
  58. data/src/core/lib/security/credentials/fake/fake_credentials.h +2 -1
  59. data/src/core/lib/security/credentials/oauth2/oauth2_credentials.cc +1 -1
  60. data/src/core/lib/security/credentials/plugin/plugin_credentials.cc +6 -4
  61. data/src/core/lib/security/credentials/plugin/plugin_credentials.h +2 -1
  62. data/src/core/lib/security/credentials/tls/grpc_tls_credentials_options.cc +20 -0
  63. data/src/core/lib/security/credentials/tls/grpc_tls_credentials_options.h +8 -0
  64. data/src/core/lib/security/credentials/tls/{spiffe_credentials.cc → tls_credentials.cc} +23 -24
  65. data/src/core/lib/security/credentials/tls/{spiffe_credentials.h → tls_credentials.h} +9 -9
  66. data/src/core/lib/security/security_connector/alts/alts_security_connector.cc +13 -0
  67. data/src/core/lib/security/security_connector/fake/fake_security_connector.cc +22 -2
  68. data/src/core/lib/security/security_connector/load_system_roots_fallback.cc +2 -2
  69. data/src/core/lib/security/security_connector/load_system_roots_linux.cc +2 -2
  70. data/src/core/lib/security/security_connector/local/local_security_connector.cc +30 -3
  71. data/src/core/lib/security/security_connector/ssl_utils.cc +45 -3
  72. data/src/core/lib/security/security_connector/ssl_utils.h +12 -0
  73. data/src/core/lib/security/security_connector/tls/{spiffe_security_connector.cc → tls_security_connector.cc} +82 -69
  74. data/src/core/lib/security/security_connector/tls/{spiffe_security_connector.h → tls_security_connector.h} +17 -18
  75. data/src/core/lib/security/transport/client_auth_filter.cc +33 -0
  76. data/src/core/lib/surface/completion_queue.cc +22 -1
  77. data/src/core/lib/surface/version.cc +1 -1
  78. data/src/core/tsi/alts/handshaker/alts_tsi_handshaker.cc +11 -1
  79. data/src/core/tsi/alts/handshaker/alts_tsi_handshaker.h +1 -1
  80. data/src/core/tsi/alts/zero_copy_frame_protector/alts_zero_copy_grpc_protector.cc +3 -3
  81. data/src/core/tsi/fake_transport_security.cc +7 -3
  82. data/src/core/tsi/fake_transport_security.h +2 -0
  83. data/src/core/tsi/ssl_transport_security.cc +144 -8
  84. data/src/core/tsi/ssl_transport_security.h +15 -1
  85. data/src/core/tsi/transport_security.cc +13 -0
  86. data/src/core/tsi/transport_security_grpc.cc +2 -2
  87. data/src/core/tsi/transport_security_grpc.h +2 -2
  88. data/src/core/tsi/transport_security_interface.h +12 -0
  89. data/src/ruby/bin/math_pb.rb +5 -5
  90. data/src/ruby/ext/grpc/rb_call_credentials.c +4 -1
  91. data/src/ruby/ext/grpc/rb_grpc_imports.generated.c +2 -0
  92. data/src/ruby/ext/grpc/rb_grpc_imports.generated.h +4 -1
  93. data/src/ruby/lib/grpc/version.rb +1 -1
  94. data/src/ruby/pb/grpc/health/v1/health_pb.rb +3 -3
  95. data/src/ruby/pb/src/proto/grpc/testing/empty_pb.rb +1 -1
  96. data/src/ruby/pb/src/proto/grpc/testing/messages_pb.rb +23 -13
  97. data/third_party/abseil-cpp/absl/algorithm/algorithm.h +159 -0
  98. data/third_party/abseil-cpp/absl/base/attributes.h +609 -0
  99. data/third_party/abseil-cpp/absl/base/call_once.h +226 -0
  100. data/third_party/abseil-cpp/absl/base/casts.h +184 -0
  101. data/third_party/abseil-cpp/absl/base/config.h +622 -0
  102. data/third_party/abseil-cpp/absl/base/const_init.h +76 -0
  103. data/third_party/abseil-cpp/absl/base/dynamic_annotations.cc +129 -0
  104. data/third_party/abseil-cpp/absl/base/dynamic_annotations.h +389 -0
  105. data/third_party/abseil-cpp/absl/base/internal/atomic_hook.h +179 -0
  106. data/third_party/abseil-cpp/absl/base/internal/bits.h +218 -0
  107. data/third_party/abseil-cpp/absl/base/internal/cycleclock.cc +107 -0
  108. data/third_party/abseil-cpp/absl/base/internal/cycleclock.h +94 -0
  109. data/third_party/abseil-cpp/absl/base/internal/endian.h +266 -0
  110. data/third_party/abseil-cpp/absl/base/internal/hide_ptr.h +51 -0
  111. data/third_party/abseil-cpp/absl/base/internal/identity.h +37 -0
  112. data/third_party/abseil-cpp/absl/base/internal/inline_variable.h +107 -0
  113. data/third_party/abseil-cpp/absl/base/internal/invoke.h +187 -0
  114. data/third_party/abseil-cpp/absl/base/internal/low_level_scheduling.h +107 -0
  115. data/third_party/abseil-cpp/absl/base/internal/per_thread_tls.h +52 -0
  116. data/third_party/abseil-cpp/absl/base/internal/raw_logging.cc +237 -0
  117. data/third_party/abseil-cpp/absl/base/internal/raw_logging.h +179 -0
  118. data/third_party/abseil-cpp/absl/base/internal/scheduling_mode.h +58 -0
  119. data/third_party/abseil-cpp/absl/base/internal/spinlock.cc +233 -0
  120. data/third_party/abseil-cpp/absl/base/internal/spinlock.h +243 -0
  121. data/third_party/abseil-cpp/absl/base/internal/spinlock_akaros.inc +35 -0
  122. data/third_party/abseil-cpp/absl/base/internal/spinlock_linux.inc +67 -0
  123. data/third_party/abseil-cpp/absl/base/internal/spinlock_posix.inc +46 -0
  124. data/third_party/abseil-cpp/absl/base/internal/spinlock_wait.cc +81 -0
  125. data/third_party/abseil-cpp/absl/base/internal/spinlock_wait.h +93 -0
  126. data/third_party/abseil-cpp/absl/base/internal/spinlock_win32.inc +37 -0
  127. data/third_party/abseil-cpp/absl/base/internal/sysinfo.cc +414 -0
  128. data/third_party/abseil-cpp/absl/base/internal/sysinfo.h +66 -0
  129. data/third_party/abseil-cpp/absl/base/internal/thread_annotations.h +271 -0
  130. data/third_party/abseil-cpp/absl/base/internal/thread_identity.cc +140 -0
  131. data/third_party/abseil-cpp/absl/base/internal/thread_identity.h +250 -0
  132. data/third_party/abseil-cpp/absl/base/internal/throw_delegate.cc +108 -0
  133. data/third_party/abseil-cpp/absl/base/internal/throw_delegate.h +75 -0
  134. data/third_party/abseil-cpp/absl/base/internal/tsan_mutex_interface.h +66 -0
  135. data/third_party/abseil-cpp/absl/base/internal/unaligned_access.h +158 -0
  136. data/third_party/abseil-cpp/absl/base/internal/unscaledcycleclock.cc +103 -0
  137. data/third_party/abseil-cpp/absl/base/internal/unscaledcycleclock.h +124 -0
  138. data/third_party/abseil-cpp/absl/base/log_severity.cc +27 -0
  139. data/third_party/abseil-cpp/absl/base/log_severity.h +121 -0
  140. data/third_party/abseil-cpp/absl/base/macros.h +220 -0
  141. data/third_party/abseil-cpp/absl/base/optimization.h +181 -0
  142. data/third_party/abseil-cpp/absl/base/options.h +214 -0
  143. data/third_party/abseil-cpp/absl/base/policy_checks.h +111 -0
  144. data/third_party/abseil-cpp/absl/base/port.h +26 -0
  145. data/third_party/abseil-cpp/absl/base/thread_annotations.h +280 -0
  146. data/third_party/abseil-cpp/absl/container/inlined_vector.h +848 -0
  147. data/third_party/abseil-cpp/absl/container/internal/compressed_tuple.h +265 -0
  148. data/third_party/abseil-cpp/absl/container/internal/inlined_vector.h +892 -0
  149. data/third_party/abseil-cpp/absl/memory/memory.h +695 -0
  150. data/third_party/abseil-cpp/absl/meta/type_traits.h +759 -0
  151. data/third_party/abseil-cpp/absl/numeric/int128.cc +404 -0
  152. data/third_party/abseil-cpp/absl/numeric/int128.h +1091 -0
  153. data/third_party/abseil-cpp/absl/numeric/int128_have_intrinsic.inc +302 -0
  154. data/third_party/abseil-cpp/absl/numeric/int128_no_intrinsic.inc +308 -0
  155. data/third_party/abseil-cpp/absl/strings/ascii.cc +200 -0
  156. data/third_party/abseil-cpp/absl/strings/ascii.h +241 -0
  157. data/third_party/abseil-cpp/absl/strings/charconv.cc +985 -0
  158. data/third_party/abseil-cpp/absl/strings/charconv.h +119 -0
  159. data/third_party/abseil-cpp/absl/strings/escaping.cc +949 -0
  160. data/third_party/abseil-cpp/absl/strings/escaping.h +164 -0
  161. data/third_party/abseil-cpp/absl/strings/internal/char_map.h +156 -0
  162. data/third_party/abseil-cpp/absl/strings/internal/charconv_bigint.cc +359 -0
  163. data/third_party/abseil-cpp/absl/strings/internal/charconv_bigint.h +421 -0
  164. data/third_party/abseil-cpp/absl/strings/internal/charconv_parse.cc +504 -0
  165. data/third_party/abseil-cpp/absl/strings/internal/charconv_parse.h +99 -0
  166. data/third_party/abseil-cpp/absl/strings/internal/escaping.cc +180 -0
  167. data/third_party/abseil-cpp/absl/strings/internal/escaping.h +58 -0
  168. data/third_party/abseil-cpp/absl/strings/internal/memutil.cc +112 -0
  169. data/third_party/abseil-cpp/absl/strings/internal/memutil.h +148 -0
  170. data/third_party/abseil-cpp/absl/strings/internal/ostringstream.cc +36 -0
  171. data/third_party/abseil-cpp/absl/strings/internal/ostringstream.h +89 -0
  172. data/third_party/abseil-cpp/absl/strings/internal/resize_uninitialized.h +73 -0
  173. data/third_party/abseil-cpp/absl/strings/internal/stl_type_traits.h +248 -0
  174. data/third_party/abseil-cpp/absl/strings/internal/str_join_internal.h +314 -0
  175. data/third_party/abseil-cpp/absl/strings/internal/str_split_internal.h +455 -0
  176. data/third_party/abseil-cpp/absl/strings/internal/utf8.cc +53 -0
  177. data/third_party/abseil-cpp/absl/strings/internal/utf8.h +50 -0
  178. data/third_party/abseil-cpp/absl/strings/match.cc +40 -0
  179. data/third_party/abseil-cpp/absl/strings/match.h +90 -0
  180. data/third_party/abseil-cpp/absl/strings/numbers.cc +916 -0
  181. data/third_party/abseil-cpp/absl/strings/numbers.h +263 -0
  182. data/third_party/abseil-cpp/absl/strings/str_cat.cc +246 -0
  183. data/third_party/abseil-cpp/absl/strings/str_cat.h +408 -0
  184. data/third_party/abseil-cpp/absl/strings/str_join.h +293 -0
  185. data/third_party/abseil-cpp/absl/strings/str_replace.cc +82 -0
  186. data/third_party/abseil-cpp/absl/strings/str_replace.h +219 -0
  187. data/third_party/abseil-cpp/absl/strings/str_split.cc +139 -0
  188. data/third_party/abseil-cpp/absl/strings/str_split.h +513 -0
  189. data/third_party/abseil-cpp/absl/strings/string_view.cc +235 -0
  190. data/third_party/abseil-cpp/absl/strings/string_view.h +615 -0
  191. data/third_party/abseil-cpp/absl/strings/strip.h +91 -0
  192. data/third_party/abseil-cpp/absl/strings/substitute.cc +171 -0
  193. data/third_party/abseil-cpp/absl/strings/substitute.h +693 -0
  194. data/third_party/abseil-cpp/absl/types/bad_optional_access.cc +48 -0
  195. data/third_party/abseil-cpp/absl/types/bad_optional_access.h +78 -0
  196. data/third_party/abseil-cpp/absl/types/internal/optional.h +396 -0
  197. data/third_party/abseil-cpp/absl/types/internal/span.h +128 -0
  198. data/third_party/abseil-cpp/absl/types/optional.h +776 -0
  199. data/third_party/abseil-cpp/absl/types/span.h +713 -0
  200. data/third_party/abseil-cpp/absl/utility/utility.h +350 -0
  201. data/third_party/upb/upb/decode.c +4 -0
  202. data/third_party/upb/upb/port.c +0 -1
  203. data/third_party/upb/upb/port_def.inc +1 -3
  204. data/third_party/upb/upb/table.c +2 -1
  205. metadata +147 -43
  206. data/src/core/lib/json/json_common.h +0 -34
  207. data/src/core/lib/json/json_reader.h +0 -146
  208. data/src/core/lib/json/json_string.cc +0 -367
  209. data/src/core/lib/json/json_writer.h +0 -84
@@ -0,0 +1,35 @@
1
+ // Copyright 2017 The Abseil Authors.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // https://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ //
15
+ // This file is an Akaros-specific part of spinlock_wait.cc
16
+
17
+ #include <atomic>
18
+
19
+ #include "absl/base/internal/scheduling_mode.h"
20
+
21
+ extern "C" {
22
+
23
+ ABSL_ATTRIBUTE_WEAK void AbslInternalSpinLockDelay(
24
+ std::atomic<uint32_t>* /* lock_word */, uint32_t /* value */,
25
+ int /* loop */, absl::base_internal::SchedulingMode /* mode */) {
26
+ // In Akaros, one must take care not to call anything that could cause a
27
+ // malloc(), a blocking system call, or a uthread_yield() while holding a
28
+ // spinlock. Our callers assume will not call into libraries or other
29
+ // arbitrary code.
30
+ }
31
+
32
+ ABSL_ATTRIBUTE_WEAK void AbslInternalSpinLockWake(
33
+ std::atomic<uint32_t>* /* lock_word */, bool /* all */) {}
34
+
35
+ } // extern "C"
@@ -0,0 +1,67 @@
1
+ // Copyright 2018 The Abseil Authors.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // https://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ //
15
+ // This file is a Linux-specific part of spinlock_wait.cc
16
+
17
+ #include <linux/futex.h>
18
+ #include <sys/syscall.h>
19
+ #include <unistd.h>
20
+
21
+ #include <atomic>
22
+ #include <cerrno>
23
+ #include <climits>
24
+ #include <cstdint>
25
+ #include <ctime>
26
+
27
+ #include "absl/base/attributes.h"
28
+
29
+ // The SpinLock lockword is `std::atomic<uint32_t>`. Here we assert that
30
+ // `std::atomic<uint32_t>` is bitwise equivalent of the `int` expected
31
+ // by SYS_futex. We also assume that reads/writes done to the lockword
32
+ // by SYS_futex have rational semantics with regard to the
33
+ // std::atomic<> API. C++ provides no guarantees of these assumptions,
34
+ // but they are believed to hold in practice.
35
+ static_assert(sizeof(std::atomic<uint32_t>) == sizeof(int),
36
+ "SpinLock lockword has the wrong size for a futex");
37
+
38
+ // Some Android headers are missing these definitions even though they
39
+ // support these futex operations.
40
+ #ifdef __BIONIC__
41
+ #ifndef SYS_futex
42
+ #define SYS_futex __NR_futex
43
+ #endif
44
+ #ifndef FUTEX_PRIVATE_FLAG
45
+ #define FUTEX_PRIVATE_FLAG 128
46
+ #endif
47
+ #endif
48
+
49
+ extern "C" {
50
+
51
+ ABSL_ATTRIBUTE_WEAK void AbslInternalSpinLockDelay(
52
+ std::atomic<uint32_t> *w, uint32_t value, int loop,
53
+ absl::base_internal::SchedulingMode) {
54
+ int save_errno = errno;
55
+ struct timespec tm;
56
+ tm.tv_sec = 0;
57
+ tm.tv_nsec = absl::base_internal::SpinLockSuggestedDelayNS(loop);
58
+ syscall(SYS_futex, w, FUTEX_WAIT | FUTEX_PRIVATE_FLAG, value, &tm);
59
+ errno = save_errno;
60
+ }
61
+
62
+ ABSL_ATTRIBUTE_WEAK void AbslInternalSpinLockWake(std::atomic<uint32_t> *w,
63
+ bool all) {
64
+ syscall(SYS_futex, w, FUTEX_WAKE | FUTEX_PRIVATE_FLAG, all ? INT_MAX : 1, 0);
65
+ }
66
+
67
+ } // extern "C"
@@ -0,0 +1,46 @@
1
+ // Copyright 2017 The Abseil Authors.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // https://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ //
15
+ // This file is a Posix-specific part of spinlock_wait.cc
16
+
17
+ #include <sched.h>
18
+ #include <atomic>
19
+ #include <ctime>
20
+ #include <cerrno>
21
+
22
+ #include "absl/base/internal/scheduling_mode.h"
23
+ #include "absl/base/port.h"
24
+
25
+ extern "C" {
26
+
27
+ ABSL_ATTRIBUTE_WEAK void AbslInternalSpinLockDelay(
28
+ std::atomic<uint32_t>* /* lock_word */, uint32_t /* value */, int loop,
29
+ absl::base_internal::SchedulingMode /* mode */) {
30
+ int save_errno = errno;
31
+ if (loop == 0) {
32
+ } else if (loop == 1) {
33
+ sched_yield();
34
+ } else {
35
+ struct timespec tm;
36
+ tm.tv_sec = 0;
37
+ tm.tv_nsec = absl::base_internal::SpinLockSuggestedDelayNS(loop);
38
+ nanosleep(&tm, nullptr);
39
+ }
40
+ errno = save_errno;
41
+ }
42
+
43
+ ABSL_ATTRIBUTE_WEAK void AbslInternalSpinLockWake(
44
+ std::atomic<uint32_t>* /* lock_word */, bool /* all */) {}
45
+
46
+ } // extern "C"
@@ -0,0 +1,81 @@
1
+ // Copyright 2017 The Abseil Authors.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // https://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+
15
+ // The OS-specific header included below must provide two calls:
16
+ // AbslInternalSpinLockDelay() and AbslInternalSpinLockWake().
17
+ // See spinlock_wait.h for the specs.
18
+
19
+ #include <atomic>
20
+ #include <cstdint>
21
+
22
+ #include "absl/base/internal/spinlock_wait.h"
23
+
24
+ #if defined(_WIN32)
25
+ #include "absl/base/internal/spinlock_win32.inc"
26
+ #elif defined(__linux__)
27
+ #include "absl/base/internal/spinlock_linux.inc"
28
+ #elif defined(__akaros__)
29
+ #include "absl/base/internal/spinlock_akaros.inc"
30
+ #else
31
+ #include "absl/base/internal/spinlock_posix.inc"
32
+ #endif
33
+
34
+ namespace absl {
35
+ ABSL_NAMESPACE_BEGIN
36
+ namespace base_internal {
37
+
38
+ // See spinlock_wait.h for spec.
39
+ uint32_t SpinLockWait(std::atomic<uint32_t> *w, int n,
40
+ const SpinLockWaitTransition trans[],
41
+ base_internal::SchedulingMode scheduling_mode) {
42
+ int loop = 0;
43
+ for (;;) {
44
+ uint32_t v = w->load(std::memory_order_acquire);
45
+ int i;
46
+ for (i = 0; i != n && v != trans[i].from; i++) {
47
+ }
48
+ if (i == n) {
49
+ SpinLockDelay(w, v, ++loop, scheduling_mode); // no matching transition
50
+ } else if (trans[i].to == v || // null transition
51
+ w->compare_exchange_strong(v, trans[i].to,
52
+ std::memory_order_acquire,
53
+ std::memory_order_relaxed)) {
54
+ if (trans[i].done) return v;
55
+ }
56
+ }
57
+ }
58
+
59
+ static std::atomic<uint64_t> delay_rand;
60
+
61
+ // Return a suggested delay in nanoseconds for iteration number "loop"
62
+ int SpinLockSuggestedDelayNS(int loop) {
63
+ // Weak pseudo-random number generator to get some spread between threads
64
+ // when many are spinning.
65
+ uint64_t r = delay_rand.load(std::memory_order_relaxed);
66
+ r = 0x5deece66dLL * r + 0xb; // numbers from nrand48()
67
+ delay_rand.store(r, std::memory_order_relaxed);
68
+
69
+ if (loop < 0 || loop > 32) { // limit loop to 0..32
70
+ loop = 32;
71
+ }
72
+ const int kMinDelay = 128 << 10; // 128us
73
+ // Double delay every 8 iterations, up to 16x (2ms).
74
+ int delay = kMinDelay << (loop / 8);
75
+ // Randomize in delay..2*delay range, for resulting 128us..4ms range.
76
+ return delay | ((delay - 1) & static_cast<int>(r));
77
+ }
78
+
79
+ } // namespace base_internal
80
+ ABSL_NAMESPACE_END
81
+ } // namespace absl
@@ -0,0 +1,93 @@
1
+ // Copyright 2017 The Abseil Authors.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // https://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+
15
+ #ifndef ABSL_BASE_INTERNAL_SPINLOCK_WAIT_H_
16
+ #define ABSL_BASE_INTERNAL_SPINLOCK_WAIT_H_
17
+
18
+ // Operations to make atomic transitions on a word, and to allow
19
+ // waiting for those transitions to become possible.
20
+
21
+ #include <stdint.h>
22
+ #include <atomic>
23
+
24
+ #include "absl/base/internal/scheduling_mode.h"
25
+
26
+ namespace absl {
27
+ ABSL_NAMESPACE_BEGIN
28
+ namespace base_internal {
29
+
30
+ // SpinLockWait() waits until it can perform one of several transitions from
31
+ // "from" to "to". It returns when it performs a transition where done==true.
32
+ struct SpinLockWaitTransition {
33
+ uint32_t from;
34
+ uint32_t to;
35
+ bool done;
36
+ };
37
+
38
+ // Wait until *w can transition from trans[i].from to trans[i].to for some i
39
+ // satisfying 0<=i<n && trans[i].done, atomically make the transition,
40
+ // then return the old value of *w. Make any other atomic transitions
41
+ // where !trans[i].done, but continue waiting.
42
+ uint32_t SpinLockWait(std::atomic<uint32_t> *w, int n,
43
+ const SpinLockWaitTransition trans[],
44
+ SchedulingMode scheduling_mode);
45
+
46
+ // If possible, wake some thread that has called SpinLockDelay(w, ...). If
47
+ // "all" is true, wake all such threads. This call is a hint, and on some
48
+ // systems it may be a no-op; threads calling SpinLockDelay() will always wake
49
+ // eventually even if SpinLockWake() is never called.
50
+ void SpinLockWake(std::atomic<uint32_t> *w, bool all);
51
+
52
+ // Wait for an appropriate spin delay on iteration "loop" of a
53
+ // spin loop on location *w, whose previously observed value was "value".
54
+ // SpinLockDelay() may do nothing, may yield the CPU, may sleep a clock tick,
55
+ // or may wait for a delay that can be truncated by a call to SpinLockWake(w).
56
+ // In all cases, it must return in bounded time even if SpinLockWake() is not
57
+ // called.
58
+ void SpinLockDelay(std::atomic<uint32_t> *w, uint32_t value, int loop,
59
+ base_internal::SchedulingMode scheduling_mode);
60
+
61
+ // Helper used by AbslInternalSpinLockDelay.
62
+ // Returns a suggested delay in nanoseconds for iteration number "loop".
63
+ int SpinLockSuggestedDelayNS(int loop);
64
+
65
+ } // namespace base_internal
66
+ ABSL_NAMESPACE_END
67
+ } // namespace absl
68
+
69
+ // In some build configurations we pass --detect-odr-violations to the
70
+ // gold linker. This causes it to flag weak symbol overrides as ODR
71
+ // violations. Because ODR only applies to C++ and not C,
72
+ // --detect-odr-violations ignores symbols not mangled with C++ names.
73
+ // By changing our extension points to be extern "C", we dodge this
74
+ // check.
75
+ extern "C" {
76
+ void AbslInternalSpinLockWake(std::atomic<uint32_t> *w, bool all);
77
+ void AbslInternalSpinLockDelay(
78
+ std::atomic<uint32_t> *w, uint32_t value, int loop,
79
+ absl::base_internal::SchedulingMode scheduling_mode);
80
+ }
81
+
82
+ inline void absl::base_internal::SpinLockWake(std::atomic<uint32_t> *w,
83
+ bool all) {
84
+ AbslInternalSpinLockWake(w, all);
85
+ }
86
+
87
+ inline void absl::base_internal::SpinLockDelay(
88
+ std::atomic<uint32_t> *w, uint32_t value, int loop,
89
+ absl::base_internal::SchedulingMode scheduling_mode) {
90
+ AbslInternalSpinLockDelay(w, value, loop, scheduling_mode);
91
+ }
92
+
93
+ #endif // ABSL_BASE_INTERNAL_SPINLOCK_WAIT_H_
@@ -0,0 +1,37 @@
1
+ // Copyright 2017 The Abseil Authors.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // https://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ //
15
+ // This file is a Win32-specific part of spinlock_wait.cc
16
+
17
+ #include <windows.h>
18
+ #include <atomic>
19
+ #include "absl/base/internal/scheduling_mode.h"
20
+
21
+ extern "C" {
22
+
23
+ void AbslInternalSpinLockDelay(std::atomic<uint32_t>* /* lock_word */,
24
+ uint32_t /* value */, int loop,
25
+ absl::base_internal::SchedulingMode /* mode */) {
26
+ if (loop == 0) {
27
+ } else if (loop == 1) {
28
+ Sleep(0);
29
+ } else {
30
+ Sleep(absl::base_internal::SpinLockSuggestedDelayNS(loop) / 1000000);
31
+ }
32
+ }
33
+
34
+ void AbslInternalSpinLockWake(std::atomic<uint32_t>* /* lock_word */,
35
+ bool /* all */) {}
36
+
37
+ } // extern "C"
@@ -0,0 +1,414 @@
1
+ // Copyright 2017 The Abseil Authors.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // https://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+
15
+ #include "absl/base/internal/sysinfo.h"
16
+
17
+ #include "absl/base/attributes.h"
18
+
19
+ #ifdef _WIN32
20
+ #include <windows.h>
21
+ #else
22
+ #include <fcntl.h>
23
+ #include <pthread.h>
24
+ #include <sys/stat.h>
25
+ #include <sys/types.h>
26
+ #include <unistd.h>
27
+ #endif
28
+
29
+ #ifdef __linux__
30
+ #include <sys/syscall.h>
31
+ #endif
32
+
33
+ #if defined(__APPLE__) || defined(__FreeBSD__)
34
+ #include <sys/sysctl.h>
35
+ #endif
36
+
37
+ #if defined(__myriad2__)
38
+ #include <rtems.h>
39
+ #endif
40
+
41
+ #include <string.h>
42
+ #include <cassert>
43
+ #include <cstdint>
44
+ #include <cstdio>
45
+ #include <cstdlib>
46
+ #include <ctime>
47
+ #include <limits>
48
+ #include <thread> // NOLINT(build/c++11)
49
+ #include <utility>
50
+ #include <vector>
51
+
52
+ #include "absl/base/call_once.h"
53
+ #include "absl/base/internal/raw_logging.h"
54
+ #include "absl/base/internal/spinlock.h"
55
+ #include "absl/base/internal/unscaledcycleclock.h"
56
+
57
+ namespace absl {
58
+ ABSL_NAMESPACE_BEGIN
59
+ namespace base_internal {
60
+
61
+ static once_flag init_system_info_once;
62
+ static int num_cpus = 0;
63
+ static double nominal_cpu_frequency = 1.0; // 0.0 might be dangerous.
64
+
65
+ static int GetNumCPUs() {
66
+ #if defined(__myriad2__)
67
+ return 1;
68
+ #else
69
+ // Other possibilities:
70
+ // - Read /sys/devices/system/cpu/online and use cpumask_parse()
71
+ // - sysconf(_SC_NPROCESSORS_ONLN)
72
+ return std::thread::hardware_concurrency();
73
+ #endif
74
+ }
75
+
76
+ #if defined(_WIN32)
77
+
78
+ static double GetNominalCPUFrequency() {
79
+ #pragma comment(lib, "advapi32.lib") // For Reg* functions.
80
+ HKEY key;
81
+ // Use the Reg* functions rather than the SH functions because shlwapi.dll
82
+ // pulls in gdi32.dll which makes process destruction much more costly.
83
+ if (RegOpenKeyExA(HKEY_LOCAL_MACHINE,
84
+ "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", 0,
85
+ KEY_READ, &key) == ERROR_SUCCESS) {
86
+ DWORD type = 0;
87
+ DWORD data = 0;
88
+ DWORD data_size = sizeof(data);
89
+ auto result = RegQueryValueExA(key, "~MHz", 0, &type,
90
+ reinterpret_cast<LPBYTE>(&data), &data_size);
91
+ RegCloseKey(key);
92
+ if (result == ERROR_SUCCESS && type == REG_DWORD &&
93
+ data_size == sizeof(data)) {
94
+ return data * 1e6; // Value is MHz.
95
+ }
96
+ }
97
+ return 1.0;
98
+ }
99
+
100
+ #elif defined(CTL_HW) && defined(HW_CPU_FREQ)
101
+
102
+ static double GetNominalCPUFrequency() {
103
+ unsigned freq;
104
+ size_t size = sizeof(freq);
105
+ int mib[2] = {CTL_HW, HW_CPU_FREQ};
106
+ if (sysctl(mib, 2, &freq, &size, nullptr, 0) == 0) {
107
+ return static_cast<double>(freq);
108
+ }
109
+ return 1.0;
110
+ }
111
+
112
+ #else
113
+
114
+ // Helper function for reading a long from a file. Returns true if successful
115
+ // and the memory location pointed to by value is set to the value read.
116
+ static bool ReadLongFromFile(const char *file, long *value) {
117
+ bool ret = false;
118
+ int fd = open(file, O_RDONLY);
119
+ if (fd != -1) {
120
+ char line[1024];
121
+ char *err;
122
+ memset(line, '\0', sizeof(line));
123
+ int len = read(fd, line, sizeof(line) - 1);
124
+ if (len <= 0) {
125
+ ret = false;
126
+ } else {
127
+ const long temp_value = strtol(line, &err, 10);
128
+ if (line[0] != '\0' && (*err == '\n' || *err == '\0')) {
129
+ *value = temp_value;
130
+ ret = true;
131
+ }
132
+ }
133
+ close(fd);
134
+ }
135
+ return ret;
136
+ }
137
+
138
+ #if defined(ABSL_INTERNAL_UNSCALED_CYCLECLOCK_FREQUENCY_IS_CPU_FREQUENCY)
139
+
140
+ // Reads a monotonic time source and returns a value in
141
+ // nanoseconds. The returned value uses an arbitrary epoch, not the
142
+ // Unix epoch.
143
+ static int64_t ReadMonotonicClockNanos() {
144
+ struct timespec t;
145
+ #ifdef CLOCK_MONOTONIC_RAW
146
+ int rc = clock_gettime(CLOCK_MONOTONIC_RAW, &t);
147
+ #else
148
+ int rc = clock_gettime(CLOCK_MONOTONIC, &t);
149
+ #endif
150
+ if (rc != 0) {
151
+ perror("clock_gettime() failed");
152
+ abort();
153
+ }
154
+ return int64_t{t.tv_sec} * 1000000000 + t.tv_nsec;
155
+ }
156
+
157
+ class UnscaledCycleClockWrapperForInitializeFrequency {
158
+ public:
159
+ static int64_t Now() { return base_internal::UnscaledCycleClock::Now(); }
160
+ };
161
+
162
+ struct TimeTscPair {
163
+ int64_t time; // From ReadMonotonicClockNanos().
164
+ int64_t tsc; // From UnscaledCycleClock::Now().
165
+ };
166
+
167
+ // Returns a pair of values (monotonic kernel time, TSC ticks) that
168
+ // approximately correspond to each other. This is accomplished by
169
+ // doing several reads and picking the reading with the lowest
170
+ // latency. This approach is used to minimize the probability that
171
+ // our thread was preempted between clock reads.
172
+ static TimeTscPair GetTimeTscPair() {
173
+ int64_t best_latency = std::numeric_limits<int64_t>::max();
174
+ TimeTscPair best;
175
+ for (int i = 0; i < 10; ++i) {
176
+ int64_t t0 = ReadMonotonicClockNanos();
177
+ int64_t tsc = UnscaledCycleClockWrapperForInitializeFrequency::Now();
178
+ int64_t t1 = ReadMonotonicClockNanos();
179
+ int64_t latency = t1 - t0;
180
+ if (latency < best_latency) {
181
+ best_latency = latency;
182
+ best.time = t0;
183
+ best.tsc = tsc;
184
+ }
185
+ }
186
+ return best;
187
+ }
188
+
189
+ // Measures and returns the TSC frequency by taking a pair of
190
+ // measurements approximately `sleep_nanoseconds` apart.
191
+ static double MeasureTscFrequencyWithSleep(int sleep_nanoseconds) {
192
+ auto t0 = GetTimeTscPair();
193
+ struct timespec ts;
194
+ ts.tv_sec = 0;
195
+ ts.tv_nsec = sleep_nanoseconds;
196
+ while (nanosleep(&ts, &ts) != 0 && errno == EINTR) {}
197
+ auto t1 = GetTimeTscPair();
198
+ double elapsed_ticks = t1.tsc - t0.tsc;
199
+ double elapsed_time = (t1.time - t0.time) * 1e-9;
200
+ return elapsed_ticks / elapsed_time;
201
+ }
202
+
203
+ // Measures and returns the TSC frequency by calling
204
+ // MeasureTscFrequencyWithSleep(), doubling the sleep interval until the
205
+ // frequency measurement stabilizes.
206
+ static double MeasureTscFrequency() {
207
+ double last_measurement = -1.0;
208
+ int sleep_nanoseconds = 1000000; // 1 millisecond.
209
+ for (int i = 0; i < 8; ++i) {
210
+ double measurement = MeasureTscFrequencyWithSleep(sleep_nanoseconds);
211
+ if (measurement * 0.99 < last_measurement &&
212
+ last_measurement < measurement * 1.01) {
213
+ // Use the current measurement if it is within 1% of the
214
+ // previous measurement.
215
+ return measurement;
216
+ }
217
+ last_measurement = measurement;
218
+ sleep_nanoseconds *= 2;
219
+ }
220
+ return last_measurement;
221
+ }
222
+
223
+ #endif // ABSL_INTERNAL_UNSCALED_CYCLECLOCK_FREQUENCY_IS_CPU_FREQUENCY
224
+
225
+ static double GetNominalCPUFrequency() {
226
+ long freq = 0;
227
+
228
+ // Google's production kernel has a patch to export the TSC
229
+ // frequency through sysfs. If the kernel is exporting the TSC
230
+ // frequency use that. There are issues where cpuinfo_max_freq
231
+ // cannot be relied on because the BIOS may be exporting an invalid
232
+ // p-state (on x86) or p-states may be used to put the processor in
233
+ // a new mode (turbo mode). Essentially, those frequencies cannot
234
+ // always be relied upon. The same reasons apply to /proc/cpuinfo as
235
+ // well.
236
+ if (ReadLongFromFile("/sys/devices/system/cpu/cpu0/tsc_freq_khz", &freq)) {
237
+ return freq * 1e3; // Value is kHz.
238
+ }
239
+
240
+ #if defined(ABSL_INTERNAL_UNSCALED_CYCLECLOCK_FREQUENCY_IS_CPU_FREQUENCY)
241
+ // On these platforms, the TSC frequency is the nominal CPU
242
+ // frequency. But without having the kernel export it directly
243
+ // though /sys/devices/system/cpu/cpu0/tsc_freq_khz, there is no
244
+ // other way to reliably get the TSC frequency, so we have to
245
+ // measure it ourselves. Some CPUs abuse cpuinfo_max_freq by
246
+ // exporting "fake" frequencies for implementing new features. For
247
+ // example, Intel's turbo mode is enabled by exposing a p-state
248
+ // value with a higher frequency than that of the real TSC
249
+ // rate. Because of this, we prefer to measure the TSC rate
250
+ // ourselves on i386 and x86-64.
251
+ return MeasureTscFrequency();
252
+ #else
253
+
254
+ // If CPU scaling is in effect, we want to use the *maximum*
255
+ // frequency, not whatever CPU speed some random processor happens
256
+ // to be using now.
257
+ if (ReadLongFromFile("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq",
258
+ &freq)) {
259
+ return freq * 1e3; // Value is kHz.
260
+ }
261
+
262
+ return 1.0;
263
+ #endif // !ABSL_INTERNAL_UNSCALED_CYCLECLOCK_FREQUENCY_IS_CPU_FREQUENCY
264
+ }
265
+
266
+ #endif
267
+
268
+ // InitializeSystemInfo() may be called before main() and before
269
+ // malloc is properly initialized, therefore this must not allocate
270
+ // memory.
271
+ static void InitializeSystemInfo() {
272
+ num_cpus = GetNumCPUs();
273
+ nominal_cpu_frequency = GetNominalCPUFrequency();
274
+ }
275
+
276
+ int NumCPUs() {
277
+ base_internal::LowLevelCallOnce(&init_system_info_once, InitializeSystemInfo);
278
+ return num_cpus;
279
+ }
280
+
281
+ double NominalCPUFrequency() {
282
+ base_internal::LowLevelCallOnce(&init_system_info_once, InitializeSystemInfo);
283
+ return nominal_cpu_frequency;
284
+ }
285
+
286
+ #if defined(_WIN32)
287
+
288
+ pid_t GetTID() {
289
+ return pid_t{GetCurrentThreadId()};
290
+ }
291
+
292
+ #elif defined(__linux__)
293
+
294
+ #ifndef SYS_gettid
295
+ #define SYS_gettid __NR_gettid
296
+ #endif
297
+
298
+ pid_t GetTID() {
299
+ return syscall(SYS_gettid);
300
+ }
301
+
302
+ #elif defined(__akaros__)
303
+
304
+ pid_t GetTID() {
305
+ // Akaros has a concept of "vcore context", which is the state the program
306
+ // is forced into when we need to make a user-level scheduling decision, or
307
+ // run a signal handler. This is analogous to the interrupt context that a
308
+ // CPU might enter if it encounters some kind of exception.
309
+ //
310
+ // There is no current thread context in vcore context, but we need to give
311
+ // a reasonable answer if asked for a thread ID (e.g., in a signal handler).
312
+ // Thread 0 always exists, so if we are in vcore context, we return that.
313
+ //
314
+ // Otherwise, we know (since we are using pthreads) that the uthread struct
315
+ // current_uthread is pointing to is the first element of a
316
+ // struct pthread_tcb, so we extract and return the thread ID from that.
317
+ //
318
+ // TODO(dcross): Akaros anticipates moving the thread ID to the uthread
319
+ // structure at some point. We should modify this code to remove the cast
320
+ // when that happens.
321
+ if (in_vcore_context())
322
+ return 0;
323
+ return reinterpret_cast<struct pthread_tcb *>(current_uthread)->id;
324
+ }
325
+
326
+ #elif defined(__myriad2__)
327
+
328
+ pid_t GetTID() {
329
+ uint32_t tid;
330
+ rtems_task_ident(RTEMS_SELF, 0, &tid);
331
+ return tid;
332
+ }
333
+
334
+ #else
335
+
336
+ // Fallback implementation of GetTID using pthread_getspecific.
337
+ static once_flag tid_once;
338
+ static pthread_key_t tid_key;
339
+ static absl::base_internal::SpinLock tid_lock(
340
+ absl::base_internal::kLinkerInitialized);
341
+
342
+ // We set a bit per thread in this array to indicate that an ID is in
343
+ // use. ID 0 is unused because it is the default value returned by
344
+ // pthread_getspecific().
345
+ static std::vector<uint32_t>* tid_array GUARDED_BY(tid_lock) = nullptr;
346
+ static constexpr int kBitsPerWord = 32; // tid_array is uint32_t.
347
+
348
+ // Returns the TID to tid_array.
349
+ static void FreeTID(void *v) {
350
+ intptr_t tid = reinterpret_cast<intptr_t>(v);
351
+ int word = tid / kBitsPerWord;
352
+ uint32_t mask = ~(1u << (tid % kBitsPerWord));
353
+ absl::base_internal::SpinLockHolder lock(&tid_lock);
354
+ assert(0 <= word && static_cast<size_t>(word) < tid_array->size());
355
+ (*tid_array)[word] &= mask;
356
+ }
357
+
358
+ static void InitGetTID() {
359
+ if (pthread_key_create(&tid_key, FreeTID) != 0) {
360
+ // The logging system calls GetTID() so it can't be used here.
361
+ perror("pthread_key_create failed");
362
+ abort();
363
+ }
364
+
365
+ // Initialize tid_array.
366
+ absl::base_internal::SpinLockHolder lock(&tid_lock);
367
+ tid_array = new std::vector<uint32_t>(1);
368
+ (*tid_array)[0] = 1; // ID 0 is never-allocated.
369
+ }
370
+
371
+ // Return a per-thread small integer ID from pthread's thread-specific data.
372
+ pid_t GetTID() {
373
+ absl::call_once(tid_once, InitGetTID);
374
+
375
+ intptr_t tid = reinterpret_cast<intptr_t>(pthread_getspecific(tid_key));
376
+ if (tid != 0) {
377
+ return tid;
378
+ }
379
+
380
+ int bit; // tid_array[word] = 1u << bit;
381
+ size_t word;
382
+ {
383
+ // Search for the first unused ID.
384
+ absl::base_internal::SpinLockHolder lock(&tid_lock);
385
+ // First search for a word in the array that is not all ones.
386
+ word = 0;
387
+ while (word < tid_array->size() && ~(*tid_array)[word] == 0) {
388
+ ++word;
389
+ }
390
+ if (word == tid_array->size()) {
391
+ tid_array->push_back(0); // No space left, add kBitsPerWord more IDs.
392
+ }
393
+ // Search for a zero bit in the word.
394
+ bit = 0;
395
+ while (bit < kBitsPerWord && (((*tid_array)[word] >> bit) & 1) != 0) {
396
+ ++bit;
397
+ }
398
+ tid = (word * kBitsPerWord) + bit;
399
+ (*tid_array)[word] |= 1u << bit; // Mark the TID as allocated.
400
+ }
401
+
402
+ if (pthread_setspecific(tid_key, reinterpret_cast<void *>(tid)) != 0) {
403
+ perror("pthread_setspecific failed");
404
+ abort();
405
+ }
406
+
407
+ return static_cast<pid_t>(tid);
408
+ }
409
+
410
+ #endif
411
+
412
+ } // namespace base_internal
413
+ ABSL_NAMESPACE_END
414
+ } // namespace absl