grpc 1.6.7 → 1.7.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 (277) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +579 -77
  3. data/include/grpc/byte_buffer.h +1 -63
  4. data/include/grpc/compression.h +27 -5
  5. data/include/grpc/fork.h +24 -0
  6. data/include/grpc/grpc.h +12 -6
  7. data/include/grpc/grpc_security.h +28 -7
  8. data/include/grpc/impl/codegen/atm.h +1 -0
  9. data/include/grpc/impl/codegen/byte_buffer.h +86 -0
  10. data/include/grpc/impl/codegen/compression_types.h +63 -5
  11. data/include/grpc/impl/codegen/fork.h +48 -0
  12. data/include/grpc/impl/codegen/grpc_types.h +26 -9
  13. data/include/grpc/impl/codegen/port_platform.h +11 -4
  14. data/include/grpc/impl/codegen/slice.h +6 -1
  15. data/include/grpc/impl/codegen/sync.h +3 -1
  16. data/include/grpc/impl/codegen/sync_custom.h +36 -0
  17. data/include/grpc/module.modulemap +75 -3
  18. data/include/grpc/slice.h +1 -5
  19. data/include/grpc/support/sync_custom.h +24 -0
  20. data/src/core/ext/census/base_resources.c +14 -14
  21. data/src/core/ext/census/context.c +7 -5
  22. data/src/core/ext/census/grpc_filter.c +12 -14
  23. data/src/core/ext/census/mlog.c +2 -1
  24. data/src/core/ext/census/resource.c +13 -9
  25. data/src/core/ext/filters/client_channel/channel_connectivity.c +15 -8
  26. data/src/core/ext/filters/client_channel/client_channel.c +418 -439
  27. data/src/core/ext/filters/client_channel/client_channel_factory.c +4 -5
  28. data/src/core/ext/filters/client_channel/client_channel_plugin.c +2 -2
  29. data/src/core/ext/filters/client_channel/http_connect_handshaker.c +7 -5
  30. data/src/core/ext/filters/client_channel/http_proxy.c +17 -21
  31. data/src/core/ext/filters/client_channel/lb_policy.c +1 -1
  32. data/src/core/ext/filters/client_channel/lb_policy/grpclb/client_load_reporting_filter.c +7 -7
  33. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.c +371 -257
  34. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_client_stats.c +7 -5
  35. data/src/core/ext/filters/client_channel/lb_policy/grpclb/load_balancer_api.c +25 -14
  36. data/src/core/ext/filters/client_channel/lb_policy/pick_first/pick_first.c +16 -16
  37. data/src/core/ext/filters/client_channel/lb_policy/round_robin/round_robin.c +33 -28
  38. data/src/core/ext/filters/client_channel/lb_policy_factory.c +10 -8
  39. data/src/core/ext/filters/client_channel/lb_policy_factory.h +1 -1
  40. data/src/core/ext/filters/client_channel/proxy_mapper_registry.c +1 -1
  41. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/dns_resolver_ares.c +7 -6
  42. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver_posix.c +62 -28
  43. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.c +29 -23
  44. data/src/core/ext/filters/client_channel/resolver/fake/fake_resolver.c +25 -14
  45. data/src/core/ext/filters/client_channel/retry_throttle.c +9 -6
  46. data/src/core/ext/filters/client_channel/subchannel.c +30 -30
  47. data/src/core/ext/filters/client_channel/subchannel.h +1 -4
  48. data/src/core/ext/filters/client_channel/subchannel_index.c +31 -15
  49. data/src/core/ext/filters/client_channel/subchannel_index.h +7 -0
  50. data/src/core/ext/filters/client_channel/uri_parser.c +4 -3
  51. data/src/core/ext/filters/deadline/deadline_filter.c +78 -39
  52. data/src/core/ext/filters/deadline/deadline_filter.h +7 -1
  53. data/src/core/ext/filters/http/client/http_client_filter.c +14 -14
  54. data/src/core/ext/filters/http/http_filters_plugin.c +1 -1
  55. data/src/core/ext/filters/http/message_compress/message_compress_filter.c +240 -175
  56. data/src/core/ext/filters/http/server/http_server_filter.c +48 -36
  57. data/src/core/ext/filters/load_reporting/{load_reporting_filter.c → server_load_reporting_filter.c} +11 -12
  58. data/src/core/ext/filters/load_reporting/{load_reporting_filter.h → server_load_reporting_filter.h} +6 -5
  59. data/src/core/ext/filters/load_reporting/{load_reporting.c → server_load_reporting_plugin.c} +19 -13
  60. data/src/core/ext/filters/load_reporting/{load_reporting.h → server_load_reporting_plugin.h} +4 -3
  61. data/src/core/ext/filters/max_age/max_age_filter.c +2 -3
  62. data/src/core/ext/filters/message_size/message_size_filter.c +4 -2
  63. data/src/core/ext/filters/workarounds/workaround_cronet_compression_filter.c +0 -1
  64. data/src/core/ext/transport/chttp2/client/chttp2_connector.c +5 -5
  65. data/src/core/ext/transport/chttp2/client/insecure/channel_create.c +1 -1
  66. data/src/core/ext/transport/chttp2/client/insecure/channel_create_posix.c +1 -1
  67. data/src/core/ext/transport/chttp2/server/chttp2_server.c +20 -18
  68. data/src/core/ext/transport/chttp2/transport/chttp2_plugin.c +1 -0
  69. data/src/core/ext/transport/chttp2/transport/chttp2_transport.c +493 -210
  70. data/src/core/ext/transport/chttp2/transport/chttp2_transport.h +1 -0
  71. data/src/core/ext/transport/chttp2/transport/flow_control.c +9 -8
  72. data/src/core/ext/transport/chttp2/transport/frame_data.c +2 -2
  73. data/src/core/ext/transport/chttp2/transport/frame_goaway.c +2 -2
  74. data/src/core/ext/transport/chttp2/transport/frame_ping.c +5 -4
  75. data/src/core/ext/transport/chttp2/transport/frame_rst_stream.c +1 -1
  76. data/src/core/ext/transport/chttp2/transport/frame_settings.c +10 -9
  77. data/src/core/ext/transport/chttp2/transport/frame_window_update.c +9 -5
  78. data/src/core/ext/transport/chttp2/transport/hpack_encoder.c +62 -41
  79. data/src/core/ext/transport/chttp2/transport/hpack_parser.c +52 -8
  80. data/src/core/ext/transport/chttp2/transport/hpack_table.c +2 -2
  81. data/src/core/ext/transport/chttp2/transport/incoming_metadata.c +3 -2
  82. data/src/core/ext/transport/chttp2/transport/internal.h +60 -30
  83. data/src/core/ext/transport/chttp2/transport/parsing.c +16 -5
  84. data/src/core/ext/transport/chttp2/transport/stream_lists.c +36 -16
  85. data/src/core/ext/transport/chttp2/transport/stream_map.c +6 -4
  86. data/src/core/ext/transport/chttp2/transport/writing.c +133 -105
  87. data/src/core/ext/transport/inproc/inproc_transport.c +61 -65
  88. data/src/core/lib/channel/channel_args.c +112 -12
  89. data/src/core/lib/channel/channel_args.h +31 -0
  90. data/src/core/lib/channel/channel_stack.c +1 -15
  91. data/src/core/lib/channel/channel_stack.h +3 -10
  92. data/src/core/lib/channel/channel_stack_builder.c +41 -10
  93. data/src/core/lib/channel/channel_stack_builder.h +10 -0
  94. data/src/core/lib/channel/connected_channel.c +94 -23
  95. data/src/core/lib/channel/handshaker.c +8 -6
  96. data/src/core/lib/channel/handshaker_registry.c +1 -1
  97. data/src/core/lib/compression/algorithm_metadata.h +14 -0
  98. data/src/core/lib/compression/compression.c +101 -1
  99. data/src/core/lib/compression/stream_compression.c +32 -146
  100. data/src/core/lib/compression/stream_compression.h +28 -4
  101. data/src/core/lib/compression/stream_compression_gzip.c +228 -0
  102. data/src/core/lib/{iomgr/ev_epoll_thread_pool_linux.h → compression/stream_compression_gzip.h} +5 -7
  103. data/src/core/lib/compression/stream_compression_identity.c +94 -0
  104. data/src/core/lib/{iomgr/ev_epoll_limited_pollers_linux.h → compression/stream_compression_identity.h} +7 -8
  105. data/src/core/lib/debug/stats.c +174 -0
  106. data/src/core/lib/debug/stats.h +61 -0
  107. data/src/core/lib/debug/stats_data.c +687 -0
  108. data/src/core/lib/debug/stats_data.h +470 -0
  109. data/src/core/lib/debug/trace.c +3 -3
  110. data/src/core/lib/debug/trace.h +1 -1
  111. data/src/core/lib/http/format_request.c +1 -1
  112. data/src/core/lib/http/httpcli.c +8 -7
  113. data/src/core/lib/http/httpcli_security_connector.c +2 -1
  114. data/src/core/lib/http/parser.c +4 -3
  115. data/src/core/lib/iomgr/call_combiner.c +202 -0
  116. data/src/core/lib/iomgr/call_combiner.h +121 -0
  117. data/src/core/lib/iomgr/closure.c +18 -4
  118. data/src/core/lib/iomgr/combiner.c +11 -4
  119. data/src/core/lib/iomgr/error.c +26 -24
  120. data/src/core/lib/iomgr/ev_epoll1_linux.c +395 -212
  121. data/src/core/lib/iomgr/ev_epollex_linux.c +141 -128
  122. data/src/core/lib/iomgr/ev_epollsig_linux.c +44 -41
  123. data/src/core/lib/iomgr/ev_poll_posix.c +99 -75
  124. data/src/core/lib/iomgr/ev_posix.c +5 -9
  125. data/src/core/lib/iomgr/ev_posix.h +1 -1
  126. data/src/core/lib/iomgr/exec_ctx.h +6 -1
  127. data/src/core/lib/iomgr/executor.c +142 -36
  128. data/src/core/lib/iomgr/executor.h +6 -1
  129. data/src/core/lib/iomgr/fork_posix.c +88 -0
  130. data/src/core/lib/iomgr/fork_windows.c +39 -0
  131. data/src/core/lib/iomgr/iocp_windows.c +2 -0
  132. data/src/core/lib/iomgr/iomgr.c +2 -8
  133. data/src/core/lib/iomgr/is_epollexclusive_available.c +6 -6
  134. data/src/core/lib/iomgr/load_file.c +2 -1
  135. data/src/core/lib/iomgr/polling_entity.c +9 -9
  136. data/src/core/lib/iomgr/polling_entity.h +7 -1
  137. data/src/core/lib/iomgr/pollset.h +1 -1
  138. data/src/core/lib/iomgr/pollset_uv.c +1 -1
  139. data/src/core/lib/iomgr/pollset_windows.c +3 -3
  140. data/src/core/lib/iomgr/port.h +4 -0
  141. data/src/core/lib/iomgr/resolve_address_posix.c +8 -7
  142. data/src/core/lib/iomgr/resolve_address_windows.c +1 -1
  143. data/src/core/lib/iomgr/resource_quota.c +24 -19
  144. data/src/core/lib/iomgr/socket_factory_posix.c +4 -4
  145. data/src/core/lib/iomgr/socket_mutator.c +4 -4
  146. data/src/core/lib/iomgr/socket_utils_windows.c +0 -4
  147. data/src/core/lib/iomgr/tcp_client_posix.c +5 -4
  148. data/src/core/lib/iomgr/tcp_posix.c +181 -20
  149. data/src/core/lib/iomgr/tcp_server_posix.c +8 -7
  150. data/src/core/lib/iomgr/tcp_server_utils_posix_common.c +1 -1
  151. data/src/core/lib/iomgr/timer.h +4 -0
  152. data/src/core/lib/iomgr/timer_generic.c +138 -3
  153. data/src/core/lib/iomgr/timer_generic.h +3 -0
  154. data/src/core/lib/iomgr/timer_heap.c +4 -4
  155. data/src/core/lib/iomgr/timer_manager.c +2 -2
  156. data/src/core/lib/iomgr/timer_uv.c +2 -0
  157. data/src/core/lib/iomgr/udp_server.c +10 -8
  158. data/src/core/lib/iomgr/unix_sockets_posix.c +4 -2
  159. data/src/core/lib/iomgr/wakeup_fd_cv.c +9 -8
  160. data/src/core/lib/iomgr/wakeup_fd_cv.h +2 -2
  161. data/src/core/lib/json/json.c +1 -1
  162. data/src/core/lib/json/json_string.c +13 -13
  163. data/src/core/lib/profiling/timers.h +18 -8
  164. data/src/core/lib/security/credentials/composite/composite_credentials.c +4 -10
  165. data/src/core/lib/security/credentials/google_default/google_default_credentials.c +2 -1
  166. data/src/core/lib/security/credentials/jwt/jwt_verifier.c +11 -6
  167. data/src/core/lib/security/credentials/oauth2/oauth2_credentials.c +4 -4
  168. data/src/core/lib/security/credentials/plugin/plugin_credentials.c +132 -50
  169. data/src/core/lib/security/credentials/plugin/plugin_credentials.h +2 -0
  170. data/src/core/lib/security/transport/client_auth_filter.c +68 -135
  171. data/src/core/lib/security/transport/secure_endpoint.c +110 -90
  172. data/src/core/lib/security/transport/secure_endpoint.h +8 -3
  173. data/src/core/lib/security/transport/security_connector.c +10 -12
  174. data/src/core/lib/security/transport/security_handshaker.c +45 -24
  175. data/src/core/lib/security/transport/server_auth_filter.c +71 -20
  176. data/src/core/lib/slice/b64.c +2 -2
  177. data/src/core/lib/slice/slice.c +16 -14
  178. data/src/core/lib/slice/slice_buffer.c +5 -4
  179. data/src/core/lib/slice/slice_hash_table.c +3 -2
  180. data/src/core/lib/slice/slice_intern.c +8 -5
  181. data/src/core/lib/support/block_annotate.h +22 -0
  182. data/src/core/lib/support/fork.c +62 -0
  183. data/src/core/lib/support/fork.h +35 -0
  184. data/src/core/lib/support/log_linux.c +1 -1
  185. data/src/core/lib/support/string.c +15 -1
  186. data/src/core/lib/support/string.h +3 -0
  187. data/src/core/lib/support/thd_internal.h +6 -0
  188. data/src/core/lib/support/thd_posix.c +56 -0
  189. data/src/core/lib/support/thd_windows.c +2 -0
  190. data/src/core/lib/surface/alarm.c +22 -15
  191. data/src/core/lib/surface/byte_buffer.c +4 -2
  192. data/src/core/lib/surface/call.c +442 -141
  193. data/src/core/lib/surface/call.h +6 -6
  194. data/src/core/lib/surface/call_log_batch.c +1 -1
  195. data/src/core/lib/surface/call_test_only.h +12 -0
  196. data/src/core/lib/surface/channel.c +39 -4
  197. data/src/core/lib/surface/channel_init.c +6 -6
  198. data/src/core/lib/surface/channel_ping.c +2 -2
  199. data/src/core/lib/surface/completion_queue.c +56 -57
  200. data/src/core/lib/surface/init.c +17 -3
  201. data/src/core/lib/surface/init_secure.c +5 -1
  202. data/src/core/lib/surface/lame_client.cc +9 -10
  203. data/src/core/lib/surface/server.c +81 -72
  204. data/src/core/lib/surface/version.c +2 -2
  205. data/src/core/lib/transport/byte_stream.c +1 -0
  206. data/src/core/lib/transport/byte_stream.h +3 -1
  207. data/src/core/lib/transport/connectivity_state.c +2 -1
  208. data/src/core/lib/transport/metadata.c +7 -4
  209. data/src/core/lib/transport/metadata_batch.c +18 -16
  210. data/src/core/lib/transport/metadata_batch.h +1 -0
  211. data/src/core/lib/transport/service_config.c +5 -3
  212. data/src/core/lib/transport/static_metadata.c +395 -614
  213. data/src/core/lib/transport/static_metadata.h +165 -133
  214. data/src/core/lib/transport/status_conversion.c +1 -1
  215. data/src/core/lib/transport/transport.c +20 -20
  216. data/src/core/lib/transport/transport.h +8 -5
  217. data/src/core/lib/transport/transport_impl.h +0 -3
  218. data/src/core/lib/transport/transport_op_string.c +8 -1
  219. data/src/core/plugin_registry/grpc_plugin_registry.c +4 -4
  220. data/src/core/tsi/fake_transport_security.c +133 -2
  221. data/src/core/tsi/fake_transport_security.h +5 -0
  222. data/src/core/tsi/ssl_transport_security.c +105 -8
  223. data/src/core/tsi/ssl_transport_security.h +30 -7
  224. data/src/core/tsi/transport_security.h +8 -2
  225. data/src/core/tsi/transport_security_grpc.c +20 -13
  226. data/src/core/tsi/transport_security_grpc.h +13 -9
  227. data/src/ruby/ext/grpc/rb_call_credentials.c +6 -2
  228. data/src/ruby/ext/grpc/rb_grpc.c +1 -1
  229. data/src/ruby/ext/grpc/rb_grpc_imports.generated.c +30 -20
  230. data/src/ruby/ext/grpc/rb_grpc_imports.generated.h +50 -35
  231. data/src/ruby/lib/grpc.rb +1 -0
  232. data/src/ruby/lib/grpc/generic/active_call.rb +34 -9
  233. data/src/ruby/lib/grpc/generic/bidi_call.rb +19 -10
  234. data/src/ruby/lib/grpc/generic/client_stub.rb +95 -38
  235. data/src/ruby/lib/grpc/generic/interceptor_registry.rb +53 -0
  236. data/src/ruby/lib/grpc/generic/interceptors.rb +186 -0
  237. data/src/ruby/lib/grpc/generic/rpc_desc.rb +66 -20
  238. data/src/ruby/lib/grpc/generic/rpc_server.rb +15 -3
  239. data/src/ruby/lib/grpc/google_rpc_status_utils.rb +1 -2
  240. data/src/ruby/lib/grpc/version.rb +1 -1
  241. data/src/ruby/pb/grpc/testing/duplicate/echo_duplicate_services_pb.rb +1 -0
  242. data/src/ruby/spec/channel_connection_spec.rb +1 -34
  243. data/src/ruby/spec/client_server_spec.rb +188 -82
  244. data/src/ruby/spec/generic/active_call_spec.rb +65 -11
  245. data/src/ruby/spec/generic/client_interceptors_spec.rb +153 -0
  246. data/src/ruby/spec/generic/interceptor_registry_spec.rb +65 -0
  247. data/src/ruby/spec/generic/rpc_desc_spec.rb +38 -0
  248. data/src/ruby/spec/generic/rpc_server_spec.rb +1 -34
  249. data/src/ruby/spec/generic/server_interceptors_spec.rb +218 -0
  250. data/src/ruby/spec/spec_helper.rb +4 -0
  251. data/src/ruby/spec/support/helpers.rb +73 -0
  252. data/src/ruby/spec/support/services.rb +147 -0
  253. data/third_party/cares/ares_build.h +21 -62
  254. data/third_party/cares/cares/ares.h +23 -1
  255. data/third_party/cares/cares/ares__close_sockets.c +2 -2
  256. data/third_party/cares/cares/ares_create_query.c +3 -3
  257. data/third_party/cares/cares/ares_expand_name.c +6 -2
  258. data/third_party/cares/cares/ares_expand_string.c +1 -1
  259. data/third_party/cares/cares/ares_getnameinfo.c +27 -7
  260. data/third_party/cares/cares/ares_init.c +407 -39
  261. data/third_party/cares/cares/ares_library_init.c +10 -0
  262. data/third_party/cares/cares/ares_library_init.h +2 -1
  263. data/third_party/cares/cares/ares_nowarn.c +6 -6
  264. data/third_party/cares/cares/ares_nowarn.h +2 -2
  265. data/third_party/cares/cares/ares_parse_naptr_reply.c +6 -1
  266. data/third_party/cares/cares/ares_private.h +11 -0
  267. data/third_party/cares/cares/ares_process.c +126 -37
  268. data/third_party/cares/cares/ares_version.h +2 -2
  269. data/third_party/cares/cares/ares_writev.c +2 -2
  270. data/third_party/cares/cares/config-win32.h +8 -34
  271. data/third_party/cares/cares/inet_net_pton.c +2 -2
  272. data/third_party/cares/cares/setup_once.h +5 -5
  273. data/third_party/cares/config_darwin/ares_config.h +98 -196
  274. data/third_party/cares/config_linux/ares_config.h +103 -203
  275. metadata +47 -20
  276. data/src/core/lib/iomgr/ev_epoll_limited_pollers_linux.c +0 -1957
  277. data/src/core/lib/iomgr/ev_epoll_thread_pool_linux.c +0 -1182
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grpc
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.7
4
+ version: 1.7.0.pre1
5
5
  platform: ruby
