grpc 0.13.1 → 0.14.1.pre1

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

Potentially problematic release.


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

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