grpc 1.0.1 → 1.1.2

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 (705) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +3696 -867
  3. data/etc/roots.pem +39 -111
  4. data/include/grpc/byte_buffer.h +64 -1
  5. data/include/grpc/census.h +40 -96
  6. data/include/grpc/compression.h +2 -1
  7. data/include/grpc/grpc.h +42 -7
  8. data/include/grpc/grpc_posix.h +8 -5
  9. data/include/grpc/impl/codegen/atm.h +3 -0
  10. data/include/grpc/impl/codegen/atm_gcc_atomic.h +2 -0
  11. data/include/grpc/impl/codegen/atm_gcc_sync.h +8 -0
  12. data/include/grpc/impl/codegen/atm_windows.h +4 -0
  13. data/include/grpc/impl/codegen/byte_buffer_reader.h +4 -4
  14. data/include/grpc/impl/codegen/compression_types.h +1 -1
  15. data/include/grpc/impl/codegen/connectivity_state.h +2 -0
  16. data/include/grpc/impl/codegen/exec_ctx_fwd.h +41 -0
  17. data/include/grpc/impl/codegen/gpr_slice.h +84 -0
  18. data/include/grpc/impl/codegen/{alloc.h → gpr_types.h} +30 -29
  19. data/include/grpc/impl/codegen/grpc_types.h +91 -9
  20. data/include/grpc/impl/codegen/port_platform.h +25 -92
  21. data/include/grpc/impl/codegen/slice.h +54 -97
  22. data/include/grpc/impl/codegen/sync.h +0 -253
  23. data/include/grpc/module.modulemap +0 -2
  24. data/include/grpc/slice.h +132 -0
  25. data/include/grpc/{impl/codegen/slice_buffer.h → slice_buffer.h} +22 -39
  26. data/include/grpc/support/alloc.h +40 -1
  27. data/include/grpc/support/log.h +80 -1
  28. data/include/grpc/support/log_windows.h +2 -0
  29. data/include/grpc/support/string_util.h +1 -1
  30. data/include/grpc/support/sync.h +252 -0
  31. data/include/grpc/support/time.h +67 -1
  32. data/src/boringssl/err_data.c +639 -627
  33. data/src/core/ext/census/base_resources.c +71 -0
  34. data/src/core/ext/census/base_resources.h +39 -0
  35. data/src/core/ext/census/gen/census.pb.c +26 -29
  36. data/src/core/ext/census/gen/census.pb.h +68 -67
  37. data/src/core/ext/census/gen/trace_context.pb.c +81 -0
  38. data/src/core/ext/census/gen/trace_context.pb.h +99 -0
  39. data/src/core/ext/census/grpc_filter.c +22 -16
  40. data/src/core/ext/census/grpc_plugin.c +2 -1
  41. data/src/core/ext/census/initialize.c +16 -4
  42. data/src/core/ext/census/mlog.h +1 -1
  43. data/src/core/ext/census/placeholders.c +0 -45
  44. data/src/core/ext/census/resource.c +312 -0
  45. data/src/core/ext/census/resource.h +63 -0
  46. data/src/core/ext/census/trace_context.c +86 -0
  47. data/src/core/ext/census/trace_context.h +68 -0
  48. data/src/core/ext/census/tracing.c +8 -2
  49. data/src/core/ext/{client_config → client_channel}/channel_connectivity.c +8 -4
  50. data/src/core/ext/client_channel/client_channel.c +1218 -0
  51. data/src/core/ext/{client_config → client_channel}/client_channel.h +8 -11
  52. data/src/core/ext/{client_config → client_channel}/client_channel_factory.c +33 -3
  53. data/src/core/ext/{client_config → client_channel}/client_channel_factory.h +15 -8
  54. data/src/core/ext/{client_config/client_config_plugin.c → client_channel/client_channel_plugin.c} +16 -15
  55. data/src/core/ext/{client_config → client_channel}/connector.c +1 -1
  56. data/src/core/ext/{client_config → client_channel}/connector.h +5 -8
  57. data/{include/grpc/support/slice_buffer.h → src/core/ext/client_channel/default_initial_connect_string.c} +4 -5
  58. data/src/core/ext/client_channel/http_connect_handshaker.c +399 -0
  59. data/src/core/ext/client_channel/http_connect_handshaker.h +52 -0
  60. data/src/core/ext/{client_config → client_channel}/initial_connect_string.c +6 -7
  61. data/src/core/ext/{client_config → client_channel}/initial_connect_string.h +10 -10
  62. data/src/core/ext/{client_config → client_channel}/lb_policy.c +11 -11
  63. data/src/core/ext/{client_config → client_channel}/lb_policy.h +68 -27
  64. data/src/core/ext/client_channel/lb_policy_factory.c +163 -0
  65. data/src/core/ext/{client_config → client_channel}/lb_policy_factory.h +64 -9
  66. data/src/core/ext/{client_config → client_channel}/lb_policy_registry.c +6 -4
  67. data/src/core/ext/{client_config → client_channel}/lb_policy_registry.h +4 -4
  68. data/src/core/ext/{client_config → client_channel}/parse_address.c +21 -14
  69. data/src/core/ext/{client_config → client_channel}/parse_address.h +8 -10
  70. data/src/core/ext/{client_config → client_channel}/resolver.c +3 -4
  71. data/src/core/ext/{client_config → client_channel}/resolver.h +11 -15
  72. data/src/core/ext/{client_config → client_channel}/resolver_factory.c +4 -3
  73. data/src/core/ext/{client_config → client_channel}/resolver_factory.h +13 -11
  74. data/src/core/ext/{client_config → client_channel}/resolver_registry.c +54 -34
  75. data/src/core/ext/{client_config → client_channel}/resolver_registry.h +21 -8
  76. data/src/core/ext/{client_config → client_channel}/subchannel.c +208 -119
  77. data/src/core/ext/{client_config → client_channel}/subchannel.h +21 -11
  78. data/src/core/ext/{client_config → client_channel}/subchannel_index.c +6 -17
  79. data/src/core/ext/{client_config → client_channel}/subchannel_index.h +7 -7
  80. data/src/core/ext/{client_config → client_channel}/uri_parser.c +21 -28
  81. data/src/core/ext/{client_config → client_channel}/uri_parser.h +3 -3
  82. data/src/core/ext/lb_policy/grpclb/grpclb.c +1406 -0
  83. data/src/core/ext/lb_policy/grpclb/grpclb.h +44 -0
  84. data/src/core/ext/lb_policy/grpclb/load_balancer_api.c +117 -37
  85. data/src/core/ext/lb_policy/grpclb/load_balancer_api.h +31 -12
  86. data/src/core/ext/lb_policy/grpclb/proto/grpc/lb/v1/load_balancer.pb.c +6 -36
  87. data/src/core/ext/lb_policy/grpclb/proto/grpc/lb/v1/load_balancer.pb.h +22 -42
  88. data/src/core/ext/lb_policy/pick_first/pick_first.c +64 -46
  89. data/src/core/ext/lb_policy/round_robin/round_robin.c +324 -160
  90. data/src/core/ext/load_reporting/load_reporting.c +7 -56
  91. data/src/core/ext/load_reporting/load_reporting.h +41 -28
  92. data/src/core/ext/load_reporting/load_reporting_filter.c +132 -42
  93. data/src/core/ext/load_reporting/load_reporting_filter.h +1 -0
  94. data/src/core/ext/resolver/dns/native/dns_resolver.c +88 -80
  95. data/src/core/ext/resolver/sockaddr/sockaddr_resolver.c +57 -102
  96. data/src/core/ext/transport/chttp2/alpn/alpn.c +1 -1
  97. data/src/core/ext/transport/chttp2/client/chttp2_connector.c +253 -0
  98. data/src/core/{lib/iomgr/ev_poll_and_epoll_posix.h → ext/transport/chttp2/client/chttp2_connector.h} +5 -5
  99. data/src/core/ext/transport/chttp2/client/insecure/channel_create.c +31 -160
  100. data/src/core/ext/transport/chttp2/client/insecure/channel_create_posix.c +5 -5
  101. data/src/core/ext/transport/chttp2/client/secure/secure_channel_create.c +44 -243
  102. data/src/core/ext/transport/chttp2/server/chttp2_server.c +342 -0
  103. data/src/core/ext/transport/chttp2/server/chttp2_server.h +47 -0
  104. data/src/core/ext/transport/chttp2/server/insecure/server_chttp2.c +11 -124
  105. data/src/core/ext/transport/chttp2/server/insecure/server_chttp2_posix.c +20 -9
  106. data/src/core/ext/transport/chttp2/server/secure/server_secure_chttp2.c +28 -236
  107. data/src/core/ext/transport/chttp2/transport/bin_decoder.c +31 -27
  108. data/src/core/ext/transport/chttp2/transport/bin_decoder.h +5 -4
  109. data/src/core/ext/transport/chttp2/transport/bin_encoder.c +25 -22
  110. data/src/core/ext/transport/chttp2/transport/bin_encoder.h +8 -7
  111. data/src/core/ext/transport/chttp2/transport/chttp2_plugin.c +0 -3
  112. data/src/core/ext/transport/chttp2/transport/chttp2_transport.c +1345 -1521
  113. data/src/core/ext/transport/chttp2/transport/chttp2_transport.h +3 -1
  114. data/src/core/ext/transport/chttp2/transport/frame.h +3 -5
  115. data/src/core/ext/transport/chttp2/transport/frame_data.c +50 -47
  116. data/src/core/ext/transport/chttp2/transport/frame_data.h +8 -9
  117. data/src/core/ext/transport/chttp2/transport/frame_goaway.c +19 -21
  118. data/src/core/ext/transport/chttp2/transport/frame_goaway.h +9 -8
  119. data/src/core/ext/transport/chttp2/transport/frame_ping.c +13 -12
  120. data/src/core/ext/transport/chttp2/transport/frame_ping.h +6 -6
  121. data/src/core/ext/transport/chttp2/transport/frame_rst_stream.c +31 -19
  122. data/src/core/ext/transport/chttp2/transport/frame_rst_stream.h +8 -7
  123. data/src/core/ext/transport/chttp2/transport/frame_settings.c +22 -25
  124. data/src/core/ext/transport/chttp2/transport/frame_settings.h +9 -8
  125. data/src/core/ext/transport/chttp2/transport/frame_window_update.c +26 -18
  126. data/src/core/ext/transport/chttp2/transport/frame_window_update.h +5 -6
  127. data/src/core/ext/transport/chttp2/transport/hpack_encoder.c +68 -58
  128. data/src/core/ext/transport/chttp2/transport/hpack_encoder.h +8 -5
  129. data/src/core/ext/transport/chttp2/transport/hpack_parser.c +327 -214
  130. data/src/core/ext/transport/chttp2/transport/hpack_parser.h +14 -9
  131. data/src/core/ext/transport/chttp2/transport/hpack_table.c +24 -19
  132. data/src/core/ext/transport/chttp2/transport/hpack_table.h +9 -6
  133. data/src/core/ext/transport/chttp2/transport/incoming_metadata.c +2 -2
  134. data/src/core/ext/transport/chttp2/transport/incoming_metadata.h +1 -1
  135. data/src/core/ext/transport/chttp2/transport/internal.h +284 -436
  136. data/src/core/ext/transport/chttp2/transport/parsing.c +355 -590
  137. data/src/core/ext/transport/chttp2/transport/stream_lists.c +36 -309
  138. data/src/core/ext/transport/chttp2/transport/stream_map.c +13 -34
  139. data/src/core/ext/transport/chttp2/transport/stream_map.h +3 -4
  140. data/src/core/ext/transport/chttp2/transport/writing.c +174 -286
  141. data/src/core/lib/channel/channel_args.c +70 -13
  142. data/src/core/lib/channel/channel_args.h +28 -2
  143. data/src/core/lib/channel/channel_stack.c +77 -28
  144. data/src/core/lib/channel/channel_stack.h +61 -23
  145. data/src/core/lib/channel/channel_stack_builder.c +33 -25
  146. data/src/core/lib/channel/channel_stack_builder.h +17 -8
  147. data/src/core/lib/channel/compress_filter.c +52 -36
  148. data/src/core/lib/channel/connected_channel.c +20 -12
  149. data/src/core/lib/channel/connected_channel.h +2 -1
  150. data/src/core/lib/channel/context.h +13 -1
  151. data/src/core/lib/channel/deadline_filter.c +344 -0
  152. data/src/core/lib/channel/deadline_filter.h +99 -0
  153. data/src/core/lib/channel/handshaker.c +240 -0
  154. data/src/core/lib/channel/handshaker.h +164 -0
  155. data/src/core/lib/{security/credentials/google_default/credentials_windows.c → channel/handshaker_factory.c} +16 -23
  156. data/src/core/lib/channel/handshaker_factory.h +66 -0
  157. data/src/core/lib/channel/handshaker_registry.c +113 -0
  158. data/src/core/{ext/client_config/client_config.h → lib/channel/handshaker_registry.h} +26 -16
  159. data/src/core/lib/channel/http_client_filter.c +248 -46
  160. data/src/core/lib/channel/http_client_filter.h +3 -0
  161. data/src/core/lib/channel/http_server_filter.c +136 -24
  162. data/src/core/lib/channel/message_size_filter.c +261 -0
  163. data/src/core/lib/channel/message_size_filter.h +39 -0
  164. data/src/core/lib/compression/message_compress.c +43 -37
  165. data/src/core/lib/compression/message_compress.h +7 -5
  166. data/src/core/lib/http/format_request.c +26 -11
  167. data/src/core/lib/http/format_request.h +7 -5
  168. data/src/core/lib/http/httpcli.c +45 -27
  169. data/src/core/lib/http/httpcli.h +4 -4
  170. data/src/core/lib/http/httpcli_security_connector.c +56 -46
  171. data/src/core/lib/http/parser.c +17 -14
  172. data/src/core/lib/http/parser.h +4 -2
  173. data/src/core/lib/iomgr/closure.c +49 -7
  174. data/src/core/lib/iomgr/closure.h +56 -14
  175. data/src/core/lib/iomgr/combiner.c +422 -0
  176. data/src/core/lib/iomgr/combiner.h +64 -0
  177. data/src/core/lib/iomgr/endpoint.c +8 -2
  178. data/src/core/lib/iomgr/endpoint.h +17 -7
  179. data/src/core/lib/iomgr/endpoint_pair.h +3 -2
  180. data/src/core/lib/iomgr/endpoint_pair_posix.c +9 -8
  181. data/src/core/{ext/client_config/lb_policy_factory.c → lib/iomgr/endpoint_pair_uv.c} +18 -13
  182. data/src/core/lib/iomgr/endpoint_pair_windows.c +7 -6
  183. data/src/core/lib/iomgr/error.c +72 -6
  184. data/src/core/lib/iomgr/error.h +30 -3
  185. data/src/core/lib/iomgr/ev_epoll_linux.c +500 -382
  186. data/src/core/lib/iomgr/ev_epoll_linux.h +3 -2
  187. data/src/core/lib/iomgr/ev_poll_posix.c +317 -30
  188. data/src/core/lib/iomgr/ev_poll_posix.h +1 -0
  189. data/src/core/lib/iomgr/ev_posix.c +26 -5
  190. data/src/core/lib/iomgr/ev_posix.h +12 -1
  191. data/src/core/lib/iomgr/exec_ctx.c +27 -94
  192. data/src/core/lib/iomgr/exec_ctx.h +19 -22
  193. data/src/core/lib/iomgr/executor.c +29 -8
  194. data/src/core/lib/iomgr/executor.h +2 -4
  195. data/src/core/lib/iomgr/iocp_windows.c +3 -4
  196. data/src/core/lib/iomgr/iomgr.c +14 -10
  197. data/src/core/lib/iomgr/iomgr.h +6 -2
  198. data/src/core/lib/iomgr/iomgr_posix.c +2 -2
  199. data/src/core/lib/iomgr/iomgr_uv.c +49 -0
  200. data/src/core/lib/iomgr/iomgr_windows.c +2 -2
  201. data/src/core/lib/iomgr/load_file.c +3 -3
  202. data/src/core/lib/iomgr/load_file.h +2 -2
  203. data/src/core/lib/iomgr/network_status_tracker.c +1 -1
  204. data/src/core/lib/iomgr/pollset_set_uv.c +62 -0
  205. data/src/core/lib/iomgr/pollset_set_windows.c +3 -3
  206. data/src/core/lib/iomgr/pollset_uv.c +142 -0
  207. data/src/core/lib/iomgr/pollset_uv.h +42 -0
  208. data/src/core/lib/iomgr/pollset_windows.c +5 -6
  209. data/src/core/lib/iomgr/port.h +129 -0
  210. data/src/core/lib/iomgr/resolve_address.h +2 -1
  211. data/src/core/lib/iomgr/resolve_address_posix.c +14 -13
  212. data/src/core/lib/iomgr/resolve_address_uv.c +233 -0
  213. data/src/core/lib/iomgr/resolve_address_windows.c +14 -12
  214. data/src/core/lib/iomgr/resource_quota.c +832 -0
  215. data/src/core/lib/iomgr/resource_quota.h +159 -0
  216. data/src/core/lib/iomgr/sockaddr.h +10 -2
  217. data/src/core/lib/iomgr/sockaddr_utils.c +63 -36
  218. data/src/core/lib/iomgr/sockaddr_utils.h +14 -14
  219. data/src/core/lib/iomgr/socket_mutator.c +98 -0
  220. data/src/core/lib/iomgr/socket_mutator.h +80 -0
  221. data/src/core/lib/iomgr/socket_utils.h +42 -0
  222. data/src/core/lib/iomgr/socket_utils_common_posix.c +28 -13
  223. data/src/core/lib/iomgr/socket_utils_linux.c +11 -5
  224. data/src/core/lib/iomgr/socket_utils_posix.c +10 -7
  225. data/src/core/lib/iomgr/socket_utils_posix.h +11 -4
  226. data/src/core/lib/iomgr/socket_utils_uv.c +49 -0
  227. data/src/core/lib/iomgr/socket_utils_windows.c +52 -0
  228. data/src/core/lib/iomgr/socket_windows.c +14 -6
  229. data/src/core/lib/iomgr/socket_windows.h +1 -0
  230. data/src/core/lib/iomgr/tcp_client.h +8 -2
  231. data/src/core/lib/iomgr/tcp_client_posix.c +131 -82
  232. data/src/core/lib/iomgr/tcp_client_posix.h +45 -0
  233. data/src/core/lib/iomgr/tcp_client_uv.c +190 -0
  234. data/src/core/lib/iomgr/tcp_client_windows.c +54 -30
  235. data/src/core/lib/iomgr/tcp_posix.c +135 -56
  236. data/src/core/lib/iomgr/tcp_posix.h +2 -2
  237. data/src/core/lib/iomgr/tcp_server.h +14 -6
  238. data/src/core/lib/iomgr/tcp_server_posix.c +154 -118
  239. data/src/core/lib/iomgr/tcp_server_uv.c +388 -0
  240. data/src/core/lib/iomgr/tcp_server_windows.c +127 -100
  241. data/src/core/lib/iomgr/tcp_uv.c +367 -0
  242. data/src/core/lib/iomgr/tcp_uv.h +59 -0
  243. data/src/core/lib/iomgr/tcp_windows.c +65 -48
  244. data/src/core/lib/iomgr/tcp_windows.h +3 -1
  245. data/src/core/lib/iomgr/timer.h +21 -21
  246. data/src/core/lib/iomgr/{timer.c → timer_generic.c} +15 -10
  247. data/src/core/lib/iomgr/timer_generic.h +49 -0
  248. data/src/core/lib/iomgr/timer_heap.c +6 -0
  249. data/src/core/lib/iomgr/timer_uv.c +99 -0
  250. data/src/core/lib/iomgr/timer_uv.h +47 -0
  251. data/src/core/lib/iomgr/udp_server.c +116 -98
  252. data/src/core/lib/iomgr/udp_server.h +5 -3
  253. data/src/core/lib/iomgr/unix_sockets_posix.c +14 -6
  254. data/src/core/lib/iomgr/unix_sockets_posix.h +6 -5
  255. data/src/core/lib/iomgr/unix_sockets_posix_noop.c +4 -4
  256. data/src/core/lib/iomgr/wakeup_fd_cv.c +118 -0
  257. data/src/core/lib/iomgr/wakeup_fd_cv.h +80 -0
  258. data/src/core/lib/iomgr/wakeup_fd_eventfd.c +3 -3
  259. data/src/core/lib/iomgr/wakeup_fd_nospecial.c +3 -3
  260. data/src/core/lib/iomgr/wakeup_fd_pipe.c +12 -6
  261. data/src/core/lib/iomgr/wakeup_fd_posix.c +34 -5
  262. data/src/core/lib/iomgr/wakeup_fd_posix.h +5 -0
  263. data/src/core/lib/iomgr/workqueue.h +12 -20
  264. data/src/core/{ext/client_config/client_config.c → lib/iomgr/workqueue_uv.c} +24 -33
  265. data/{include/grpc/support/slice.h → src/core/lib/iomgr/workqueue_uv.h} +4 -6
  266. data/src/core/lib/iomgr/workqueue_windows.c +9 -8
  267. data/src/core/lib/json/json.c +3 -3
  268. data/src/core/lib/json/json.h +11 -11
  269. data/src/core/lib/json/json_reader.c +9 -5
  270. data/src/core/lib/profiling/basic_timers.c +10 -1
  271. data/src/core/lib/profiling/timers.h +2 -0
  272. data/src/core/lib/security/context/security_context.c +13 -3
  273. data/src/core/lib/security/context/security_context.h +20 -0
  274. data/src/core/lib/security/credentials/composite/composite_credentials.c +28 -14
  275. data/src/core/lib/security/credentials/composite/composite_credentials.h +2 -2
  276. data/src/core/lib/security/credentials/credentials.c +48 -19
  277. data/src/core/lib/security/credentials/credentials.h +36 -19
  278. data/src/core/lib/security/credentials/credentials_metadata.c +11 -8
  279. data/src/core/lib/security/credentials/fake/fake_credentials.c +15 -11
  280. data/src/core/lib/security/credentials/google_default/{credentials_posix.c → credentials_generic.c} +7 -14
  281. data/src/core/lib/security/credentials/google_default/google_default_credentials.c +33 -21
  282. data/src/core/lib/security/credentials/google_default/google_default_credentials.h +14 -0
  283. data/src/core/lib/security/credentials/iam/iam_credentials.c +3 -2
  284. data/src/core/lib/security/credentials/jwt/json_token.c +1 -0
  285. data/src/core/lib/security/credentials/jwt/json_token.h +1 -1
  286. data/src/core/lib/security/credentials/jwt/jwt_credentials.c +54 -19
  287. data/src/core/lib/security/credentials/jwt/jwt_credentials.h +2 -1
  288. data/src/core/lib/security/credentials/jwt/jwt_verifier.c +129 -79
  289. data/src/core/lib/security/credentials/jwt/jwt_verifier.h +9 -6
  290. data/src/core/lib/security/credentials/oauth2/oauth2_credentials.c +63 -28
  291. data/src/core/lib/security/credentials/oauth2/oauth2_credentials.h +1 -1
  292. data/src/core/lib/security/credentials/plugin/plugin_credentials.c +32 -11
  293. data/src/core/lib/security/credentials/ssl/ssl_credentials.c +13 -9
  294. data/src/core/lib/security/transport/client_auth_filter.c +33 -27
  295. data/src/core/lib/security/transport/secure_endpoint.c +93 -68
  296. data/src/core/lib/security/transport/secure_endpoint.h +2 -2
  297. data/src/core/lib/security/transport/security_connector.c +133 -168
  298. data/src/core/lib/security/transport/security_connector.h +31 -46
  299. data/src/core/lib/security/transport/security_handshaker.c +501 -0
  300. data/src/core/lib/security/transport/{handshake.h → security_handshaker.h} +10 -10
  301. data/src/core/lib/security/transport/server_auth_filter.c +50 -38
  302. data/src/core/lib/security/util/b64.c +11 -8
  303. data/src/core/lib/security/util/b64.h +5 -4
  304. data/src/core/lib/slice/percent_encoding.c +182 -0
  305. data/src/core/lib/slice/percent_encoding.h +78 -0
  306. data/src/core/lib/{support → slice}/slice.c +81 -50
  307. data/src/core/lib/{support → slice}/slice_buffer.c +78 -60
  308. data/src/core/lib/slice/slice_internal.h +49 -0
  309. data/src/core/lib/slice/slice_string_helpers.c +90 -0
  310. data/src/core/lib/{iomgr/workqueue_posix.h → slice/slice_string_helpers.h} +18 -18
  311. data/src/core/lib/support/backoff.c +24 -13
  312. data/src/core/lib/support/backoff.h +5 -2
  313. data/src/core/lib/support/env.h +0 -2
  314. data/src/core/lib/support/log.c +5 -4
  315. data/src/core/lib/support/log_linux.c +0 -1
  316. data/src/core/lib/support/log_posix.c +1 -1
  317. data/src/core/lib/support/mpscq.c +83 -0
  318. data/src/core/lib/support/mpscq.h +65 -0
  319. data/src/core/lib/support/string.c +58 -49
  320. data/src/core/lib/support/string.h +11 -8
  321. data/src/core/lib/support/subprocess_posix.c +5 -2
  322. data/src/core/lib/support/thd.c +1 -1
  323. data/src/core/lib/support/time.c +43 -79
  324. data/src/core/lib/support/time_posix.c +1 -1
  325. data/src/core/lib/support/tmpfile.h +0 -2
  326. data/src/core/lib/surface/alarm.c +4 -1
  327. data/src/core/lib/surface/byte_buffer.c +17 -11
  328. data/src/core/lib/surface/byte_buffer_reader.c +23 -15
  329. data/src/core/lib/surface/call.c +294 -276
  330. data/src/core/lib/surface/call.h +24 -9
  331. data/src/core/lib/surface/call_log_batch.c +5 -3
  332. data/src/core/lib/surface/channel.c +127 -111
  333. data/src/core/lib/surface/channel.h +14 -5
  334. data/src/core/lib/surface/channel_init.c +1 -1
  335. data/src/core/lib/surface/channel_init.h +10 -1
  336. data/src/core/lib/surface/channel_ping.c +7 -6
  337. data/src/core/lib/surface/completion_queue.c +154 -18
  338. data/src/core/lib/surface/completion_queue.h +5 -0
  339. data/src/core/lib/surface/init.c +40 -6
  340. data/src/core/lib/surface/init.h +1 -0
  341. data/src/core/lib/surface/init_secure.c +5 -2
  342. data/src/core/lib/surface/lame_client.c +28 -18
  343. data/src/core/lib/surface/server.c +134 -87
  344. data/src/core/lib/surface/server.h +8 -0
  345. data/src/core/lib/surface/validate_metadata.c +1 -1
  346. data/src/core/lib/surface/version.c +3 -1
  347. data/src/core/lib/transport/byte_stream.c +7 -4
  348. data/src/core/lib/transport/byte_stream.h +6 -10
  349. data/src/core/lib/transport/connectivity_state.c +21 -12
  350. data/src/core/lib/transport/connectivity_state.h +4 -1
  351. data/src/core/lib/transport/mdstr_hash_table.c +118 -0
  352. data/src/core/lib/transport/mdstr_hash_table.h +77 -0
  353. data/src/core/lib/transport/metadata.c +83 -60
  354. data/src/core/lib/transport/metadata.h +41 -23
  355. data/src/core/lib/transport/metadata_batch.c +17 -11
  356. data/src/core/lib/transport/metadata_batch.h +20 -6
  357. data/src/core/lib/transport/pid_controller.c +57 -0
  358. data/src/core/lib/transport/pid_controller.h +64 -0
  359. data/src/core/lib/transport/service_config.c +251 -0
  360. data/src/core/lib/transport/service_config.h +71 -0
  361. data/src/core/lib/transport/static_metadata.c +18 -16
  362. data/src/core/lib/transport/static_metadata.h +113 -107
  363. data/src/core/{ext/transport/chttp2 → lib}/transport/timeout_encoding.c +3 -3
  364. data/src/core/{ext/transport/chttp2 → lib}/transport/timeout_encoding.h +7 -7
  365. data/src/core/lib/transport/transport.c +84 -23
  366. data/src/core/lib/transport/transport.h +53 -8
  367. data/src/core/lib/transport/transport_impl.h +3 -0
  368. data/src/core/lib/transport/transport_op_string.c +92 -20
  369. data/src/core/lib/tsi/ssl_transport_security.c +3 -1
  370. data/src/core/plugin_registry/grpc_plugin_registry.c +8 -4
  371. data/src/ruby/ext/grpc/extconf.rb +0 -1
  372. data/src/ruby/ext/grpc/rb_byte_buffer.c +8 -7
  373. data/src/ruby/ext/grpc/rb_call.c +15 -5
  374. data/src/ruby/ext/grpc/rb_channel.c +1 -1
  375. data/src/ruby/ext/grpc/rb_compression_options.c +466 -0
  376. data/src/{core/ext/client_config/default_initial_connect_string.c → ruby/ext/grpc/rb_compression_options.h} +10 -5
  377. data/src/ruby/ext/grpc/rb_grpc.c +3 -1
  378. data/src/ruby/ext/grpc/rb_grpc_imports.generated.c +198 -190
  379. data/src/ruby/ext/grpc/rb_grpc_imports.generated.h +306 -294
  380. data/src/ruby/ext/grpc/rb_server.c +18 -12
  381. data/src/ruby/lib/grpc/errors.rb +154 -2
  382. data/src/ruby/lib/grpc/generic/active_call.rb +144 -63
  383. data/src/ruby/lib/grpc/generic/bidi_call.rb +18 -2
  384. data/src/ruby/lib/grpc/generic/client_stub.rb +7 -5
  385. data/src/ruby/lib/grpc/generic/rpc_desc.rb +39 -13
  386. data/src/ruby/lib/grpc/generic/rpc_server.rb +51 -24
  387. data/src/ruby/lib/grpc/generic/service.rb +3 -2
  388. data/src/ruby/lib/grpc/version.rb +1 -1
  389. data/src/ruby/pb/grpc/health/checker.rb +3 -1
  390. data/src/ruby/pb/src/proto/grpc/testing/test_services_pb.rb +7 -0
  391. data/src/ruby/pb/test/client.rb +307 -7
  392. data/src/ruby/pb/test/server.rb +26 -1
  393. data/src/ruby/spec/compression_options_spec.rb +164 -0
  394. data/src/ruby/spec/error_sanity_spec.rb +64 -0
  395. data/src/ruby/spec/generic/active_call_spec.rb +290 -12
  396. data/src/ruby/spec/generic/client_stub_spec.rb +91 -41
  397. data/src/ruby/spec/generic/rpc_desc_spec.rb +36 -16
  398. data/src/ruby/spec/generic/rpc_server_pool_spec.rb +22 -28
  399. data/src/ruby/spec/generic/rpc_server_spec.rb +6 -6
  400. data/src/ruby/spec/pb/health/checker_spec.rb +27 -19
  401. data/src/ruby/spec/spec_helper.rb +2 -0
  402. data/third_party/boringssl/crypto/aes/aes.c +12 -12
  403. data/third_party/boringssl/crypto/aes/mode_wrappers.c +6 -2
  404. data/third_party/boringssl/crypto/asn1/a_d2i_fp.c +28 -13
  405. data/third_party/boringssl/crypto/asn1/a_gentm.c +2 -0
  406. data/third_party/boringssl/crypto/asn1/a_object.c +7 -3
  407. data/third_party/boringssl/crypto/asn1/a_strnid.c +1 -0
  408. data/third_party/boringssl/crypto/asn1/a_time.c +0 -11
  409. data/third_party/boringssl/crypto/asn1/a_type.c +0 -2
  410. data/third_party/boringssl/crypto/asn1/a_utctm.c +1 -30
  411. data/third_party/boringssl/crypto/asn1/asn1_lib.c +56 -76
  412. data/third_party/boringssl/crypto/asn1/asn1_locl.h +0 -10
  413. data/third_party/boringssl/crypto/asn1/asn1_par.c +0 -322
  414. data/third_party/boringssl/crypto/asn1/f_enum.c +1 -108
  415. data/third_party/boringssl/crypto/asn1/f_int.c +1 -106
  416. data/third_party/boringssl/crypto/asn1/f_string.c +1 -106
  417. data/third_party/boringssl/crypto/asn1/tasn_dec.c +10 -14
  418. data/third_party/boringssl/crypto/asn1/tasn_enc.c +17 -11
  419. data/third_party/boringssl/crypto/asn1/tasn_typ.c +29 -42
  420. data/third_party/boringssl/crypto/asn1/tasn_utl.c +1 -1
  421. data/third_party/boringssl/crypto/base64/base64.c +249 -285
  422. data/third_party/boringssl/crypto/bio/bio.c +13 -23
  423. data/third_party/boringssl/crypto/bio/bio_mem.c +3 -2
  424. data/third_party/boringssl/crypto/bio/connect.c +12 -3
  425. data/third_party/boringssl/crypto/bio/fd.c +22 -15
  426. data/third_party/boringssl/crypto/bio/file.c +2 -38
  427. data/third_party/boringssl/crypto/bio/hexdump.c +1 -2
  428. data/third_party/boringssl/crypto/bio/internal.h +3 -0
  429. data/third_party/boringssl/crypto/bio/pair.c +1 -1
  430. data/third_party/boringssl/crypto/bio/socket.c +10 -2
  431. data/third_party/boringssl/crypto/bio/socket_helper.c +2 -2
  432. data/third_party/boringssl/crypto/bn/asm/x86_64-gcc.c +0 -8
  433. data/third_party/boringssl/crypto/bn/bn.c +38 -0
  434. data/third_party/boringssl/crypto/bn/cmp.c +25 -0
  435. data/third_party/boringssl/crypto/bn/convert.c +73 -76
  436. data/third_party/boringssl/crypto/bn/div.c +136 -70
  437. data/third_party/boringssl/crypto/bn/exponentiation.c +86 -381
  438. data/third_party/boringssl/crypto/bn/gcd.c +213 -296
  439. data/third_party/boringssl/crypto/bn/generic.c +0 -80
  440. data/third_party/boringssl/crypto/bn/internal.h +15 -3
  441. data/third_party/boringssl/crypto/bn/montgomery.c +57 -207
  442. data/third_party/boringssl/crypto/bn/montgomery_inv.c +160 -0
  443. data/third_party/boringssl/crypto/bn/mul.c +2 -1
  444. data/third_party/boringssl/crypto/bn/prime.c +24 -8
  445. data/third_party/boringssl/crypto/bn/random.c +47 -33
  446. data/third_party/boringssl/crypto/bn/sqrt.c +4 -5
  447. data/third_party/boringssl/crypto/buf/buf.c +25 -21
  448. data/third_party/boringssl/crypto/bytestring/ber.c +1 -0
  449. data/third_party/boringssl/crypto/bytestring/cbb.c +50 -22
  450. data/third_party/boringssl/crypto/bytestring/cbs.c +28 -4
  451. data/third_party/boringssl/crypto/chacha/{chacha_generic.c → chacha.c} +56 -29
  452. data/third_party/boringssl/crypto/cipher/aead.c +11 -22
  453. data/third_party/boringssl/crypto/cipher/cipher.c +2 -2
  454. data/third_party/boringssl/crypto/cipher/e_aes.c +53 -103
  455. data/third_party/boringssl/crypto/cipher/e_chacha20poly1305.c +2 -8
  456. data/third_party/boringssl/crypto/cipher/e_des.c +3 -5
  457. data/third_party/boringssl/crypto/cipher/e_null.c +1 -1
  458. data/third_party/boringssl/crypto/cipher/e_rc2.c +1 -1
  459. data/third_party/boringssl/crypto/cipher/e_rc4.c +1 -1
  460. data/third_party/boringssl/crypto/cipher/e_ssl3.c +3 -63
  461. data/third_party/boringssl/crypto/cipher/e_tls.c +12 -83
  462. data/third_party/boringssl/crypto/cipher/internal.h +8 -10
  463. data/third_party/boringssl/crypto/cipher/tls_cbc.c +69 -40
  464. data/third_party/boringssl/crypto/conf/conf.c +2 -1
  465. data/third_party/boringssl/crypto/cpu-aarch64-linux.c +61 -0
  466. data/third_party/boringssl/crypto/cpu-arm-linux.c +360 -0
  467. data/third_party/boringssl/crypto/cpu-arm.c +0 -161
  468. data/third_party/boringssl/crypto/cpu-intel.c +5 -3
  469. data/third_party/boringssl/{ssl/test/scoped_types.h → crypto/cpu-ppc64le.c} +21 -9
  470. data/third_party/boringssl/crypto/crypto.c +29 -7
  471. data/third_party/boringssl/crypto/curve25519/curve25519.c +284 -242
  472. data/third_party/boringssl/crypto/curve25519/internal.h +64 -0
  473. data/third_party/boringssl/crypto/curve25519/spake25519.c +464 -0
  474. data/third_party/boringssl/crypto/curve25519/x25519-x86_64.c +21 -0
  475. data/third_party/boringssl/crypto/dh/check.c +22 -6
  476. data/third_party/boringssl/crypto/dh/dh.c +45 -21
  477. data/third_party/boringssl/crypto/dh/dh_asn1.c +96 -20
  478. data/third_party/boringssl/crypto/dh/params.c +30 -78
  479. data/third_party/boringssl/crypto/digest/digest.c +3 -3
  480. data/third_party/boringssl/crypto/dsa/dsa.c +59 -29
  481. data/third_party/boringssl/crypto/dsa/dsa_asn1.c +4 -0
  482. data/third_party/boringssl/crypto/ec/ec.c +84 -140
  483. data/third_party/boringssl/crypto/ec/ec_asn1.c +82 -52
  484. data/third_party/boringssl/crypto/ec/ec_key.c +15 -15
  485. data/third_party/boringssl/crypto/ec/ec_montgomery.c +87 -50
  486. data/third_party/boringssl/crypto/ec/internal.h +12 -36
  487. data/third_party/boringssl/crypto/ec/oct.c +11 -11
  488. data/third_party/boringssl/crypto/ec/p224-64.c +59 -116
  489. data/third_party/boringssl/crypto/ec/p256-64.c +88 -163
  490. data/third_party/boringssl/crypto/ec/p256-x86_64.c +46 -58
  491. data/third_party/boringssl/crypto/ec/simple.c +81 -201
  492. data/third_party/boringssl/crypto/ec/util-64.c +0 -74
  493. data/third_party/boringssl/crypto/ecdh/ecdh.c +7 -1
  494. data/third_party/boringssl/crypto/ecdsa/ecdsa.c +28 -46
  495. data/third_party/boringssl/crypto/ecdsa/ecdsa_asn1.c +1 -0
  496. data/third_party/boringssl/crypto/engine/engine.c +1 -1
  497. data/third_party/boringssl/crypto/err/err.c +3 -3
  498. data/third_party/boringssl/crypto/evp/evp.c +14 -59
  499. data/third_party/boringssl/crypto/evp/evp_asn1.c +144 -87
  500. data/third_party/boringssl/crypto/evp/evp_ctx.c +7 -7
  501. data/third_party/boringssl/crypto/evp/internal.h +4 -46
  502. data/third_party/boringssl/crypto/evp/p_dsa_asn1.c +8 -157
  503. data/third_party/boringssl/crypto/evp/p_ec.c +1 -1
  504. data/third_party/boringssl/crypto/evp/p_ec_asn1.c +22 -170
  505. data/third_party/boringssl/crypto/evp/p_rsa.c +1 -1
  506. data/third_party/boringssl/crypto/evp/p_rsa_asn1.c +10 -548
  507. data/third_party/boringssl/crypto/evp/print.c +520 -0
  508. data/third_party/boringssl/crypto/ex_data.c +4 -6
  509. data/third_party/boringssl/crypto/hkdf/hkdf.c +38 -17
  510. data/third_party/boringssl/crypto/hmac/hmac.c +6 -6
  511. data/third_party/boringssl/crypto/internal.h +57 -77
  512. data/third_party/boringssl/crypto/lhash/lhash.c +6 -10
  513. data/third_party/boringssl/crypto/md4/md4.c +9 -0
  514. data/third_party/boringssl/crypto/mem.c +19 -19
  515. data/third_party/boringssl/crypto/modes/cfb.c +5 -6
  516. data/third_party/boringssl/crypto/modes/ctr.c +10 -18
  517. data/third_party/boringssl/crypto/modes/gcm.c +100 -66
  518. data/third_party/boringssl/crypto/modes/internal.h +15 -27
  519. data/third_party/boringssl/crypto/modes/ofb.c +9 -22
  520. data/third_party/boringssl/crypto/newhope/error_correction.c +131 -0
  521. data/third_party/boringssl/crypto/newhope/internal.h +71 -0
  522. data/third_party/boringssl/crypto/newhope/newhope.c +174 -0
  523. data/third_party/boringssl/crypto/newhope/ntt.c +148 -0
  524. data/third_party/boringssl/crypto/newhope/poly.c +183 -0
  525. data/third_party/boringssl/crypto/newhope/precomp.c +306 -0
  526. data/third_party/boringssl/crypto/newhope/reduce.c +42 -0
  527. data/third_party/boringssl/crypto/obj/obj.c +111 -135
  528. data/third_party/boringssl/crypto/obj/obj_dat.h +4 -10
  529. data/third_party/boringssl/crypto/pem/pem_lib.c +6 -43
  530. data/third_party/boringssl/crypto/pem/pem_pkey.c +10 -19
  531. data/third_party/boringssl/crypto/pkcs8/p5_pbe.c +1 -0
  532. data/third_party/boringssl/crypto/pkcs8/p5_pbev2.c +2 -1
  533. data/third_party/boringssl/crypto/pkcs8/p8_pkey.c +2 -2
  534. data/third_party/boringssl/crypto/pkcs8/pkcs8.c +95 -87
  535. data/third_party/boringssl/crypto/{test/test_util.h → poly1305/internal.h} +15 -10
  536. data/third_party/boringssl/crypto/poly1305/poly1305.c +8 -15
  537. data/third_party/boringssl/crypto/poly1305/poly1305_arm.c +1 -0
  538. data/third_party/boringssl/crypto/poly1305/poly1305_vec.c +3 -3
  539. data/third_party/boringssl/crypto/rand/deterministic.c +47 -0
  540. data/third_party/boringssl/crypto/rand/rand.c +4 -1
  541. data/third_party/boringssl/crypto/rand/urandom.c +5 -7
  542. data/third_party/boringssl/crypto/rand/windows.c +5 -8
  543. data/third_party/boringssl/crypto/rc4/rc4.c +24 -209
  544. data/third_party/boringssl/crypto/refcount_lock.c +2 -2
  545. data/third_party/boringssl/crypto/rsa/blinding.c +74 -232
  546. data/third_party/boringssl/crypto/rsa/internal.h +5 -13
  547. data/third_party/boringssl/crypto/rsa/padding.c +64 -63
  548. data/third_party/boringssl/crypto/rsa/rsa.c +50 -28
  549. data/third_party/boringssl/crypto/rsa/rsa_asn1.c +8 -16
  550. data/third_party/boringssl/crypto/rsa/rsa_impl.c +134 -122
  551. data/third_party/boringssl/crypto/sha/sha256.c +2 -2
  552. data/third_party/boringssl/crypto/sha/sha512.c +7 -7
  553. data/third_party/boringssl/crypto/stack/stack.c +13 -22
  554. data/third_party/boringssl/crypto/thread.c +21 -12
  555. data/third_party/boringssl/crypto/thread_none.c +6 -2
  556. data/third_party/boringssl/crypto/thread_pthread.c +16 -7
  557. data/third_party/boringssl/crypto/thread_win.c +38 -85
  558. data/third_party/boringssl/crypto/x509/a_sign.c +3 -3
  559. data/third_party/boringssl/crypto/x509/a_strex.c +1 -1
  560. data/third_party/boringssl/crypto/x509/a_verify.c +2 -2
  561. data/third_party/boringssl/crypto/{evp → x509}/algorithm.c +37 -53
  562. data/third_party/boringssl/crypto/x509/asn1_gen.c +1 -2
  563. data/third_party/boringssl/crypto/x509/by_dir.c +6 -6
  564. data/third_party/boringssl/crypto/x509/internal.h +66 -0
  565. data/third_party/boringssl/crypto/x509/rsa_pss.c +385 -0
  566. data/third_party/boringssl/crypto/x509/t_x509.c +10 -12
  567. data/third_party/boringssl/crypto/x509/x509.c +5 -0
  568. data/third_party/boringssl/crypto/x509/x509_att.c +9 -3
  569. data/third_party/boringssl/crypto/x509/x509_lu.c +34 -44
  570. data/third_party/boringssl/crypto/x509/x509_obj.c +19 -2
  571. data/third_party/boringssl/crypto/x509/x509_r2x.c +9 -5
  572. data/third_party/boringssl/crypto/x509/x509_set.c +5 -0
  573. data/third_party/boringssl/crypto/x509/x509_txt.c +5 -0
  574. data/third_party/boringssl/crypto/x509/x509_vfy.c +63 -32
  575. data/third_party/boringssl/crypto/x509/x509_vpm.c +29 -18
  576. data/third_party/boringssl/crypto/x509/x509cset.c +2 -1
  577. data/third_party/boringssl/crypto/x509/x_crl.c +2 -2
  578. data/third_party/boringssl/crypto/x509/x_name.c +14 -17
  579. data/third_party/boringssl/crypto/x509/x_pubkey.c +10 -7
  580. data/third_party/boringssl/crypto/x509/x_x509.c +67 -6
  581. data/third_party/boringssl/crypto/x509v3/pcy_cache.c +2 -2
  582. data/third_party/boringssl/crypto/x509v3/pcy_tree.c +2 -1
  583. data/third_party/boringssl/crypto/x509v3/v3_conf.c +4 -3
  584. data/third_party/boringssl/crypto/x509v3/v3_cpols.c +5 -0
  585. data/third_party/boringssl/crypto/x509v3/v3_prn.c +0 -3
  586. data/third_party/boringssl/crypto/x509v3/v3_purp.c +2 -2
  587. data/third_party/boringssl/crypto/x509v3/v3_utl.c +2 -1
  588. data/third_party/boringssl/include/openssl/aead.h +72 -73
  589. data/third_party/boringssl/include/openssl/arm_arch.h +0 -6
  590. data/third_party/boringssl/include/openssl/asn1.h +103 -235
  591. data/third_party/boringssl/include/openssl/asn1_mac.h +17 -74
  592. data/third_party/boringssl/include/openssl/asn1t.h +1 -11
  593. data/third_party/boringssl/include/openssl/base.h +145 -3
  594. data/third_party/boringssl/include/openssl/base64.h +20 -17
  595. data/third_party/boringssl/include/openssl/bio.h +59 -34
  596. data/third_party/boringssl/include/openssl/bn.h +118 -51
  597. data/third_party/boringssl/include/openssl/buf.h +15 -0
  598. data/third_party/boringssl/include/openssl/bytestring.h +52 -4
  599. data/third_party/boringssl/include/openssl/chacha.h +2 -2
  600. data/third_party/boringssl/include/openssl/cipher.h +18 -1
  601. data/third_party/boringssl/include/openssl/cmac.h +11 -0
  602. data/third_party/boringssl/include/openssl/conf.h +13 -2
  603. data/third_party/boringssl/include/openssl/cpu.h +20 -23
  604. data/third_party/boringssl/include/openssl/crypto.h +22 -1
  605. data/third_party/boringssl/include/openssl/curve25519.h +96 -4
  606. data/third_party/boringssl/include/openssl/dh.h +71 -16
  607. data/third_party/boringssl/include/openssl/digest.h +38 -11
  608. data/third_party/boringssl/include/openssl/dsa.h +40 -4
  609. data/third_party/boringssl/include/openssl/ec.h +44 -18
  610. data/third_party/boringssl/include/openssl/ec_key.h +27 -6
  611. data/third_party/boringssl/include/openssl/ecdsa.h +11 -0
  612. data/third_party/boringssl/include/openssl/engine.h +11 -0
  613. data/third_party/boringssl/include/openssl/evp.h +52 -88
  614. data/third_party/boringssl/include/openssl/hkdf.h +24 -4
  615. data/third_party/boringssl/include/openssl/hmac.h +20 -6
  616. data/third_party/boringssl/include/openssl/md4.h +4 -0
  617. data/third_party/boringssl/include/openssl/mem.h +19 -0
  618. data/third_party/boringssl/include/openssl/newhope.h +158 -0
  619. data/third_party/boringssl/include/openssl/nid.h +4166 -0
  620. data/third_party/boringssl/include/openssl/obj.h +31 -3
  621. data/third_party/boringssl/include/openssl/obj_mac.h +17 -4143
  622. data/third_party/boringssl/include/openssl/{opensslfeatures.h → opensslconf.h} +3 -3
  623. data/third_party/boringssl/include/openssl/pem.h +5 -0
  624. data/third_party/boringssl/include/openssl/pkcs8.h +12 -0
  625. data/third_party/boringssl/include/openssl/rand.h +6 -0
  626. data/third_party/boringssl/include/openssl/rc4.h +6 -0
  627. data/third_party/boringssl/{crypto/dh/internal.h → include/openssl/ripemd.h} +38 -11
  628. data/third_party/boringssl/include/openssl/rsa.h +127 -65
  629. data/third_party/boringssl/include/openssl/sha.h +14 -10
  630. data/third_party/boringssl/include/openssl/ssl.h +561 -275
  631. data/third_party/boringssl/include/openssl/ssl3.h +18 -25
  632. data/third_party/boringssl/include/openssl/stack.h +2 -4
  633. data/third_party/boringssl/include/openssl/stack_macros.h +321 -353
  634. data/third_party/boringssl/include/openssl/thread.h +31 -13
  635. data/third_party/boringssl/include/openssl/time_support.h +1 -0
  636. data/third_party/boringssl/include/openssl/tls1.h +37 -33
  637. data/third_party/boringssl/include/openssl/x509.h +69 -26
  638. data/third_party/boringssl/include/openssl/x509_vfy.h +12 -10
  639. data/third_party/boringssl/include/openssl/x509v3.h +23 -2
  640. data/third_party/boringssl/ssl/custom_extensions.c +3 -5
  641. data/third_party/boringssl/ssl/d1_both.c +463 -499
  642. data/third_party/boringssl/ssl/d1_lib.c +38 -109
  643. data/third_party/boringssl/ssl/d1_pkt.c +173 -334
  644. data/third_party/boringssl/ssl/d1_srtp.c +20 -18
  645. data/third_party/boringssl/ssl/{d1_meth.c → dtls_method.c} +88 -15
  646. data/third_party/boringssl/ssl/dtls_record.c +27 -26
  647. data/third_party/boringssl/ssl/{s3_clnt.c → handshake_client.c} +816 -904
  648. data/third_party/boringssl/ssl/handshake_server.c +1932 -0
  649. data/third_party/boringssl/ssl/internal.h +712 -439
  650. data/third_party/boringssl/ssl/s3_both.c +445 -257
  651. data/third_party/boringssl/ssl/s3_enc.c +53 -36
  652. data/third_party/boringssl/ssl/s3_lib.c +23 -268
  653. data/third_party/boringssl/ssl/s3_pkt.c +168 -364
  654. data/third_party/boringssl/ssl/ssl_aead_ctx.c +46 -17
  655. data/third_party/boringssl/ssl/ssl_asn1.c +56 -26
  656. data/third_party/boringssl/ssl/ssl_buffer.c +16 -24
  657. data/third_party/boringssl/ssl/ssl_cert.c +324 -49
  658. data/third_party/boringssl/ssl/ssl_cipher.c +205 -150
  659. data/third_party/boringssl/ssl/ssl_ecdh.c +287 -51
  660. data/third_party/boringssl/ssl/ssl_file.c +21 -68
  661. data/third_party/boringssl/ssl/ssl_lib.c +881 -510
  662. data/third_party/boringssl/ssl/ssl_rsa.c +404 -34
  663. data/third_party/boringssl/ssl/ssl_session.c +324 -103
  664. data/third_party/boringssl/ssl/ssl_stat.c +6 -88
  665. data/third_party/boringssl/ssl/t1_enc.c +23 -39
  666. data/third_party/boringssl/ssl/t1_lib.c +1120 -622
  667. data/third_party/boringssl/ssl/tls13_both.c +440 -0
  668. data/third_party/boringssl/ssl/tls13_client.c +682 -0
  669. data/third_party/boringssl/ssl/tls13_enc.c +391 -0
  670. data/third_party/boringssl/ssl/tls13_server.c +672 -0
  671. data/third_party/boringssl/ssl/{s3_meth.c → tls_method.c} +100 -21
  672. data/third_party/boringssl/ssl/tls_record.c +159 -77
  673. data/third_party/nanopb/pb.h +60 -28
  674. data/third_party/nanopb/pb_decode.c +120 -92
  675. data/third_party/nanopb/pb_decode.h +3 -3
  676. data/third_party/nanopb/pb_encode.c +73 -67
  677. data/third_party/nanopb/pb_encode.h +4 -4
  678. metadata +155 -89
  679. data/include/grpc/impl/codegen/byte_buffer.h +0 -122
  680. data/include/grpc/impl/codegen/log.h +0 -118
  681. data/include/grpc/impl/codegen/time.h +0 -130
  682. data/src/core/ext/client_config/client_channel.c +0 -593
  683. data/src/core/ext/client_config/subchannel_call_holder.c +0 -272
  684. data/src/core/ext/client_config/subchannel_call_holder.h +0 -99
  685. data/src/core/lib/iomgr/ev_poll_and_epoll_posix.c +0 -2046
  686. data/src/core/lib/iomgr/workqueue_posix.c +0 -151
  687. data/src/core/lib/security/transport/handshake.c +0 -368
  688. data/third_party/boringssl/crypto/asn1/a_bytes.c +0 -308
  689. data/third_party/boringssl/crypto/asn1/bio_asn1.c +0 -477
  690. data/third_party/boringssl/crypto/asn1/bio_ndef.c +0 -251
  691. data/third_party/boringssl/crypto/asn1/t_pkey.c +0 -110
  692. data/third_party/boringssl/crypto/asn1/tasn_prn.c +0 -596
  693. data/third_party/boringssl/crypto/chacha/chacha_vec.c +0 -328
  694. data/third_party/boringssl/crypto/directory.h +0 -66
  695. data/third_party/boringssl/crypto/directory_posix.c +0 -108
  696. data/third_party/boringssl/crypto/directory_win.c +0 -144
  697. data/third_party/boringssl/crypto/test/scoped_types.h +0 -140
  698. data/third_party/boringssl/include/openssl/pqueue.h +0 -146
  699. data/third_party/boringssl/ssl/d1_clnt.c +0 -561
  700. data/third_party/boringssl/ssl/d1_srvr.c +0 -476
  701. data/third_party/boringssl/ssl/pqueue/pqueue.c +0 -197
  702. data/third_party/boringssl/ssl/s3_srvr.c +0 -2272
  703. data/third_party/boringssl/ssl/test/async_bio.h +0 -45
  704. data/third_party/boringssl/ssl/test/packeted_bio.h +0 -44
  705. data/third_party/boringssl/ssl/test/test_config.h +0 -110