6
6
  authors:
7
7
  - gRPC Authors
8
8
  autorequire:
9
9
  bindir: src/ruby/bin
10
10
  cert_chain: []
11
- date: 2017-10-19 00:00:00.000000000 Z
11
+ date: 2017-10-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: google-protobuf
@@ -100,28 +100,28 @@ dependencies:
100
100
  requirements:
101
101
  - - "~>"
102
102
  - !ruby/object:Gem::Version
103
- version: '0.9'
103
+ version: 0.14.1
104
104
  type: :development
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
108
  - - "~>"
109
109
  - !ruby/object:Gem::Version
110
- version: '0.9'
110
+ version: 0.14.1
111
111
  - !ruby/object:Gem::Dependency
112
112
  name: rake
113
113
  requirement: !ruby/object:Gem::Requirement
114
114
  requirements:
115
115
  - - "~>"
116
116
  - !ruby/object:Gem::Version
117
- version: '10.4'
117
+ version: '12.0'
118
118
  type: :development
119
119
  prerelease: false
120
120
  version_requirements: !ruby/object:Gem::Requirement
121
121
  requirements:
122
122
  - - "~>"
123
123
  - !ruby/object:Gem::Version
124
- version: '10.4'
124
+ version: '12.0'
125
125
  - !ruby/object:Gem::Dependency
126
126
  name: rake-compiler
127
127
  requirement: !ruby/object:Gem::Requirement
@@ -156,28 +156,28 @@ dependencies:
156
156
  requirements:
157
157
  - - "~>"
158
158
  - !ruby/object:Gem::Version
159
- version: '3.2'
159
+ version: '3.6'
160
160
  type: :development
161
161
  prerelease: false
162
162
  version_requirements: !ruby/object:Gem::Requirement
163
163
  requirements:
164
164
  - - "~>"
165
165
  - !ruby/object:Gem::Version
166
- version: '3.2'
166
+ version: '3.6'
167
167
  - !ruby/object:Gem::Dependency
168
168
  name: rubocop
169
169
  requirement: !ruby/object:Gem::Requirement
170
170
  requirements:
171
171
  - - "~>"
172
172
  - !ruby/object:Gem::Version
173
- version: 0.30.0
173
+ version: 0.49.1
174
174
  type: :development
175
175
  prerelease: false
176
176
  version_requirements: !ruby/object:Gem::Requirement
177
177
  requirements:
178
178
  - - "~>"
179
179
  - !ruby/object:Gem::Version
180
- version: 0.30.0
180
+ version: 0.49.1
181
181
  - !ruby/object:Gem::Dependency
182
182
  name: signet
183
183
  requirement: !ruby/object:Gem::Requirement
@@ -206,6 +206,7 @@ files:
206
206
  - include/grpc/byte_buffer_reader.h
207
207
  - include/grpc/census.h
208
208
  - include/grpc/compression.h
209
+ - include/grpc/fork.h
209
210
  - include/grpc/grpc.h
210
211
  - include/grpc/grpc_cronet.h
211
212
  - include/grpc/grpc_posix.h
@@ -215,10 +216,12 @@ files:
215
216
  - include/grpc/impl/codegen/atm_gcc_atomic.h
216
217
  - include/grpc/impl/codegen/atm_gcc_sync.h
217
218
  - include/grpc/impl/codegen/atm_windows.h
219
+ - include/grpc/impl/codegen/byte_buffer.h
218
220
  - include/grpc/impl/codegen/byte_buffer_reader.h
219
221
  - include/grpc/impl/codegen/compression_types.h
220
222
  - include/grpc/impl/codegen/connectivity_state.h
221
223
  - include/grpc/impl/codegen/exec_ctx_fwd.h
224
+ - include/grpc/impl/codegen/fork.h
222
225
  - include/grpc/impl/codegen/gpr_slice.h
223
226
  - include/grpc/impl/codegen/gpr_types.h
224
227
  - include/grpc/impl/codegen/grpc_types.h
@@ -227,6 +230,7 @@ files:
227
230
  - include/grpc/impl/codegen/slice.h
228
231
  - include/grpc/impl/codegen/status.h
229
232
  - include/grpc/impl/codegen/sync.h
233
+ - include/grpc/impl/codegen/sync_custom.h
230
234
  - include/grpc/impl/codegen/sync_generic.h
231
235
  - include/grpc/impl/codegen/sync_posix.h
232
236
  - include/grpc/impl/codegen/sync_windows.h
@@ -251,6 +255,7 @@ files:
251
255
  - include/grpc/support/string_util.h
252
256
  - include/grpc/support/subprocess.h
253
257
  - include/grpc/support/sync.h
258
+ - include/grpc/support/sync_custom.h
254
259
  - include/grpc/support/sync_generic.h
255
260
  - include/grpc/support/sync_posix.h
256
261
  - include/grpc/support/sync_windows.h
@@ -367,10 +372,10 @@ files:
367
372
  - src/core/ext/filters/http/message_compress/message_compress_filter.h
368
373
  - src/core/ext/filters/http/server/http_server_filter.c
369
374
  - src/core/ext/filters/http/server/http_server_filter.h
370
- - src/core/ext/filters/load_reporting/load_reporting.c
371
- - src/core/ext/filters/load_reporting/load_reporting.h
372
- - src/core/ext/filters/load_reporting/load_reporting_filter.c
373
- - src/core/ext/filters/load_reporting/load_reporting_filter.h
375
+ - src/core/ext/filters/load_reporting/server_load_reporting_filter.c
376
+ - src/core/ext/filters/load_reporting/server_load_reporting_filter.h
377
+ - src/core/ext/filters/load_reporting/server_load_reporting_plugin.c
378
+ - src/core/ext/filters/load_reporting/server_load_reporting_plugin.h
374
379
  - src/core/ext/filters/max_age/max_age_filter.c
375
380
  - src/core/ext/filters/max_age/max_age_filter.h
376
381
  - src/core/ext/filters/message_size/message_size_filter.c
@@ -456,6 +461,14 @@ files:
456
461
  - src/core/lib/compression/message_compress.h
457
462
  - src/core/lib/compression/stream_compression.c
458
463
  - src/core/lib/compression/stream_compression.h
464
+ - src/core/lib/compression/stream_compression_gzip.c
465
+ - src/core/lib/compression/stream_compression_gzip.h
466
+ - src/core/lib/compression/stream_compression_identity.c
467
+ - src/core/lib/compression/stream_compression_identity.h
468
+ - src/core/lib/debug/stats.c
469
+ - src/core/lib/debug/stats.h
470
+ - src/core/lib/debug/stats_data.c
471
+ - src/core/lib/debug/stats_data.h
459
472
  - src/core/lib/debug/trace.c
460
473
  - src/core/lib/debug/trace.h
461
474
  - src/core/lib/http/format_request.c
@@ -465,6 +478,8 @@ files:
465
478
  - src/core/lib/http/httpcli_security_connector.c
466
479
  - src/core/lib/http/parser.c
467
480
  - src/core/lib/http/parser.h
481
+ - src/core/lib/iomgr/call_combiner.c
482
+ - src/core/lib/iomgr/call_combiner.h
468
483
  - src/core/lib/iomgr/closure.c
469
484
  - src/core/lib/iomgr/closure.h
470
485
  - src/core/lib/iomgr/combiner.c
@@ -480,10 +495,6 @@ files:
480
495
  - src/core/lib/iomgr/error_internal.h
481
496
  - src/core/lib/iomgr/ev_epoll1_linux.c
482
497
  - src/core/lib/iomgr/ev_epoll1_linux.h
483
- - src/core/lib/iomgr/ev_epoll_limited_pollers_linux.c
484
- - src/core/lib/iomgr/ev_epoll_limited_pollers_linux.h
485
- - src/core/lib/iomgr/ev_epoll_thread_pool_linux.c
486
- - src/core/lib/iomgr/ev_epoll_thread_pool_linux.h
487
498
  - src/core/lib/iomgr/ev_epollex_linux.c
488
499
  - src/core/lib/iomgr/ev_epollex_linux.h
489
500
  - src/core/lib/iomgr/ev_epollsig_linux.c
@@ -497,6 +508,8 @@ files:
497
508
  - src/core/lib/iomgr/exec_ctx.h
498
509
  - src/core/lib/iomgr/executor.c
499
510
  - src/core/lib/iomgr/executor.h
511
+ - src/core/lib/iomgr/fork_posix.c
512
+ - src/core/lib/iomgr/fork_windows.c
500
513
  - src/core/lib/iomgr/gethostname.h
501
514
  - src/core/lib/iomgr/gethostname_fallback.c
502
515
  - src/core/lib/iomgr/gethostname_host_name_max.c
@@ -684,6 +697,8 @@ files:
684
697
  - src/core/lib/support/env_linux.c
685
698
  - src/core/lib/support/env_posix.c
686
699
  - src/core/lib/support/env_windows.c
700
+ - src/core/lib/support/fork.c
701
+ - src/core/lib/support/fork.h
687
702
  - src/core/lib/support/histogram.c
688
703
  - src/core/lib/support/host_port.c
689
704
  - src/core/lib/support/log.c
@@ -847,6 +862,8 @@ files:
847
862
  - src/ruby/lib/grpc/generic/active_call.rb
848
863
  - src/ruby/lib/grpc/generic/bidi_call.rb
849
864
  - src/ruby/lib/grpc/generic/client_stub.rb
865
+ - src/ruby/lib/grpc/generic/interceptor_registry.rb
866
+ - src/ruby/lib/grpc/generic/interceptors.rb
850
867
  - src/ruby/lib/grpc/generic/rpc_desc.rb
851
868
  - src/ruby/lib/grpc/generic/rpc_server.rb
852
869
  - src/ruby/lib/grpc/generic/service.rb
@@ -879,10 +896,13 @@ files:
879
896
  - src/ruby/spec/compression_options_spec.rb
880
897
  - src/ruby/spec/error_sanity_spec.rb
881
898
  - src/ruby/spec/generic/active_call_spec.rb
899
+ - src/ruby/spec/generic/client_interceptors_spec.rb
882
900
  - src/ruby/spec/generic/client_stub_spec.rb
901
+ - src/ruby/spec/generic/interceptor_registry_spec.rb
883
902
  - src/ruby/spec/generic/rpc_desc_spec.rb
884
903
  - src/ruby/spec/generic/rpc_server_pool_spec.rb
885
904
  - src/ruby/spec/generic/rpc_server_spec.rb
905
+ - src/ruby/spec/generic/server_interceptors_spec.rb
886
906
  - src/ruby/spec/generic/service_spec.rb
887
907
  - src/ruby/spec/google_rpc_status_utils_spec.rb
888
908
  - src/ruby/spec/pb/duplicate/codegen_spec.rb
@@ -890,6 +910,8 @@ files:
890
910
  - src/ruby/spec/server_credentials_spec.rb
891
911
  - src/ruby/spec/server_spec.rb
892
912
  - src/ruby/spec/spec_helper.rb
913
+ - src/ruby/spec/support/helpers.rb
914
+ - src/ruby/spec/support/services.rb
893
915
  - src/ruby/spec/testdata/README
894
916
  - src/ruby/spec/testdata/ca.pem
895
917
  - src/ruby/spec/testdata/client.key
@@ -1426,9 +1448,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
1426
1448
  version: 2.0.0
1427
1449
  required_rubygems_version: !ruby/object:Gem::Requirement
1428
1450
  requirements:
1429
- - - ">="
1451
+ - - ">"
1430
1452
  - !ruby/object:Gem::Version
1431
- version: '0'
1453
+ version: 1.3.1
1432
1454
  requirements: []
1433
1455
  rubyforge_project:
1434
1456
  rubygems_version: 2.6.14
@@ -1437,6 +1459,8 @@ specification_version: 4
1437
1459
  summary: GRPC system in Ruby
1438
1460
  test_files:
1439
1461
  - src/ruby/spec/server_spec.rb
1462
+ - src/ruby/spec/support/services.rb
1463
+ - src/ruby/spec/support/helpers.rb
1440
1464
  - src/ruby/spec/channel_spec.rb
1441
1465
  - src/ruby/spec/client_auth_spec.rb
1442
1466
  - src/ruby/spec/testdata/README
@@ -1450,10 +1474,13 @@ test_files:
1450
1474
  - src/ruby/spec/channel_connection_spec.rb
1451
1475
  - src/ruby/spec/call_spec.rb
1452
1476
  - src/ruby/spec/generic/rpc_server_spec.rb
1477
+ - src/ruby/spec/generic/server_interceptors_spec.rb
1478
+ - src/ruby/spec/generic/client_interceptors_spec.rb
1453
1479
  - src/ruby/spec/generic/rpc_desc_spec.rb
1454
1480
  - src/ruby/spec/generic/active_call_spec.rb
1455
1481
  - src/ruby/spec/generic/client_stub_spec.rb
1456
1482
  - src/ruby/spec/generic/rpc_server_pool_spec.rb
1483
+ - src/ruby/spec/generic/interceptor_registry_spec.rb
1457
1484
  - src/ruby/spec/generic/service_spec.rb
1458
1485
  - src/ruby/spec/pb/duplicate/codegen_spec.rb
1459
1486
  - src/ruby/spec/pb/health/checker_spec.rb