@@ -1,272 +0,0 @@
1
- /*
2
- *
3
- * Copyright 2015, Google Inc.
4
- * All rights reserved.
5
- *
6
- * Redistribution and use in source and binary forms, with or without
7
- * modification, are permitted provided that the following conditions are
8
- * met:
9
- *
10
- * * Redistributions of source code must retain the above copyright
11
- * notice, this list of conditions and the following disclaimer.
12
- * * Redistributions in binary form must reproduce the above
13
- * copyright notice, this list of conditions and the following disclaimer
14
- * in the documentation and/or other materials provided with the
15
- * distribution.
16
- * * Neither the name of Google Inc. nor the names of its
17
- * contributors may be used to endorse or promote products derived from
18
- * this software without specific prior written permission.
19
- *
20
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24
- * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
- *
32
- */
33
-
34
- #include "src/core/ext/client_config/subchannel_call_holder.h"
35
-
36
- #include <grpc/support/alloc.h>
37
-
38
- #include "src/core/lib/profiling/timers.h"
39
-
40
- #define GET_CALL(holder) \
41
- ((grpc_subchannel_call *)(gpr_atm_acq_load(&(holder)->subchannel_call)))
42
-
43
- #define CANCELLED_CALL ((grpc_subchannel_call *)1)
44
-
45
- static void subchannel_ready(grpc_exec_ctx *exec_ctx, void *holder,
46
- grpc_error *error);
47
- static void retry_ops(grpc_exec_ctx *exec_ctx, void *retry_ops_args,
48
- grpc_error *error);
49
-
50
- static void add_waiting_locked(grpc_subchannel_call_holder *holder,
51
- grpc_transport_stream_op *op);
52
- static void fail_locked(grpc_exec_ctx *exec_ctx,
53
- grpc_subchannel_call_holder *holder, grpc_error *error);
54
- static void retry_waiting_locked(grpc_exec_ctx *exec_ctx,
55
- grpc_subchannel_call_holder *holder);
56
-
57
- void grpc_subchannel_call_holder_init(
58
- grpc_subchannel_call_holder *holder,
59
- grpc_subchannel_call_holder_pick_subchannel pick_subchannel,
60
- void *pick_subchannel_arg, grpc_call_stack *owning_call) {
61
- gpr_atm_rel_store(&holder->subchannel_call, 0);
62
- holder->pick_subchannel = pick_subchannel;
63
- holder->pick_subchannel_arg = pick_subchannel_arg;
64
- gpr_mu_init(&holder->mu);
65
- holder->connected_subchannel = NULL;
66
- holder->waiting_ops = NULL;
67
- holder->waiting_ops_count = 0;
68
- holder->waiting_ops_capacity = 0;
69
- holder->creation_phase = GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING;
70
- holder->owning_call = owning_call;
71
- holder->pollent = NULL;
72
- }
73
-
74
- void grpc_subchannel_call_holder_destroy(grpc_exec_ctx *exec_ctx,
75
- grpc_subchannel_call_holder *holder) {
76
- grpc_subchannel_call *call = GET_CALL(holder);
77
- if (call != NULL && call != CANCELLED_CALL) {
78
- GRPC_SUBCHANNEL_CALL_UNREF(exec_ctx, call, "holder");
79
- }
80
- GPR_ASSERT(holder->creation_phase ==
81
- GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING);
82
- gpr_mu_destroy(&holder->mu);
83
- GPR_ASSERT(holder->waiting_ops_count == 0);
84
- gpr_free(holder->waiting_ops);
85
- }
86
-
87
- void grpc_subchannel_call_holder_perform_op(grpc_exec_ctx *exec_ctx,
88
- grpc_subchannel_call_holder *holder,
89
- grpc_transport_stream_op *op) {
90
- /* try to (atomically) get the call */
91
- grpc_subchannel_call *call = GET_CALL(holder);
92
- GPR_TIMER_BEGIN("grpc_subchannel_call_holder_perform_op", 0);
93
- if (call == CANCELLED_CALL) {
94
- grpc_transport_stream_op_finish_with_failure(exec_ctx, op,
95
- GRPC_ERROR_CANCELLED);
96
- GPR_TIMER_END("grpc_subchannel_call_holder_perform_op", 0);
97
- return;
98
- }
99
- if (call != NULL) {
100
- grpc_subchannel_call_process_op(exec_ctx, call, op);
101
- GPR_TIMER_END("grpc_subchannel_call_holder_perform_op", 0);
102
- return;
103
- }
104
- /* we failed; lock and figure out what to do */
105
- gpr_mu_lock(&holder->mu);
106
- retry:
107
- /* need to recheck that another thread hasn't set the call */
108
- call = GET_CALL(holder);
109
- if (call == CANCELLED_CALL) {
110
- gpr_mu_unlock(&holder->mu);
111
- grpc_transport_stream_op_finish_with_failure(exec_ctx, op,
112
- GRPC_ERROR_CANCELLED);
113
- GPR_TIMER_END("grpc_subchannel_call_holder_perform_op", 0);
114
- return;
115
- }
116
- if (call != NULL) {
117
- gpr_mu_unlock(&holder->mu);
118
- grpc_subchannel_call_process_op(exec_ctx, call, op);
119
- GPR_TIMER_END("grpc_subchannel_call_holder_perform_op", 0);
120
- return;
121
- }
122
- /* if this is a cancellation, then we can raise our cancelled flag */
123
- if (op->cancel_error != GRPC_ERROR_NONE) {
124
- if (!gpr_atm_rel_cas(&holder->subchannel_call, 0, 1)) {
125
- goto retry;
126
- } else {
127
- switch (holder->creation_phase) {
128
- case GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING:
129
- fail_locked(exec_ctx, holder, GRPC_ERROR_REF(op->cancel_error));
130
- break;
131
- case GRPC_SUBCHANNEL_CALL_HOLDER_PICKING_SUBCHANNEL:
132
- holder->pick_subchannel(exec_ctx, holder->pick_subchannel_arg, NULL,
133
- 0, &holder->connected_subchannel, NULL);
134
- break;
135
- }
136
- gpr_mu_unlock(&holder->mu);
137
- grpc_transport_stream_op_finish_with_failure(exec_ctx, op,
138
- GRPC_ERROR_CANCELLED);
139
- GPR_TIMER_END("grpc_subchannel_call_holder_perform_op", 0);
140
- return;
141
- }
142
- }
143
- /* if we don't have a subchannel, try to get one */
144
- if (holder->creation_phase == GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING &&
145
- holder->connected_subchannel == NULL &&
146
- op->send_initial_metadata != NULL) {
147
- holder->creation_phase = GRPC_SUBCHANNEL_CALL_HOLDER_PICKING_SUBCHANNEL;
148
- grpc_closure_init(&holder->next_step, subchannel_ready, holder);
149
- GRPC_CALL_STACK_REF(holder->owning_call, "pick_subchannel");
150
- if (holder->pick_subchannel(
151
- exec_ctx, holder->pick_subchannel_arg, op->send_initial_metadata,
152
- op->send_initial_metadata_flags, &holder->connected_subchannel,
153
- &holder->next_step)) {
154
- holder->creation_phase = GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING;
155
- GRPC_CALL_STACK_UNREF(exec_ctx, holder->owning_call, "pick_subchannel");
156
- }
157
- }
158
- /* if we've got a subchannel, then let's ask it to create a call */
159
- if (holder->creation_phase == GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING &&
160
- holder->connected_subchannel != NULL) {
161
- gpr_atm_rel_store(
162
- &holder->subchannel_call,
163
- (gpr_atm)(uintptr_t)grpc_connected_subchannel_create_call(
164
- exec_ctx, holder->connected_subchannel, holder->pollent));
165
- retry_waiting_locked(exec_ctx, holder);
166
- goto retry;
167
- }
168
- /* nothing to be done but wait */
169
- add_waiting_locked(holder, op);
170
- gpr_mu_unlock(&holder->mu);
171
- GPR_TIMER_END("grpc_subchannel_call_holder_perform_op", 0);
172
- }
173
-
174
- static void subchannel_ready(grpc_exec_ctx *exec_ctx, void *arg,
175
- grpc_error *error) {
176
- grpc_subchannel_call_holder *holder = arg;
177
- gpr_mu_lock(&holder->mu);
178
- GPR_ASSERT(holder->creation_phase ==
179
- GRPC_SUBCHANNEL_CALL_HOLDER_PICKING_SUBCHANNEL);
180
- holder->creation_phase = GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING;
181
- if (holder->connected_subchannel == NULL) {
182
- gpr_atm_no_barrier_store(&holder->subchannel_call, 1);
183
- fail_locked(exec_ctx, holder,
184
- GRPC_ERROR_CREATE_REFERENCING("Failed to create subchannel",
185
- &error, 1));
186
- } else if (1 == gpr_atm_acq_load(&holder->subchannel_call)) {
187
- /* already cancelled before subchannel became ready */
188
- fail_locked(exec_ctx, holder,
189
- GRPC_ERROR_CREATE_REFERENCING(
190
- "Cancelled before creating subchannel", &error, 1));
191
- } else {
192
- gpr_atm_rel_store(
193
- &holder->subchannel_call,
194
- (gpr_atm)(uintptr_t)grpc_connected_subchannel_create_call(
195
- exec_ctx, holder->connected_subchannel, holder->pollent));
196
- retry_waiting_locked(exec_ctx, holder);
197
- }
198
- gpr_mu_unlock(&holder->mu);
199
- GRPC_CALL_STACK_UNREF(exec_ctx, holder->owning_call, "pick_subchannel");
200
- }
201
-
202
- typedef struct {
203
- grpc_transport_stream_op *ops;
204
- size_t nops;
205
- grpc_subchannel_call *call;
206
- } retry_ops_args;
207
-
208
- static void retry_waiting_locked(grpc_exec_ctx *exec_ctx,
209
- grpc_subchannel_call_holder *holder) {
210
- retry_ops_args *a = gpr_malloc(sizeof(*a));
211
- a->ops = holder->waiting_ops;
212
- a->nops = holder->waiting_ops_count;
213
- a->call = GET_CALL(holder);
214
- if (a->call == CANCELLED_CALL) {
215
- gpr_free(a);
216
- fail_locked(exec_ctx, holder, GRPC_ERROR_CANCELLED);
217
- return;
218
- }
219
- holder->waiting_ops = NULL;
220
- holder->waiting_ops_count = 0;
221
- holder->waiting_ops_capacity = 0;
222
- GRPC_SUBCHANNEL_CALL_REF(a->call, "retry_ops");
223
- grpc_exec_ctx_sched(exec_ctx, grpc_closure_create(retry_ops, a),
224
- GRPC_ERROR_NONE, NULL);
225
- }
226
-
227
- static void retry_ops(grpc_exec_ctx *exec_ctx, void *args, grpc_error *error) {
228
- retry_ops_args *a = args;
229
- size_t i;
230
- for (i = 0; i < a->nops; i++) {
231
- grpc_subchannel_call_process_op(exec_ctx, a->call, &a->ops[i]);
232
- }
233
- GRPC_SUBCHANNEL_CALL_UNREF(exec_ctx, a->call, "retry_ops");
234
- gpr_free(a->ops);
235
- gpr_free(a);
236
- }
237
-
238
- static void add_waiting_locked(grpc_subchannel_call_holder *holder,
239
- grpc_transport_stream_op *op) {
240
- GPR_TIMER_BEGIN("add_waiting_locked", 0);
241
- if (holder->waiting_ops_count == holder->waiting_ops_capacity) {
242
- holder->waiting_ops_capacity = GPR_MAX(3, 2 * holder->waiting_ops_capacity);
243
- holder->waiting_ops =
244
- gpr_realloc(holder->waiting_ops, holder->waiting_ops_capacity *
245
- sizeof(*holder->waiting_ops));
246
- }
247
- holder->waiting_ops[holder->waiting_ops_count++] = *op;
248
- GPR_TIMER_END("add_waiting_locked", 0);
249
- }
250
-
251
- static void fail_locked(grpc_exec_ctx *exec_ctx,
252
- grpc_subchannel_call_holder *holder,
253
- grpc_error *error) {
254
- size_t i;
255
- for (i = 0; i < holder->waiting_ops_count; i++) {
256
- grpc_transport_stream_op_finish_with_failure(
257
- exec_ctx, &holder->waiting_ops[i], GRPC_ERROR_REF(error));
258
- }
259
- holder->waiting_ops_count = 0;
260
- GRPC_ERROR_UNREF(error);
261
- }
262
-
263
- char *grpc_subchannel_call_holder_get_peer(
264
- grpc_exec_ctx *exec_ctx, grpc_subchannel_call_holder *holder) {
265
- grpc_subchannel_call *subchannel_call = GET_CALL(holder);
266
-
267
- if (subchannel_call == NULL || subchannel_call == CANCELLED_CALL) {
268
- return NULL;
269
- } else {
270
- return grpc_subchannel_call_get_peer(exec_ctx, subchannel_call);
271
- }
272
- }
@@ -1,99 +0,0 @@
1
- /*
2
- *
3
- * Copyright 2015, Google Inc.
4
- * All rights reserved.
5
- *
6
- * Redistribution and use in source and binary forms, with or without
7
- * modification, are permitted provided that the following conditions are
8
- * met:
9
- *
10
- * * Redistributions of source code must retain the above copyright
11
- * notice, this list of conditions and the following disclaimer.
12
- * * Redistributions in binary form must reproduce the above
13
- * copyright notice, this list of conditions and the following disclaimer
14
- * in the documentation and/or other materials provided with the
15
- * distribution.
16
- * * Neither the name of Google Inc. nor the names of its
17
- * contributors may be used to endorse or promote products derived from
18
- * this software without specific prior written permission.
19
- *
20
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24
- * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
- *
32
- */
33
-
34
- #ifndef GRPC_CORE_EXT_CLIENT_CONFIG_SUBCHANNEL_CALL_HOLDER_H
35
- #define GRPC_CORE_EXT_CLIENT_CONFIG_SUBCHANNEL_CALL_HOLDER_H
36
-
37
- #include "src/core/ext/client_config/subchannel.h"
38
- #include "src/core/lib/iomgr/polling_entity.h"
39
-
40
- /** Pick a subchannel for grpc_subchannel_call_holder;
41
- Return 1 if subchannel is available immediately (in which case on_ready
42
- should not be called), or 0 otherwise (in which case on_ready should be
43
- called when the subchannel is available) */
44
- typedef int (*grpc_subchannel_call_holder_pick_subchannel)(
45
- grpc_exec_ctx *exec_ctx, void *arg, grpc_metadata_batch *initial_metadata,
46
- uint32_t initial_metadata_flags,
47
- grpc_connected_subchannel **connected_subchannel, grpc_closure *on_ready);
48
-
49
- typedef enum {
50
- GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING,
51
- GRPC_SUBCHANNEL_CALL_HOLDER_PICKING_SUBCHANNEL
52
- } grpc_subchannel_call_holder_creation_phase;
53
-
54
- /** Wrapper for holding a pointer to grpc_subchannel_call, and the
55
- associated machinery to create such a pointer.
56
- Handles queueing of stream ops until a call object is ready, waiting
57
- for initial metadata before trying to create a call object,
58
- and handling cancellation gracefully.
59
-
60
- The channel filter uses this as their call_data. */
61
- typedef struct grpc_subchannel_call_holder {
62
- /** either 0 for no call, 1 for cancelled, or a pointer to a
63
- grpc_subchannel_call */
64
- gpr_atm subchannel_call;
65
- /** Helper function to choose the subchannel on which to create
66
- the call object. Channel filter delegates to the load
67
- balancing policy (once it's ready). */
68
- grpc_subchannel_call_holder_pick_subchannel pick_subchannel;
69
- void *pick_subchannel_arg;
70
-
71
- gpr_mu mu;
72
-
73
- grpc_subchannel_call_holder_creation_phase creation_phase;
74
- grpc_connected_subchannel *connected_subchannel;
75
- grpc_polling_entity *pollent;
76
-
77
- grpc_transport_stream_op *waiting_ops;
78
- size_t waiting_ops_count;
79
- size_t waiting_ops_capacity;
80
-
81
- grpc_closure next_step;
82
-
83
- grpc_call_stack *owning_call;
84
- } grpc_subchannel_call_holder;
85
-
86
- void grpc_subchannel_call_holder_init(
87
- grpc_subchannel_call_holder *holder,
88
- grpc_subchannel_call_holder_pick_subchannel pick_subchannel,
89
- void *pick_subchannel_arg, grpc_call_stack *owning_call);
90
- void grpc_subchannel_call_holder_destroy(grpc_exec_ctx *exec_ctx,
91
- grpc_subchannel_call_holder *holder);
92
-
93
- void grpc_subchannel_call_holder_perform_op(grpc_exec_ctx *exec_ctx,
94
- grpc_subchannel_call_holder *holder,
95
- grpc_transport_stream_op *op);
96
- char *grpc_subchannel_call_holder_get_peer(grpc_exec_ctx *exec_ctx,
97
- grpc_subchannel_call_holder *holder);
98
-
99
- #endif /* GRPC_CORE_EXT_CLIENT_CONFIG_SUBCHANNEL_CALL_HOLDER_H */
@@ -1,2046 +0,0 @@
1
- /*
2
- *
3
- * Copyright 2015, Google Inc.
4
- * All rights reserved.
5
- *
6
- * Redistribution and use in source and binary forms, with or without
7
- * modification, are permitted provided that the following conditions are
8
- * met:
9
- *
10
- * * Redistributions of source code must retain the above copyright
11
- * notice, this list of conditions and the following disclaimer.
12
- * * Redistributions in binary form must reproduce the above
13
- * copyright notice, this list of conditions and the following disclaimer
14
- * in the documentation and/or other materials provided with the
15
- * distribution.
16
- * * Neither the name of Google Inc. nor the names of its
17
- * contributors may be used to endorse or promote products derived from
18
- * this software without specific prior written permission.
19
- *
20
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24
- * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
- *
32
- */
33
-
34
- /* This file will be removed shortly: it's here to keep refactoring
35
- * steps simple and auditable.
36
- * It's the combination of the old files:
37
- * - fd_posix.{h,c}
38
- * - pollset_posix.{h,c}
39
- * - pullset_multipoller_with_{poll,epoll}.{h,c}
40
- * The new version will be split into:
41
- * - ev_poll_posix.{h,c}
42
- * - ev_epoll_posix.{h,c}
43
- */
44
-
45
- #include <grpc/support/port_platform.h>
46
-
47
- #ifdef GPR_POSIX_SOCKET
48
-
49
- #include "src/core/lib/iomgr/ev_poll_and_epoll_posix.h"
50
-
51
- #include <assert.h>
52
- #include <errno.h>
53
- #include <poll.h>
54
- #include <string.h>
55
- #include <sys/socket.h>
56
- #include <unistd.h>
57
-
58
- #include <grpc/support/alloc.h>
59
- #include <grpc/support/log.h>
60
- #include <grpc/support/string_util.h>
61
- #include <grpc/support/tls.h>
62
- #include <grpc/support/useful.h>
63
-
64
- #include "src/core/lib/iomgr/iomgr_internal.h"
65
- #include "src/core/lib/iomgr/wakeup_fd_posix.h"
66
- #include "src/core/lib/profiling/timers.h"
67
- #include "src/core/lib/support/block_annotate.h"
68
-
69
- /*******************************************************************************
70
- * FD declarations
71
- */
72
-
73
- typedef struct grpc_fd_watcher {
74
- struct grpc_fd_watcher *next;
75
- struct grpc_fd_watcher *prev;
76
- grpc_pollset *pollset;
77
- grpc_pollset_worker *worker;
78
- grpc_fd *fd;
79
- } grpc_fd_watcher;
80
-
81
- struct grpc_fd {
82
- int fd;
83
- /* refst format:
84
- bit0: 1=active/0=orphaned
85
- bit1-n: refcount
86
- meaning that mostly we ref by two to avoid altering the orphaned bit,
87
- and just unref by 1 when we're ready to flag the object as orphaned */
88
- gpr_atm refst;
89
-
90
- gpr_mu mu;
91
- int shutdown;
92
- int closed;
93
- int released;
94
-
95
- /* The watcher list.
96
-
97
- The following watcher related fields are protected by watcher_mu.
98
-
99
- An fd_watcher is an ephemeral object created when an fd wants to
100
- begin polling, and destroyed after the poll.
101
-
102
- It denotes the fd's interest in whether to read poll or write poll
103
- or both or neither on this fd.
104
-
105
- If a watcher is asked to poll for reads or writes, the read_watcher
106
- or write_watcher fields are set respectively. A watcher may be asked
107
- to poll for both, in which case both fields will be set.
108
-
109
- read_watcher and write_watcher may be NULL if no watcher has been
110
- asked to poll for reads or writes.
111
-
112
- If an fd_watcher is not asked to poll for reads or writes, it's added
113
- to a linked list of inactive watchers, rooted at inactive_watcher_root.
114
- If at a later time there becomes need of a poller to poll, one of
115
- the inactive pollers may be kicked out of their poll loops to take
116
- that responsibility. */
117
- grpc_fd_watcher inactive_watcher_root;
118
- grpc_fd_watcher *read_watcher;
119
- grpc_fd_watcher *write_watcher;
120
-
121
- grpc_closure *read_closure;
122
- grpc_closure *write_closure;
123
-
124
- struct grpc_fd *freelist_next;
125
-
126
- grpc_closure *on_done_closure;
127
-
128
- grpc_iomgr_object iomgr_object;
129
-
130
- /* The pollset that last noticed and notified that the fd is readable */
131
- grpc_pollset *read_notifier_pollset;
132
- };
133
-
134
- /* Begin polling on an fd.
135
- Registers that the given pollset is interested in this fd - so that if read
136
- or writability interest changes, the pollset can be kicked to pick up that
137
- new interest.
138
- Return value is:
139
- (fd_needs_read? read_mask : 0) | (fd_needs_write? write_mask : 0)
140
- i.e. a combination of read_mask and write_mask determined by the fd's current
141
- interest in said events.
142
- Polling strategies that do not need to alter their behavior depending on the
143
- fd's current interest (such as epoll) do not need to call this function.
144
- MUST NOT be called with a pollset lock taken */
145
- static uint32_t fd_begin_poll(grpc_fd *fd, grpc_pollset *pollset,
146
- grpc_pollset_worker *worker, uint32_t read_mask,
147
- uint32_t write_mask, grpc_fd_watcher *rec);
148
- /* Complete polling previously started with fd_begin_poll
149
- MUST NOT be called with a pollset lock taken
150
- if got_read or got_write are 1, also does the become_{readable,writable} as
151
- appropriate. */
152
- static void fd_end_poll(grpc_exec_ctx *exec_ctx, grpc_fd_watcher *rec,
153
- int got_read, int got_write,
154
- grpc_pollset *read_notifier_pollset);
155
-
156
- /* Return 1 if this fd is orphaned, 0 otherwise */
157
- static bool fd_is_orphaned(grpc_fd *fd);
158
-
159
- /* Reference counting for fds */
160
- /*#define GRPC_FD_REF_COUNT_DEBUG*/
161
- #ifdef GRPC_FD_REF_COUNT_DEBUG
162
- static void fd_ref(grpc_fd *fd, const char *reason, const char *file, int line);
163
- static void fd_unref(grpc_fd *fd, const char *reason, const char *file,
164
- int line);
165
- #define GRPC_FD_REF(fd, reason) fd_ref(fd, reason, __FILE__, __LINE__)
166
- #define GRPC_FD_UNREF(fd, reason) fd_unref(fd, reason, __FILE__, __LINE__)
167
- #else
168
- static void fd_ref(grpc_fd *fd);
169
- static void fd_unref(grpc_fd *fd);
170
- #define GRPC_FD_REF(fd, reason) fd_ref(fd)
171
- #define GRPC_FD_UNREF(fd, reason) fd_unref(fd)
172
- #endif
173
-
174
- static void fd_global_init(void);
175
- static void fd_global_shutdown(void);
176
-
177
- #define CLOSURE_NOT_READY ((grpc_closure *)0)
178
- #define CLOSURE_READY ((grpc_closure *)1)
179
-
180
- /*******************************************************************************
181
- * pollset declarations
182
- */
183
-
184
- typedef struct grpc_pollset_vtable grpc_pollset_vtable;
185
-
186
- typedef struct grpc_cached_wakeup_fd {
187
- grpc_wakeup_fd fd;
188
- struct grpc_cached_wakeup_fd *next;
189
- } grpc_cached_wakeup_fd;
190
-
191
- struct grpc_pollset_worker {
192
- grpc_cached_wakeup_fd *wakeup_fd;
193
- int reevaluate_polling_on_wakeup;
194
- int kicked_specifically;
195
- struct grpc_pollset_worker *next;
196
- struct grpc_pollset_worker *prev;
197
- };
198
-
199
- struct grpc_pollset {
200
- /* pollsets under posix can mutate representation as fds are added and
201
- removed.
202
- For example, we may choose a poll() based implementation on linux for
203
- few fds, and an epoll() based implementation for many fds */
204
- const grpc_pollset_vtable *vtable;
205
- gpr_mu mu;
206
- grpc_pollset_worker root_worker;
207
- int in_flight_cbs;
208
- int shutting_down;
209
- int called_shutdown;
210
- int kicked_without_pollers;
211
- grpc_closure *shutdown_done;
212
- grpc_closure_list idle_jobs;
213
- union {
214
- int fd;
215
- void *ptr;
216
- } data;
217
- /* Local cache of eventfds for workers */
218
- grpc_cached_wakeup_fd *local_wakeup_cache;
219
- };
220
-
221
- struct grpc_pollset_vtable {
222
- void (*add_fd)(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
223
- struct grpc_fd *fd, int and_unlock_pollset);
224
- grpc_error *(*maybe_work_and_unlock)(grpc_exec_ctx *exec_ctx,
225
- grpc_pollset *pollset,
226
- grpc_pollset_worker *worker,
227
- gpr_timespec deadline, gpr_timespec now);
228
- void (*finish_shutdown)(grpc_pollset *pollset);
229
- void (*destroy)(grpc_pollset *pollset);
230
- };
231
-
232
- /* Add an fd to a pollset */
233
- static void pollset_add_fd(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
234
- struct grpc_fd *fd);
235
-
236
- static void pollset_set_add_fd(grpc_exec_ctx *exec_ctx,
237
- grpc_pollset_set *pollset_set, grpc_fd *fd);
238
-
239
- /* Convert a timespec to milliseconds:
240
- - very small or negative poll times are clamped to zero to do a
241
- non-blocking poll (which becomes spin polling)
242
- - other small values are rounded up to one millisecond
243
- - longer than a millisecond polls are rounded up to the next nearest
244
- millisecond to avoid spinning
245
- - infinite timeouts are converted to -1 */
246
- static int poll_deadline_to_millis_timeout(gpr_timespec deadline,
247
- gpr_timespec now);
248
-
249
- /* Allow kick to wakeup the currently polling worker */
250
- #define GRPC_POLLSET_CAN_KICK_SELF 1
251
- /* Force the wakee to repoll when awoken */
252
- #define GRPC_POLLSET_REEVALUATE_POLLING_ON_WAKEUP 2
253
- /* As per pollset_kick, with an extended set of flags (defined above)
254
- -- mostly for fd_posix's use. */
255
- static grpc_error *pollset_kick_ext(grpc_pollset *p,
256
- grpc_pollset_worker *specific_worker,
257
- uint32_t flags) GRPC_MUST_USE_RESULT;
258
-
259
- /* turn a pollset into a multipoller: platform specific */
260
- typedef void (*platform_become_multipoller_type)(grpc_exec_ctx *exec_ctx,
261
- grpc_pollset *pollset,
262
- struct grpc_fd **fds,
263
- size_t fd_count);
264
- static platform_become_multipoller_type platform_become_multipoller;
265
-
266
- /* Return 1 if the pollset has active threads in pollset_work (pollset must
267
- * be locked) */
268
- static int pollset_has_workers(grpc_pollset *pollset);
269
-
270
- static void remove_fd_from_all_epoll_sets(int fd);
271
-
272
- /*******************************************************************************
273
- * pollset_set definitions
274
- */
275
-
276
- struct grpc_pollset_set {
277
- gpr_mu mu;
278
-
279
- size_t pollset_count;
280
- size_t pollset_capacity;
281
- grpc_pollset **pollsets;
282
-
283
- size_t pollset_set_count;
284
- size_t pollset_set_capacity;
285
- struct grpc_pollset_set **pollset_sets;
286
-
287
- size_t fd_count;
288
- size_t fd_capacity;
289
- grpc_fd **fds;
290
- };
291
-
292
- /*******************************************************************************
293
- * fd_posix.c
294
- */
295
-
296
- /* We need to keep a freelist not because of any concerns of malloc performance
297
- * but instead so that implementations with multiple threads in (for example)
298
- * epoll_wait deal with the race between pollset removal and incoming poll
299
- * notifications.
300
- *
301
- * The problem is that the poller ultimately holds a reference to this
302
- * object, so it is very difficult to know when is safe to free it, at least
303
- * without some expensive synchronization.
304
- *
305
- * If we keep the object freelisted, in the worst case losing this race just
306
- * becomes a spurious read notification on a reused fd.
307
- */
308
- /* TODO(klempner): We could use some form of polling generation count to know
309
- * when these are safe to free. */
310
- /* TODO(klempner): Consider disabling freelisting if we don't have multiple
311
- * threads in poll on the same fd */
312
- /* TODO(klempner): Batch these allocations to reduce fragmentation */
313
- static grpc_fd *fd_freelist = NULL;
314
- static gpr_mu fd_freelist_mu;
315
-
316
- static void freelist_fd(grpc_fd *fd) {
317
- gpr_mu_lock(&fd_freelist_mu);
318
- fd->freelist_next = fd_freelist;
319
- fd_freelist = fd;
320
- grpc_iomgr_unregister_object(&fd->iomgr_object);
321
- gpr_mu_unlock(&fd_freelist_mu);
322
- }
323
-
324
- static grpc_fd *alloc_fd(int fd) {
325
- grpc_fd *r = NULL;
326
- gpr_mu_lock(&fd_freelist_mu);
327
- if (fd_freelist != NULL) {
328
- r = fd_freelist;
329
- fd_freelist = fd_freelist->freelist_next;
330
- }
331
- gpr_mu_unlock(&fd_freelist_mu);
332
- if (r == NULL) {
333
- r = gpr_malloc(sizeof(grpc_fd));
334
- gpr_mu_init(&r->mu);
335
- }
336
-
337
- gpr_mu_lock(&r->mu);
338
- gpr_atm_rel_store(&r->refst, 1);
339
- r->shutdown = 0;
340
- r->read_closure = CLOSURE_NOT_READY;
341
- r->write_closure = CLOSURE_NOT_READY;
342
- r->fd = fd;
343
- r->inactive_watcher_root.next = r->inactive_watcher_root.prev =
344
- &r->inactive_watcher_root;
345
- r->freelist_next = NULL;
346
- r->read_watcher = r->write_watcher = NULL;
347
- r->on_done_closure = NULL;
348
- r->closed = 0;
349
- r->released = 0;
350
- r->read_notifier_pollset = NULL;
351
- gpr_mu_unlock(&r->mu);
352
- return r;
353
- }
354
-
355
- static void destroy(grpc_fd *fd) {
356
- gpr_mu_destroy(&fd->mu);
357
- gpr_free(fd);
358
- }
359
-
360
- #ifdef GRPC_FD_REF_COUNT_DEBUG
361
- #define REF_BY(fd, n, reason) ref_by(fd, n, reason, __FILE__, __LINE__)
362
- #define UNREF_BY(fd, n, reason) unref_by(fd, n, reason, __FILE__, __LINE__)
363
- static void ref_by(grpc_fd *fd, int n, const char *reason, const char *file,
364
- int line) {
365
- gpr_log(GPR_DEBUG, "FD %d %p ref %d %d -> %d [%s; %s:%d]", fd->fd, fd, n,
366
- gpr_atm_no_barrier_load(&fd->refst),
367
- gpr_atm_no_barrier_load(&fd->refst) + n, reason, file, line);
368
- #else
369
- #define REF_BY(fd, n, reason) ref_by(fd, n)
370
- #define UNREF_BY(fd, n, reason) unref_by(fd, n)
371
- static void ref_by(grpc_fd *fd, int n) {
372
- #endif
373
- GPR_ASSERT(gpr_atm_no_barrier_fetch_add(&fd->refst, n) > 0);
374
- }
375
-
376
- #ifdef GRPC_FD_REF_COUNT_DEBUG
377
- static void unref_by(grpc_fd *fd, int n, const char *reason, const char *file,
378
- int line) {
379
- gpr_atm old;
380
- gpr_log(GPR_DEBUG, "FD %d %p unref %d %d -> %d [%s; %s:%d]", fd->fd, fd, n,
381
- gpr_atm_no_barrier_load(&fd->refst),
382
- gpr_atm_no_barrier_load(&fd->refst) - n, reason, file, line);
383
- #else
384
- static void unref_by(grpc_fd *fd, int n) {
385
- gpr_atm old;
386
- #endif
387
- old = gpr_atm_full_fetch_add(&fd->refst, -n);
388
- if (old == n) {
389
- freelist_fd(fd);
390
- } else {
391
- GPR_ASSERT(old > n);
392
- }
393
- }
394
-
395
- static void fd_global_init(void) { gpr_mu_init(&fd_freelist_mu); }
396
-
397
- static void fd_global_shutdown(void) {
398
- gpr_mu_lock(&fd_freelist_mu);
399
- gpr_mu_unlock(&fd_freelist_mu);
400
- while (fd_freelist != NULL) {
401
- grpc_fd *fd = fd_freelist;
402
- fd_freelist = fd_freelist->freelist_next;
403
- destroy(fd);
404
- }
405
- gpr_mu_destroy(&fd_freelist_mu);
406
- }
407
-
408
- static grpc_fd *fd_create(int fd, const char *name) {
409
- grpc_fd *r = alloc_fd(fd);
410
- char *name2;
411
- gpr_asprintf(&name2, "%s fd=%d", name, fd);
412
- grpc_iomgr_register_object(&r->iomgr_object, name2);
413
- gpr_free(name2);
414
- #ifdef GRPC_FD_REF_COUNT_DEBUG
415
- gpr_log(GPR_DEBUG, "FD %d %p create %s", fd, r, name);
416
- #endif
417
- return r;
418
- }
419
-
420
- static bool fd_is_orphaned(grpc_fd *fd) {
421
- return (gpr_atm_acq_load(&fd->refst) & 1) == 0;
422
- }
423
-
424
- static grpc_error *pollset_kick_locked(grpc_fd_watcher *watcher) {
425
- gpr_mu_lock(&watcher->pollset->mu);
426
- GPR_ASSERT(watcher->worker);
427
- grpc_error *err = pollset_kick_ext(watcher->pollset, watcher->worker,
428
- GRPC_POLLSET_REEVALUATE_POLLING_ON_WAKEUP);
429
- gpr_mu_unlock(&watcher->pollset->mu);
430
- return err;
431
- }
432
-
433
- static void maybe_wake_one_watcher_locked(grpc_fd *fd) {
434
- if (fd->inactive_watcher_root.next != &fd->inactive_watcher_root) {
435
- pollset_kick_locked(fd->inactive_watcher_root.next);
436
- } else if (fd->read_watcher) {
437
- pollset_kick_locked(fd->read_watcher);
438
- } else if (fd->write_watcher) {
439
- pollset_kick_locked(fd->write_watcher);
440
- }
441
- }
442
-
443
- static void wake_all_watchers_locked(grpc_fd *fd) {
444
- grpc_fd_watcher *watcher;
445
- for (watcher = fd->inactive_watcher_root.next;
446
- watcher != &fd->inactive_watcher_root; watcher = watcher->next) {
447
- pollset_kick_locked(watcher);
448
- }
449
- if (fd->read_watcher) {
450
- pollset_kick_locked(fd->read_watcher);
451
- }
452
- if (fd->write_watcher && fd->write_watcher != fd->read_watcher) {
453
- pollset_kick_locked(fd->write_watcher);
454
- }
455
- }
456
-
457
- static int has_watchers(grpc_fd *fd) {
458
- return fd->read_watcher != NULL || fd->write_watcher != NULL ||
459
- fd->inactive_watcher_root.next != &fd->inactive_watcher_root;
460
- }
461
-
462
- static void close_fd_locked(grpc_exec_ctx *exec_ctx, grpc_fd *fd) {
463
- fd->closed = 1;
464
- if (!fd->released) {
465
- close(fd->fd);
466
- } else {
467
- remove_fd_from_all_epoll_sets(fd->fd);
468
- }
469
- grpc_exec_ctx_sched(exec_ctx, fd->on_done_closure, GRPC_ERROR_NONE, NULL);
470
- }
471
-
472
- static int fd_wrapped_fd(grpc_fd *fd) {
473
- if (fd->released || fd->closed) {
474
- return -1;
475
- } else {
476
- return fd->fd;
477
- }
478
- }
479
-
480
- static void fd_orphan(grpc_exec_ctx *exec_ctx, grpc_fd *fd,
481
- grpc_closure *on_done, int *release_fd,
482
- const char *reason) {
483
- fd->on_done_closure = on_done;
484
- fd->released = release_fd != NULL;
485
- if (!fd->released) {
486
- shutdown(fd->fd, SHUT_RDWR);
487
- } else {
488
- *release_fd = fd->fd;
489
- }
490
- gpr_mu_lock(&fd->mu);
491
- REF_BY(fd, 1, reason); /* remove active status, but keep referenced */
492
- if (!has_watchers(fd)) {
493
- close_fd_locked(exec_ctx, fd);
494
- } else {
495
- wake_all_watchers_locked(fd);
496
- }
497
- gpr_mu_unlock(&fd->mu);
498
- UNREF_BY(fd, 2, reason); /* drop the reference */
499
- }
500
-
501
- /* increment refcount by two to avoid changing the orphan bit */
502
- #ifdef GRPC_FD_REF_COUNT_DEBUG
503
- static void fd_ref(grpc_fd *fd, const char *reason, const char *file,
504
- int line) {
505
- ref_by(fd, 2, reason, file, line);
506
- }
507
-
508
- static void fd_unref(grpc_fd *fd, const char *reason, const char *file,
509
- int line) {
510
- unref_by(fd, 2, reason, file, line);
511
- }
512
- #else
513
- static void fd_ref(grpc_fd *fd) { ref_by(fd, 2); }
514
-
515
- static void fd_unref(grpc_fd *fd) { unref_by(fd, 2); }
516
- #endif
517
-
518
- static grpc_error *fd_shutdown_error(bool shutdown) {
519
- if (!shutdown) {
520
- return GRPC_ERROR_NONE;
521
- } else {
522
- return GRPC_ERROR_CREATE("FD shutdown");
523
- }
524
- }
525
-
526
- static void notify_on_locked(grpc_exec_ctx *exec_ctx, grpc_fd *fd,
527
- grpc_closure **st, grpc_closure *closure) {
528
- if (fd->shutdown) {
529
- grpc_exec_ctx_sched(exec_ctx, closure, GRPC_ERROR_CREATE("FD shutdown"),
530
- NULL);
531
- } else if (*st == CLOSURE_NOT_READY) {
532
- /* not ready ==> switch to a waiting state by setting the closure */
533
- *st = closure;
534
- } else if (*st == CLOSURE_READY) {
535
- /* already ready ==> queue the closure to run immediately */
536
- *st = CLOSURE_NOT_READY;
537
- grpc_exec_ctx_sched(exec_ctx, closure, fd_shutdown_error(fd->shutdown),
538
- NULL);
539
- maybe_wake_one_watcher_locked(fd);
540
- } else {
541
- /* upcallptr was set to a different closure. This is an error! */
542
- gpr_log(GPR_ERROR,
543
- "User called a notify_on function with a previous callback still "
544
- "pending");
545
- abort();
546
- }
547
- }
548
-
549
- /* returns 1 if state becomes not ready */
550
- static int set_ready_locked(grpc_exec_ctx *exec_ctx, grpc_fd *fd,
551
- grpc_closure **st) {
552
- if (*st == CLOSURE_READY) {
553
- /* duplicate ready ==> ignore */
554
- return 0;
555
- } else if (*st == CLOSURE_NOT_READY) {
556
- /* not ready, and not waiting ==> flag ready */
557
- *st = CLOSURE_READY;
558
- return 0;
559
- } else {
560
- /* waiting ==> queue closure */
561
- grpc_exec_ctx_sched(exec_ctx, *st, fd_shutdown_error(fd->shutdown), NULL);
562
- *st = CLOSURE_NOT_READY;
563
- return 1;
564
- }
565
- }
566
-
567
- static void set_read_notifier_pollset_locked(
568
- grpc_exec_ctx *exec_ctx, grpc_fd *fd, grpc_pollset *read_notifier_pollset) {
569
- fd->read_notifier_pollset = read_notifier_pollset;
570
- }
571
-
572
- static void fd_shutdown(grpc_exec_ctx *exec_ctx, grpc_fd *fd) {
573
- gpr_mu_lock(&fd->mu);
574
- /* only shutdown once */
575
- if (!fd->shutdown) {
576
- fd->shutdown = 1;
577
- /* signal read/write closed to OS so that future operations fail */
578
- shutdown(fd->fd, SHUT_RDWR);
579
- set_ready_locked(exec_ctx, fd, &fd->read_closure);
580
- set_ready_locked(exec_ctx, fd, &fd->write_closure);
581
- }
582
- gpr_mu_unlock(&fd->mu);
583
- }
584
-
585
- static bool fd_is_shutdown(grpc_fd *fd) {
586
- gpr_mu_lock(&fd->mu);
587
- bool r = fd->shutdown;
588
- gpr_mu_unlock(&fd->mu);
589
- return r;
590
- }
591
-
592
- static void fd_notify_on_read(grpc_exec_ctx *exec_ctx, grpc_fd *fd,
593
- grpc_closure *closure) {
594
- gpr_mu_lock(&fd->mu);
595
- notify_on_locked(exec_ctx, fd, &fd->read_closure, closure);
596
- gpr_mu_unlock(&fd->mu);
597
- }
598
-
599
- static void fd_notify_on_write(grpc_exec_ctx *exec_ctx, grpc_fd *fd,
600
- grpc_closure *closure) {
601
- gpr_mu_lock(&fd->mu);
602
- notify_on_locked(exec_ctx, fd, &fd->write_closure, closure);
603
- gpr_mu_unlock(&fd->mu);
604
- }
605
-
606
- /* Return the read-notifier pollset */
607
- static grpc_pollset *fd_get_read_notifier_pollset(grpc_exec_ctx *exec_ctx,
608
- grpc_fd *fd) {
609
- grpc_pollset *notifier = NULL;
610
-
611
- gpr_mu_lock(&fd->mu);
612
- notifier = fd->read_notifier_pollset;
613
- gpr_mu_unlock(&fd->mu);
614
-
615
- return notifier;
616
- }
617
-
618
- static uint32_t fd_begin_poll(grpc_fd *fd, grpc_pollset *pollset,
619
- grpc_pollset_worker *worker, uint32_t read_mask,
620
- uint32_t write_mask, grpc_fd_watcher *watcher) {
621
- uint32_t mask = 0;
622
- grpc_closure *cur;
623
- int requested;
624
- /* keep track of pollers that have requested our events, in case they change
625
- */
626
- GRPC_FD_REF(fd, "poll");
627
-
628
- gpr_mu_lock(&fd->mu);
629
-
630
- /* if we are shutdown, then don't add to the watcher set */
631
- if (fd->shutdown) {
632
- watcher->fd = NULL;
633
- watcher->pollset = NULL;
634
- watcher->worker = NULL;
635
- gpr_mu_unlock(&fd->mu);
636
- GRPC_FD_UNREF(fd, "poll");
637
- return 0;
638
- }
639
-
640
- /* if there is nobody polling for read, but we need to, then start doing so */
641
- cur = fd->read_closure;
642
- requested = cur != CLOSURE_READY;
643
- if (read_mask && fd->read_watcher == NULL && requested) {
644
- fd->read_watcher = watcher;
645
- mask |= read_mask;
646
- }
647
- /* if there is nobody polling for write, but we need to, then start doing so
648
- */
649
- cur = fd->write_closure;
650
- requested = cur != CLOSURE_READY;
651
- if (write_mask && fd->write_watcher == NULL && requested) {
652
- fd->write_watcher = watcher;
653
- mask |= write_mask;
654
- }
655
- /* if not polling, remember this watcher in case we need someone to later */
656
- if (mask == 0 && worker != NULL) {
657
- watcher->next = &fd->inactive_watcher_root;
658
- watcher->prev = watcher->next->prev;
659
- watcher->next->prev = watcher->prev->next = watcher;
660
- }
661
- watcher->pollset = pollset;
662
- watcher->worker = worker;
663
- watcher->fd = fd;
664
- gpr_mu_unlock(&fd->mu);
665
-
666
- return mask;
667
- }
668
-
669
- static void fd_end_poll(grpc_exec_ctx *exec_ctx, grpc_fd_watcher *watcher,
670
- int got_read, int got_write,
671
- grpc_pollset *read_notifier_pollset) {
672
- int was_polling = 0;
673
- int kick = 0;
674
- grpc_fd *fd = watcher->fd;
675
-
676
- if (fd == NULL) {
677
- return;
678
- }
679
-
680
- gpr_mu_lock(&fd->mu);
681
-
682
- if (watcher == fd->read_watcher) {
683
- /* remove read watcher, kick if we still need a read */
684
- was_polling = 1;
685
- if (!got_read) {
686
- kick = 1;
687
- }
688
- fd->read_watcher = NULL;
689
- }
690
- if (watcher == fd->write_watcher) {
691
- /* remove write watcher, kick if we still need a write */
692
- was_polling = 1;
693
- if (!got_write) {
694
- kick = 1;
695
- }
696
- fd->write_watcher = NULL;
697
- }
698
- if (!was_polling && watcher->worker != NULL) {
699
- /* remove from inactive list */
700
- watcher->next->prev = watcher->prev;
701
- watcher->prev->next = watcher->next;
702
- }
703
- if (got_read) {
704
- if (set_ready_locked(exec_ctx, fd, &fd->read_closure)) {
705
- kick = 1;
706
- }
707
-
708
- if (read_notifier_pollset != NULL) {
709
- set_read_notifier_pollset_locked(exec_ctx, fd, read_notifier_pollset);
710
- }
711
- }
712
- if (got_write) {
713
- if (set_ready_locked(exec_ctx, fd, &fd->write_closure)) {
714
- kick = 1;
715
- }
716
- }
717
- if (kick) {
718
- maybe_wake_one_watcher_locked(fd);
719
- }
720
- if (fd_is_orphaned(fd) && !has_watchers(fd) && !fd->closed) {
721
- close_fd_locked(exec_ctx, fd);
722
- }
723
- gpr_mu_unlock(&fd->mu);
724
-
725
- GRPC_FD_UNREF(fd, "poll");
726
- }
727
-
728
- static grpc_workqueue *fd_get_workqueue(grpc_fd *fd) { return NULL; }
729
-
730
- /*******************************************************************************
731
- * pollset_posix.c
732
- */
733
-
734
- GPR_TLS_DECL(g_current_thread_poller);
735
- GPR_TLS_DECL(g_current_thread_worker);
736
-
737
- /** The alarm system needs to be able to wakeup 'some poller' sometimes
738
- * (specifically when a new alarm needs to be triggered earlier than the next
739
- * alarm 'epoch').
740
- * This wakeup_fd gives us something to alert on when such a case occurs. */
741
- grpc_wakeup_fd grpc_global_wakeup_fd;
742
-
743
- static void remove_worker(grpc_pollset *p, grpc_pollset_worker *worker) {
744
- worker->prev->next = worker->next;
745
- worker->next->prev = worker->prev;
746
- }
747
-
748
- static int pollset_has_workers(grpc_pollset *p) {
749
- return p->root_worker.next != &p->root_worker;
750
- }
751
-
752
- static grpc_pollset_worker *pop_front_worker(grpc_pollset *p) {
753
- if (pollset_has_workers(p)) {
754
- grpc_pollset_worker *w = p->root_worker.next;
755
- remove_worker(p, w);
756
- return w;
757
- } else {
758
- return NULL;
759
- }
760
- }
761
-
762
- static void push_back_worker(grpc_pollset *p, grpc_pollset_worker *worker) {
763
- worker->next = &p->root_worker;
764
- worker->prev = worker->next->prev;
765
- worker->prev->next = worker->next->prev = worker;
766
- }
767
-
768
- static void push_front_worker(grpc_pollset *p, grpc_pollset_worker *worker) {
769
- worker->prev = &p->root_worker;
770
- worker->next = worker->prev->next;
771
- worker->prev->next = worker->next->prev = worker;
772
- }
773
-
774
- static void kick_append_error(grpc_error **composite, grpc_error *error) {
775
- if (error == GRPC_ERROR_NONE) return;
776
- if (*composite == GRPC_ERROR_NONE) {
777
- *composite = GRPC_ERROR_CREATE("Kick Failure");
778
- }
779
- *composite = grpc_error_add_child(*composite, error);
780
- }
781
-
782
- static grpc_error *pollset_kick_ext(grpc_pollset *p,
783
- grpc_pollset_worker *specific_worker,
784
- uint32_t flags) {
785
- GPR_TIMER_BEGIN("pollset_kick_ext", 0);
786
- grpc_error *error = GRPC_ERROR_NONE;
787
-
788
- /* pollset->mu already held */
789
- if (specific_worker != NULL) {
790
- if (specific_worker == GRPC_POLLSET_KICK_BROADCAST) {
791
- GPR_TIMER_BEGIN("pollset_kick_ext.broadcast", 0);
792
- GPR_ASSERT((flags & GRPC_POLLSET_REEVALUATE_POLLING_ON_WAKEUP) == 0);
793
- for (specific_worker = p->root_worker.next;
794
- specific_worker != &p->root_worker;
795
- specific_worker = specific_worker->next) {
796
- kick_append_error(
797
- &error, grpc_wakeup_fd_wakeup(&specific_worker->wakeup_fd->fd));
798
- }
799
- p->kicked_without_pollers = true;
800
- GPR_TIMER_END("pollset_kick_ext.broadcast", 0);
801
- } else if (gpr_tls_get(&g_current_thread_worker) !=
802
- (intptr_t)specific_worker) {
803
- GPR_TIMER_MARK("different_thread_worker", 0);
804
- if ((flags & GRPC_POLLSET_REEVALUATE_POLLING_ON_WAKEUP) != 0) {
805
- specific_worker->reevaluate_polling_on_wakeup = true;
806
- }
807
- specific_worker->kicked_specifically = true;
808
- kick_append_error(&error,
809
- grpc_wakeup_fd_wakeup(&specific_worker->wakeup_fd->fd));
810
- } else if ((flags & GRPC_POLLSET_CAN_KICK_SELF) != 0) {
811
- GPR_TIMER_MARK("kick_yoself", 0);
812
- if ((flags & GRPC_POLLSET_REEVALUATE_POLLING_ON_WAKEUP) != 0) {
813
- specific_worker->reevaluate_polling_on_wakeup = true;
814
- }
815
- specific_worker->kicked_specifically = true;
816
- kick_append_error(&error,
817
- grpc_wakeup_fd_wakeup(&specific_worker->wakeup_fd->fd));
818
- }
819
- } else if (gpr_tls_get(&g_current_thread_poller) != (intptr_t)p) {
820
- GPR_ASSERT((flags & GRPC_POLLSET_REEVALUATE_POLLING_ON_WAKEUP) == 0);
821
- GPR_TIMER_MARK("kick_anonymous", 0);
822
- specific_worker = pop_front_worker(p);
823
- if (specific_worker != NULL) {
824
- if (gpr_tls_get(&g_current_thread_worker) == (intptr_t)specific_worker) {
825
- GPR_TIMER_MARK("kick_anonymous_not_self", 0);
826
- push_back_worker(p, specific_worker);
827
- specific_worker = pop_front_worker(p);
828
- if ((flags & GRPC_POLLSET_CAN_KICK_SELF) == 0 &&
829
- gpr_tls_get(&g_current_thread_worker) ==
830
- (intptr_t)specific_worker) {
831
- push_back_worker(p, specific_worker);
832
- specific_worker = NULL;
833
- }
834
- }
835
- if (specific_worker != NULL) {
836
- GPR_TIMER_MARK("finally_kick", 0);
837
- push_back_worker(p, specific_worker);
838
- kick_append_error(
839
- &error, grpc_wakeup_fd_wakeup(&specific_worker->wakeup_fd->fd));
840
- }
841
- } else {
842
- GPR_TIMER_MARK("kicked_no_pollers", 0);
843
- p->kicked_without_pollers = true;
844
- }
845
- }
846
-
847
- GPR_TIMER_END("pollset_kick_ext", 0);
848
- return error;
849
- }
850
-
851
- static grpc_error *pollset_kick(grpc_pollset *p,
852
- grpc_pollset_worker *specific_worker) {
853
- return pollset_kick_ext(p, specific_worker, 0);
854
- }
855
-
856
- /* global state management */
857
-
858
- static grpc_error *pollset_global_init(void) {
859
- gpr_tls_init(&g_current_thread_poller);
860
- gpr_tls_init(&g_current_thread_worker);
861
- return grpc_wakeup_fd_init(&grpc_global_wakeup_fd);
862
- }
863
-
864
- static void pollset_global_shutdown(void) {
865
- grpc_wakeup_fd_destroy(&grpc_global_wakeup_fd);
866
- gpr_tls_destroy(&g_current_thread_poller);
867
- gpr_tls_destroy(&g_current_thread_worker);
868
- }
869
-
870
- static grpc_error *kick_poller(void) {
871
- return grpc_wakeup_fd_wakeup(&grpc_global_wakeup_fd);
872
- }
873
-
874
- /* main interface */
875
-
876
- static void become_basic_pollset(grpc_pollset *pollset, grpc_fd *fd_or_null);
877
-
878
- static void pollset_init(grpc_pollset *pollset, gpr_mu **mu) {
879
- gpr_mu_init(&pollset->mu);
880
- *mu = &pollset->mu;
881
- pollset->root_worker.next = pollset->root_worker.prev = &pollset->root_worker;
882
- pollset->in_flight_cbs = 0;
883
- pollset->shutting_down = 0;
884
- pollset->called_shutdown = 0;
885
- pollset->kicked_without_pollers = 0;
886
- pollset->idle_jobs.head = pollset->idle_jobs.tail = NULL;
887
- pollset->local_wakeup_cache = NULL;
888
- pollset->kicked_without_pollers = 0;
889
- become_basic_pollset(pollset, NULL);
890
- }
891
-
892
- static void pollset_destroy(grpc_pollset *pollset) {
893
- GPR_ASSERT(pollset->in_flight_cbs == 0);
894
- GPR_ASSERT(!pollset_has_workers(pollset));
895
- GPR_ASSERT(pollset->idle_jobs.head == pollset->idle_jobs.tail);
896
- pollset->vtable->destroy(pollset);
897
- while (pollset->local_wakeup_cache) {
898
- grpc_cached_wakeup_fd *next = pollset->local_wakeup_cache->next;
899
- grpc_wakeup_fd_destroy(&pollset->local_wakeup_cache->fd);
900
- gpr_free(pollset->local_wakeup_cache);
901
- pollset->local_wakeup_cache = next;
902
- }
903
- gpr_mu_destroy(&pollset->mu);
904
- }
905
-
906
- static void pollset_reset(grpc_pollset *pollset) {
907
- GPR_ASSERT(pollset->shutting_down);
908
- GPR_ASSERT(pollset->in_flight_cbs == 0);
909
- GPR_ASSERT(!pollset_has_workers(pollset));
910
- GPR_ASSERT(pollset->idle_jobs.head == pollset->idle_jobs.tail);
911
- pollset->vtable->destroy(pollset);
912
- pollset->shutting_down = 0;
913
- pollset->called_shutdown = 0;
914
- pollset->kicked_without_pollers = 0;
915
- become_basic_pollset(pollset, NULL);
916
- }
917
-
918
- static void pollset_add_fd(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
919
- grpc_fd *fd) {
920
- gpr_mu_lock(&pollset->mu);
921
- pollset->vtable->add_fd(exec_ctx, pollset, fd, 1);
922
- /* the following (enabled only in debug) will reacquire and then release
923
- our lock - meaning that if the unlocking flag passed to add_fd above is
924
- not respected, the code will deadlock (in a way that we have a chance of
925
- debugging) */
926
- #ifndef NDEBUG
927
- gpr_mu_lock(&pollset->mu);
928
- gpr_mu_unlock(&pollset->mu);
929
- #endif
930
- }
931
-
932
- static void finish_shutdown(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset) {
933
- GPR_ASSERT(grpc_closure_list_empty(pollset->idle_jobs));
934
- pollset->vtable->finish_shutdown(pollset);
935
- grpc_exec_ctx_sched(exec_ctx, pollset->shutdown_done, GRPC_ERROR_NONE, NULL);
936
- }
937
-
938
- static void work_combine_error(grpc_error **composite, grpc_error *error) {
939
- if (error == GRPC_ERROR_NONE) return;
940
- if (*composite == GRPC_ERROR_NONE) {
941
- *composite = GRPC_ERROR_CREATE("pollset_work");
942
- }
943
- *composite = grpc_error_add_child(*composite, error);
944
- }
945
-
946
- static grpc_error *pollset_work(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
947
- grpc_pollset_worker **worker_hdl,
948
- gpr_timespec now, gpr_timespec deadline) {
949
- grpc_pollset_worker worker;
950
- *worker_hdl = &worker;
951
- grpc_error *error = GRPC_ERROR_NONE;
952
-
953
- /* pollset->mu already held */
954
- int added_worker = 0;
955
- int locked = 1;
956
- int queued_work = 0;
957
- int keep_polling = 0;
958
- GPR_TIMER_BEGIN("pollset_work", 0);
959
- /* this must happen before we (potentially) drop pollset->mu */
960
- worker.next = worker.prev = NULL;
961
- worker.reevaluate_polling_on_wakeup = 0;
962
- if (pollset->local_wakeup_cache != NULL) {
963
- worker.wakeup_fd = pollset->local_wakeup_cache;
964
- pollset->local_wakeup_cache = worker.wakeup_fd->next;
965
- } else {
966
- worker.wakeup_fd = gpr_malloc(sizeof(*worker.wakeup_fd));
967
- error = grpc_wakeup_fd_init(&worker.wakeup_fd->fd);
968
- if (error != GRPC_ERROR_NONE) {
969
- return error;
970
- }
971
- }
972
- worker.kicked_specifically = 0;
973
- /* If there's work waiting for the pollset to be idle, and the
974
- pollset is idle, then do that work */
975
- if (!pollset_has_workers(pollset) &&
976
- !grpc_closure_list_empty(pollset->idle_jobs)) {
977
- GPR_TIMER_MARK("pollset_work.idle_jobs", 0);
978
- grpc_exec_ctx_enqueue_list(exec_ctx, &pollset->idle_jobs, NULL);
979
- goto done;
980
- }
981
- /* If we're shutting down then we don't execute any extended work */
982
- if (pollset->shutting_down) {
983
- GPR_TIMER_MARK("pollset_work.shutting_down", 0);
984
- goto done;
985
- }
986
- /* Give do_promote priority so we don't starve it out */
987
- if (pollset->in_flight_cbs) {
988
- GPR_TIMER_MARK("pollset_work.in_flight_cbs", 0);
989
- gpr_mu_unlock(&pollset->mu);
990
- locked = 0;
991
- goto done;
992
- }
993
- /* Start polling, and keep doing so while we're being asked to
994
- re-evaluate our pollers (this allows poll() based pollers to
995
- ensure they don't miss wakeups) */
996
- keep_polling = 1;
997
- while (keep_polling) {
998
- keep_polling = 0;
999
- if (!pollset->kicked_without_pollers) {
1000
- if (!added_worker) {
1001
- push_front_worker(pollset, &worker);
1002
- added_worker = 1;
1003
- gpr_tls_set(&g_current_thread_worker, (intptr_t)&worker);
1004
- }
1005
- gpr_tls_set(&g_current_thread_poller, (intptr_t)pollset);
1006
- GPR_TIMER_BEGIN("maybe_work_and_unlock", 0);
1007
- work_combine_error(&error,
1008
- pollset->vtable->maybe_work_and_unlock(
1009
- exec_ctx, pollset, &worker, deadline, now));
1010
- GPR_TIMER_END("maybe_work_and_unlock", 0);
1011
- locked = 0;
1012
- gpr_tls_set(&g_current_thread_poller, 0);
1013
- } else {
1014
- GPR_TIMER_MARK("pollset_work.kicked_without_pollers", 0);
1015
- pollset->kicked_without_pollers = 0;
1016
- }
1017
- /* Finished execution - start cleaning up.
1018
- Note that we may arrive here from outside the enclosing while() loop.
1019
- In that case we won't loop though as we haven't added worker to the
1020
- worker list, which means nobody could ask us to re-evaluate polling). */
1021
- done:
1022
- if (!locked) {
1023
- queued_work |= grpc_exec_ctx_flush(exec_ctx);
1024
- gpr_mu_lock(&pollset->mu);
1025
- locked = 1;
1026
- }
1027
- /* If we're forced to re-evaluate polling (via pollset_kick with
1028
- GRPC_POLLSET_REEVALUATE_POLLING_ON_WAKEUP) then we land here and force
1029
- a loop */
1030
- if (worker.reevaluate_polling_on_wakeup) {
1031
- worker.reevaluate_polling_on_wakeup = 0;
1032
- pollset->kicked_without_pollers = 0;
1033
- if (queued_work || worker.kicked_specifically) {
1034
- /* If there's queued work on the list, then set the deadline to be
1035
- immediate so we get back out of the polling loop quickly */
1036
- deadline = gpr_inf_past(GPR_CLOCK_MONOTONIC);
1037
- }
1038
- keep_polling = 1;
1039
- }
1040
- }
1041
- if (added_worker) {
1042
- remove_worker(pollset, &worker);
1043
- gpr_tls_set(&g_current_thread_worker, 0);
1044
- }
1045
- /* release wakeup fd to the local pool */
1046
- worker.wakeup_fd->next = pollset->local_wakeup_cache;
1047
- pollset->local_wakeup_cache = worker.wakeup_fd;
1048
- /* check shutdown conditions */
1049
- if (pollset->shutting_down) {
1050
- if (pollset_has_workers(pollset)) {
1051
- pollset_kick(pollset, NULL);
1052
- } else if (!pollset->called_shutdown && pollset->in_flight_cbs == 0) {
1053
- pollset->called_shutdown = 1;
1054
- gpr_mu_unlock(&pollset->mu);
1055
- finish_shutdown(exec_ctx, pollset);
1056
- grpc_exec_ctx_flush(exec_ctx);
1057
- /* Continuing to access pollset here is safe -- it is the caller's
1058
- * responsibility to not destroy when it has outstanding calls to
1059
- * pollset_work.
1060
- * TODO(dklempner): Can we refactor the shutdown logic to avoid this? */
1061
- gpr_mu_lock(&pollset->mu);
1062
- } else if (!grpc_closure_list_empty(pollset->idle_jobs)) {
1063
- grpc_exec_ctx_enqueue_list(exec_ctx, &pollset->idle_jobs, NULL);
1064
- gpr_mu_unlock(&pollset->mu);
1065
- grpc_exec_ctx_flush(exec_ctx);
1066
- gpr_mu_lock(&pollset->mu);
1067
- }
1068
- }
1069
- *worker_hdl = NULL;
1070
- GPR_TIMER_END("pollset_work", 0);
1071
- return error;
1072
- }
1073
-
1074
- static void pollset_shutdown(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
1075
- grpc_closure *closure) {
1076
- GPR_ASSERT(!pollset->shutting_down);
1077
- pollset->shutting_down = 1;
1078
- pollset->shutdown_done = closure;
1079
- pollset_kick(pollset, GRPC_POLLSET_KICK_BROADCAST);
1080
- if (!pollset_has_workers(pollset)) {
1081
- grpc_exec_ctx_enqueue_list(exec_ctx, &pollset->idle_jobs, NULL);
1082
- }
1083
- if (!pollset->called_shutdown && pollset->in_flight_cbs == 0 &&
1084
- !pollset_has_workers(pollset)) {
1085
- pollset->called_shutdown = 1;
1086
- finish_shutdown(exec_ctx, pollset);
1087
- }
1088
- }
1089
-
1090
- static int poll_deadline_to_millis_timeout(gpr_timespec deadline,
1091
- gpr_timespec now) {
1092
- gpr_timespec timeout;
1093
- static const int64_t max_spin_polling_us = 10;
1094
- if (gpr_time_cmp(deadline, gpr_inf_future(deadline.clock_type)) == 0) {
1095
- return -1;
1096
- }
1097
- if (gpr_time_cmp(deadline, gpr_time_add(now, gpr_time_from_micros(
1098
- max_spin_polling_us,
1099
- GPR_TIMESPAN))) <= 0) {
1100
- return 0;
1101
- }
1102
- timeout = gpr_time_sub(deadline, now);
1103
- return gpr_time_to_millis(gpr_time_add(
1104
- timeout, gpr_time_from_nanos(GPR_NS_PER_MS - 1, GPR_TIMESPAN)));
1105
- }
1106
-
1107
- /*
1108
- * basic_pollset - a vtable that provides polling for zero or one file
1109
- * descriptor via poll()
1110
- */
1111
-
1112
- typedef struct grpc_unary_promote_args {
1113
- const grpc_pollset_vtable *original_vtable;
1114
- grpc_pollset *pollset;
1115
- grpc_fd *fd;
1116
- grpc_closure promotion_closure;
1117
- } grpc_unary_promote_args;
1118
-
1119
- static void basic_do_promote(grpc_exec_ctx *exec_ctx, void *args,
1120
- grpc_error *error) {
1121
- grpc_unary_promote_args *up_args = args;
1122
- const grpc_pollset_vtable *original_vtable = up_args->original_vtable;
1123
- grpc_pollset *pollset = up_args->pollset;
1124
- grpc_fd *fd = up_args->fd;
1125
-
1126
- /*
1127
- * This is quite tricky. There are a number of cases to keep in mind here:
1128
- * 1. fd may have been orphaned
1129
- * 2. The pollset may no longer be a unary poller (and we can't let case #1
1130
- * leak to other pollset types!)
1131
- * 3. pollset's fd (which may have changed) may have been orphaned
1132
- * 4. The pollset may be shutting down.
1133
- */
1134
-
1135
- gpr_mu_lock(&pollset->mu);
1136
- /* First we need to ensure that nobody is polling concurrently */
1137
- GPR_ASSERT(!pollset_has_workers(pollset));
1138
-
1139
- gpr_free(up_args);
1140
- /* At this point the pollset may no longer be a unary poller. In that case
1141
- * we should just call the right add function and be done. */
1142
- /* TODO(klempner): If we're not careful this could cause infinite recursion.
1143
- * That's not a problem for now because empty_pollset has a trivial poller
1144
- * and we don't have any mechanism to unbecome multipoller. */
1145
- pollset->in_flight_cbs--;
1146
- if (pollset->shutting_down) {
1147
- /* We don't care about this pollset anymore. */
1148
- if (pollset->in_flight_cbs == 0 && !pollset->called_shutdown) {
1149
- pollset->called_shutdown = 1;
1150
- finish_shutdown(exec_ctx, pollset);
1151
- }
1152
- } else if (fd_is_orphaned(fd)) {
1153
- /* Don't try to add it to anything, we'll drop our ref on it below */
1154
- } else if (pollset->vtable != original_vtable) {
1155
- pollset->vtable->add_fd(exec_ctx, pollset, fd, 0);
1156
- } else if (fd != pollset->data.ptr) {
1157
- grpc_fd *fds[2];
1158
- fds[0] = pollset->data.ptr;
1159
- fds[1] = fd;
1160
-
1161
- if (fds[0] && !fd_is_orphaned(fds[0])) {
1162
- platform_become_multipoller(exec_ctx, pollset, fds, GPR_ARRAY_SIZE(fds));
1163
- GRPC_FD_UNREF(fds[0], "basicpoll");
1164
- } else {
1165
- /* old fd is orphaned and we haven't cleaned it up until now, so remain a
1166
- * unary poller */
1167
- /* Note that it is possible that fds[1] is also orphaned at this point.
1168
- * That's okay, we'll correct it at the next add or poll. */
1169
- if (fds[0]) GRPC_FD_UNREF(fds[0], "basicpoll");
1170
- pollset->data.ptr = fd;
1171
- GRPC_FD_REF(fd, "basicpoll");
1172
- }
1173
- }
1174
-
1175
- gpr_mu_unlock(&pollset->mu);
1176
-
1177
- /* Matching ref in basic_pollset_add_fd */
1178
- GRPC_FD_UNREF(fd, "basicpoll_add");
1179
- }
1180
-
1181
- static void basic_pollset_add_fd(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
1182
- grpc_fd *fd, int and_unlock_pollset) {
1183
- grpc_unary_promote_args *up_args;
1184
- GPR_ASSERT(fd);
1185
- if (fd == pollset->data.ptr) goto exit;
1186
-
1187
- if (!pollset_has_workers(pollset)) {
1188
- /* Fast path -- no in flight cbs */
1189
- /* TODO(klempner): Comment this out and fix any test failures or establish
1190
- * they are due to timing issues */
1191
- grpc_fd *fds[2];
1192
- fds[0] = pollset->data.ptr;
1193
- fds[1] = fd;
1194
-
1195
- if (fds[0] == NULL) {
1196
- pollset->data.ptr = fd;
1197
- GRPC_FD_REF(fd, "basicpoll");
1198
- } else if (!fd_is_orphaned(fds[0])) {
1199
- platform_become_multipoller(exec_ctx, pollset, fds, GPR_ARRAY_SIZE(fds));
1200
- GRPC_FD_UNREF(fds[0], "basicpoll");
1201
- } else {
1202
- /* old fd is orphaned and we haven't cleaned it up until now, so remain a
1203
- * unary poller */
1204
- GRPC_FD_UNREF(fds[0], "basicpoll");
1205
- pollset->data.ptr = fd;
1206
- GRPC_FD_REF(fd, "basicpoll");
1207
- }
1208
- goto exit;
1209
- }
1210
-
1211
- /* Now we need to promote. This needs to happen when we're not polling. Since
1212
- * this may be called from poll, the wait needs to happen asynchronously. */
1213
- GRPC_FD_REF(fd, "basicpoll_add");
1214
- pollset->in_flight_cbs++;
1215
- up_args = gpr_malloc(sizeof(*up_args));
1216
- up_args->fd = fd;
1217
- up_args->original_vtable = pollset->vtable;
1218
- up_args->pollset = pollset;
1219
- up_args->promotion_closure.cb = basic_do_promote;
1220
- up_args->promotion_closure.cb_arg = up_args;
1221
-
1222
- grpc_closure_list_append(&pollset->idle_jobs, &up_args->promotion_closure,
1223
- GRPC_ERROR_NONE);
1224
- pollset_kick(pollset, GRPC_POLLSET_KICK_BROADCAST);
1225
-
1226
- exit:
1227
- if (and_unlock_pollset) {
1228
- gpr_mu_unlock(&pollset->mu);
1229
- }
1230
- }
1231
-
1232
- static grpc_error *basic_pollset_maybe_work_and_unlock(
1233
- grpc_exec_ctx *exec_ctx, grpc_pollset *pollset, grpc_pollset_worker *worker,
1234
- gpr_timespec deadline, gpr_timespec now) {
1235
- #define POLLOUT_CHECK (POLLOUT | POLLHUP | POLLERR)
1236
- #define POLLIN_CHECK (POLLIN | POLLHUP | POLLERR)
1237
-
1238
- struct pollfd pfd[3];
1239
- grpc_fd *fd;
1240
- grpc_fd_watcher fd_watcher;
1241
- int timeout;
1242
- int r;
1243
- nfds_t nfds;
1244
- grpc_error *error = GRPC_ERROR_NONE;
1245
-
1246
- fd = pollset->data.ptr;
1247
- if (fd && fd_is_orphaned(fd)) {
1248
- GRPC_FD_UNREF(fd, "basicpoll");
1249
- fd = pollset->data.ptr = NULL;
1250
- }
1251
- timeout = poll_deadline_to_millis_timeout(deadline, now);
1252
- pfd[0].fd = GRPC_WAKEUP_FD_GET_READ_FD(&grpc_global_wakeup_fd);
1253
- pfd[0].events = POLLIN;
1254
- pfd[0].revents = 0;
1255
- pfd[1].fd = GRPC_WAKEUP_FD_GET_READ_FD(&worker->wakeup_fd->fd);
1256
- pfd[1].events = POLLIN;
1257
- pfd[1].revents = 0;
1258
- nfds = 2;
1259
- if (fd) {
1260
- pfd[2].fd = fd->fd;
1261
- pfd[2].revents = 0;
1262
- GRPC_FD_REF(fd, "basicpoll_begin");
1263
- gpr_mu_unlock(&pollset->mu);
1264
- pfd[2].events =
1265
- (short)fd_begin_poll(fd, pollset, worker, POLLIN, POLLOUT, &fd_watcher);
1266
- if (pfd[2].events != 0) {
1267
- nfds++;
1268
- }
1269
- } else {
1270
- gpr_mu_unlock(&pollset->mu);
1271
- }
1272
-
1273
- /* TODO(vpai): Consider first doing a 0 timeout poll here to avoid
1274
- even going into the blocking annotation if possible */
1275
- /* poll fd count (argument 2) is shortened by one if we have no events
1276
- to poll on - such that it only includes the kicker */
1277
- GPR_TIMER_BEGIN("poll", 0);
1278
- GRPC_SCHEDULING_START_BLOCKING_REGION;
1279
- r = grpc_poll_function(pfd, nfds, timeout);
1280
- GRPC_SCHEDULING_END_BLOCKING_REGION;
1281
- GPR_TIMER_END("poll", 0);
1282
-
1283
- if (r < 0) {
1284
- if (errno != EINTR) {
1285
- work_combine_error(&error, GRPC_OS_ERROR(errno, "poll"));
1286
- }
1287
- if (fd) {
1288
- fd_end_poll(exec_ctx, &fd_watcher, 0, 0, NULL);
1289
- }
1290
- } else if (r == 0) {
1291
- if (fd) {
1292
- fd_end_poll(exec_ctx, &fd_watcher, 0, 0, NULL);
1293
- }
1294
- } else {
1295
- if (pfd[0].revents & POLLIN_CHECK) {
1296
- work_combine_error(&error,
1297
- grpc_wakeup_fd_consume_wakeup(&grpc_global_wakeup_fd));
1298
- }
1299
- if (pfd[1].revents & POLLIN_CHECK) {
1300
- work_combine_error(&error,
1301
- grpc_wakeup_fd_consume_wakeup(&worker->wakeup_fd->fd));
1302
- }
1303
- if (nfds > 2) {
1304
- fd_end_poll(exec_ctx, &fd_watcher, pfd[2].revents & POLLIN_CHECK,
1305
- pfd[2].revents & POLLOUT_CHECK, pollset);
1306
- } else if (fd) {
1307
- fd_end_poll(exec_ctx, &fd_watcher, 0, 0, NULL);
1308
- }
1309
- }
1310
-
1311
- if (fd) {
1312
- GRPC_FD_UNREF(fd, "basicpoll_begin");
1313
- }
1314
-
1315
- return error;
1316
- }
1317
-
1318
- static void basic_pollset_destroy(grpc_pollset *pollset) {
1319
- if (pollset->data.ptr != NULL) {
1320
- GRPC_FD_UNREF(pollset->data.ptr, "basicpoll");
1321
- pollset->data.ptr = NULL;
1322
- }
1323
- }
1324
-
1325
- static const grpc_pollset_vtable basic_pollset = {
1326
- basic_pollset_add_fd, basic_pollset_maybe_work_and_unlock,
1327
- basic_pollset_destroy, basic_pollset_destroy};
1328
-
1329
- static void become_basic_pollset(grpc_pollset *pollset, grpc_fd *fd_or_null) {
1330
- pollset->vtable = &basic_pollset;
1331
- pollset->data.ptr = fd_or_null;
1332
- if (fd_or_null != NULL) {
1333
- GRPC_FD_REF(fd_or_null, "basicpoll");
1334
- }
1335
- }
1336
-
1337
- /*******************************************************************************
1338
- * pollset_multipoller_with_poll_posix.c
1339
- */
1340
-
1341
- #ifndef GPR_LINUX_MULTIPOLL_WITH_EPOLL
1342
-
1343
- typedef struct {
1344
- /* all polled fds */
1345
- size_t fd_count;
1346
- size_t fd_capacity;
1347
- grpc_fd **fds;
1348
- /* fds that have been removed from the pollset explicitly */
1349
- size_t del_count;
1350
- size_t del_capacity;
1351
- grpc_fd **dels;
1352
- } poll_hdr;
1353
-
1354
- static void multipoll_with_poll_pollset_add_fd(grpc_exec_ctx *exec_ctx,
1355
- grpc_pollset *pollset,
1356
- grpc_fd *fd,
1357
- int and_unlock_pollset) {
1358
- size_t i;
1359
- poll_hdr *h = pollset->data.ptr;
1360
- /* TODO(ctiller): this is O(num_fds^2); maybe switch to a hash set here */
1361
- for (i = 0; i < h->fd_count; i++) {
1362
- if (h->fds[i] == fd) goto exit;
1363
- }
1364
- if (h->fd_count == h->fd_capacity) {
1365
- h->fd_capacity = GPR_MAX(h->fd_capacity + 8, h->fd_count * 3 / 2);
1366
- h->fds = gpr_realloc(h->fds, sizeof(grpc_fd *) * h->fd_capacity);
1367
- }
1368
- h->fds[h->fd_count++] = fd;
1369
- GRPC_FD_REF(fd, "multipoller");
1370
- exit:
1371
- if (and_unlock_pollset) {
1372
- gpr_mu_unlock(&pollset->mu);
1373
- }
1374
- }
1375
-
1376
- static grpc_error *multipoll_with_poll_pollset_maybe_work_and_unlock(
1377
- grpc_exec_ctx *exec_ctx, grpc_pollset *pollset, grpc_pollset_worker *worker,
1378
- gpr_timespec deadline, gpr_timespec now) {
1379
- #define POLLOUT_CHECK (POLLOUT | POLLHUP | POLLERR)
1380
- #define POLLIN_CHECK (POLLIN | POLLHUP | POLLERR)
1381
-
1382
- int timeout;
1383
- int r;
1384
- size_t i, j, fd_count;
1385
- nfds_t pfd_count;
1386
- poll_hdr *h;
1387
- /* TODO(ctiller): inline some elements to avoid an allocation */
1388
- grpc_fd_watcher *watchers;
1389
- struct pollfd *pfds;
1390
- grpc_error *error = GRPC_ERROR_NONE;
1391
-
1392
- h = pollset->data.ptr;
1393
- timeout = poll_deadline_to_millis_timeout(deadline, now);
1394
- /* TODO(ctiller): perform just one malloc here if we exceed the inline case */
1395
- pfds = gpr_malloc(sizeof(*pfds) * (h->fd_count + 2));
1396
- watchers = gpr_malloc(sizeof(*watchers) * (h->fd_count + 2));
1397
- fd_count = 0;
1398
- pfd_count = 2;
1399
- pfds[0].fd = GRPC_WAKEUP_FD_GET_READ_FD(&grpc_global_wakeup_fd);
1400
- pfds[0].events = POLLIN;
1401
- pfds[0].revents = 0;
1402
- pfds[1].fd = GRPC_WAKEUP_FD_GET_READ_FD(&worker->wakeup_fd->fd);
1403
- pfds[1].events = POLLIN;
1404
- pfds[1].revents = 0;
1405
- for (i = 0; i < h->fd_count; i++) {
1406
- int remove = fd_is_orphaned(h->fds[i]);
1407
- for (j = 0; !remove && j < h->del_count; j++) {
1408
- if (h->fds[i] == h->dels[j]) remove = 1;
1409
- }
1410
- if (remove) {
1411
- GRPC_FD_UNREF(h->fds[i], "multipoller");
1412
- } else {
1413
- h->fds[fd_count++] = h->fds[i];
1414
- watchers[pfd_count].fd = h->fds[i];
1415
- GRPC_FD_REF(watchers[pfd_count].fd, "multipoller_start");
1416
- pfds[pfd_count].fd = h->fds[i]->fd;
1417
- pfds[pfd_count].revents = 0;
1418
- pfd_count++;
1419
- }
1420
- }
1421
- for (j = 0; j < h->del_count; j++) {
1422
- GRPC_FD_UNREF(h->dels[j], "multipoller_del");
1423
- }
1424
- h->del_count = 0;
1425
- h->fd_count = fd_count;
1426
- gpr_mu_unlock(&pollset->mu);
1427
-
1428
- for (i = 2; i < pfd_count; i++) {
1429
- grpc_fd *fd = watchers[i].fd;
1430
- pfds[i].events = (short)fd_begin_poll(fd, pollset, worker, POLLIN, POLLOUT,
1431
- &watchers[i]);
1432
- GRPC_FD_UNREF(fd, "multipoller_start");
1433
- }
1434
-
1435
- /* TODO(vpai): Consider first doing a 0 timeout poll here to avoid
1436
- even going into the blocking annotation if possible */
1437
- GRPC_SCHEDULING_START_BLOCKING_REGION;
1438
- r = grpc_poll_function(pfds, pfd_count, timeout);
1439
- GRPC_SCHEDULING_END_BLOCKING_REGION;
1440
-
1441
- if (r < 0) {
1442
- if (errno != EINTR) {
1443
- work_combine_error(&error, GRPC_OS_ERROR(errno, "poll"));
1444
- }
1445
- for (i = 2; i < pfd_count; i++) {
1446
- fd_end_poll(exec_ctx, &watchers[i], 0, 0, NULL);
1447
- }
1448
- } else if (r == 0) {
1449
- for (i = 2; i < pfd_count; i++) {
1450
- fd_end_poll(exec_ctx, &watchers[i], 0, 0, NULL);
1451
- }
1452
- } else {
1453
- if (pfds[0].revents & POLLIN_CHECK) {
1454
- work_combine_error(&error,
1455
- grpc_wakeup_fd_consume_wakeup(&grpc_global_wakeup_fd));
1456
- }
1457
- if (pfds[1].revents & POLLIN_CHECK) {
1458
- work_combine_error(&error,
1459
- grpc_wakeup_fd_consume_wakeup(&worker->wakeup_fd->fd));
1460
- }
1461
- for (i = 2; i < pfd_count; i++) {
1462
- if (watchers[i].fd == NULL) {
1463
- fd_end_poll(exec_ctx, &watchers[i], 0, 0, NULL);
1464
- continue;
1465
- }
1466
- fd_end_poll(exec_ctx, &watchers[i], pfds[i].revents & POLLIN_CHECK,
1467
- pfds[i].revents & POLLOUT_CHECK, pollset);
1468
- }
1469
- }
1470
-
1471
- gpr_free(pfds);
1472
- gpr_free(watchers);
1473
-
1474
- return error;
1475
- }
1476
-
1477
- static void multipoll_with_poll_pollset_finish_shutdown(grpc_pollset *pollset) {
1478
- size_t i;
1479
- poll_hdr *h = pollset->data.ptr;
1480
- for (i = 0; i < h->fd_count; i++) {
1481
- GRPC_FD_UNREF(h->fds[i], "multipoller");
1482
- }
1483
- for (i = 0; i < h->del_count; i++) {
1484
- GRPC_FD_UNREF(h->dels[i], "multipoller_del");
1485
- }
1486
- h->fd_count = 0;
1487
- h->del_count = 0;
1488
- }
1489
-
1490
- static void multipoll_with_poll_pollset_destroy(grpc_pollset *pollset) {
1491
- poll_hdr *h = pollset->data.ptr;
1492
- multipoll_with_poll_pollset_finish_shutdown(pollset);
1493
- gpr_free(h->fds);
1494
- gpr_free(h->dels);
1495
- gpr_free(h);
1496
- }
1497
-
1498
- static const grpc_pollset_vtable multipoll_with_poll_pollset = {
1499
- multipoll_with_poll_pollset_add_fd,
1500
- multipoll_with_poll_pollset_maybe_work_and_unlock,
1501
- multipoll_with_poll_pollset_finish_shutdown,
1502
- multipoll_with_poll_pollset_destroy};
1503
-
1504
- static void poll_become_multipoller(grpc_exec_ctx *exec_ctx,
1505
- grpc_pollset *pollset, grpc_fd **fds,
1506
- size_t nfds) {
1507
- size_t i;
1508
- poll_hdr *h = gpr_malloc(sizeof(poll_hdr));
1509
- pollset->vtable = &multipoll_with_poll_pollset;
1510
- pollset->data.ptr = h;
1511
- h->fd_count = nfds;
1512
- h->fd_capacity = nfds;
1513
- h->fds = gpr_malloc(nfds * sizeof(grpc_fd *));
1514
- h->del_count = 0;
1515
- h->del_capacity = 0;
1516
- h->dels = NULL;
1517
- for (i = 0; i < nfds; i++) {
1518
- h->fds[i] = fds[i];
1519
- GRPC_FD_REF(fds[i], "multipoller");
1520
- }
1521
- }
1522
-
1523
- #endif /* !GPR_LINUX_MULTIPOLL_WITH_EPOLL */
1524
-
1525
- /*******************************************************************************
1526
- * pollset_multipoller_with_epoll_posix.c
1527
- */
1528
-
1529
- #ifdef GPR_LINUX_MULTIPOLL_WITH_EPOLL
1530
-
1531
- #include <errno.h>
1532
- #include <poll.h>
1533
- #include <string.h>
1534
- #include <sys/epoll.h>
1535
- #include <unistd.h>
1536
-
1537
- #include <grpc/support/alloc.h>
1538
- #include <grpc/support/log.h>
1539
- #include <grpc/support/useful.h>
1540
-
1541
- #include "src/core/lib/iomgr/ev_posix.h"
1542
- #include "src/core/lib/profiling/timers.h"
1543
- #include "src/core/lib/support/block_annotate.h"
1544
-
1545
- static void set_ready(grpc_exec_ctx *exec_ctx, grpc_fd *fd, grpc_closure **st,
1546
- grpc_pollset *read_notifier_pollset) {
1547
- /* only one set_ready can be active at once (but there may be a racing
1548
- notify_on) */
1549
- gpr_mu_lock(&fd->mu);
1550
- set_ready_locked(exec_ctx, fd, st);
1551
-
1552
- /* A non-NULL read_notifier_pollset means that the fd is readable. */
1553
- if (read_notifier_pollset != NULL) {
1554
- /* Note: Since the fd might be a part of multiple pollsets, this might be
1555
- * called multiple times (for each time the fd becomes readable) and it is
1556
- * okay to set the fd's read-notifier pollset to anyone of these pollsets */
1557
- set_read_notifier_pollset_locked(exec_ctx, fd, read_notifier_pollset);
1558
- }
1559
-
1560
- gpr_mu_unlock(&fd->mu);
1561
- }
1562
-
1563
- static void fd_become_readable(grpc_exec_ctx *exec_ctx, grpc_fd *fd,
1564
- grpc_pollset *notifier_pollset) {
1565
- set_ready(exec_ctx, fd, &fd->read_closure, notifier_pollset);
1566
- }
1567
-
1568
- static void fd_become_writable(grpc_exec_ctx *exec_ctx, grpc_fd *fd) {
1569
- set_ready(exec_ctx, fd, &fd->write_closure, NULL);
1570
- }
1571
-
1572
- struct epoll_fd_list {
1573
- int *epoll_fds;
1574
- size_t count;
1575
- size_t capacity;
1576
- };
1577
-
1578
- static struct epoll_fd_list epoll_fd_global_list;
1579
- static gpr_once init_epoll_fd_list_mu = GPR_ONCE_INIT;
1580
- static gpr_mu epoll_fd_list_mu;
1581
-
1582
- static void init_mu(void) { gpr_mu_init(&epoll_fd_list_mu); }
1583
-
1584
- static void add_epoll_fd_to_global_list(int epoll_fd) {
1585
- gpr_once_init(&init_epoll_fd_list_mu, init_mu);
1586
-
1587
- gpr_mu_lock(&epoll_fd_list_mu);
1588
- if (epoll_fd_global_list.count == epoll_fd_global_list.capacity) {
1589
- epoll_fd_global_list.capacity =
1590
- GPR_MAX((size_t)8, epoll_fd_global_list.capacity * 2);
1591
- epoll_fd_global_list.epoll_fds =
1592
- gpr_realloc(epoll_fd_global_list.epoll_fds,
1593
- epoll_fd_global_list.capacity * sizeof(int));
1594
- }
1595
- epoll_fd_global_list.epoll_fds[epoll_fd_global_list.count++] = epoll_fd;
1596
- gpr_mu_unlock(&epoll_fd_list_mu);
1597
- }
1598
-
1599
- static void remove_epoll_fd_from_global_list(int epoll_fd) {
1600
- gpr_mu_lock(&epoll_fd_list_mu);
1601
- GPR_ASSERT(epoll_fd_global_list.count > 0);
1602
- for (size_t i = 0; i < epoll_fd_global_list.count; i++) {
1603
- if (epoll_fd == epoll_fd_global_list.epoll_fds[i]) {
1604
- epoll_fd_global_list.epoll_fds[i] =
1605
- epoll_fd_global_list.epoll_fds[--(epoll_fd_global_list.count)];
1606
- break;
1607
- }
1608
- }
1609
- gpr_mu_unlock(&epoll_fd_list_mu);
1610
- }
1611
-
1612
- static void remove_fd_from_all_epoll_sets(int fd) {
1613
- int err;
1614
- gpr_once_init(&init_epoll_fd_list_mu, init_mu);
1615
- gpr_mu_lock(&epoll_fd_list_mu);
1616
- if (epoll_fd_global_list.count == 0) {
1617
- gpr_mu_unlock(&epoll_fd_list_mu);
1618
- return;
1619
- }
1620
- for (size_t i = 0; i < epoll_fd_global_list.count; i++) {
1621
- err = epoll_ctl(epoll_fd_global_list.epoll_fds[i], EPOLL_CTL_DEL, fd, NULL);
1622
- if (err < 0 && errno != ENOENT) {
1623
- gpr_log(GPR_ERROR, "epoll_ctl del for %d failed: %s", fd,
1624
- strerror(errno));
1625
- }
1626
- }
1627
- gpr_mu_unlock(&epoll_fd_list_mu);
1628
- }
1629
-
1630
- typedef struct {
1631
- grpc_pollset *pollset;
1632
- grpc_fd *fd;
1633
- grpc_closure closure;
1634
- } delayed_add;
1635
-
1636
- typedef struct { int epoll_fd; } epoll_hdr;
1637
-
1638
- static void finally_add_fd(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
1639
- grpc_fd *fd) {
1640
- epoll_hdr *h = pollset->data.ptr;
1641
- struct epoll_event ev;
1642
- int err;
1643
- grpc_fd_watcher watcher;
1644
-
1645
- /* We pretend to be polling whilst adding an fd to keep the fd from being
1646
- closed during the add. This may result in a spurious wakeup being assigned
1647
- to this pollset whilst adding, but that should be benign. */
1648
- GPR_ASSERT(fd_begin_poll(fd, pollset, NULL, 0, 0, &watcher) == 0);
1649
- if (watcher.fd != NULL) {
1650
- ev.events = (uint32_t)(EPOLLIN | EPOLLOUT | EPOLLET);
1651
- ev.data.ptr = fd;
1652
- err = epoll_ctl(h->epoll_fd, EPOLL_CTL_ADD, fd->fd, &ev);
1653
- if (err < 0) {
1654
- /* FDs may be added to a pollset multiple times, so EEXIST is normal. */
1655
- if (errno != EEXIST) {
1656
- gpr_log(GPR_ERROR, "epoll_ctl add for %d failed: %s", fd->fd,
1657
- strerror(errno));
1658
- }
1659
- }
1660
- }
1661
- fd_end_poll(exec_ctx, &watcher, 0, 0, NULL);
1662
- }
1663
-
1664
- static void perform_delayed_add(grpc_exec_ctx *exec_ctx, void *arg,
1665
- grpc_error *error) {
1666
- delayed_add *da = arg;
1667
-
1668
- if (!fd_is_orphaned(da->fd)) {
1669
- finally_add_fd(exec_ctx, da->pollset, da->fd);
1670
- }
1671
-
1672
- gpr_mu_lock(&da->pollset->mu);
1673
- da->pollset->in_flight_cbs--;
1674
- if (da->pollset->shutting_down) {
1675
- /* We don't care about this pollset anymore. */
1676
- if (da->pollset->in_flight_cbs == 0 && !da->pollset->called_shutdown) {
1677
- da->pollset->called_shutdown = 1;
1678
- grpc_exec_ctx_sched(exec_ctx, da->pollset->shutdown_done, GRPC_ERROR_NONE,
1679
- NULL);
1680
- }
1681
- }
1682
- gpr_mu_unlock(&da->pollset->mu);
1683
-
1684
- GRPC_FD_UNREF(da->fd, "delayed_add");
1685
-
1686
- gpr_free(da);
1687
- }
1688
-
1689
- static void multipoll_with_epoll_pollset_add_fd(grpc_exec_ctx *exec_ctx,
1690
- grpc_pollset *pollset,
1691
- grpc_fd *fd,
1692
- int and_unlock_pollset) {
1693
- if (and_unlock_pollset) {
1694
- gpr_mu_unlock(&pollset->mu);
1695
- finally_add_fd(exec_ctx, pollset, fd);
1696
- } else {
1697
- delayed_add *da = gpr_malloc(sizeof(*da));
1698
- da->pollset = pollset;
1699
- da->fd = fd;
1700
- GRPC_FD_REF(fd, "delayed_add");
1701
- grpc_closure_init(&da->closure, perform_delayed_add, da);
1702
- pollset->in_flight_cbs++;
1703
- grpc_exec_ctx_sched(exec_ctx, &da->closure, GRPC_ERROR_NONE, NULL);
1704
- }
1705
- }
1706
-
1707
- /* TODO(klempner): We probably want to turn this down a bit */
1708
- #define GRPC_EPOLL_MAX_EVENTS 1000
1709
-
1710
- static grpc_error *multipoll_with_epoll_pollset_maybe_work_and_unlock(
1711
- grpc_exec_ctx *exec_ctx, grpc_pollset *pollset, grpc_pollset_worker *worker,
1712
- gpr_timespec deadline, gpr_timespec now) {
1713
- struct epoll_event ep_ev[GRPC_EPOLL_MAX_EVENTS];
1714
- int ep_rv;
1715
- int poll_rv;
1716
- epoll_hdr *h = pollset->data.ptr;
1717
- int timeout_ms;
1718
- struct pollfd pfds[2];
1719
- grpc_error *error = GRPC_ERROR_NONE;
1720
-
1721
- /* If you want to ignore epoll's ability to sanely handle parallel pollers,
1722
- * for a more apples-to-apples performance comparison with poll, add a
1723
- * if (pollset->counter != 0) { return 0; }
1724
- * here.
1725
- */
1726
-
1727
- gpr_mu_unlock(&pollset->mu);
1728
-
1729
- timeout_ms = poll_deadline_to_millis_timeout(deadline, now);
1730
-
1731
- pfds[0].fd = GRPC_WAKEUP_FD_GET_READ_FD(&worker->wakeup_fd->fd);
1732
- pfds[0].events = POLLIN;
1733
- pfds[0].revents = 0;
1734
- pfds[1].fd = h->epoll_fd;
1735
- pfds[1].events = POLLIN;
1736
- pfds[1].revents = 0;
1737
-
1738
- /* TODO(vpai): Consider first doing a 0 timeout poll here to avoid
1739
- even going into the blocking annotation if possible */
1740
- GPR_TIMER_BEGIN("poll", 0);
1741
- GRPC_SCHEDULING_START_BLOCKING_REGION;
1742
- poll_rv = grpc_poll_function(pfds, 2, timeout_ms);
1743
- GRPC_SCHEDULING_END_BLOCKING_REGION;
1744
- GPR_TIMER_END("poll", 0);
1745
-
1746
- if (poll_rv < 0) {
1747
- if (errno != EINTR) {
1748
- work_combine_error(&error, GRPC_OS_ERROR(errno, "poll"));
1749
- }
1750
- } else if (poll_rv == 0) {
1751
- /* do nothing */
1752
- } else {
1753
- if (pfds[0].revents) {
1754
- work_combine_error(&error,
1755
- grpc_wakeup_fd_consume_wakeup(&worker->wakeup_fd->fd));
1756
- }
1757
- if (pfds[1].revents) {
1758
- do {
1759
- /* The following epoll_wait never blocks; it has a timeout of 0 */
1760
- ep_rv = epoll_wait(h->epoll_fd, ep_ev, GRPC_EPOLL_MAX_EVENTS, 0);
1761
- if (ep_rv < 0) {
1762
- if (errno != EINTR) {
1763
- work_combine_error(&error, GRPC_OS_ERROR(errno, "epoll_wait"));
1764
- }
1765
- } else {
1766
- int i;
1767
- for (i = 0; i < ep_rv; ++i) {
1768
- grpc_fd *fd = ep_ev[i].data.ptr;
1769
- /* TODO(klempner): We might want to consider making err and pri
1770
- * separate events */
1771
- int cancel = ep_ev[i].events & (EPOLLERR | EPOLLHUP);
1772
- int read_ev = ep_ev[i].events & (EPOLLIN | EPOLLPRI);
1773
- int write_ev = ep_ev[i].events & EPOLLOUT;
1774
- if (fd == NULL) {
1775
- work_combine_error(&error, grpc_wakeup_fd_consume_wakeup(
1776
- &grpc_global_wakeup_fd));
1777
- } else {
1778
- if (read_ev || cancel) {
1779
- fd_become_readable(exec_ctx, fd, pollset);
1780
- }
1781
- if (write_ev || cancel) {
1782
- fd_become_writable(exec_ctx, fd);
1783
- }
1784
- }
1785
- }
1786
- }
1787
- } while (ep_rv == GRPC_EPOLL_MAX_EVENTS);
1788
- }
1789
- }
1790
- return error;
1791
- }
1792
-
1793
- static void multipoll_with_epoll_pollset_finish_shutdown(
1794
- grpc_pollset *pollset) {}
1795
-
1796
- static void multipoll_with_epoll_pollset_destroy(grpc_pollset *pollset) {
1797
- epoll_hdr *h = pollset->data.ptr;
1798
- close(h->epoll_fd);
1799
- remove_epoll_fd_from_global_list(h->epoll_fd);
1800
- gpr_free(h);
1801
- }
1802
-
1803
- static const grpc_pollset_vtable multipoll_with_epoll_pollset = {
1804
- multipoll_with_epoll_pollset_add_fd,
1805
- multipoll_with_epoll_pollset_maybe_work_and_unlock,
1806
- multipoll_with_epoll_pollset_finish_shutdown,
1807
- multipoll_with_epoll_pollset_destroy};
1808
-
1809
- static void epoll_become_multipoller(grpc_exec_ctx *exec_ctx,
1810
- grpc_pollset *pollset, grpc_fd **fds,
1811
- size_t nfds) {
1812
- size_t i;
1813
- epoll_hdr *h = gpr_malloc(sizeof(epoll_hdr));
1814
- struct epoll_event ev;
1815
- int err;
1816
-
1817
- pollset->vtable = &multipoll_with_epoll_pollset;
1818
- pollset->data.ptr = h;
1819
- h->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
1820
- if (h->epoll_fd < 0) {
1821
- /* TODO(klempner): Fall back to poll here, especially on ENOSYS */
1822
- gpr_log(GPR_ERROR, "epoll_create1 failed: %s", strerror(errno));
1823
- abort();
1824
- }
1825
- add_epoll_fd_to_global_list(h->epoll_fd);
1826
-
1827
- ev.events = (uint32_t)(EPOLLIN | EPOLLET);
1828
- ev.data.ptr = NULL;
1829
- err = epoll_ctl(h->epoll_fd, EPOLL_CTL_ADD,
1830
- GRPC_WAKEUP_FD_GET_READ_FD(&grpc_global_wakeup_fd), &ev);
1831
- if (err < 0) {
1832
- gpr_log(GPR_ERROR, "epoll_ctl add for %d failed: %s",
1833
- GRPC_WAKEUP_FD_GET_READ_FD(&grpc_global_wakeup_fd),
1834
- strerror(errno));
1835
- }
1836
-
1837
- for (i = 0; i < nfds; i++) {
1838
- multipoll_with_epoll_pollset_add_fd(exec_ctx, pollset, fds[i], 0);
1839
- }
1840
- }
1841
-
1842
- #else /* GPR_LINUX_MULTIPOLL_WITH_EPOLL */
1843
-
1844
- static void remove_fd_from_all_epoll_sets(int fd) {}
1845
-
1846
- #endif /* GPR_LINUX_MULTIPOLL_WITH_EPOLL */
1847
-
1848
- /*******************************************************************************
1849
- * pollset_set_posix.c
1850
- */
1851
-
1852
- static grpc_pollset_set *pollset_set_create(void) {
1853
- grpc_pollset_set *pollset_set = gpr_malloc(sizeof(*pollset_set));
1854
- memset(pollset_set, 0, sizeof(*pollset_set));
1855
- gpr_mu_init(&pollset_set->mu);
1856
- return pollset_set;
1857
- }
1858
-
1859
- static void pollset_set_destroy(grpc_pollset_set *pollset_set) {
1860
- size_t i;
1861
- gpr_mu_destroy(&pollset_set->mu);
1862
- for (i = 0; i < pollset_set->fd_count; i++) {
1863
- GRPC_FD_UNREF(pollset_set->fds[i], "pollset_set");
1864
- }
1865
- gpr_free(pollset_set->pollsets);
1866
- gpr_free(pollset_set->pollset_sets);
1867
- gpr_free(pollset_set->fds);
1868
- gpr_free(pollset_set);
1869
- }
1870
-
1871
- static void pollset_set_add_pollset(grpc_exec_ctx *exec_ctx,
1872
- grpc_pollset_set *pollset_set,
1873
- grpc_pollset *pollset) {
1874
- size_t i, j;
1875
- gpr_mu_lock(&pollset_set->mu);
1876
- if (pollset_set->pollset_count == pollset_set->pollset_capacity) {
1877
- pollset_set->pollset_capacity =
1878
- GPR_MAX(8, 2 * pollset_set->pollset_capacity);
1879
- pollset_set->pollsets =
1880
- gpr_realloc(pollset_set->pollsets, pollset_set->pollset_capacity *
1881
- sizeof(*pollset_set->pollsets));
1882
- }
1883
- pollset_set->pollsets[pollset_set->pollset_count++] = pollset;
1884
- for (i = 0, j = 0; i < pollset_set->fd_count; i++) {
1885
- if (fd_is_orphaned(pollset_set->fds[i])) {
1886
- GRPC_FD_UNREF(pollset_set->fds[i], "pollset_set");
1887
- } else {
1888
- pollset_add_fd(exec_ctx, pollset, pollset_set->fds[i]);
1889
- pollset_set->fds[j++] = pollset_set->fds[i];
1890
- }
1891
- }
1892
- pollset_set->fd_count = j;
1893
- gpr_mu_unlock(&pollset_set->mu);
1894
- }
1895
-
1896
- static void pollset_set_del_pollset(grpc_exec_ctx *exec_ctx,
1897
- grpc_pollset_set *pollset_set,
1898
- grpc_pollset *pollset) {
1899
- size_t i;
1900
- gpr_mu_lock(&pollset_set->mu);
1901
- for (i = 0; i < pollset_set->pollset_count; i++) {
1902
- if (pollset_set->pollsets[i] == pollset) {
1903
- pollset_set->pollset_count--;
1904
- GPR_SWAP(grpc_pollset *, pollset_set->pollsets[i],
1905
- pollset_set->pollsets[pollset_set->pollset_count]);
1906
- break;
1907
- }
1908
- }
1909
- gpr_mu_unlock(&pollset_set->mu);
1910
- }
1911
-
1912
- static void pollset_set_add_pollset_set(grpc_exec_ctx *exec_ctx,
1913
- grpc_pollset_set *bag,
1914
- grpc_pollset_set *item) {
1915
- size_t i, j;
1916
- gpr_mu_lock(&bag->mu);
1917
- if (bag->pollset_set_count == bag->pollset_set_capacity) {
1918
- bag->pollset_set_capacity = GPR_MAX(8, 2 * bag->pollset_set_capacity);
1919
- bag->pollset_sets =
1920
- gpr_realloc(bag->pollset_sets,
1921
- bag->pollset_set_capacity * sizeof(*bag->pollset_sets));
1922
- }
1923
- bag->pollset_sets[bag->pollset_set_count++] = item;
1924
- for (i = 0, j = 0; i < bag->fd_count; i++) {
1925
- if (fd_is_orphaned(bag->fds[i])) {
1926
- GRPC_FD_UNREF(bag->fds[i], "pollset_set");
1927
- } else {
1928
- pollset_set_add_fd(exec_ctx, item, bag->fds[i]);
1929
- bag->fds[j++] = bag->fds[i];
1930
- }
1931
- }
1932
- bag->fd_count = j;
1933
- gpr_mu_unlock(&bag->mu);
1934
- }
1935
-
1936
- static void pollset_set_del_pollset_set(grpc_exec_ctx *exec_ctx,
1937
- grpc_pollset_set *bag,
1938
- grpc_pollset_set *item) {
1939
- size_t i;
1940
- gpr_mu_lock(&bag->mu);
1941
- for (i = 0; i < bag->pollset_set_count; i++) {
1942
- if (bag->pollset_sets[i] == item) {
1943
- bag->pollset_set_count--;
1944
- GPR_SWAP(grpc_pollset_set *, bag->pollset_sets[i],
1945
- bag->pollset_sets[bag->pollset_set_count]);
1946
- break;
1947
- }
1948
- }
1949
- gpr_mu_unlock(&bag->mu);
1950
- }
1951
-
1952
- static void pollset_set_add_fd(grpc_exec_ctx *exec_ctx,
1953
- grpc_pollset_set *pollset_set, grpc_fd *fd) {
1954
- size_t i;
1955
- gpr_mu_lock(&pollset_set->mu);
1956
- if (pollset_set->fd_count == pollset_set->fd_capacity) {
1957
- pollset_set->fd_capacity = GPR_MAX(8, 2 * pollset_set->fd_capacity);
1958
- pollset_set->fds = gpr_realloc(
1959
- pollset_set->fds, pollset_set->fd_capacity * sizeof(*pollset_set->fds));
1960
- }
1961
- GRPC_FD_REF(fd, "pollset_set");
1962
- pollset_set->fds[pollset_set->fd_count++] = fd;
1963
- for (i = 0; i < pollset_set->pollset_count; i++) {
1964
- pollset_add_fd(exec_ctx, pollset_set->pollsets[i], fd);
1965
- }
1966
- for (i = 0; i < pollset_set->pollset_set_count; i++) {
1967
- pollset_set_add_fd(exec_ctx, pollset_set->pollset_sets[i], fd);
1968
- }
1969
- gpr_mu_unlock(&pollset_set->mu);
1970
- }
1971
-
1972
- static void pollset_set_del_fd(grpc_exec_ctx *exec_ctx,
1973
- grpc_pollset_set *pollset_set, grpc_fd *fd) {
1974
- size_t i;
1975
- gpr_mu_lock(&pollset_set->mu);
1976
- for (i = 0; i < pollset_set->fd_count; i++) {
1977
- if (pollset_set->fds[i] == fd) {
1978
- pollset_set->fd_count--;
1979
- GPR_SWAP(grpc_fd *, pollset_set->fds[i],
1980
- pollset_set->fds[pollset_set->fd_count]);
1981
- GRPC_FD_UNREF(fd, "pollset_set");
1982
- break;
1983
- }
1984
- }
1985
- for (i = 0; i < pollset_set->pollset_set_count; i++) {
1986
- pollset_set_del_fd(exec_ctx, pollset_set->pollset_sets[i], fd);
1987
- }
1988
- gpr_mu_unlock(&pollset_set->mu);
1989
- }
1990
-
1991
- /*******************************************************************************
1992
- * event engine binding
1993
- */
1994
-
1995
- static void shutdown_engine(void) {
1996
- fd_global_shutdown();
1997
- pollset_global_shutdown();
1998
- }
1999
-
2000
- static const grpc_event_engine_vtable vtable = {
2001
- .pollset_size = sizeof(grpc_pollset),
2002
-
2003
- .fd_create = fd_create,
2004
- .fd_wrapped_fd = fd_wrapped_fd,
2005
- .fd_orphan = fd_orphan,
2006
- .fd_shutdown = fd_shutdown,
2007
- .fd_is_shutdown = fd_is_shutdown,
2008
- .fd_notify_on_read = fd_notify_on_read,
2009
- .fd_notify_on_write = fd_notify_on_write,
2010
- .fd_get_read_notifier_pollset = fd_get_read_notifier_pollset,
2011
- .fd_get_workqueue = fd_get_workqueue,
2012
-
2013
- .pollset_init = pollset_init,
2014
- .pollset_shutdown = pollset_shutdown,
2015
- .pollset_reset = pollset_reset,
2016
- .pollset_destroy = pollset_destroy,
2017
- .pollset_work = pollset_work,
2018
- .pollset_kick = pollset_kick,
2019
- .pollset_add_fd = pollset_add_fd,
2020
-
2021
- .pollset_set_create = pollset_set_create,
2022
- .pollset_set_destroy = pollset_set_destroy,
2023
- .pollset_set_add_pollset = pollset_set_add_pollset,
2024
- .pollset_set_del_pollset = pollset_set_del_pollset,
2025
- .pollset_set_add_pollset_set = pollset_set_add_pollset_set,
2026
- .pollset_set_del_pollset_set = pollset_set_del_pollset_set,
2027
- .pollset_set_add_fd = pollset_set_add_fd,
2028
- .pollset_set_del_fd = pollset_set_del_fd,
2029
-
2030
- .kick_poller = kick_poller,
2031
-
2032
- .shutdown_engine = shutdown_engine,
2033
- };
2034
-
2035
- const grpc_event_engine_vtable *grpc_init_poll_and_epoll_posix(void) {
2036
- #ifdef GPR_LINUX_MULTIPOLL_WITH_EPOLL
2037
- platform_become_multipoller = epoll_become_multipoller;
2038
- #else
2039
- platform_become_multipoller = poll_become_multipoller;
2040
- #endif
2041
- fd_global_init();
2042
- pollset_global_init();
2043
- return &vtable;
2044
- }
2045
-
2046
- #endif