@@ -1,1957 +0,0 @@
1
- /*
2
- *
3
- * Copyright 2017 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 "src/core/lib/iomgr/port.h"
20
-
21
- /* This polling engine is only relevant on linux kernels supporting epoll() */
22
- #ifdef GRPC_LINUX_EPOLL
23
-
24
- #include "src/core/lib/iomgr/ev_epoll_limited_pollers_linux.h"
25
-
26
- #include <assert.h>
27
- #include <errno.h>
28
- #include <limits.h>
29
- #include <poll.h>
30
- #include <pthread.h>
31
- #include <signal.h>
32
- #include <string.h>
33
- #include <sys/epoll.h>
34
- #include <sys/socket.h>
35
- #include <unistd.h>
36
-
37
- #include <grpc/support/alloc.h>
38
- #include <grpc/support/log.h>
39
- #include <grpc/support/string_util.h>
40
- #include <grpc/support/tls.h>
41
- #include <grpc/support/useful.h>
42
-
43
- #include "src/core/lib/debug/trace.h"
44
- #include "src/core/lib/iomgr/ev_posix.h"
45
- #include "src/core/lib/iomgr/iomgr_internal.h"
46
- #include "src/core/lib/iomgr/lockfree_event.h"
47
- #include "src/core/lib/iomgr/timer.h"
48
- #include "src/core/lib/iomgr/wakeup_fd_posix.h"
49
- #include "src/core/lib/profiling/timers.h"
50
- #include "src/core/lib/support/block_annotate.h"
51
- #include "src/core/lib/support/env.h"
52
-
53
- #define GRPC_POLLING_TRACE(fmt, ...) \
54
- if (GRPC_TRACER_ON(grpc_polling_trace)) { \
55
- gpr_log(GPR_INFO, (fmt), __VA_ARGS__); \
56
- }
57
-
58
- #define GRPC_POLLSET_KICK_BROADCAST ((grpc_pollset_worker *)1)
59
-
60
- /* The maximum number of polling threads per polling island. By default no
61
- limit */
62
- static int g_max_pollers_per_pi = INT_MAX;
63
-
64
- static int grpc_wakeup_signal = -1;
65
- static bool is_grpc_wakeup_signal_initialized = false;
66
-
67
- /* Implements the function defined in grpc_posix.h. This function might be
68
- * called before even calling grpc_init() to set either a different signal to
69
- * use. If signum == -1, then the use of signals is disabled */
70
- static void grpc_use_signal(int signum) {
71
- grpc_wakeup_signal = signum;
72
- is_grpc_wakeup_signal_initialized = true;
73
-
74
- if (grpc_wakeup_signal < 0) {
75
- gpr_log(GPR_INFO,
76
- "Use of signals is disabled. Epoll engine will not be used");
77
- } else {
78
- gpr_log(GPR_INFO, "epoll engine will be using signal: %d",
79
- grpc_wakeup_signal);
80
- }
81
- }
82
-
83
- struct polling_island;
84
-
85
- typedef enum {
86
- POLL_OBJ_FD,
87
- POLL_OBJ_POLLSET,
88
- POLL_OBJ_POLLSET_SET
89
- } poll_obj_type;
90
-
91
- typedef struct poll_obj {
92
- #ifndef NDEBUG
93
- poll_obj_type obj_type;
94
- #endif
95
- gpr_mu mu;
96
- struct polling_island *pi;
97
- } poll_obj;
98
-
99
- static const char *poll_obj_string(poll_obj_type po_type) {
100
- switch (po_type) {
101
- case POLL_OBJ_FD:
102
- return "fd";
103
- case POLL_OBJ_POLLSET:
104
- return "pollset";
105
- case POLL_OBJ_POLLSET_SET:
106
- return "pollset_set";
107
- }
108
-
109
- GPR_UNREACHABLE_CODE(return "UNKNOWN");
110
- }
111
-
112
- /*******************************************************************************
113
- * Fd Declarations
114
- */
115
-
116
- #define FD_FROM_PO(po) ((grpc_fd *)(po))
117
-
118
- struct grpc_fd {
119
- poll_obj po;
120
-
121
- int fd;
122
- /* refst format:
123
- bit 0 : 1=Active / 0=Orphaned
124
- bits 1-n : refcount
125
- Ref/Unref by two to avoid altering the orphaned bit */
126
- gpr_atm refst;
127
-
128
- /* The fd is either closed or we relinquished control of it. In either
129
- cases, this indicates that the 'fd' on this structure is no longer
130
- valid */
131
- bool orphaned;
132
-
133
- gpr_atm read_closure;
134
- gpr_atm write_closure;
135
-
136
- struct grpc_fd *freelist_next;
137
- grpc_closure *on_done_closure;
138
-
139
- /* The pollset that last noticed that the fd is readable. The actual type
140
- * stored in this is (grpc_pollset *) */
141
- gpr_atm read_notifier_pollset;
142
-
143
- grpc_iomgr_object iomgr_object;
144
- };
145
-
146
- /* Reference counting for fds */
147
- #ifndef NDEBUG
148
- static void fd_ref(grpc_fd *fd, const char *reason, const char *file, int line);
149
- static void fd_unref(grpc_fd *fd, const char *reason, const char *file,
150
- int line);
151
- #define GRPC_FD_REF(fd, reason) fd_ref(fd, reason, __FILE__, __LINE__)
152
- #define GRPC_FD_UNREF(fd, reason) fd_unref(fd, reason, __FILE__, __LINE__)
153
- #else
154
- static void fd_ref(grpc_fd *fd);
155
- static void fd_unref(grpc_fd *fd);
156
- #define GRPC_FD_REF(fd, reason) fd_ref(fd)
157
- #define GRPC_FD_UNREF(fd, reason) fd_unref(fd)
158
- #endif
159
-
160
- static void fd_global_init(void);
161
- static void fd_global_shutdown(void);
162
-
163
- /*******************************************************************************
164
- * Polling island Declarations
165
- */
166
-
167
- #ifndef NDEBUG
168
-
169
- #define PI_ADD_REF(p, r) pi_add_ref_dbg((p), (r), __FILE__, __LINE__)
170
- #define PI_UNREF(exec_ctx, p, r) \
171
- pi_unref_dbg((exec_ctx), (p), (r), __FILE__, __LINE__)
172
-
173
- #else
174
-
175
- #define PI_ADD_REF(p, r) pi_add_ref((p))
176
- #define PI_UNREF(exec_ctx, p, r) pi_unref((exec_ctx), (p))
177
-
178
- #endif
179
-
180
- typedef struct worker_node {
181
- struct worker_node *next;
182
- struct worker_node *prev;
183
- } worker_node;
184
-
185
- /* This is also used as grpc_workqueue (by directly casing it) */
186
- typedef struct polling_island {
187
- gpr_mu mu;
188
- /* Ref count. Use PI_ADD_REF() and PI_UNREF() macros to increment/decrement
189
- the refcount.
190
- Once the ref count becomes zero, this structure is destroyed which means
191
- we should ensure that there is never a scenario where a PI_ADD_REF() is
192
- racing with a PI_UNREF() that just made the ref_count zero. */
193
- gpr_atm ref_count;
194
-
195
- /* Pointer to the polling_island this merged into.
196
- * merged_to value is only set once in polling_island's lifetime (and that too
197
- * only if the island is merged with another island). Because of this, we can
198
- * use gpr_atm type here so that we can do atomic access on this and reduce
199
- * lock contention on 'mu' mutex.
200
- *
201
- * Note that if this field is not NULL (i.e not 0), all the remaining fields
202
- * (except mu and ref_count) are invalid and must be ignored. */
203
- gpr_atm merged_to;
204
-
205
- /* Number of threads currently polling on this island */
206
- gpr_atm poller_count;
207
-
208
- /* The list of workers waiting to do polling on this polling island */
209
- gpr_mu worker_list_mu;
210
- worker_node worker_list_head;
211
-
212
- /* The fd of the underlying epoll set */
213
- int epoll_fd;
214
-
215
- /* The file descriptors in the epoll set */
216
- size_t fd_cnt;
217
- size_t fd_capacity;
218
- grpc_fd **fds;
219
- } polling_island;
220
-
221
- /*******************************************************************************
222
- * Pollset Declarations
223
- */
224
- #define WORKER_FROM_WORKER_LIST_NODE(p) \
225
- (struct grpc_pollset_worker *)(((char *)(p)) - \
226
- offsetof(grpc_pollset_worker, pi_list_link))
227
- struct grpc_pollset_worker {
228
- /* Thread id of this worker */
229
- pthread_t pt_id;
230
-
231
- /* Used to prevent a worker from getting kicked multiple times */
232
- gpr_atm is_kicked;
233
-
234
- struct grpc_pollset_worker *next;
235
- struct grpc_pollset_worker *prev;
236
-
237
- /* Indicates if it is this worker's turn to do epoll */
238
- gpr_atm is_polling_turn;
239
-
240
- /* Node in the polling island's worker list. */
241
- worker_node pi_list_link;
242
- };
243
-
244
- struct grpc_pollset {
245
- poll_obj po;
246
-
247
- grpc_pollset_worker root_worker;
248
- bool kicked_without_pollers;
249
-
250
- bool shutting_down; /* Is the pollset shutting down ? */
251
- bool finish_shutdown_called; /* Is the 'finish_shutdown_locked()' called ? */
252
- grpc_closure *shutdown_done; /* Called after after shutdown is complete */
253
- };
254
-
255
- /*******************************************************************************
256
- * Pollset-set Declarations
257
- */
258
- struct grpc_pollset_set {
259
- poll_obj po;
260
- };
261
-
262
- /*******************************************************************************
263
- * Common helpers
264
- */
265
-
266
- static bool append_error(grpc_error **composite, grpc_error *error,
267
- const char *desc) {
268
- if (error == GRPC_ERROR_NONE) return true;
269
- if (*composite == GRPC_ERROR_NONE) {
270
- *composite = GRPC_ERROR_CREATE_FROM_COPIED_STRING(desc);
271
- }
272
- *composite = grpc_error_add_child(*composite, error);
273
- return false;
274
- }
275
-
276
- /*******************************************************************************
277
- * Polling island Definitions
278
- */
279
-
280
- /* The wakeup fd that is used to wake up all threads in a Polling island. This
281
- is useful in the polling island merge operation where we need to wakeup all
282
- the threads currently polling the smaller polling island (so that they can
283
- start polling the new/merged polling island)
284
-
285
- NOTE: This fd is initialized to be readable and MUST NOT be consumed i.e the
286
- threads that woke up MUST NOT call grpc_wakeup_fd_consume_wakeup() */
287
- static grpc_wakeup_fd polling_island_wakeup_fd;
288
-
289
- /* The polling island being polled right now.
290
- See comments in workqueue_maybe_wakeup for why this is tracked. */
291
- static __thread polling_island *g_current_thread_polling_island;
292
-
293
- /* Forward declaration */
294
- static void polling_island_delete(grpc_exec_ctx *exec_ctx, polling_island *pi);
295
-
296
- #ifdef GRPC_TSAN
297
- /* Currently TSAN may incorrectly flag data races between epoll_ctl and
298
- epoll_wait for any grpc_fd structs that are added to the epoll set via
299
- epoll_ctl and are returned (within a very short window) via epoll_wait().
300
-
301
- To work-around this race, we establish a happens-before relation between
302
- the code just-before epoll_ctl() and the code after epoll_wait() by using
303
- this atomic */
304
- gpr_atm g_epoll_sync;
305
- #endif /* defined(GRPC_TSAN) */
306
-
307
- static void pi_add_ref(polling_island *pi);
308
- static void pi_unref(grpc_exec_ctx *exec_ctx, polling_island *pi);
309
-
310
- #ifndef NDEBUG
311
- static void pi_add_ref_dbg(polling_island *pi, const char *reason,
312
- const char *file, int line) {
313
- if (GRPC_TRACER_ON(grpc_polling_trace)) {
314
- gpr_atm old_cnt = gpr_atm_acq_load(&pi->ref_count);
315
- gpr_log(GPR_DEBUG, "Add ref pi: %p, old:%" PRIdPTR " -> new:%" PRIdPTR
316
- " (%s) - (%s, %d)",
317
- pi, old_cnt, old_cnt + 1, reason, file, line);
318
- }
319
- pi_add_ref(pi);
320
- }
321
-
322
- static void pi_unref_dbg(grpc_exec_ctx *exec_ctx, polling_island *pi,
323
- const char *reason, const char *file, int line) {
324
- if (GRPC_TRACER_ON(grpc_polling_trace)) {
325
- gpr_atm old_cnt = gpr_atm_acq_load(&pi->ref_count);
326
- gpr_log(GPR_DEBUG, "Unref pi: %p, old:%" PRIdPTR " -> new:%" PRIdPTR
327
- " (%s) - (%s, %d)",
328
- pi, old_cnt, (old_cnt - 1), reason, file, line);
329
- }
330
- pi_unref(exec_ctx, pi);
331
- }
332
- #endif
333
-
334
- static void pi_add_ref(polling_island *pi) {
335
- gpr_atm_no_barrier_fetch_add(&pi->ref_count, 1);
336
- }
337
-
338
- static void pi_unref(grpc_exec_ctx *exec_ctx, polling_island *pi) {
339
- /* If ref count went to zero, delete the polling island.
340
- Note that this deletion not be done under a lock. Once the ref count goes
341
- to zero, we are guaranteed that no one else holds a reference to the
342
- polling island (and that there is no racing pi_add_ref() call either).
343
-
344
- Also, if we are deleting the polling island and the merged_to field is
345
- non-empty, we should remove a ref to the merged_to polling island
346
- */
347
- if (1 == gpr_atm_full_fetch_add(&pi->ref_count, -1)) {
348
- polling_island *next = (polling_island *)gpr_atm_acq_load(&pi->merged_to);
349
- polling_island_delete(exec_ctx, pi);
350
- if (next != NULL) {
351
- PI_UNREF(exec_ctx, next, "pi_delete"); /* Recursive call */
352
- }
353
- }
354
- }
355
-
356
- static void worker_node_init(worker_node *node) {
357
- node->next = node->prev = node;
358
- }
359
-
360
- /* Not thread safe. Do under a list-level lock */
361
- static void push_back_worker_node(worker_node *head, worker_node *node) {
362
- node->next = head;
363
- node->prev = head->prev;
364
- head->prev->next = node;
365
- head->prev = node;
366
- }
367
-
368
- /* Not thread safe. Do under a list-level lock */
369
- static void remove_worker_node(worker_node *node) {
370
- node->next->prev = node->prev;
371
- node->prev->next = node->next;
372
- /* If node's next and prev point to itself, the node is considered detached
373
- * from the list*/
374
- node->next = node->prev = node;
375
- }
376
-
377
- /* Not thread safe. Do under a list-level lock */
378
- static worker_node *pop_front_worker_node(worker_node *head) {
379
- worker_node *node = head->next;
380
- if (node != head) {
381
- remove_worker_node(node);
382
- } else {
383
- node = NULL;
384
- }
385
-
386
- return node;
387
- }
388
-
389
- /* Returns true if the node's next and prev are pointing to itself (which
390
- indicates that the node is not in the list */
391
- static bool is_worker_node_detached(worker_node *node) {
392
- return (node->next == node->prev && node->next == node);
393
- }
394
-
395
- /* The caller is expected to hold pi->mu lock before calling this function
396
- */
397
- static void polling_island_add_fds_locked(polling_island *pi, grpc_fd **fds,
398
- size_t fd_count, bool add_fd_refs,
399
- grpc_error **error) {
400
- int err;
401
- size_t i;
402
- struct epoll_event ev;
403
- char *err_msg;
404
- const char *err_desc = "polling_island_add_fds";
405
-
406
- #ifdef GRPC_TSAN
407
- /* See the definition of g_epoll_sync for more context */
408
- gpr_atm_rel_store(&g_epoll_sync, (gpr_atm)0);
409
- #endif /* defined(GRPC_TSAN) */
410
-
411
- for (i = 0; i < fd_count; i++) {
412
- ev.events = (uint32_t)(EPOLLIN | EPOLLOUT | EPOLLET);
413
- ev.data.ptr = fds[i];
414
- err = epoll_ctl(pi->epoll_fd, EPOLL_CTL_ADD, fds[i]->fd, &ev);
415
-
416
- if (err < 0) {
417
- if (errno != EEXIST) {
418
- gpr_asprintf(
419
- &err_msg,
420
- "epoll_ctl (epoll_fd: %d) add fd: %d failed with error: %d (%s)",
421
- pi->epoll_fd, fds[i]->fd, errno, strerror(errno));
422
- append_error(error, GRPC_OS_ERROR(errno, err_msg), err_desc);
423
- gpr_free(err_msg);
424
- }
425
-
426
- continue;
427
- }
428
-
429
- if (pi->fd_cnt == pi->fd_capacity) {
430
- pi->fd_capacity = GPR_MAX(pi->fd_capacity + 8, pi->fd_cnt * 3 / 2);
431
- pi->fds = gpr_realloc(pi->fds, sizeof(grpc_fd *) * pi->fd_capacity);
432
- }
433
-
434
- pi->fds[pi->fd_cnt++] = fds[i];
435
- if (add_fd_refs) {
436
- GRPC_FD_REF(fds[i], "polling_island");
437
- }
438
- }
439
- }
440
-
441
- /* The caller is expected to hold pi->mu before calling this */
442
- static void polling_island_add_wakeup_fd_locked(polling_island *pi,
443
- grpc_wakeup_fd *wakeup_fd,
444
- grpc_error **error) {
445
- struct epoll_event ev;
446
- int err;
447
- char *err_msg;
448
- const char *err_desc = "polling_island_add_wakeup_fd";
449
-
450
- ev.events = (uint32_t)(EPOLLIN | EPOLLET);
451
- ev.data.ptr = wakeup_fd;
452
- err = epoll_ctl(pi->epoll_fd, EPOLL_CTL_ADD,
453
- GRPC_WAKEUP_FD_GET_READ_FD(wakeup_fd), &ev);
454
- if (err < 0 && errno != EEXIST) {
455
- gpr_asprintf(&err_msg,
456
- "epoll_ctl (epoll_fd: %d) add wakeup fd: %d failed with "
457
- "error: %d (%s)",
458
- pi->epoll_fd, GRPC_WAKEUP_FD_GET_READ_FD(wakeup_fd), errno,
459
- strerror(errno));
460
- append_error(error, GRPC_OS_ERROR(errno, err_msg), err_desc);
461
- gpr_free(err_msg);
462
- }
463
- }
464
-
465
- /* The caller is expected to hold pi->mu lock before calling this function */
466
- static void polling_island_remove_all_fds_locked(polling_island *pi,
467
- bool remove_fd_refs,
468
- grpc_error **error) {
469
- int err;
470
- size_t i;
471
- char *err_msg;
472
- const char *err_desc = "polling_island_remove_fds";
473
-
474
- for (i = 0; i < pi->fd_cnt; i++) {
475
- err = epoll_ctl(pi->epoll_fd, EPOLL_CTL_DEL, pi->fds[i]->fd, NULL);
476
- if (err < 0 && errno != ENOENT) {
477
- gpr_asprintf(&err_msg,
478
- "epoll_ctl (epoll_fd: %d) delete fds[%zu]: %d failed with "
479
- "error: %d (%s)",
480
- pi->epoll_fd, i, pi->fds[i]->fd, errno, strerror(errno));
481
- append_error(error, GRPC_OS_ERROR(errno, err_msg), err_desc);
482
- gpr_free(err_msg);
483
- }
484
-
485
- if (remove_fd_refs) {
486
- GRPC_FD_UNREF(pi->fds[i], "polling_island");
487
- }
488
- }
489
-
490
- pi->fd_cnt = 0;
491
- }
492
-
493
- /* The caller is expected to hold pi->mu lock before calling this function */
494
- static void polling_island_remove_fd_locked(polling_island *pi, grpc_fd *fd,
495
- bool is_fd_closed,
496
- grpc_error **error) {
497
- int err;
498
- size_t i;
499
- char *err_msg;
500
- const char *err_desc = "polling_island_remove_fd";
501
-
502
- /* If fd is already closed, then it would have been automatically been removed
503
- from the epoll set */
504
- if (!is_fd_closed) {
505
- err = epoll_ctl(pi->epoll_fd, EPOLL_CTL_DEL, fd->fd, NULL);
506
- if (err < 0 && errno != ENOENT) {
507
- gpr_asprintf(
508
- &err_msg,
509
- "epoll_ctl (epoll_fd: %d) del fd: %d failed with error: %d (%s)",
510
- pi->epoll_fd, fd->fd, errno, strerror(errno));
511
- append_error(error, GRPC_OS_ERROR(errno, err_msg), err_desc);
512
- gpr_free(err_msg);
513
- }
514
- }
515
-
516
- for (i = 0; i < pi->fd_cnt; i++) {
517
- if (pi->fds[i] == fd) {
518
- pi->fds[i] = pi->fds[--pi->fd_cnt];
519
- GRPC_FD_UNREF(fd, "polling_island");
520
- break;
521
- }
522
- }
523
- }
524
-
525
- /* Might return NULL in case of an error */
526
- static polling_island *polling_island_create(grpc_exec_ctx *exec_ctx,
527
- grpc_fd *initial_fd,
528
- grpc_error **error) {
529
- polling_island *pi = NULL;
530
- const char *err_desc = "polling_island_create";
531
-
532
- *error = GRPC_ERROR_NONE;
533
-
534
- pi = gpr_malloc(sizeof(*pi));
535
- gpr_mu_init(&pi->mu);
536
- pi->fd_cnt = 0;
537
- pi->fd_capacity = 0;
538
- pi->fds = NULL;
539
- pi->epoll_fd = -1;
540
-
541
- gpr_atm_rel_store(&pi->ref_count, 0);
542
- gpr_atm_rel_store(&pi->poller_count, 0);
543
- gpr_atm_rel_store(&pi->merged_to, (gpr_atm)NULL);
544
-
545
- gpr_mu_init(&pi->worker_list_mu);
546
- worker_node_init(&pi->worker_list_head);
547
-
548
- pi->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
549
-
550
- if (pi->epoll_fd < 0) {
551
- append_error(error, GRPC_OS_ERROR(errno, "epoll_create1"), err_desc);
552
- goto done;
553
- }
554
-
555
- if (initial_fd != NULL) {
556
- polling_island_add_fds_locked(pi, &initial_fd, 1, true, error);
557
- }
558
-
559
- done:
560
- if (*error != GRPC_ERROR_NONE) {
561
- polling_island_delete(exec_ctx, pi);
562
- pi = NULL;
563
- }
564
- return pi;
565
- }
566
-
567
- static void polling_island_delete(grpc_exec_ctx *exec_ctx, polling_island *pi) {
568
- GPR_ASSERT(pi->fd_cnt == 0);
569
-
570
- if (pi->epoll_fd >= 0) {
571
- close(pi->epoll_fd);
572
- }
573
- gpr_mu_destroy(&pi->mu);
574
- gpr_mu_destroy(&pi->worker_list_mu);
575
- GPR_ASSERT(is_worker_node_detached(&pi->worker_list_head));
576
-
577
- gpr_free(pi->fds);
578
- gpr_free(pi);
579
- }
580
-
581
- /* Attempts to gets the last polling island in the linked list (liked by the
582
- * 'merged_to' field). Since this does not lock the polling island, there are no
583
- * guarantees that the island returned is the last island */
584
- static polling_island *polling_island_maybe_get_latest(polling_island *pi) {
585
- polling_island *next = (polling_island *)gpr_atm_acq_load(&pi->merged_to);
586
- while (next != NULL) {
587
- pi = next;
588
- next = (polling_island *)gpr_atm_acq_load(&pi->merged_to);
589
- }
590
-
591
- return pi;
592
- }
593
-
594
- /* Gets the lock on the *latest* polling island i.e the last polling island in
595
- the linked list (linked by the 'merged_to' field). Call gpr_mu_unlock on the
596
- returned polling island's mu.
597
- Usage: To lock/unlock polling island "pi", do the following:
598
- polling_island *pi_latest = polling_island_lock(pi);
599
- ...
600
- ... critical section ..
601
- ...
602
- gpr_mu_unlock(&pi_latest->mu); // NOTE: use pi_latest->mu. NOT pi->mu */
603
- static polling_island *polling_island_lock(polling_island *pi) {
604
- polling_island *next = NULL;
605
-
606
- while (true) {
607
- next = (polling_island *)gpr_atm_acq_load(&pi->merged_to);
608
- if (next == NULL) {
609
- /* Looks like 'pi' is the last node in the linked list but unless we check
610
- this by holding the pi->mu lock, we cannot be sure (i.e without the
611
- pi->mu lock, we don't prevent island merges).
612
- To be absolutely sure, check once more by holding the pi->mu lock */
613
- gpr_mu_lock(&pi->mu);
614
- next = (polling_island *)gpr_atm_acq_load(&pi->merged_to);
615
- if (next == NULL) {
616
- /* pi is infact the last node and we have the pi->mu lock. we're done */
617
- break;
618
- }
619
-
620
- /* pi->merged_to is not NULL i.e pi isn't the last node anymore. pi->mu
621
- * isn't the lock we are interested in. Continue traversing the list */
622
- gpr_mu_unlock(&pi->mu);
623
- }
624
-
625
- pi = next;
626
- }
627
-
628
- return pi;
629
- }
630
-
631
- /* Gets the lock on the *latest* polling islands in the linked lists pointed by
632
- *p and *q (and also updates *p and *q to point to the latest polling islands)
633
-
634
- This function is needed because calling the following block of code to obtain
635
- locks on polling islands (*p and *q) is prone to deadlocks.
636
- {
637
- polling_island_lock(*p, true);
638
- polling_island_lock(*q, true);
639
- }
640
-
641
- Usage/example:
642
- polling_island *p1;
643
- polling_island *p2;
644
- ..
645
- polling_island_lock_pair(&p1, &p2);
646
- ..
647
- .. Critical section with both p1 and p2 locked
648
- ..
649
- // Release locks: Always call polling_island_unlock_pair() to release locks
650
- polling_island_unlock_pair(p1, p2);
651
- */
652
- static void polling_island_lock_pair(polling_island **p, polling_island **q) {
653
- polling_island *pi_1 = *p;
654
- polling_island *pi_2 = *q;
655
- polling_island *next_1 = NULL;
656
- polling_island *next_2 = NULL;
657
-
658
- /* The algorithm is simple:
659
- - Go to the last polling islands in the linked lists *pi_1 and *pi_2 (and
660
- keep updating pi_1 and pi_2)
661
- - Then obtain locks on the islands by following a lock order rule of
662
- locking polling_island with lower address first
663
- Special case: Before obtaining the locks, check if pi_1 and pi_2 are
664
- pointing to the same island. If that is the case, we can just call
665
- polling_island_lock()
666
- - After obtaining both the locks, double check that the polling islands
667
- are still the last polling islands in their respective linked lists
668
- (this is because there might have been polling island merges before
669
- we got the lock)
670
- - If the polling islands are the last islands, we are done. If not,
671
- release the locks and continue the process from the first step */
672
- while (true) {
673
- next_1 = (polling_island *)gpr_atm_acq_load(&pi_1->merged_to);
674
- while (next_1 != NULL) {
675
- pi_1 = next_1;
676
- next_1 = (polling_island *)gpr_atm_acq_load(&pi_1->merged_to);
677
- }
678
-
679
- next_2 = (polling_island *)gpr_atm_acq_load(&pi_2->merged_to);
680
- while (next_2 != NULL) {
681
- pi_2 = next_2;
682
- next_2 = (polling_island *)gpr_atm_acq_load(&pi_2->merged_to);
683
- }
684
-
685
- if (pi_1 == pi_2) {
686
- pi_1 = pi_2 = polling_island_lock(pi_1);
687
- break;
688
- }
689
-
690
- if (pi_1 < pi_2) {
691
- gpr_mu_lock(&pi_1->mu);
692
- gpr_mu_lock(&pi_2->mu);
693
- } else {
694
- gpr_mu_lock(&pi_2->mu);
695
- gpr_mu_lock(&pi_1->mu);
696
- }
697
-
698
- next_1 = (polling_island *)gpr_atm_acq_load(&pi_1->merged_to);
699
- next_2 = (polling_island *)gpr_atm_acq_load(&pi_2->merged_to);
700
- if (next_1 == NULL && next_2 == NULL) {
701
- break;
702
- }
703
-
704
- gpr_mu_unlock(&pi_1->mu);
705
- gpr_mu_unlock(&pi_2->mu);
706
- }
707
-
708
- *p = pi_1;
709
- *q = pi_2;
710
- }
711
-
712
- static void polling_island_unlock_pair(polling_island *p, polling_island *q) {
713
- if (p == q) {
714
- gpr_mu_unlock(&p->mu);
715
- } else {
716
- gpr_mu_unlock(&p->mu);
717
- gpr_mu_unlock(&q->mu);
718
- }
719
- }
720
-
721
- static polling_island *polling_island_merge(polling_island *p,
722
- polling_island *q,
723
- grpc_error **error) {
724
- /* Get locks on both the polling islands */
725
- polling_island_lock_pair(&p, &q);
726
-
727
- if (p != q) {
728
- /* Make sure that p points to the polling island with fewer fds than q */
729
- if (p->fd_cnt > q->fd_cnt) {
730
- GPR_SWAP(polling_island *, p, q);
731
- }
732
-
733
- /* Merge p with q i.e move all the fds from p (The one with fewer fds) to q
734
- Note that the refcounts on the fds being moved will not change here.
735
- This is why the last param in the following two functions is 'false') */
736
- polling_island_add_fds_locked(q, p->fds, p->fd_cnt, false, error);
737
- polling_island_remove_all_fds_locked(p, false, error);
738
-
739
- /* Wakeup all the pollers (if any) on p so that they pickup this change */
740
- polling_island_add_wakeup_fd_locked(p, &polling_island_wakeup_fd, error);
741
-
742
- /* Add the 'merged_to' link from p --> q */
743
- gpr_atm_rel_store(&p->merged_to, (gpr_atm)q);
744
- PI_ADD_REF(q, "pi_merge"); /* To account for the new incoming ref from p */
745
- }
746
- /* else if p == q, nothing needs to be done */
747
-
748
- polling_island_unlock_pair(p, q);
749
-
750
- /* Return the merged polling island (Note that no merge would have happened
751
- if p == q which is ok) */
752
- return q;
753
- }
754
-
755
- static grpc_error *polling_island_global_init() {
756
- grpc_error *error = GRPC_ERROR_NONE;
757
-
758
- error = grpc_wakeup_fd_init(&polling_island_wakeup_fd);
759
- if (error == GRPC_ERROR_NONE) {
760
- error = grpc_wakeup_fd_wakeup(&polling_island_wakeup_fd);
761
- }
762
-
763
- return error;
764
- }
765
-
766
- static void polling_island_global_shutdown() {
767
- grpc_wakeup_fd_destroy(&polling_island_wakeup_fd);
768
- }
769
-
770
- /*******************************************************************************
771
- * Fd Definitions
772
- */
773
-
774
- /* We need to keep a freelist not because of any concerns of malloc performance
775
- * but instead so that implementations with multiple threads in (for example)
776
- * epoll_wait deal with the race between pollset removal and incoming poll
777
- * notifications.
778
- *
779
- * The problem is that the poller ultimately holds a reference to this
780
- * object, so it is very difficult to know when is safe to free it, at least
781
- * without some expensive synchronization.
782
- *
783
- * If we keep the object freelisted, in the worst case losing this race just
784
- * becomes a spurious read notification on a reused fd.
785
- */
786
-
787
- /* The alarm system needs to be able to wakeup 'some poller' sometimes
788
- * (specifically when a new alarm needs to be triggered earlier than the next
789
- * alarm 'epoch'). This wakeup_fd gives us something to alert on when such a
790
- * case occurs. */
791
-
792
- static grpc_fd *fd_freelist = NULL;
793
- static gpr_mu fd_freelist_mu;
794
-
795
- #ifndef NDEBUG
796
- #define REF_BY(fd, n, reason) ref_by(fd, n, reason, __FILE__, __LINE__)
797
- #define UNREF_BY(fd, n, reason) unref_by(fd, n, reason, __FILE__, __LINE__)
798
- static void ref_by(grpc_fd *fd, int n, const char *reason, const char *file,
799
- int line) {
800
- if (GRPC_TRACER_ON(grpc_trace_fd_refcount)) {
801
- gpr_log(GPR_DEBUG,
802
- "FD %d %p ref %d %" PRIdPTR " -> %" PRIdPTR " [%s; %s:%d]",
803
- fd->fd, fd, n, gpr_atm_no_barrier_load(&fd->refst),
804
- gpr_atm_no_barrier_load(&fd->refst) + n, reason, file, line);
805
- }
806
- #else
807
- #define REF_BY(fd, n, reason) ref_by(fd, n)
808
- #define UNREF_BY(fd, n, reason) unref_by(fd, n)
809
- static void ref_by(grpc_fd *fd, int n) {
810
- #endif
811
- GPR_ASSERT(gpr_atm_no_barrier_fetch_add(&fd->refst, n) > 0);
812
- }
813
-
814
- #ifndef NDEBUG
815
- static void unref_by(grpc_fd *fd, int n, const char *reason, const char *file,
816
- int line) {
817
- if (GRPC_TRACER_ON(grpc_trace_fd_refcount)) {
818
- gpr_log(GPR_DEBUG,
819
- "FD %d %p unref %d %" PRIdPTR " -> %" PRIdPTR " [%s; %s:%d]",
820
- fd->fd, fd, n, gpr_atm_no_barrier_load(&fd->refst),
821
- gpr_atm_no_barrier_load(&fd->refst) - n, reason, file, line);
822
- }
823
- #else
824
- static void unref_by(grpc_fd *fd, int n) {
825
- #endif
826
- gpr_atm old = gpr_atm_full_fetch_add(&fd->refst, -n);
827
- if (old == n) {
828
- /* Add the fd to the freelist */
829
- gpr_mu_lock(&fd_freelist_mu);
830
- fd->freelist_next = fd_freelist;
831
- fd_freelist = fd;
832
- grpc_iomgr_unregister_object(&fd->iomgr_object);
833
-
834
- grpc_lfev_destroy(&fd->read_closure);
835
- grpc_lfev_destroy(&fd->write_closure);
836
-
837
- gpr_mu_unlock(&fd_freelist_mu);
838
- } else {
839
- GPR_ASSERT(old > n);
840
- }
841
- }
842
-
843
- /* Increment refcount by two to avoid changing the orphan bit */
844
- #ifndef NDEBUG
845
- static void fd_ref(grpc_fd *fd, const char *reason, const char *file,
846
- int line) {
847
- ref_by(fd, 2, reason, file, line);
848
- }
849
-
850
- static void fd_unref(grpc_fd *fd, const char *reason, const char *file,
851
- int line) {
852
- unref_by(fd, 2, reason, file, line);
853
- }
854
- #else
855
- static void fd_ref(grpc_fd *fd) { ref_by(fd, 2); }
856
- static void fd_unref(grpc_fd *fd) { unref_by(fd, 2); }
857
- #endif
858
-
859
- static void fd_global_init(void) { gpr_mu_init(&fd_freelist_mu); }
860
-
861
- static void fd_global_shutdown(void) {
862
- gpr_mu_lock(&fd_freelist_mu);
863
- gpr_mu_unlock(&fd_freelist_mu);
864
- while (fd_freelist != NULL) {
865
- grpc_fd *fd = fd_freelist;
866
- fd_freelist = fd_freelist->freelist_next;
867
- gpr_mu_destroy(&fd->po.mu);
868
- gpr_free(fd);
869
- }
870
- gpr_mu_destroy(&fd_freelist_mu);
871
- }
872
-
873
- static grpc_fd *fd_create(int fd, const char *name) {
874
- grpc_fd *new_fd = NULL;
875
-
876
- gpr_mu_lock(&fd_freelist_mu);
877
- if (fd_freelist != NULL) {
878
- new_fd = fd_freelist;
879
- fd_freelist = fd_freelist->freelist_next;
880
- }
881
- gpr_mu_unlock(&fd_freelist_mu);
882
-
883
- if (new_fd == NULL) {
884
- new_fd = gpr_malloc(sizeof(grpc_fd));
885
- gpr_mu_init(&new_fd->po.mu);
886
- }
887
-
888
- /* Note: It is not really needed to get the new_fd->po.mu lock here. If this
889
- * is a newly created fd (or an fd we got from the freelist), no one else
890
- * would be holding a lock to it anyway. */
891
- gpr_mu_lock(&new_fd->po.mu);
892
- new_fd->po.pi = NULL;
893
- #ifndef NDEBUG
894
- new_fd->po.obj_type = POLL_OBJ_FD;
895
- #endif
896
-
897
- gpr_atm_rel_store(&new_fd->refst, (gpr_atm)1);
898
- new_fd->fd = fd;
899
- new_fd->orphaned = false;
900
- grpc_lfev_init(&new_fd->read_closure);
901
- grpc_lfev_init(&new_fd->write_closure);
902
- gpr_atm_no_barrier_store(&new_fd->read_notifier_pollset, (gpr_atm)NULL);
903
-
904
- new_fd->freelist_next = NULL;
905
- new_fd->on_done_closure = NULL;
906
-
907
- gpr_mu_unlock(&new_fd->po.mu);
908
-
909
- char *fd_name;
910
- gpr_asprintf(&fd_name, "%s fd=%d", name, fd);
911
- grpc_iomgr_register_object(&new_fd->iomgr_object, fd_name);
912
- #ifndef NDEBUG
913
- if (GRPC_TRACER_ON(grpc_trace_fd_refcount)) {
914
- gpr_log(GPR_DEBUG, "FD %d %p create %s", fd, new_fd, fd_name);
915
- }
916
- #endif
917
- gpr_free(fd_name);
918
- return new_fd;
919
- }
920
-
921
- static int fd_wrapped_fd(grpc_fd *fd) {
922
- int ret_fd = -1;
923
- gpr_mu_lock(&fd->po.mu);
924
- if (!fd->orphaned) {
925
- ret_fd = fd->fd;
926
- }
927
- gpr_mu_unlock(&fd->po.mu);
928
-
929
- return ret_fd;
930
- }
931
-
932
- static void fd_orphan(grpc_exec_ctx *exec_ctx, grpc_fd *fd,
933
- grpc_closure *on_done, int *release_fd,
934
- bool already_closed, const char *reason) {
935
- grpc_error *error = GRPC_ERROR_NONE;
936
- polling_island *unref_pi = NULL;
937
-
938
- gpr_mu_lock(&fd->po.mu);
939
- fd->on_done_closure = on_done;
940
-
941
- /* Remove the active status but keep referenced. We want this grpc_fd struct
942
- to be alive (and not added to freelist) until the end of this function */
943
- REF_BY(fd, 1, reason);
944
-
945
- /* Remove the fd from the polling island:
946
- - Get a lock on the latest polling island (i.e the last island in the
947
- linked list pointed by fd->po.pi). This is the island that
948
- would actually contain the fd
949
- - Remove the fd from the latest polling island
950
- - Unlock the latest polling island
951
- - Set fd->po.pi to NULL (but remove the ref on the polling island
952
- before doing this.) */
953
- if (fd->po.pi != NULL) {
954
- polling_island *pi_latest = polling_island_lock(fd->po.pi);
955
- polling_island_remove_fd_locked(pi_latest, fd, already_closed, &error);
956
- gpr_mu_unlock(&pi_latest->mu);
957
-
958
- unref_pi = fd->po.pi;
959
- fd->po.pi = NULL;
960
- }
961
-
962
- /* If release_fd is not NULL, we should be relinquishing control of the file
963
- descriptor fd->fd (but we still own the grpc_fd structure). */
964
- if (release_fd != NULL) {
965
- *release_fd = fd->fd;
966
- } else {
967
- close(fd->fd);
968
- }
969
-
970
- fd->orphaned = true;
971
-
972
- GRPC_CLOSURE_SCHED(exec_ctx, fd->on_done_closure, GRPC_ERROR_REF(error));
973
-
974
- gpr_mu_unlock(&fd->po.mu);
975
- UNREF_BY(fd, 2, reason); /* Drop the reference */
976
- if (unref_pi != NULL) {
977
- /* Unref stale polling island here, outside the fd lock above.
978
- The polling island owns a workqueue which owns an fd, and unreffing
979
- inside the lock can cause an eventual lock loop that makes TSAN very
980
- unhappy. */
981
- PI_UNREF(exec_ctx, unref_pi, "fd_orphan");
982
- }
983
- GRPC_LOG_IF_ERROR("fd_orphan", GRPC_ERROR_REF(error));
984
- GRPC_ERROR_UNREF(error);
985
- }
986
-
987
- static grpc_pollset *fd_get_read_notifier_pollset(grpc_exec_ctx *exec_ctx,
988
- grpc_fd *fd) {
989
- gpr_atm notifier = gpr_atm_acq_load(&fd->read_notifier_pollset);
990
- return (grpc_pollset *)notifier;
991
- }
992
-
993
- static bool fd_is_shutdown(grpc_fd *fd) {
994
- return grpc_lfev_is_shutdown(&fd->read_closure);
995
- }
996
-
997
- /* Might be called multiple times */
998
- static void fd_shutdown(grpc_exec_ctx *exec_ctx, grpc_fd *fd, grpc_error *why) {
999
- if (grpc_lfev_set_shutdown(exec_ctx, &fd->read_closure,
1000
- GRPC_ERROR_REF(why))) {
1001
- shutdown(fd->fd, SHUT_RDWR);
1002
- grpc_lfev_set_shutdown(exec_ctx, &fd->write_closure, GRPC_ERROR_REF(why));
1003
- }
1004
- GRPC_ERROR_UNREF(why);
1005
- }
1006
-
1007
- static void fd_notify_on_read(grpc_exec_ctx *exec_ctx, grpc_fd *fd,
1008
- grpc_closure *closure) {
1009
- grpc_lfev_notify_on(exec_ctx, &fd->read_closure, closure, "read");
1010
- }
1011
-
1012
- static void fd_notify_on_write(grpc_exec_ctx *exec_ctx, grpc_fd *fd,
1013
- grpc_closure *closure) {
1014
- grpc_lfev_notify_on(exec_ctx, &fd->write_closure, closure, "write");
1015
- }
1016
-
1017
- /*******************************************************************************
1018
- * Pollset Definitions
1019
- */
1020
- GPR_TLS_DECL(g_current_thread_pollset);
1021
- GPR_TLS_DECL(g_current_thread_worker);
1022
- static __thread bool g_initialized_sigmask;
1023
- static __thread sigset_t g_orig_sigmask;
1024
- static __thread sigset_t g_wakeup_sig_set;
1025
-
1026
- static void sig_handler(int sig_num) {
1027
- #ifdef GRPC_EPOLL_DEBUG
1028
- gpr_log(GPR_INFO, "Received signal %d", sig_num);
1029
- #endif
1030
- }
1031
-
1032
- static void pollset_worker_init(grpc_pollset_worker *worker) {
1033
- worker->pt_id = pthread_self();
1034
- worker->next = worker->prev = NULL;
1035
- gpr_atm_no_barrier_store(&worker->is_kicked, (gpr_atm)0);
1036
- gpr_atm_no_barrier_store(&worker->is_polling_turn, (gpr_atm)0);
1037
- worker_node_init(&worker->pi_list_link);
1038
- }
1039
-
1040
- static void poller_kick_init() { signal(grpc_wakeup_signal, sig_handler); }
1041
-
1042
- /* Global state management */
1043
- static grpc_error *pollset_global_init(void) {
1044
- gpr_tls_init(&g_current_thread_pollset);
1045
- gpr_tls_init(&g_current_thread_worker);
1046
- poller_kick_init();
1047
- return GRPC_ERROR_NONE;
1048
- }
1049
-
1050
- static void pollset_global_shutdown(void) {
1051
- gpr_tls_destroy(&g_current_thread_pollset);
1052
- gpr_tls_destroy(&g_current_thread_worker);
1053
- }
1054
-
1055
- static grpc_error *worker_kick(grpc_pollset_worker *worker,
1056
- gpr_atm *is_kicked) {
1057
- grpc_error *err = GRPC_ERROR_NONE;
1058
-
1059
- /* Kick the worker only if it was not already kicked */
1060
- if (gpr_atm_no_barrier_cas(is_kicked, (gpr_atm)0, (gpr_atm)1)) {
1061
- GRPC_POLLING_TRACE(
1062
- "pollset_worker_kick: Kicking worker: %p (thread id: %ld)",
1063
- (void *)worker, (long int)worker->pt_id);
1064
- int err_num = pthread_kill(worker->pt_id, grpc_wakeup_signal);
1065
- if (err_num != 0) {
1066
- err = GRPC_OS_ERROR(err_num, "pthread_kill");
1067
- }
1068
- }
1069
- return err;
1070
- }
1071
-
1072
- static grpc_error *pollset_worker_kick(grpc_pollset_worker *worker) {
1073
- return worker_kick(worker, &worker->is_kicked);
1074
- }
1075
-
1076
- static grpc_error *poller_kick(grpc_pollset_worker *worker) {
1077
- return worker_kick(worker, &worker->is_polling_turn);
1078
- }
1079
-
1080
- /* Return 1 if the pollset has active threads in pollset_work (pollset must
1081
- * be locked) */
1082
- static int pollset_has_workers(grpc_pollset *p) {
1083
- return p->root_worker.next != &p->root_worker;
1084
- }
1085
-
1086
- static void remove_worker(grpc_pollset *p, grpc_pollset_worker *worker) {
1087
- worker->prev->next = worker->next;
1088
- worker->next->prev = worker->prev;
1089
- }
1090
-
1091
- static grpc_pollset_worker *pop_front_worker(grpc_pollset *p) {
1092
- if (pollset_has_workers(p)) {
1093
- grpc_pollset_worker *w = p->root_worker.next;
1094
- remove_worker(p, w);
1095
- return w;
1096
- } else {
1097
- return NULL;
1098
- }
1099
- }
1100
-
1101
- static void push_back_worker(grpc_pollset *p, grpc_pollset_worker *worker) {
1102
- worker->next = &p->root_worker;
1103
- worker->prev = worker->next->prev;
1104
- worker->prev->next = worker->next->prev = worker;
1105
- }
1106
-
1107
- static void push_front_worker(grpc_pollset *p, grpc_pollset_worker *worker) {
1108
- worker->prev = &p->root_worker;
1109
- worker->next = worker->prev->next;
1110
- worker->prev->next = worker->next->prev = worker;
1111
- }
1112
-
1113
- /* p->mu must be held before calling this function */
1114
- static grpc_error *pollset_kick(grpc_pollset *p,
1115
- grpc_pollset_worker *specific_worker) {
1116
- GPR_TIMER_BEGIN("pollset_kick", 0);
1117
- grpc_error *error = GRPC_ERROR_NONE;
1118
- const char *err_desc = "Kick Failure";
1119
- grpc_pollset_worker *worker = specific_worker;
1120
- if (worker != NULL) {
1121
- if (worker == GRPC_POLLSET_KICK_BROADCAST) {
1122
- if (pollset_has_workers(p)) {
1123
- GPR_TIMER_BEGIN("pollset_kick.broadcast", 0);
1124
- for (worker = p->root_worker.next; worker != &p->root_worker;
1125
- worker = worker->next) {
1126
- if (gpr_tls_get(&g_current_thread_worker) != (intptr_t)worker) {
1127
- append_error(&error, pollset_worker_kick(worker), err_desc);
1128
- }
1129
- }
1130
- GPR_TIMER_END("pollset_kick.broadcast", 0);
1131
- } else {
1132
- p->kicked_without_pollers = true;
1133
- }
1134
- } else {
1135
- GPR_TIMER_MARK("kicked_specifically", 0);
1136
- if (gpr_tls_get(&g_current_thread_worker) != (intptr_t)worker) {
1137
- append_error(&error, pollset_worker_kick(worker), err_desc);
1138
- }
1139
- }
1140
- } else if (gpr_tls_get(&g_current_thread_pollset) != (intptr_t)p) {
1141
- /* Since worker == NULL, it means that we can kick "any" worker on this
1142
- pollset 'p'. If 'p' happens to be the same pollset this thread is
1143
- currently polling (i.e in pollset_work() function), then there is no need
1144
- to kick any other worker since the current thread can just absorb the
1145
- kick. This is the reason why we enter this case only when
1146
- g_current_thread_pollset is != p */
1147
-
1148
- GPR_TIMER_MARK("kick_anonymous", 0);
1149
- worker = pop_front_worker(p);
1150
- if (worker != NULL) {
1151
- GPR_TIMER_MARK("finally_kick", 0);
1152
- push_back_worker(p, worker);
1153
- append_error(&error, pollset_worker_kick(worker), err_desc);
1154
- } else {
1155
- GPR_TIMER_MARK("kicked_no_pollers", 0);
1156
- p->kicked_without_pollers = true;
1157
- }
1158
- }
1159
-
1160
- GPR_TIMER_END("pollset_kick", 0);
1161
- GRPC_LOG_IF_ERROR("pollset_kick", GRPC_ERROR_REF(error));
1162
- return error;
1163
- }
1164
-
1165
- static void pollset_init(grpc_pollset *pollset, gpr_mu **mu) {
1166
- gpr_mu_init(&pollset->po.mu);
1167
- *mu = &pollset->po.mu;
1168
- pollset->po.pi = NULL;
1169
- #ifndef NDEBUG
1170
- pollset->po.obj_type = POLL_OBJ_POLLSET;
1171
- #endif
1172
-
1173
- pollset->root_worker.next = pollset->root_worker.prev = &pollset->root_worker;
1174
- pollset->kicked_without_pollers = false;
1175
-
1176
- pollset->shutting_down = false;
1177
- pollset->finish_shutdown_called = false;
1178
- pollset->shutdown_done = NULL;
1179
- }
1180
-
1181
- /* Convert millis to timespec (clock-type is assumed to be GPR_TIMESPAN) */
1182
- static struct timespec millis_to_timespec(int millis) {
1183
- struct timespec linux_ts;
1184
- gpr_timespec gpr_ts;
1185
-
1186
- if (millis == -1) {
1187
- gpr_ts = gpr_inf_future(GPR_TIMESPAN);
1188
- } else {
1189
- gpr_ts = gpr_time_from_millis(millis, GPR_TIMESPAN);
1190
- }
1191
-
1192
- linux_ts.tv_sec = (time_t)gpr_ts.tv_sec;
1193
- linux_ts.tv_nsec = gpr_ts.tv_nsec;
1194
- return linux_ts;
1195
- }
1196
-
1197
- /* Convert a timespec to milliseconds:
1198
- - Very small or negative poll times are clamped to zero to do a non-blocking
1199
- poll (which becomes spin polling)
1200
- - Other small values are rounded up to one millisecond
1201
- - Longer than a millisecond polls are rounded up to the next nearest
1202
- millisecond to avoid spinning
1203
- - Infinite timeouts are converted to -1 */
1204
- static int poll_deadline_to_millis_timeout(gpr_timespec deadline,
1205
- gpr_timespec now) {
1206
- gpr_timespec timeout;
1207
- static const int64_t max_spin_polling_us = 10;
1208
- if (gpr_time_cmp(deadline, gpr_inf_future(deadline.clock_type)) == 0) {
1209
- return -1;
1210
- }
1211
-
1212
- if (gpr_time_cmp(deadline, gpr_time_add(now, gpr_time_from_micros(
1213
- max_spin_polling_us,
1214
- GPR_TIMESPAN))) <= 0) {
1215
- return 0;
1216
- }
1217
- timeout = gpr_time_sub(deadline, now);
1218
- int millis = gpr_time_to_millis(gpr_time_add(
1219
- timeout, gpr_time_from_nanos(GPR_NS_PER_MS - 1, GPR_TIMESPAN)));
1220
- return millis >= 1 ? millis : 1;
1221
- }
1222
-
1223
- static void fd_become_readable(grpc_exec_ctx *exec_ctx, grpc_fd *fd,
1224
- grpc_pollset *notifier) {
1225
- grpc_lfev_set_ready(exec_ctx, &fd->read_closure, "read");
1226
-
1227
- /* Note, it is possible that fd_become_readable might be called twice with
1228
- different 'notifier's when an fd becomes readable and it is in two epoll
1229
- sets (This can happen briefly during polling island merges). In such cases
1230
- it does not really matter which notifer is set as the read_notifier_pollset
1231
- (They would both point to the same polling island anyway) */
1232
- /* Use release store to match with acquire load in fd_get_read_notifier */
1233
- gpr_atm_rel_store(&fd->read_notifier_pollset, (gpr_atm)notifier);
1234
- }
1235
-
1236
- static void fd_become_writable(grpc_exec_ctx *exec_ctx, grpc_fd *fd) {
1237
- grpc_lfev_set_ready(exec_ctx, &fd->write_closure, "write");
1238
- }
1239
-
1240
- static void pollset_release_polling_island(grpc_exec_ctx *exec_ctx,
1241
- grpc_pollset *ps, char *reason) {
1242
- if (ps->po.pi != NULL) {
1243
- PI_UNREF(exec_ctx, ps->po.pi, reason);
1244
- }
1245
- ps->po.pi = NULL;
1246
- }
1247
-
1248
- static void finish_shutdown_locked(grpc_exec_ctx *exec_ctx,
1249
- grpc_pollset *pollset) {
1250
- /* The pollset cannot have any workers if we are at this stage */
1251
- GPR_ASSERT(!pollset_has_workers(pollset));
1252
-
1253
- pollset->finish_shutdown_called = true;
1254
-
1255
- /* Release the ref and set pollset->po.pi to NULL */
1256
- pollset_release_polling_island(exec_ctx, pollset, "ps_shutdown");
1257
- GRPC_CLOSURE_SCHED(exec_ctx, pollset->shutdown_done, GRPC_ERROR_NONE);
1258
- }
1259
-
1260
- /* pollset->po.mu lock must be held by the caller before calling this */
1261
- static void pollset_shutdown(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
1262
- grpc_closure *closure) {
1263
- GPR_TIMER_BEGIN("pollset_shutdown", 0);
1264
- GPR_ASSERT(!pollset->shutting_down);
1265
- pollset->shutting_down = true;
1266
- pollset->shutdown_done = closure;
1267
- pollset_kick(pollset, GRPC_POLLSET_KICK_BROADCAST);
1268
-
1269
- /* If the pollset has any workers, we cannot call finish_shutdown_locked()
1270
- because it would release the underlying polling island. In such a case, we
1271
- let the last worker call finish_shutdown_locked() from pollset_work() */
1272
- if (!pollset_has_workers(pollset)) {
1273
- GPR_ASSERT(!pollset->finish_shutdown_called);
1274
- GPR_TIMER_MARK("pollset_shutdown.finish_shutdown_locked", 0);
1275
- finish_shutdown_locked(exec_ctx, pollset);
1276
- }
1277
- GPR_TIMER_END("pollset_shutdown", 0);
1278
- }
1279
-
1280
- /* pollset_shutdown is guaranteed to be called before pollset_destroy. So other
1281
- * than destroying the mutexes, there is nothing special that needs to be done
1282
- * here */
1283
- static void pollset_destroy(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset) {
1284
- GPR_ASSERT(!pollset_has_workers(pollset));
1285
- gpr_mu_destroy(&pollset->po.mu);
1286
- }
1287
-
1288
- /* NOTE: This function may modify 'now' */
1289
- static bool acquire_polling_lease(grpc_pollset_worker *worker,
1290
- polling_island *pi, gpr_timespec deadline,
1291
- gpr_timespec *now) {
1292
- bool is_lease_acquired = false;
1293
-
1294
- gpr_mu_lock(&pi->worker_list_mu); // LOCK
1295
- long num_pollers = gpr_atm_no_barrier_load(&pi->poller_count);
1296
-
1297
- if (num_pollers >= g_max_pollers_per_pi) {
1298
- push_back_worker_node(&pi->worker_list_head, &worker->pi_list_link);
1299
- gpr_mu_unlock(&pi->worker_list_mu); // UNLOCK
1300
-
1301
- bool is_timeout = false;
1302
- int ret;
1303
- int timeout_ms = poll_deadline_to_millis_timeout(deadline, *now);
1304
- if (timeout_ms == -1) {
1305
- ret = sigwaitinfo(&g_wakeup_sig_set, NULL);
1306
- } else {
1307
- struct timespec sigwait_timeout = millis_to_timespec(timeout_ms);
1308
- GRPC_SCHEDULING_START_BLOCKING_REGION;
1309
- ret = sigtimedwait(&g_wakeup_sig_set, NULL, &sigwait_timeout);
1310
- GRPC_SCHEDULING_END_BLOCKING_REGION;
1311
- }
1312
-
1313
- if (ret == -1) {
1314
- if (errno == EAGAIN) {
1315
- is_timeout = true;
1316
- } else {
1317
- /* NOTE: This should not happen. If we see these log messages, it means
1318
- we are most likely doing something incorrect in the setup * needed
1319
- for sigwaitinfo/sigtimedwait */
1320
- gpr_log(GPR_ERROR,
1321
- "sigtimedwait failed with retcode: %d (timeout_ms: %d)", errno,
1322
- timeout_ms);
1323
- }
1324
- }
1325
-
1326
- /* Did the worker come out of sigtimedwait due to a thread that just
1327
- exited epoll and kicking it (in release_polling_lease function). */
1328
- bool is_polling_turn = gpr_atm_acq_load(&worker->is_polling_turn);
1329
-
1330
- /* Did the worker come out of sigtimedwait due to a thread alerting it that
1331
- some completion event was (likely) available in the completion queue */
1332
- bool is_kicked = gpr_atm_no_barrier_load(&worker->is_kicked);
1333
-
1334
- if (is_kicked || is_timeout) {
1335
- *now = deadline; /* Essentially make the epoll timeout = 0 */
1336
- } else if (is_polling_turn) {
1337
- *now = gpr_now(GPR_CLOCK_MONOTONIC); /* Reduce the epoll timeout */
1338
- }
1339
-
1340
- gpr_mu_lock(&pi->worker_list_mu); // LOCK
1341
- /* The node might have already been removed from the list by the poller
1342
- that kicked this. However it is safe to call 'remove_worker_node' on
1343
- an already detached node */
1344
- remove_worker_node(&worker->pi_list_link);
1345
- /* It is important to read the num_pollers again under the lock so that we
1346
- * have the latest num_pollers value that doesn't change while we are doing
1347
- * the "(num_pollers < g_max_pollers_per_pi)" a a few lines below */
1348
- num_pollers = gpr_atm_no_barrier_load(&pi->poller_count);
1349
- }
1350
-
1351
- if (num_pollers < g_max_pollers_per_pi) {
1352
- gpr_atm_no_barrier_fetch_add(&pi->poller_count, 1);
1353
- is_lease_acquired = true;
1354
- }
1355
-
1356
- gpr_mu_unlock(&pi->worker_list_mu); // UNLOCK
1357
- return is_lease_acquired;
1358
- }
1359
-
1360
- static void release_polling_lease(polling_island *pi, grpc_error **error) {
1361
- gpr_mu_lock(&pi->worker_list_mu);
1362
-
1363
- gpr_atm_no_barrier_fetch_add(&pi->poller_count, -1);
1364
- worker_node *node = pop_front_worker_node(&pi->worker_list_head);
1365
- if (node != NULL) {
1366
- grpc_pollset_worker *next_worker = WORKER_FROM_WORKER_LIST_NODE(node);
1367
- append_error(error, poller_kick(next_worker), "poller kick error");
1368
- }
1369
-
1370
- gpr_mu_unlock(&pi->worker_list_mu);
1371
- }
1372
-
1373
- #define GRPC_EPOLL_MAX_EVENTS 100
1374
- static void pollset_do_epoll_pwait(grpc_exec_ctx *exec_ctx, int epoll_fd,
1375
- grpc_pollset *pollset, polling_island *pi,
1376
- grpc_pollset_worker *worker,
1377
- gpr_timespec now, gpr_timespec deadline,
1378
- sigset_t *sig_mask, grpc_error **error) {
1379
- /* Only g_max_pollers_per_pi threads can be doing polling in parallel.
1380
- If we cannot get a lease, we cannot continue to do epoll_pwait() */
1381
- if (!acquire_polling_lease(worker, pi, deadline, &now)) {
1382
- return;
1383
- }
1384
-
1385
- struct epoll_event ep_ev[GRPC_EPOLL_MAX_EVENTS];
1386
- int ep_rv;
1387
- char *err_msg;
1388
- const char *err_desc = "pollset_work_and_unlock";
1389
-
1390
- /* timeout_ms is the time between 'now' and 'deadline' */
1391
- int timeout_ms = poll_deadline_to_millis_timeout(deadline, now);
1392
-
1393
- GRPC_SCHEDULING_START_BLOCKING_REGION;
1394
- ep_rv =
1395
- epoll_pwait(epoll_fd, ep_ev, GRPC_EPOLL_MAX_EVENTS, timeout_ms, sig_mask);
1396
- GRPC_SCHEDULING_END_BLOCKING_REGION;
1397
-
1398
- /* Give back the lease right away so that some other thread can enter */
1399
- release_polling_lease(pi, error);
1400
-
1401
- if (ep_rv < 0) {
1402
- if (errno != EINTR) {
1403
- gpr_asprintf(&err_msg,
1404
- "epoll_wait() epoll fd: %d failed with error: %d (%s)",
1405
- epoll_fd, errno, strerror(errno));
1406
- append_error(error, GRPC_OS_ERROR(errno, err_msg), err_desc);
1407
- } else {
1408
- /* We were interrupted. Save an interation by doing a zero timeout
1409
- epoll_wait to see if there are any other events of interest */
1410
- GRPC_POLLING_TRACE("pollset_work: pollset: %p, worker: %p received kick",
1411
- (void *)pollset, (void *)worker);
1412
- ep_rv = epoll_wait(epoll_fd, ep_ev, GRPC_EPOLL_MAX_EVENTS, 0);
1413
- }
1414
- }
1415
-
1416
- #ifdef GRPC_TSAN
1417
- /* See the definition of g_poll_sync for more details */
1418
- gpr_atm_acq_load(&g_epoll_sync);
1419
- #endif /* defined(GRPC_TSAN) */
1420
-
1421
- for (int i = 0; i < ep_rv; ++i) {
1422
- void *data_ptr = ep_ev[i].data.ptr;
1423
- if (data_ptr == &polling_island_wakeup_fd) {
1424
- GRPC_POLLING_TRACE(
1425
- "pollset_work: pollset: %p, worker: %p polling island (epoll_fd: "
1426
- "%d) got merged",
1427
- (void *)pollset, (void *)worker, epoll_fd);
1428
- /* This means that our polling island is merged with a different
1429
- island. We do not have to do anything here since the subsequent call
1430
- to the function pollset_work_and_unlock() will pick up the correct
1431
- epoll_fd */
1432
- } else {
1433
- grpc_fd *fd = data_ptr;
1434
- int cancel = ep_ev[i].events & (EPOLLERR | EPOLLHUP);
1435
- int read_ev = ep_ev[i].events & (EPOLLIN | EPOLLPRI);
1436
- int write_ev = ep_ev[i].events & EPOLLOUT;
1437
- if (read_ev || cancel) {
1438
- fd_become_readable(exec_ctx, fd, pollset);
1439
- }
1440
- if (write_ev || cancel) {
1441
- fd_become_writable(exec_ctx, fd);
1442
- }
1443
- }
1444
- }
1445
- }
1446
-
1447
- /* Note: sig_mask contains the signal mask to use *during* epoll_wait() */
1448
- static void pollset_work_and_unlock(grpc_exec_ctx *exec_ctx,
1449
- grpc_pollset *pollset,
1450
- grpc_pollset_worker *worker,
1451
- gpr_timespec now, gpr_timespec deadline,
1452
- sigset_t *sig_mask, grpc_error **error) {
1453
- int epoll_fd = -1;
1454
- polling_island *pi = NULL;
1455
- GPR_TIMER_BEGIN("pollset_work_and_unlock", 0);
1456
-
1457
- /* We need to get the epoll_fd to wait on. The epoll_fd is in inside the
1458
- latest polling island pointed by pollset->po.pi
1459
-
1460
- Since epoll_fd is immutable, it is safe to read it without a lock on the
1461
- polling island. There is however a possibility that the polling island from
1462
- which we got the epoll_fd, got merged with another island in the meantime.
1463
- This is okay because in such a case, we will wakeup right-away from
1464
- epoll_pwait() (because any merge will poison the old polling island's epoll
1465
- set 'polling_island_wakeup_fd') and then pick up the latest polling_island
1466
- the next time this function - pollset_work_and_unlock()) is called */
1467
-
1468
- if (pollset->po.pi == NULL) {
1469
- pollset->po.pi = polling_island_create(exec_ctx, NULL, error);
1470
- if (pollset->po.pi == NULL) {
1471
- GPR_TIMER_END("pollset_work_and_unlock", 0);
1472
- return; /* Fatal error. Cannot continue */
1473
- }
1474
-
1475
- PI_ADD_REF(pollset->po.pi, "ps");
1476
- GRPC_POLLING_TRACE("pollset_work: pollset: %p created new pi: %p",
1477
- (void *)pollset, (void *)pollset->po.pi);
1478
- }
1479
-
1480
- pi = polling_island_maybe_get_latest(pollset->po.pi);
1481
- epoll_fd = pi->epoll_fd;
1482
-
1483
- /* Update the pollset->po.pi since the island being pointed by
1484
- pollset->po.pi maybe older than the one pointed by pi) */
1485
- if (pollset->po.pi != pi) {
1486
- /* Always do PI_ADD_REF before PI_UNREF because PI_UNREF may cause the
1487
- polling island to be deleted */
1488
- PI_ADD_REF(pi, "ps");
1489
- PI_UNREF(exec_ctx, pollset->po.pi, "ps");
1490
- pollset->po.pi = pi;
1491
- }
1492
-
1493
- /* Add an extra ref so that the island does not get destroyed (which means
1494
- the epoll_fd won't be closed) while we are are doing an epoll_wait() on the
1495
- epoll_fd */
1496
- PI_ADD_REF(pi, "ps_work");
1497
- gpr_mu_unlock(&pollset->po.mu);
1498
-
1499
- g_current_thread_polling_island = pi;
1500
- pollset_do_epoll_pwait(exec_ctx, epoll_fd, pollset, pi, worker, now, deadline,
1501
- sig_mask, error);
1502
- g_current_thread_polling_island = NULL;
1503
-
1504
- GPR_ASSERT(pi != NULL);
1505
-
1506
- /* Before leaving, release the extra ref we added to the polling island. It
1507
- is important to use "pi" here (i.e our old copy of pollset->po.pi
1508
- that we got before releasing the polling island lock). This is because
1509
- pollset->po.pi pointer might get udpated in other parts of the
1510
- code when there is an island merge while we are doing epoll_wait() above */
1511
- PI_UNREF(exec_ctx, pi, "ps_work");
1512
-
1513
- GPR_TIMER_END("pollset_work_and_unlock", 0);
1514
- }
1515
-
1516
- /* pollset->po.mu lock must be held by the caller before calling this.
1517
- The function pollset_work() may temporarily release the lock (pollset->po.mu)
1518
- during the course of its execution but it will always re-acquire the lock and
1519
- ensure that it is held by the time the function returns */
1520
- static grpc_error *pollset_work(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
1521
- grpc_pollset_worker **worker_hdl,
1522
- gpr_timespec now, gpr_timespec deadline) {
1523
- GPR_TIMER_BEGIN("pollset_work", 0);
1524
- grpc_error *error = GRPC_ERROR_NONE;
1525
-
1526
- grpc_pollset_worker worker;
1527
- pollset_worker_init(&worker);
1528
-
1529
- if (worker_hdl) *worker_hdl = &worker;
1530
-
1531
- gpr_tls_set(&g_current_thread_pollset, (intptr_t)pollset);
1532
- gpr_tls_set(&g_current_thread_worker, (intptr_t)&worker);
1533
-
1534
- if (pollset->kicked_without_pollers) {
1535
- /* If the pollset was kicked without pollers, pretend that the current
1536
- worker got the kick and skip polling. A kick indicates that there is some
1537
- work that needs attention like an event on the completion queue or an
1538
- alarm */
1539
- GPR_TIMER_MARK("pollset_work.kicked_without_pollers", 0);
1540
- pollset->kicked_without_pollers = 0;
1541
- } else if (!pollset->shutting_down) {
1542
- /* We use the posix-signal with number 'grpc_wakeup_signal' for waking up
1543
- (i.e 'kicking') a worker in the pollset. A 'kick' is a way to inform the
1544
- worker that there is some pending work that needs immediate attention
1545
- (like an event on the completion queue, or a polling island merge that
1546
- results in a new epoll-fd to wait on) and that the worker should not
1547
- spend time waiting in epoll_pwait().
1548
-
1549
- A worker can be kicked anytime from the point it is added to the pollset
1550
- via push_front_worker() (or push_back_worker()) to the point it is
1551
- removed via remove_worker().
1552
- If the worker is kicked before/during it calls epoll_pwait(), it should
1553
- immediately exit from epoll_wait(). If the worker is kicked after it
1554
- returns from epoll_wait(), then nothing really needs to be done.
1555
-
1556
- To accomplish this, we mask 'grpc_wakeup_signal' on this thread at all
1557
- times *except* when it is in epoll_pwait(). This way, the worker never
1558
- misses acting on a kick */
1559
-
1560
- if (!g_initialized_sigmask) {
1561
- sigemptyset(&g_wakeup_sig_set);
1562
- sigaddset(&g_wakeup_sig_set, grpc_wakeup_signal);
1563
- pthread_sigmask(SIG_BLOCK, &g_wakeup_sig_set, &g_orig_sigmask);
1564
- sigdelset(&g_orig_sigmask, grpc_wakeup_signal);
1565
- g_initialized_sigmask = true;
1566
- /* new_mask: The new thread mask which blocks 'grpc_wakeup_signal'.
1567
- This is the mask used at all times *except during
1568
- epoll_wait()*"
1569
- g_orig_sigmask: The thread mask which allows 'grpc_wakeup_signal' and
1570
- this is the mask to use *during epoll_wait()*
1571
-
1572
- The new_mask is set on the worker before it is added to the pollset
1573
- (i.e before it can be kicked) */
1574
- }
1575
-
1576
- push_front_worker(pollset, &worker); /* Add worker to pollset */
1577
-
1578
- pollset_work_and_unlock(exec_ctx, pollset, &worker, now, deadline,
1579
- &g_orig_sigmask, &error);
1580
- grpc_exec_ctx_flush(exec_ctx);
1581
-
1582
- gpr_mu_lock(&pollset->po.mu);
1583
-
1584
- /* Note: There is no need to reset worker.is_kicked to 0 since we are no
1585
- longer going to use this worker */
1586
- remove_worker(pollset, &worker);
1587
- }
1588
-
1589
- /* If we are the last worker on the pollset (i.e pollset_has_workers() is
1590
- false at this point) and the pollset is shutting down, we may have to
1591
- finish the shutdown process by calling finish_shutdown_locked().
1592
- See pollset_shutdown() for more details.
1593
-
1594
- Note: Continuing to access pollset here is safe; it is the caller's
1595
- responsibility to not destroy a pollset when it has outstanding calls to
1596
- pollset_work() */
1597
- if (pollset->shutting_down && !pollset_has_workers(pollset) &&
1598
- !pollset->finish_shutdown_called) {
1599
- GPR_TIMER_MARK("pollset_work.finish_shutdown_locked", 0);
1600
- finish_shutdown_locked(exec_ctx, pollset);
1601
-
1602
- gpr_mu_unlock(&pollset->po.mu);
1603
- grpc_exec_ctx_flush(exec_ctx);
1604
- gpr_mu_lock(&pollset->po.mu);
1605
- }
1606
-
1607
- if (worker_hdl) *worker_hdl = NULL;
1608
-
1609
- gpr_tls_set(&g_current_thread_pollset, (intptr_t)0);
1610
- gpr_tls_set(&g_current_thread_worker, (intptr_t)0);
1611
-
1612
- GPR_TIMER_END("pollset_work", 0);
1613
-
1614
- GRPC_LOG_IF_ERROR("pollset_work", GRPC_ERROR_REF(error));
1615
- return error;
1616
- }
1617
-
1618
- static void add_poll_object(grpc_exec_ctx *exec_ctx, poll_obj *bag,
1619
- poll_obj_type bag_type, poll_obj *item,
1620
- poll_obj_type item_type) {
1621
- GPR_TIMER_BEGIN("add_poll_object", 0);
1622
-
1623
- #ifndef NDEBUG
1624
- GPR_ASSERT(item->obj_type == item_type);
1625
- GPR_ASSERT(bag->obj_type == bag_type);
1626
- #endif
1627
-
1628
- grpc_error *error = GRPC_ERROR_NONE;
1629
- polling_island *pi_new = NULL;
1630
-
1631
- gpr_mu_lock(&bag->mu);
1632
- gpr_mu_lock(&item->mu);
1633
-
1634
- retry:
1635
- /*
1636
- * 1) If item->pi and bag->pi are both non-NULL and equal, do nothing
1637
- * 2) If item->pi and bag->pi are both NULL, create a new polling island (with
1638
- * a refcount of 2) and point item->pi and bag->pi to the new island
1639
- * 3) If exactly one of item->pi or bag->pi is NULL, update it to point to
1640
- * the other's non-NULL pi
1641
- * 4) Finally if item->pi and bag-pi are non-NULL and not-equal, merge the
1642
- * polling islands and update item->pi and bag->pi to point to the new
1643
- * island
1644
- */
1645
-
1646
- /* Early out if we are trying to add an 'fd' to a 'bag' but the fd is already
1647
- * orphaned */
1648
- if (item_type == POLL_OBJ_FD && (FD_FROM_PO(item))->orphaned) {
1649
- gpr_mu_unlock(&item->mu);
1650
- gpr_mu_unlock(&bag->mu);
1651
- return;
1652
- }
1653
-
1654
- if (item->pi == bag->pi) {
1655
- pi_new = item->pi;
1656
- if (pi_new == NULL) {
1657
- /* GPR_ASSERT(item->pi == bag->pi == NULL) */
1658
-
1659
- /* If we are adding an fd to a bag (i.e pollset or pollset_set), then
1660
- * we need to do some extra work to make TSAN happy */
1661
- if (item_type == POLL_OBJ_FD) {
1662
- /* Unlock before creating a new polling island: the polling island will
1663
- create a workqueue which creates a file descriptor, and holding an fd
1664
- lock here can eventually cause a loop to appear to TSAN (making it
1665
- unhappy). We don't think it's a real loop (there's an epoch point
1666
- where that loop possibility disappears), but the advantages of
1667
- keeping TSAN happy outweigh any performance advantage we might have
1668
- by keeping the lock held. */
1669
- gpr_mu_unlock(&item->mu);
1670
- pi_new = polling_island_create(exec_ctx, FD_FROM_PO(item), &error);
1671
- gpr_mu_lock(&item->mu);
1672
-
1673
- /* Need to reverify any assumptions made between the initial lock and
1674
- getting to this branch: if they've changed, we need to throw away our
1675
- work and figure things out again. */
1676
- if (item->pi != NULL) {
1677
- GRPC_POLLING_TRACE(
1678
- "add_poll_object: Raced creating new polling island. pi_new: %p "
1679
- "(fd: %d, %s: %p)",
1680
- (void *)pi_new, FD_FROM_PO(item)->fd, poll_obj_string(bag_type),
1681
- (void *)bag);
1682
- /* No need to lock 'pi_new' here since this is a new polling island
1683
- and no one has a reference to it yet */
1684
- polling_island_remove_all_fds_locked(pi_new, true, &error);
1685
-
1686
- /* Ref and unref so that the polling island gets deleted during unref
1687
- */
1688
- PI_ADD_REF(pi_new, "dance_of_destruction");
1689
- PI_UNREF(exec_ctx, pi_new, "dance_of_destruction");
1690
- goto retry;
1691
- }
1692
- } else {
1693
- pi_new = polling_island_create(exec_ctx, NULL, &error);
1694
- }
1695
-
1696
- GRPC_POLLING_TRACE(
1697
- "add_poll_object: Created new polling island. pi_new: %p (%s: %p, "
1698
- "%s: %p)",
1699
- (void *)pi_new, poll_obj_string(item_type), (void *)item,
1700
- poll_obj_string(bag_type), (void *)bag);
1701
- } else {
1702
- GRPC_POLLING_TRACE(
1703
- "add_poll_object: Same polling island. pi: %p (%s, %s)",
1704
- (void *)pi_new, poll_obj_string(item_type),
1705
- poll_obj_string(bag_type));
1706
- }
1707
- } else if (item->pi == NULL) {
1708
- /* GPR_ASSERT(bag->pi != NULL) */
1709
- /* Make pi_new point to latest pi*/
1710
- pi_new = polling_island_lock(bag->pi);
1711
-
1712
- if (item_type == POLL_OBJ_FD) {
1713
- grpc_fd *fd = FD_FROM_PO(item);
1714
- polling_island_add_fds_locked(pi_new, &fd, 1, true, &error);
1715
- }
1716
-
1717
- gpr_mu_unlock(&pi_new->mu);
1718
- GRPC_POLLING_TRACE(
1719
- "add_poll_obj: item->pi was NULL. pi_new: %p (item(%s): %p, "
1720
- "bag(%s): %p)",
1721
- (void *)pi_new, poll_obj_string(item_type), (void *)item,
1722
- poll_obj_string(bag_type), (void *)bag);
1723
- } else if (bag->pi == NULL) {
1724
- /* GPR_ASSERT(item->pi != NULL) */
1725
- /* Make pi_new to point to latest pi */
1726
- pi_new = polling_island_lock(item->pi);
1727
- gpr_mu_unlock(&pi_new->mu);
1728
- GRPC_POLLING_TRACE(
1729
- "add_poll_obj: bag->pi was NULL. pi_new: %p (item(%s): %p, "
1730
- "bag(%s): %p)",
1731
- (void *)pi_new, poll_obj_string(item_type), (void *)item,
1732
- poll_obj_string(bag_type), (void *)bag);
1733
- } else {
1734
- pi_new = polling_island_merge(item->pi, bag->pi, &error);
1735
- GRPC_POLLING_TRACE(
1736
- "add_poll_obj: polling islands merged. pi_new: %p (item(%s): %p, "
1737
- "bag(%s): %p)",
1738
- (void *)pi_new, poll_obj_string(item_type), (void *)item,
1739
- poll_obj_string(bag_type), (void *)bag);
1740
- }
1741
-
1742
- /* At this point, pi_new is the polling island that both item->pi and bag->pi
1743
- MUST be pointing to */
1744
-
1745
- if (item->pi != pi_new) {
1746
- PI_ADD_REF(pi_new, poll_obj_string(item_type));
1747
- if (item->pi != NULL) {
1748
- PI_UNREF(exec_ctx, item->pi, poll_obj_string(item_type));
1749
- }
1750
- item->pi = pi_new;
1751
- }
1752
-
1753
- if (bag->pi != pi_new) {
1754
- PI_ADD_REF(pi_new, poll_obj_string(bag_type));
1755
- if (bag->pi != NULL) {
1756
- PI_UNREF(exec_ctx, bag->pi, poll_obj_string(bag_type));
1757
- }
1758
- bag->pi = pi_new;
1759
- }
1760
-
1761
- gpr_mu_unlock(&item->mu);
1762
- gpr_mu_unlock(&bag->mu);
1763
-
1764
- GRPC_LOG_IF_ERROR("add_poll_object", error);
1765
- GPR_TIMER_END("add_poll_object", 0);
1766
- }
1767
-
1768
- static void pollset_add_fd(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
1769
- grpc_fd *fd) {
1770
- add_poll_object(exec_ctx, &pollset->po, POLL_OBJ_POLLSET, &fd->po,
1771
- POLL_OBJ_FD);
1772
- }
1773
-
1774
- /*******************************************************************************
1775
- * Pollset-set Definitions
1776
- */
1777
-
1778
- static grpc_pollset_set *pollset_set_create(void) {
1779
- grpc_pollset_set *pss = gpr_malloc(sizeof(*pss));
1780
- gpr_mu_init(&pss->po.mu);
1781
- pss->po.pi = NULL;
1782
- #ifndef NDEBUG
1783
- pss->po.obj_type = POLL_OBJ_POLLSET_SET;
1784
- #endif
1785
- return pss;
1786
- }
1787
-
1788
- static void pollset_set_destroy(grpc_exec_ctx *exec_ctx,
1789
- grpc_pollset_set *pss) {
1790
- gpr_mu_destroy(&pss->po.mu);
1791
-
1792
- if (pss->po.pi != NULL) {
1793
- PI_UNREF(exec_ctx, pss->po.pi, "pss_destroy");
1794
- }
1795
-
1796
- gpr_free(pss);
1797
- }
1798
-
1799
- static void pollset_set_add_fd(grpc_exec_ctx *exec_ctx, grpc_pollset_set *pss,
1800
- grpc_fd *fd) {
1801
- add_poll_object(exec_ctx, &pss->po, POLL_OBJ_POLLSET_SET, &fd->po,
1802
- POLL_OBJ_FD);
1803
- }
1804
-
1805
- static void pollset_set_del_fd(grpc_exec_ctx *exec_ctx, grpc_pollset_set *pss,
1806
- grpc_fd *fd) {
1807
- /* Nothing to do */
1808
- }
1809
-
1810
- static void pollset_set_add_pollset(grpc_exec_ctx *exec_ctx,
1811
- grpc_pollset_set *pss, grpc_pollset *ps) {
1812
- add_poll_object(exec_ctx, &pss->po, POLL_OBJ_POLLSET_SET, &ps->po,
1813
- POLL_OBJ_POLLSET);
1814
- }
1815
-
1816
- static void pollset_set_del_pollset(grpc_exec_ctx *exec_ctx,
1817
- grpc_pollset_set *pss, grpc_pollset *ps) {
1818
- /* Nothing to do */
1819
- }
1820
-
1821
- static void pollset_set_add_pollset_set(grpc_exec_ctx *exec_ctx,
1822
- grpc_pollset_set *bag,
1823
- grpc_pollset_set *item) {
1824
- add_poll_object(exec_ctx, &bag->po, POLL_OBJ_POLLSET_SET, &item->po,
1825
- POLL_OBJ_POLLSET_SET);
1826
- }
1827
-
1828
- static void pollset_set_del_pollset_set(grpc_exec_ctx *exec_ctx,
1829
- grpc_pollset_set *bag,
1830
- grpc_pollset_set *item) {
1831
- /* Nothing to do */
1832
- }
1833
-
1834
- /*******************************************************************************
1835
- * Event engine binding
1836
- */
1837
-
1838
- static void shutdown_engine(void) {
1839
- fd_global_shutdown();
1840
- pollset_global_shutdown();
1841
- polling_island_global_shutdown();
1842
- }
1843
-
1844
- static const grpc_event_engine_vtable vtable = {
1845
- .pollset_size = sizeof(grpc_pollset),
1846
-
1847
- .fd_create = fd_create,
1848
- .fd_wrapped_fd = fd_wrapped_fd,
1849
- .fd_orphan = fd_orphan,
1850
- .fd_shutdown = fd_shutdown,
1851
- .fd_is_shutdown = fd_is_shutdown,
1852
- .fd_notify_on_read = fd_notify_on_read,
1853
- .fd_notify_on_write = fd_notify_on_write,
1854
- .fd_get_read_notifier_pollset = fd_get_read_notifier_pollset,
1855
-
1856
- .pollset_init = pollset_init,
1857
- .pollset_shutdown = pollset_shutdown,
1858
- .pollset_destroy = pollset_destroy,
1859
- .pollset_work = pollset_work,
1860
- .pollset_kick = pollset_kick,
1861
- .pollset_add_fd = pollset_add_fd,
1862
-
1863
- .pollset_set_create = pollset_set_create,
1864
- .pollset_set_destroy = pollset_set_destroy,
1865
- .pollset_set_add_pollset = pollset_set_add_pollset,
1866
- .pollset_set_del_pollset = pollset_set_del_pollset,
1867
- .pollset_set_add_pollset_set = pollset_set_add_pollset_set,
1868
- .pollset_set_del_pollset_set = pollset_set_del_pollset_set,
1869
- .pollset_set_add_fd = pollset_set_add_fd,
1870
- .pollset_set_del_fd = pollset_set_del_fd,
1871
-
1872
- .shutdown_engine = shutdown_engine,
1873
- };
1874
-
1875
- /* It is possible that GLIBC has epoll but the underlying kernel doesn't.
1876
- * Create a dummy epoll_fd to make sure epoll support is available */
1877
- static bool is_epoll_available() {
1878
- int fd = epoll_create1(EPOLL_CLOEXEC);
1879
- if (fd < 0) {
1880
- gpr_log(
1881
- GPR_ERROR,
1882
- "epoll_create1 failed with error: %d. Not using epoll polling engine",
1883
- fd);
1884
- return false;
1885
- }
1886
- close(fd);
1887
- return true;
1888
- }
1889
-
1890
- /* This is mainly for testing purposes. Checks to see if environment variable
1891
- * GRPC_MAX_POLLERS_PER_PI is set and if so, assigns that value to
1892
- * g_max_pollers_per_pi (any negative value is considered INT_MAX) */
1893
- static void set_max_pollers_per_island() {
1894
- char *s = gpr_getenv("GRPC_MAX_POLLERS_PER_PI");
1895
- if (s) {
1896
- g_max_pollers_per_pi = (int)strtol(s, NULL, 10);
1897
- if (g_max_pollers_per_pi < 0) {
1898
- g_max_pollers_per_pi = INT_MAX;
1899
- }
1900
- } else {
1901
- g_max_pollers_per_pi = INT_MAX;
1902
- }
1903
-
1904
- gpr_log(GPR_INFO, "Max number of pollers per polling island: %d",
1905
- g_max_pollers_per_pi);
1906
- }
1907
-
1908
- const grpc_event_engine_vtable *grpc_init_epoll_limited_pollers_linux(
1909
- bool explicitly_requested) {
1910
- if (!explicitly_requested) {
1911
- return NULL;
1912
- }
1913
-
1914
- /* If use of signals is disabled, we cannot use epoll engine*/
1915
- if (is_grpc_wakeup_signal_initialized && grpc_wakeup_signal < 0) {
1916
- return NULL;
1917
- }
1918
-
1919
- if (!grpc_has_wakeup_fd()) {
1920
- return NULL;
1921
- }
1922
-
1923
- if (!is_epoll_available()) {
1924
- return NULL;
1925
- }
1926
-
1927
- if (!is_grpc_wakeup_signal_initialized) {
1928
- grpc_use_signal(SIGRTMIN + 6);
1929
- }
1930
-
1931
- set_max_pollers_per_island();
1932
-
1933
- fd_global_init();
1934
-
1935
- if (!GRPC_LOG_IF_ERROR("pollset_global_init", pollset_global_init())) {
1936
- return NULL;
1937
- }
1938
-
1939
- if (!GRPC_LOG_IF_ERROR("polling_island_global_init",
1940
- polling_island_global_init())) {
1941
- return NULL;
1942
- }
1943
-
1944
- return &vtable;
1945
- }
1946
-
1947
- #else /* defined(GRPC_LINUX_EPOLL) */
1948
- #if defined(GRPC_POSIX_SOCKET)
1949
- #include "src/core/lib/iomgr/ev_posix.h"
1950
- /* If GRPC_LINUX_EPOLL is not defined, it means epoll is not available. Return
1951
- * NULL */
1952
- const grpc_event_engine_vtable *grpc_init_epoll_limited_pollers_linux(
1953
- bool explicitly_requested) {
1954
- return NULL;
1955
- }
1956
- #endif /* defined(GRPC_POSIX_SOCKET) */
1957
- #endif /* !defined(GRPC_LINUX_EPOLL) */