grpc 1.3.4 → 1.4.0

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

Potentially problematic release.


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

Files changed (286) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +581 -450
  3. data/include/grpc/census.h +49 -49
  4. data/include/grpc/grpc.h +16 -70
  5. data/include/grpc/grpc_security.h +59 -59
  6. data/include/grpc/grpc_security_constants.h +9 -9
  7. data/include/grpc/impl/codegen/atm.h +1 -1
  8. data/include/grpc/impl/codegen/atm_windows.h +4 -4
  9. data/include/grpc/impl/codegen/byte_buffer_reader.h +2 -2
  10. data/include/grpc/impl/codegen/compression_types.h +4 -5
  11. data/include/grpc/impl/codegen/gpr_slice.h +5 -5
  12. data/include/grpc/impl/codegen/gpr_types.h +6 -7
  13. data/include/grpc/impl/codegen/grpc_types.h +128 -59
  14. data/include/grpc/impl/codegen/port_platform.h +6 -0
  15. data/include/grpc/impl/codegen/propagation_bits.h +2 -2
  16. data/include/grpc/impl/codegen/slice.h +13 -12
  17. data/include/grpc/impl/codegen/status.h +23 -18
  18. data/include/grpc/impl/codegen/sync.h +1 -1
  19. data/include/grpc/load_reporting.h +6 -6
  20. data/include/grpc/slice.h +47 -25
  21. data/include/grpc/slice_buffer.h +18 -14
  22. data/include/grpc/support/alloc.h +7 -7
  23. data/include/grpc/support/cmdline.h +10 -10
  24. data/include/grpc/support/cpu.h +3 -3
  25. data/include/grpc/support/histogram.h +1 -1
  26. data/include/grpc/support/host_port.h +2 -2
  27. data/include/grpc/support/log.h +9 -9
  28. data/include/grpc/support/log_windows.h +1 -1
  29. data/include/grpc/support/string_util.h +3 -3
  30. data/include/grpc/support/subprocess.h +3 -3
  31. data/include/grpc/support/sync.h +31 -31
  32. data/include/grpc/support/thd.h +11 -11
  33. data/include/grpc/support/time.h +12 -12
  34. data/include/grpc/support/tls.h +1 -1
  35. data/include/grpc/support/tls_gcc.h +2 -2
  36. data/include/grpc/support/tls_msvc.h +1 -1
  37. data/include/grpc/support/tls_pthread.h +1 -1
  38. data/include/grpc/support/useful.h +2 -2
  39. data/include/grpc/support/workaround_list.h +46 -0
  40. data/src/core/ext/census/context.c +1 -1
  41. data/src/core/ext/census/intrusive_hash_map.c +319 -0
  42. data/src/core/ext/census/intrusive_hash_map.h +167 -0
  43. data/src/core/ext/census/intrusive_hash_map_internal.h +63 -0
  44. data/src/core/ext/census/resource.c +3 -1
  45. data/src/core/ext/filters/client_channel/channel_connectivity.c +1 -1
  46. data/src/core/ext/filters/client_channel/client_channel.c +173 -103
  47. data/src/core/ext/filters/client_channel/client_channel_plugin.c +3 -2
  48. data/src/core/ext/filters/client_channel/lb_policy.c +2 -1
  49. data/src/core/ext/filters/client_channel/lb_policy.h +8 -7
  50. data/src/core/ext/filters/client_channel/lb_policy/grpclb/client_load_reporting_filter.c +153 -0
  51. data/src/core/ext/filters/client_channel/lb_policy/grpclb/client_load_reporting_filter.h +42 -0
  52. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.c +405 -102
  53. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_client_stats.c +133 -0
  54. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_client_stats.h +65 -0
  55. data/src/core/ext/filters/client_channel/lb_policy/grpclb/load_balancer_api.c +90 -51
  56. data/src/core/ext/filters/client_channel/lb_policy/grpclb/load_balancer_api.h +7 -1
  57. data/src/core/ext/filters/client_channel/lb_policy/grpclb/proto/grpc/lb/v1/load_balancer.pb.c +19 -8
  58. data/src/core/ext/filters/client_channel/lb_policy/grpclb/proto/grpc/lb/v1/load_balancer.pb.h +63 -34
  59. data/src/core/ext/filters/client_channel/lb_policy/pick_first/pick_first.c +2 -1
  60. data/src/core/ext/filters/client_channel/lb_policy/round_robin/round_robin.c +188 -294
  61. data/src/core/ext/filters/client_channel/lb_policy_factory.c +28 -5
  62. data/src/core/ext/filters/client_channel/lb_policy_factory.h +18 -4
  63. data/src/core/ext/filters/client_channel/parse_address.c +90 -59
  64. data/src/core/ext/filters/client_channel/parse_address.h +17 -8
  65. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/dns_resolver_ares.c +11 -7
  66. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.c +59 -14
  67. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.h +6 -0
  68. data/src/core/ext/filters/client_channel/resolver/sockaddr/sockaddr_resolver.c +3 -3
  69. data/src/core/ext/filters/client_channel/subchannel.c +20 -17
  70. data/src/core/ext/filters/client_channel/subchannel.h +1 -0
  71. data/src/core/ext/filters/client_channel/subchannel_index.c +11 -1
  72. data/src/core/ext/filters/client_channel/uri_parser.c +36 -22
  73. data/src/core/ext/filters/client_channel/uri_parser.h +1 -1
  74. data/src/core/{lib/channel → ext/filters/deadline}/deadline_filter.c +42 -17
  75. data/src/core/{lib/channel → ext/filters/deadline}/deadline_filter.h +8 -9
  76. data/src/core/{lib/channel → ext/filters/http/client}/http_client_filter.c +19 -11
  77. data/src/core/{lib/channel → ext/filters/http/client}/http_client_filter.h +3 -6
  78. data/src/core/ext/filters/http/http_filters_plugin.c +104 -0
  79. data/src/core/{lib/channel/compress_filter.c → ext/filters/http/message_compress/message_compress_filter.c} +124 -23
  80. data/src/core/{lib/channel/compress_filter.h → ext/filters/http/message_compress/message_compress_filter.h} +5 -6
  81. data/src/core/{lib/channel → ext/filters/http/server}/http_server_filter.c +4 -6
  82. data/src/core/{lib/channel → ext/filters/http/server}/http_server_filter.h +3 -3
  83. data/src/core/ext/filters/load_reporting/load_reporting.c +2 -25
  84. data/src/core/ext/filters/load_reporting/load_reporting_filter.c +26 -1
  85. data/src/core/ext/filters/max_age/max_age_filter.c +14 -14
  86. data/src/core/{lib/channel → ext/filters/message_size}/message_size_filter.c +91 -47
  87. data/src/core/{lib/channel → ext/filters/message_size}/message_size_filter.h +3 -3
  88. data/src/core/ext/filters/workarounds/workaround_cronet_compression_filter.c +223 -0
  89. data/src/core/ext/filters/workarounds/workaround_cronet_compression_filter.h +40 -0
  90. data/src/core/ext/filters/workarounds/workaround_utils.c +65 -0
  91. data/src/core/ext/filters/workarounds/workaround_utils.h +52 -0
  92. data/src/core/ext/transport/chttp2/client/insecure/channel_create.c +1 -1
  93. data/src/core/ext/transport/chttp2/server/chttp2_server.c +3 -2
  94. data/src/core/ext/transport/chttp2/transport/bin_decoder.c +2 -2
  95. data/src/core/ext/transport/chttp2/transport/bin_encoder.c +3 -3
  96. data/src/core/ext/transport/chttp2/transport/chttp2_transport.c +319 -175
  97. data/src/core/ext/transport/chttp2/transport/chttp2_transport.h +3 -2
  98. data/src/core/ext/transport/chttp2/transport/frame_data.c +203 -164
  99. data/src/core/ext/transport/chttp2/transport/frame_data.h +8 -14
  100. data/src/core/ext/transport/chttp2/transport/frame_goaway.c +1 -1
  101. data/src/core/ext/transport/chttp2/transport/frame_ping.c +1 -1
  102. data/src/core/ext/transport/chttp2/transport/frame_rst_stream.c +1 -1
  103. data/src/core/ext/transport/chttp2/transport/frame_settings.c +5 -5
  104. data/src/core/ext/transport/chttp2/transport/frame_window_update.c +1 -1
  105. data/src/core/ext/transport/chttp2/transport/hpack_encoder.c +4 -4
  106. data/src/core/ext/transport/chttp2/transport/hpack_parser.c +2 -4
  107. data/src/core/ext/transport/chttp2/transport/hpack_table.c +4 -3
  108. data/src/core/ext/transport/chttp2/transport/internal.h +50 -33
  109. data/src/core/ext/transport/chttp2/transport/parsing.c +10 -11
  110. data/src/core/ext/transport/chttp2/transport/writing.c +32 -13
  111. data/src/core/lib/channel/channel_args.c +30 -9
  112. data/src/core/lib/channel/channel_args.h +5 -1
  113. data/src/core/lib/channel/channel_stack.c +1 -1
  114. data/src/core/lib/channel/channel_stack.h +2 -2
  115. data/src/core/lib/channel/channel_stack_builder.c +13 -1
  116. data/src/core/lib/channel/channel_stack_builder.h +5 -1
  117. data/src/core/lib/channel/connected_channel.c +3 -1
  118. data/src/core/lib/channel/context.h +2 -2
  119. data/src/core/lib/compression/message_compress.c +2 -2
  120. data/src/core/lib/debug/trace.c +13 -6
  121. data/src/core/lib/debug/trace.h +27 -1
  122. data/src/core/lib/http/httpcli.c +1 -1
  123. data/src/core/lib/http/httpcli_security_connector.c +9 -11
  124. data/src/core/lib/http/parser.c +2 -2
  125. data/src/core/lib/http/parser.h +2 -1
  126. data/src/core/lib/iomgr/combiner.c +6 -6
  127. data/src/core/lib/iomgr/combiner.h +2 -1
  128. data/src/core/lib/iomgr/error.c +12 -5
  129. data/src/core/lib/iomgr/error.h +13 -13
  130. data/src/core/lib/iomgr/ev_epoll1_linux.c +984 -0
  131. data/src/core/lib/iomgr/ev_epoll1_linux.h +44 -0
  132. data/src/core/lib/iomgr/ev_epoll_limited_pollers_linux.c +2146 -0
  133. data/src/core/lib/iomgr/ev_epoll_limited_pollers_linux.h +43 -0
  134. data/src/core/lib/iomgr/ev_epoll_thread_pool_linux.c +1337 -0
  135. data/src/core/lib/iomgr/ev_epoll_thread_pool_linux.h +43 -0
  136. data/src/core/lib/iomgr/ev_epollex_linux.c +1511 -0
  137. data/src/core/lib/iomgr/ev_epollex_linux.h +43 -0
  138. data/src/core/lib/iomgr/{ev_epoll_linux.c → ev_epollsig_linux.c} +41 -33
  139. data/src/core/lib/iomgr/{ev_epoll_linux.h → ev_epollsig_linux.h} +4 -4
  140. data/src/core/lib/iomgr/ev_poll_posix.c +12 -27
  141. data/src/core/lib/iomgr/ev_poll_posix.h +2 -2
  142. data/src/core/lib/iomgr/ev_posix.c +22 -8
  143. data/src/core/lib/iomgr/ev_posix.h +4 -3
  144. data/src/core/lib/iomgr/ev_windows.c +43 -0
  145. data/src/core/lib/iomgr/exec_ctx.c +5 -0
  146. data/src/core/lib/iomgr/exec_ctx.h +2 -0
  147. data/src/core/lib/iomgr/iomgr.c +4 -0
  148. data/src/core/lib/iomgr/iomgr.h +3 -0
  149. data/src/core/lib/iomgr/is_epollexclusive_available.c +116 -0
  150. data/src/core/lib/iomgr/is_epollexclusive_available.h +41 -0
  151. data/src/core/lib/iomgr/lockfree_event.c +16 -0
  152. data/src/core/lib/iomgr/pollset.h +2 -5
  153. data/src/core/lib/iomgr/pollset_uv.c +1 -1
  154. data/src/core/lib/iomgr/pollset_windows.c +3 -3
  155. data/src/core/lib/iomgr/resource_quota.c +9 -8
  156. data/src/core/lib/iomgr/resource_quota.h +2 -1
  157. data/src/core/lib/iomgr/sockaddr_utils.h +1 -1
  158. data/src/core/lib/iomgr/socket_mutator.h +2 -0
  159. data/src/core/lib/iomgr/sys_epoll_wrapper.h +43 -0
  160. data/src/core/lib/iomgr/tcp_client_posix.c +6 -6
  161. data/src/core/lib/iomgr/tcp_client_uv.c +3 -3
  162. data/src/core/lib/iomgr/tcp_posix.c +7 -7
  163. data/src/core/lib/iomgr/tcp_posix.h +2 -1
  164. data/src/core/lib/iomgr/tcp_server_posix.c +1 -1
  165. data/src/core/lib/iomgr/tcp_uv.c +6 -6
  166. data/src/core/lib/iomgr/tcp_uv.h +2 -1
  167. data/src/core/lib/iomgr/tcp_windows.c +1 -1
  168. data/src/core/lib/iomgr/timer_generic.c +24 -25
  169. data/src/core/lib/iomgr/timer_manager.c +276 -0
  170. data/src/core/lib/iomgr/timer_manager.h +52 -0
  171. data/src/core/lib/iomgr/timer_uv.c +6 -0
  172. data/src/core/lib/iomgr/udp_server.c +42 -9
  173. data/src/core/lib/iomgr/udp_server.h +3 -1
  174. data/src/core/lib/security/credentials/credentials.c +0 -1
  175. data/src/core/lib/security/credentials/fake/fake_credentials.c +23 -0
  176. data/src/core/lib/security/credentials/fake/fake_credentials.h +12 -9
  177. data/src/core/lib/security/credentials/google_default/google_default_credentials.c +1 -1
  178. data/src/core/lib/security/credentials/jwt/jwt_credentials.c +1 -1
  179. data/src/core/lib/security/credentials/oauth2/oauth2_credentials.c +1 -1
  180. data/src/core/lib/security/credentials/ssl/ssl_credentials.c +24 -53
  181. data/src/core/lib/security/transport/client_auth_filter.c +9 -3
  182. data/src/core/lib/security/transport/secure_endpoint.c +7 -7
  183. data/src/core/lib/security/transport/secure_endpoint.h +1 -1
  184. data/src/core/lib/security/transport/security_connector.c +45 -57
  185. data/src/core/lib/security/transport/security_connector.h +10 -14
  186. data/src/core/lib/security/transport/security_handshaker.c +123 -97
  187. data/src/core/lib/slice/b64.c +1 -1
  188. data/src/core/lib/slice/percent_encoding.c +3 -3
  189. data/src/core/lib/slice/slice.c +66 -33
  190. data/src/core/lib/slice/slice_buffer.c +25 -6
  191. data/src/core/lib/slice/slice_hash_table.c +33 -35
  192. data/src/core/lib/slice/slice_hash_table.h +7 -12
  193. data/src/core/lib/support/atomic.h +45 -0
  194. data/src/core/lib/support/atomic_with_atm.h +70 -0
  195. data/src/core/lib/support/atomic_with_std.h +48 -0
  196. data/src/core/lib/support/avl.c +14 -14
  197. data/src/core/lib/support/cmdline.c +3 -3
  198. data/src/core/lib/support/histogram.c +2 -2
  199. data/src/core/lib/support/host_port.c +1 -1
  200. data/src/core/lib/support/memory.h +74 -0
  201. data/src/core/lib/support/mpscq.c +36 -2
  202. data/src/core/lib/support/mpscq.h +28 -1
  203. data/src/core/lib/support/stack_lockfree.c +3 -36
  204. data/src/core/lib/support/string.c +12 -12
  205. data/src/core/lib/support/string_posix.c +1 -1
  206. data/src/core/lib/support/subprocess_posix.c +2 -2
  207. data/src/core/lib/support/thd_posix.c +1 -1
  208. data/src/core/lib/support/time_posix.c +8 -0
  209. data/src/core/lib/support/tmpfile_posix.c +10 -10
  210. data/src/core/lib/surface/alarm.c +3 -1
  211. data/src/core/lib/surface/api_trace.c +2 -1
  212. data/src/core/lib/surface/api_trace.h +2 -2
  213. data/src/core/lib/surface/byte_buffer_reader.c +1 -1
  214. data/src/core/lib/surface/call.c +65 -22
  215. data/src/core/lib/surface/call.h +4 -2
  216. data/src/core/lib/surface/channel_init.c +2 -19
  217. data/src/core/lib/surface/channel_stack_type.c +18 -0
  218. data/src/core/lib/surface/channel_stack_type.h +2 -0
  219. data/src/core/lib/surface/completion_queue.c +694 -247
  220. data/src/core/lib/surface/completion_queue.h +30 -13
  221. data/src/core/lib/surface/completion_queue_factory.c +24 -9
  222. data/src/core/lib/surface/init.c +1 -52
  223. data/src/core/lib/surface/{lame_client.c → lame_client.cc} +37 -26
  224. data/src/core/lib/surface/server.c +79 -110
  225. data/src/core/lib/surface/server.h +2 -1
  226. data/src/core/lib/surface/version.c +2 -2
  227. data/src/core/lib/transport/bdp_estimator.c +25 -9
  228. data/src/core/lib/transport/bdp_estimator.h +7 -1
  229. data/src/core/lib/transport/byte_stream.c +23 -9
  230. data/src/core/lib/transport/byte_stream.h +15 -6
  231. data/src/core/lib/transport/connectivity_state.c +6 -6
  232. data/src/core/lib/transport/connectivity_state.h +2 -1
  233. data/src/core/lib/transport/service_config.c +6 -13
  234. data/src/core/lib/transport/service_config.h +2 -2
  235. data/src/core/lib/transport/static_metadata.c +403 -389
  236. data/src/core/lib/transport/static_metadata.h +127 -114
  237. data/src/core/plugin_registry/grpc_plugin_registry.c +16 -0
  238. data/src/core/tsi/fake_transport_security.c +5 -4
  239. data/src/core/tsi/ssl_transport_security.c +71 -82
  240. data/src/core/tsi/ssl_transport_security.h +39 -61
  241. data/src/core/tsi/transport_security.c +83 -2
  242. data/src/core/tsi/transport_security.h +27 -2
  243. data/src/core/tsi/transport_security_adapter.c +236 -0
  244. data/src/core/tsi/transport_security_adapter.h +62 -0
  245. data/src/core/tsi/transport_security_interface.h +179 -66
  246. data/src/ruby/ext/grpc/extconf.rb +2 -1
  247. data/src/ruby/ext/grpc/rb_byte_buffer.c +8 -6
  248. data/src/ruby/ext/grpc/rb_call.c +56 -48
  249. data/src/ruby/ext/grpc/rb_call.h +3 -4
  250. data/src/ruby/ext/grpc/rb_call_credentials.c +23 -22
  251. data/src/ruby/ext/grpc/rb_channel.c +2 -3
  252. data/src/ruby/ext/grpc/rb_channel_args.c +11 -9
  253. data/src/ruby/ext/grpc/rb_channel_credentials.c +16 -12
  254. data/src/ruby/ext/grpc/rb_completion_queue.c +7 -9
  255. data/src/ruby/ext/grpc/rb_compression_options.c +7 -6
  256. data/src/ruby/ext/grpc/rb_event_thread.c +10 -12
  257. data/src/ruby/ext/grpc/rb_event_thread.h +1 -2
  258. data/src/ruby/ext/grpc/rb_grpc.c +11 -15
  259. data/src/ruby/ext/grpc/rb_grpc.h +2 -2
  260. data/src/ruby/ext/grpc/rb_grpc_imports.generated.c +16 -6
  261. data/src/ruby/ext/grpc/rb_grpc_imports.generated.h +25 -10
  262. data/src/ruby/ext/grpc/rb_server.c +26 -28
  263. data/src/ruby/lib/grpc/grpc.rb +1 -1
  264. data/src/ruby/lib/grpc/version.rb +1 -1
  265. data/third_party/cares/config_linux/ares_config.h +36 -2
  266. data/third_party/zlib/adler32.c +14 -7
  267. data/third_party/zlib/compress.c +24 -18
  268. data/third_party/zlib/crc32.c +29 -12
  269. data/third_party/zlib/deflate.c +499 -303
  270. data/third_party/zlib/deflate.h +19 -16
  271. data/third_party/zlib/gzguts.h +16 -7
  272. data/third_party/zlib/gzlib.c +17 -14
  273. data/third_party/zlib/gzread.c +108 -48
  274. data/third_party/zlib/gzwrite.c +210 -122
  275. data/third_party/zlib/infback.c +2 -2
  276. data/third_party/zlib/inffast.c +34 -51
  277. data/third_party/zlib/inflate.c +86 -37
  278. data/third_party/zlib/inflate.h +7 -4
  279. data/third_party/zlib/inftrees.c +12 -14
  280. data/third_party/zlib/trees.c +38 -61
  281. data/third_party/zlib/uncompr.c +66 -32
  282. data/third_party/zlib/zconf.h +32 -9
  283. data/third_party/zlib/zlib.h +298 -154
  284. data/third_party/zlib/zutil.c +25 -24
  285. data/third_party/zlib/zutil.h +35 -17
  286. metadata +63 -30
@@ -0,0 +1,43 @@
1
+ /*
2
+ *
3
+ * Copyright 2015, Google Inc.
4
+ * All rights reserved.
5
+ *
6
+ * Redistribution and use in source and binary forms, with or without
7
+ * modification, are permitted provided that the following conditions are
8
+ * met:
9
+ *
10
+ * * Redistributions of source code must retain the above copyright
11
+ * notice, this list of conditions and the following disclaimer.
12
+ * * Redistributions in binary form must reproduce the above
13
+ * copyright notice, this list of conditions and the following disclaimer
14
+ * in the documentation and/or other materials provided with the
15
+ * distribution.
16
+ * * Neither the name of Google Inc. nor the names of its
17
+ * contributors may be used to endorse or promote products derived from
18
+ * this software without specific prior written permission.
19
+ *
20
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
+ *
32
+ */
33
+
34
+ #ifndef GRPC_CORE_LIB_IOMGR_EV_EPOLL_LIMITED_POLLERS_LINUX_H
35
+ #define GRPC_CORE_LIB_IOMGR_EV_EPOLL_LIMITED_POLLERS_LINUX_H
36
+
37
+ #include "src/core/lib/iomgr/ev_posix.h"
38
+ #include "src/core/lib/iomgr/port.h"
39
+
40
+ const grpc_event_engine_vtable *grpc_init_epoll_limited_pollers_linux(
41
+ bool explicitly_requested);
42
+
43
+ #endif /* GRPC_CORE_LIB_IOMGR_EV_EPOLL_LIMITED_POLLERS_LINUX_H */
@@ -0,0 +1,1337 @@
1
+ /*
2
+ *
3
+ * Copyright 2017, Google Inc.
4
+ * All rights reserved.
5
+ *
6
+ * Redistribution and use in source and binary forms, with or without
7
+ * modification, are permitted provided that the following conditions are
8
+ * met:
9
+ *
10
+ * * Redistributions of source code must retain the above copyright
11
+ * notice, this list of conditions and the following disclaimer.
12
+ * * Redistributions in binary form must reproduce the above
13
+ * copyright notice, this list of conditions and the following disclaimer
14
+ * in the documentation and/or other materials provided with the
15
+ * distribution.
16
+ * * Neither the name of Google Inc. nor the names of its
17
+ * contributors may be used to endorse or promote products derived from
18
+ * this software without specific prior written permission.
19
+ *
20
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
+ *
32
+ */
33
+
34
+ #include "src/core/lib/iomgr/port.h"
35
+
36
+ /* This polling engine is only relevant on linux kernels supporting epoll() */
37
+ #ifdef GRPC_LINUX_EPOLL
38
+
39
+ #include "src/core/lib/iomgr/ev_epoll_thread_pool_linux.h"
40
+
41
+ #include <assert.h>
42
+ #include <errno.h>
43
+ #include <limits.h>
44
+ #include <poll.h>
45
+ #include <pthread.h>
46
+ #include <string.h>
47
+ #include <sys/epoll.h>
48
+ #include <sys/socket.h>
49
+ #include <unistd.h>
50
+
51
+ #include <grpc/support/alloc.h>
52
+ #include <grpc/support/cpu.h>
53
+ #include <grpc/support/log.h>
54
+ #include <grpc/support/string_util.h>
55
+ #include <grpc/support/thd.h>
56
+ #include <grpc/support/tls.h>
57
+ #include <grpc/support/useful.h>
58
+
59
+ #include "src/core/lib/iomgr/ev_posix.h"
60
+ #include "src/core/lib/iomgr/iomgr_internal.h"
61
+ #include "src/core/lib/iomgr/lockfree_event.h"
62
+ #include "src/core/lib/iomgr/timer.h"
63
+ #include "src/core/lib/iomgr/wakeup_fd_posix.h"
64
+ #include "src/core/lib/iomgr/workqueue.h"
65
+ #include "src/core/lib/profiling/timers.h"
66
+ #include "src/core/lib/support/block_annotate.h"
67
+
68
+ /* TODO: sreek - Move this to init.c and initialize this like other tracers. */
69
+ #define GRPC_POLLING_TRACE(fmt, ...) \
70
+ if (GRPC_TRACER_ON(grpc_polling_trace)) { \
71
+ gpr_log(GPR_INFO, (fmt), __VA_ARGS__); \
72
+ }
73
+
74
+ /* The alarm system needs to be able to wakeup 'some poller' sometimes
75
+ * (specifically when a new alarm needs to be triggered earlier than the next
76
+ * alarm 'epoch'). This wakeup_fd gives us something to alert on when such a
77
+ * case occurs. */
78
+
79
+ struct epoll_set;
80
+
81
+ #define GRPC_POLLSET_KICK_BROADCAST ((grpc_pollset_worker *)1)
82
+
83
+ /*******************************************************************************
84
+ * Fd Declarations
85
+ */
86
+ struct grpc_fd {
87
+ gpr_mu mu;
88
+ struct epoll_set *eps;
89
+
90
+ int fd;
91
+
92
+ /* The fd is either closed or we relinquished control of it. In either cases,
93
+ this indicates that the 'fd' on this structure is no longer valid */
94
+ bool orphaned;
95
+
96
+ gpr_atm read_closure;
97
+ gpr_atm write_closure;
98
+
99
+ struct grpc_fd *freelist_next;
100
+ grpc_closure *on_done_closure;
101
+
102
+ grpc_iomgr_object iomgr_object;
103
+ };
104
+
105
+ static void fd_global_init(void);
106
+ static void fd_global_shutdown(void);
107
+
108
+ /*******************************************************************************
109
+ * epoll set Declarations
110
+ */
111
+
112
+ #ifdef GRPC_WORKQUEUE_REFCOUNT_DEBUG
113
+
114
+ #define EPS_ADD_REF(p, r) eps_add_ref_dbg((p), (r), __FILE__, __LINE__)
115
+ #define EPS_UNREF(exec_ctx, p, r) \
116
+ eps_unref_dbg((exec_ctx), (p), (r), __FILE__, __LINE__)
117
+
118
+ #else /* defined(GRPC_WORKQUEUE_REFCOUNT_DEBUG) */
119
+
120
+ #define EPS_ADD_REF(p, r) eps_add_ref((p))
121
+ #define EPS_UNREF(exec_ctx, p, r) eps_unref((exec_ctx), (p))
122
+
123
+ #endif /* !defined(GRPC_EPS_REF_COUNT_DEBUG) */
124
+
125
+ /* This is also used as grpc_workqueue (by directly casting it) */
126
+ typedef struct epoll_set {
127
+ grpc_closure_scheduler workqueue_scheduler;
128
+
129
+ /* Mutex poller should acquire to poll this. This enforces that only one
130
+ * poller can be polling on epoll_set at any time */
131
+ gpr_mu mu;
132
+
133
+ /* Ref count. Use EPS_ADD_REF() and EPS_UNREF() macros to increment/decrement
134
+ the refcount. Once the ref count becomes zero, this structure is destroyed
135
+ which means we should ensure that there is never a scenario where a
136
+ EPS_ADD_REF() is racing with a EPS_UNREF() that just made the ref_count
137
+ zero. */
138
+ gpr_atm ref_count;
139
+
140
+ /* Number of threads currently polling on this epoll set*/
141
+ gpr_atm poller_count;
142
+ /* Mutex guarding the read end of the workqueue (must be held to pop from
143
+ * workqueue_items) */
144
+ gpr_mu workqueue_read_mu;
145
+ /* Queue of closures to be executed */
146
+ gpr_mpscq workqueue_items;
147
+ /* Count of items in workqueue_items */
148
+ gpr_atm workqueue_item_count;
149
+ /* Wakeup fd used to wake pollers to check the contents of workqueue_items */
150
+ grpc_wakeup_fd workqueue_wakeup_fd;
151
+
152
+ /* Is the epoll set shutdown */
153
+ gpr_atm is_shutdown;
154
+
155
+ /* The fd of the underlying epoll set */
156
+ int epoll_fd;
157
+ } epoll_set;
158
+
159
+ /*******************************************************************************
160
+ * Pollset Declarations
161
+ */
162
+ struct grpc_pollset_worker {
163
+ gpr_cv kick_cv;
164
+
165
+ struct grpc_pollset_worker *next;
166
+ struct grpc_pollset_worker *prev;
167
+ };
168
+
169
+ struct grpc_pollset {
170
+ gpr_mu mu;
171
+ struct epoll_set *eps;
172
+
173
+ grpc_pollset_worker root_worker;
174
+ bool kicked_without_pollers;
175
+
176
+ bool shutting_down; /* Is the pollset shutting down ? */
177
+ bool finish_shutdown_called; /* Is the 'finish_shutdown_locked()' called ? */
178
+ grpc_closure *shutdown_done; /* Called after after shutdown is complete */
179
+ };
180
+
181
+ /*******************************************************************************
182
+ * Pollset-set Declarations
183
+ */
184
+ struct grpc_pollset_set {};
185
+
186
+ /*****************************************************************************
187
+ * Dedicated polling threads and pollsets - Declarations
188
+ */
189
+
190
+ size_t g_num_eps = 1;
191
+ struct epoll_set **g_epoll_sets = NULL;
192
+ gpr_atm g_next_eps;
193
+ size_t g_num_threads_per_eps = 1;
194
+ gpr_thd_id *g_poller_threads = NULL;
195
+
196
+ /* Used as read-notifier pollsets for fds. We won't be using read notifier
197
+ * pollsets with this polling engine. So it does not matter what pollset we
198
+ * return */
199
+ grpc_pollset g_read_notifier;
200
+
201
+ static void add_fd_to_eps(grpc_fd *fd);
202
+ static bool init_epoll_sets();
203
+ static void shutdown_epoll_sets();
204
+ static void poller_thread_loop(void *arg);
205
+ static void start_poller_threads();
206
+ static void shutdown_poller_threads();
207
+
208
+ /*******************************************************************************
209
+ * Common helpers
210
+ */
211
+
212
+ static bool append_error(grpc_error **composite, grpc_error *error,
213
+ const char *desc) {
214
+ if (error == GRPC_ERROR_NONE) return true;
215
+ if (*composite == GRPC_ERROR_NONE) {
216
+ *composite = GRPC_ERROR_CREATE_FROM_COPIED_STRING(desc);
217
+ }
218
+ *composite = grpc_error_add_child(*composite, error);
219
+ return false;
220
+ }
221
+
222
+ /*******************************************************************************
223
+ * epoll set Definitions
224
+ */
225
+
226
+ /* The wakeup fd that is used to wake up all threads in an epoll_set informing
227
+ that the epoll set is shutdown. This wakeup fd initialized to be readable
228
+ and MUST NOT be consumed i.e the threads that woke up MUST NOT call
229
+ grpc_wakeup_fd_consume_wakeup() */
230
+ static grpc_wakeup_fd epoll_set_wakeup_fd;
231
+
232
+ /* The epoll set being polled right now.
233
+ See comments in workqueue_maybe_wakeup for why this is tracked. */
234
+ static __thread epoll_set *g_current_thread_epoll_set;
235
+
236
+ /* Forward declaration */
237
+ static void epoll_set_delete(epoll_set *eps);
238
+ static void workqueue_enqueue(grpc_exec_ctx *exec_ctx, grpc_closure *closure,
239
+ grpc_error *error);
240
+
241
+ #ifdef GRPC_TSAN
242
+ /* Currently TSAN may incorrectly flag data races between epoll_ctl and
243
+ epoll_wait for any grpc_fd structs that are added to the epoll set via
244
+ epoll_ctl and are returned (within a very short window) via epoll_wait().
245
+
246
+ To work-around this race, we establish a happens-before relation between
247
+ the code just-before epoll_ctl() and the code after epoll_wait() by using
248
+ this atomic */
249
+ gpr_atm g_epoll_sync;
250
+ #endif /* defined(GRPC_TSAN) */
251
+
252
+ static const grpc_closure_scheduler_vtable workqueue_scheduler_vtable = {
253
+ workqueue_enqueue, workqueue_enqueue, "workqueue"};
254
+
255
+ static void eps_add_ref(epoll_set *eps);
256
+ static void eps_unref(grpc_exec_ctx *exec_ctx, epoll_set *eps);
257
+
258
+ #ifdef GRPC_WORKQUEUE_REFCOUNT_DEBUG
259
+ static void eps_add_ref_dbg(epoll_set *eps, const char *reason,
260
+ const char *file, int line) {
261
+ long old_cnt = gpr_atm_acq_load(&eps->ref_count);
262
+ eps_add_ref(eps);
263
+ gpr_log(GPR_DEBUG, "Add ref eps: %p, old: %ld -> new:%ld (%s) - (%s, %d)",
264
+ (void *)eps, old_cnt, old_cnt + 1, reason, file, line);
265
+ }
266
+
267
+ static void eps_unref_dbg(grpc_exec_ctx *exec_ctx, epoll_set *eps,
268
+ const char *reason, const char *file, int line) {
269
+ long old_cnt = gpr_atm_acq_load(&eps->ref_count);
270
+ eps_unref(exec_ctx, eps);
271
+ gpr_log(GPR_DEBUG, "Unref eps: %p, old:%ld -> new:%ld (%s) - (%s, %d)",
272
+ (void *)eps, old_cnt, (old_cnt - 1), reason, file, line);
273
+ }
274
+
275
+ static grpc_workqueue *workqueue_ref(grpc_workqueue *workqueue,
276
+ const char *file, int line,
277
+ const char *reason) {
278
+ if (workqueue != NULL) {
279
+ eps_add_ref_dbg((epoll_set *)workqueue, reason, file, line);
280
+ }
281
+ return workqueue;
282
+ }
283
+
284
+ static void workqueue_unref(grpc_exec_ctx *exec_ctx, grpc_workqueue *workqueue,
285
+ const char *file, int line, const char *reason) {
286
+ if (workqueue != NULL) {
287
+ eps_unref_dbg(exec_ctx, (epoll_set *)workqueue, reason, file, line);
288
+ }
289
+ }
290
+ #else
291
+ static grpc_workqueue *workqueue_ref(grpc_workqueue *workqueue) {
292
+ if (workqueue != NULL) {
293
+ eps_add_ref((epoll_set *)workqueue);
294
+ }
295
+ return workqueue;
296
+ }
297
+
298
+ static void workqueue_unref(grpc_exec_ctx *exec_ctx,
299
+ grpc_workqueue *workqueue) {
300
+ if (workqueue != NULL) {
301
+ eps_unref(exec_ctx, (epoll_set *)workqueue);
302
+ }
303
+ }
304
+ #endif
305
+
306
+ static void eps_add_ref(epoll_set *eps) {
307
+ gpr_atm_no_barrier_fetch_add(&eps->ref_count, 1);
308
+ }
309
+
310
+ static void eps_unref(grpc_exec_ctx *exec_ctx, epoll_set *eps) {
311
+ /* If ref count went to zero, delete the epoll set. This deletion is
312
+ not done under a lock since once the ref count goes to zero, we are
313
+ guaranteed that no one else holds a reference to the epoll set (and
314
+ that there is no racing eps_add_ref() call either).*/
315
+ if (1 == gpr_atm_full_fetch_add(&eps->ref_count, -1)) {
316
+ epoll_set_delete(eps);
317
+ }
318
+ }
319
+
320
+ static void epoll_set_add_fd_locked(epoll_set *eps, grpc_fd *fd,
321
+ grpc_error **error) {
322
+ int err;
323
+ struct epoll_event ev;
324
+ char *err_msg;
325
+ const char *err_desc = "epoll_set_add_fd_locked";
326
+
327
+ #ifdef GRPC_TSAN
328
+ /* See the definition of g_epoll_sync for more context */
329
+ gpr_atm_rel_store(&g_epoll_sync, (gpr_atm)0);
330
+ #endif /* defined(GRPC_TSAN) */
331
+
332
+ ev.events = (uint32_t)(EPOLLIN | EPOLLOUT | EPOLLET);
333
+ ev.data.ptr = fd;
334
+ err = epoll_ctl(eps->epoll_fd, EPOLL_CTL_ADD, fd->fd, &ev);
335
+ if (err < 0 && errno != EEXIST) {
336
+ gpr_asprintf(
337
+ &err_msg,
338
+ "epoll_ctl (epoll_fd: %d) add fd: %d failed with error: %d (%s)",
339
+ eps->epoll_fd, fd->fd, errno, strerror(errno));
340
+ append_error(error, GRPC_OS_ERROR(errno, err_msg), err_desc);
341
+ gpr_free(err_msg);
342
+ }
343
+ }
344
+
345
+ static void epoll_set_add_wakeup_fd_locked(epoll_set *eps,
346
+ grpc_wakeup_fd *wakeup_fd,
347
+ grpc_error **error) {
348
+ struct epoll_event ev;
349
+ int err;
350
+ char *err_msg;
351
+ const char *err_desc = "epoll_set_add_wakeup_fd";
352
+
353
+ ev.events = (uint32_t)(EPOLLIN | EPOLLET);
354
+ ev.data.ptr = wakeup_fd;
355
+ err = epoll_ctl(eps->epoll_fd, EPOLL_CTL_ADD,
356
+ GRPC_WAKEUP_FD_GET_READ_FD(wakeup_fd), &ev);
357
+ if (err < 0 && errno != EEXIST) {
358
+ gpr_asprintf(&err_msg,
359
+ "epoll_ctl (epoll_fd: %d) add wakeup fd: %d failed with "
360
+ "error: %d (%s)",
361
+ eps->epoll_fd, GRPC_WAKEUP_FD_GET_READ_FD(wakeup_fd), errno,
362
+ strerror(errno));
363
+ append_error(error, GRPC_OS_ERROR(errno, err_msg), err_desc);
364
+ gpr_free(err_msg);
365
+ }
366
+ }
367
+
368
+ static void epoll_set_remove_fd(epoll_set *eps, grpc_fd *fd, bool is_fd_closed,
369
+ grpc_error **error) {
370
+ int err;
371
+ char *err_msg;
372
+ const char *err_desc = "epoll_set_remove_fd";
373
+
374
+ /* If fd is already closed, then it would have been automatically been removed
375
+ from the epoll set */
376
+ if (!is_fd_closed) {
377
+ err = epoll_ctl(eps->epoll_fd, EPOLL_CTL_DEL, fd->fd, NULL);
378
+ if (err < 0 && errno != ENOENT) {
379
+ gpr_asprintf(
380
+ &err_msg,
381
+ "epoll_ctl (epoll_fd: %d) del fd: %d failed with error: %d (%s)",
382
+ eps->epoll_fd, fd->fd, errno, strerror(errno));
383
+ append_error(error, GRPC_OS_ERROR(errno, err_msg), err_desc);
384
+ gpr_free(err_msg);
385
+ }
386
+ }
387
+ }
388
+
389
+ /* Might return NULL in case of an error */
390
+ static epoll_set *epoll_set_create(grpc_error **error) {
391
+ epoll_set *eps = NULL;
392
+ const char *err_desc = "epoll_set_create";
393
+
394
+ *error = GRPC_ERROR_NONE;
395
+
396
+ eps = gpr_malloc(sizeof(*eps));
397
+ eps->workqueue_scheduler.vtable = &workqueue_scheduler_vtable;
398
+ eps->epoll_fd = -1;
399
+
400
+ gpr_mu_init(&eps->mu);
401
+ gpr_mu_init(&eps->workqueue_read_mu);
402
+ gpr_mpscq_init(&eps->workqueue_items);
403
+ gpr_atm_rel_store(&eps->workqueue_item_count, 0);
404
+
405
+ gpr_atm_rel_store(&eps->ref_count, 0);
406
+ gpr_atm_rel_store(&eps->poller_count, 0);
407
+
408
+ gpr_atm_rel_store(&eps->is_shutdown, false);
409
+
410
+ if (!append_error(error, grpc_wakeup_fd_init(&eps->workqueue_wakeup_fd),
411
+ err_desc)) {
412
+ goto done;
413
+ }
414
+
415
+ eps->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
416
+
417
+ if (eps->epoll_fd < 0) {
418
+ append_error(error, GRPC_OS_ERROR(errno, "epoll_create1"), err_desc);
419
+ goto done;
420
+ }
421
+
422
+ epoll_set_add_wakeup_fd_locked(eps, &eps->workqueue_wakeup_fd, error);
423
+
424
+ done:
425
+ if (*error != GRPC_ERROR_NONE) {
426
+ epoll_set_delete(eps);
427
+ eps = NULL;
428
+ }
429
+ return eps;
430
+ }
431
+
432
+ static void epoll_set_delete(epoll_set *eps) {
433
+ if (eps->epoll_fd >= 0) {
434
+ close(eps->epoll_fd);
435
+ }
436
+
437
+ GPR_ASSERT(gpr_atm_no_barrier_load(&eps->workqueue_item_count) == 0);
438
+ gpr_mu_destroy(&eps->mu);
439
+ gpr_mu_destroy(&eps->workqueue_read_mu);
440
+ gpr_mpscq_destroy(&eps->workqueue_items);
441
+ grpc_wakeup_fd_destroy(&eps->workqueue_wakeup_fd);
442
+
443
+ gpr_free(eps);
444
+ }
445
+
446
+ static void workqueue_maybe_wakeup(epoll_set *eps) {
447
+ /* If this thread is the current poller, then it may be that it's about to
448
+ decrement the current poller count, so we need to look past this thread */
449
+ bool is_current_poller = (g_current_thread_epoll_set == eps);
450
+ gpr_atm min_current_pollers_for_wakeup = is_current_poller ? 1 : 0;
451
+ gpr_atm current_pollers = gpr_atm_no_barrier_load(&eps->poller_count);
452
+ /* Only issue a wakeup if it's likely that some poller could come in and take
453
+ it right now. Note that since we do an anticipatory mpscq_pop every poll
454
+ loop, it's ok if we miss the wakeup here, as we'll get the work item when
455
+ the next poller enters anyway. */
456
+ if (current_pollers > min_current_pollers_for_wakeup) {
457
+ GRPC_LOG_IF_ERROR("workqueue_wakeup_fd",
458
+ grpc_wakeup_fd_wakeup(&eps->workqueue_wakeup_fd));
459
+ }
460
+ }
461
+
462
+ static void workqueue_enqueue(grpc_exec_ctx *exec_ctx, grpc_closure *closure,
463
+ grpc_error *error) {
464
+ GPR_TIMER_BEGIN("workqueue.enqueue", 0);
465
+ grpc_workqueue *workqueue = (grpc_workqueue *)closure->scheduler;
466
+ /* take a ref to the workqueue: otherwise it can happen that whatever events
467
+ * this kicks off ends up destroying the workqueue before this function
468
+ * completes */
469
+ GRPC_WORKQUEUE_REF(workqueue, "enqueue");
470
+ epoll_set *eps = (epoll_set *)workqueue;
471
+ gpr_atm last = gpr_atm_no_barrier_fetch_add(&eps->workqueue_item_count, 1);
472
+ closure->error_data.error = error;
473
+ gpr_mpscq_push(&eps->workqueue_items, &closure->next_data.atm_next);
474
+ if (last == 0) {
475
+ workqueue_maybe_wakeup(eps);
476
+ }
477
+
478
+ GRPC_WORKQUEUE_UNREF(exec_ctx, workqueue, "enqueue");
479
+ GPR_TIMER_END("workqueue.enqueue", 0);
480
+ }
481
+
482
+ static grpc_closure_scheduler *workqueue_scheduler(grpc_workqueue *workqueue) {
483
+ epoll_set *eps = (epoll_set *)workqueue;
484
+ return workqueue == NULL ? grpc_schedule_on_exec_ctx
485
+ : &eps->workqueue_scheduler;
486
+ }
487
+
488
+ static grpc_error *epoll_set_global_init() {
489
+ grpc_error *error = GRPC_ERROR_NONE;
490
+
491
+ error = grpc_wakeup_fd_init(&epoll_set_wakeup_fd);
492
+ if (error == GRPC_ERROR_NONE) {
493
+ error = grpc_wakeup_fd_wakeup(&epoll_set_wakeup_fd);
494
+ }
495
+
496
+ return error;
497
+ }
498
+
499
+ static void epoll_set_global_shutdown() {
500
+ grpc_wakeup_fd_destroy(&epoll_set_wakeup_fd);
501
+ }
502
+
503
+ /*******************************************************************************
504
+ * Fd Definitions
505
+ */
506
+
507
+ /* We need to keep a freelist not because of any concerns of malloc performance
508
+ * but instead so that implementations with multiple threads in (for example)
509
+ * epoll_wait deal with the race between pollset removal and incoming poll
510
+ * notifications.
511
+ *
512
+ * The problem is that the poller ultimately holds a reference to this
513
+ * object, so it is very difficult to know when is safe to free it, at least
514
+ * without some expensive synchronization.
515
+ *
516
+ * If we keep the object freelisted, in the worst case losing this race just
517
+ * becomes a spurious read notification on a reused fd.
518
+ */
519
+
520
+ static grpc_fd *fd_freelist = NULL;
521
+ static gpr_mu fd_freelist_mu;
522
+
523
+ static grpc_fd *get_fd_from_freelist() {
524
+ grpc_fd *new_fd = NULL;
525
+
526
+ gpr_mu_lock(&fd_freelist_mu);
527
+ if (fd_freelist != NULL) {
528
+ new_fd = fd_freelist;
529
+ fd_freelist = fd_freelist->freelist_next;
530
+ }
531
+ gpr_mu_unlock(&fd_freelist_mu);
532
+ return new_fd;
533
+ }
534
+
535
+ static void add_fd_to_freelist(grpc_fd *fd) {
536
+ gpr_mu_lock(&fd_freelist_mu);
537
+ fd->freelist_next = fd_freelist;
538
+ fd_freelist = fd;
539
+ grpc_iomgr_unregister_object(&fd->iomgr_object);
540
+
541
+ grpc_lfev_destroy(&fd->read_closure);
542
+ grpc_lfev_destroy(&fd->write_closure);
543
+
544
+ gpr_mu_unlock(&fd_freelist_mu);
545
+ }
546
+
547
+ static void fd_global_init(void) { gpr_mu_init(&fd_freelist_mu); }
548
+
549
+ static void fd_global_shutdown(void) {
550
+ gpr_mu_lock(&fd_freelist_mu);
551
+ gpr_mu_unlock(&fd_freelist_mu);
552
+ while (fd_freelist != NULL) {
553
+ grpc_fd *fd = fd_freelist;
554
+ fd_freelist = fd_freelist->freelist_next;
555
+ gpr_mu_destroy(&fd->mu);
556
+ gpr_free(fd);
557
+ }
558
+ gpr_mu_destroy(&fd_freelist_mu);
559
+ }
560
+
561
+ static grpc_fd *fd_create(int fd, const char *name) {
562
+ grpc_fd *new_fd = get_fd_from_freelist();
563
+ if (new_fd == NULL) {
564
+ new_fd = gpr_malloc(sizeof(grpc_fd));
565
+ gpr_mu_init(&new_fd->mu);
566
+ }
567
+
568
+ /* Note: It is not really needed to get the new_fd->mu lock here. If this
569
+ * is a newly created fd (or an fd we got from the freelist), no one else
570
+ * would be holding a lock to it anyway. */
571
+ gpr_mu_lock(&new_fd->mu);
572
+ new_fd->eps = NULL;
573
+
574
+ new_fd->fd = fd;
575
+ new_fd->orphaned = false;
576
+ grpc_lfev_init(&new_fd->read_closure);
577
+ grpc_lfev_init(&new_fd->write_closure);
578
+
579
+ new_fd->freelist_next = NULL;
580
+ new_fd->on_done_closure = NULL;
581
+
582
+ gpr_mu_unlock(&new_fd->mu);
583
+
584
+ char *fd_name;
585
+ gpr_asprintf(&fd_name, "%s fd=%d", name, fd);
586
+ grpc_iomgr_register_object(&new_fd->iomgr_object, fd_name);
587
+ gpr_log(GPR_DEBUG, "FD %d %p create %s", fd, (void *)new_fd, fd_name);
588
+ gpr_free(fd_name);
589
+
590
+ /* Associate the fd with one of the eps */
591
+ add_fd_to_eps(new_fd);
592
+ return new_fd;
593
+ }
594
+
595
+ static int fd_wrapped_fd(grpc_fd *fd) {
596
+ int ret_fd = -1;
597
+ gpr_mu_lock(&fd->mu);
598
+ if (!fd->orphaned) {
599
+ ret_fd = fd->fd;
600
+ }
601
+ gpr_mu_unlock(&fd->mu);
602
+
603
+ return ret_fd;
604
+ }
605
+
606
+ static void fd_orphan(grpc_exec_ctx *exec_ctx, grpc_fd *fd,
607
+ grpc_closure *on_done, int *release_fd,
608
+ const char *reason) {
609
+ bool is_fd_closed = false;
610
+ grpc_error *error = GRPC_ERROR_NONE;
611
+ epoll_set *unref_eps = NULL;
612
+
613
+ gpr_mu_lock(&fd->mu);
614
+ fd->on_done_closure = on_done;
615
+
616
+ /* If release_fd is not NULL, we should be relinquishing control of the file
617
+ descriptor fd->fd (but we still own the grpc_fd structure). */
618
+ if (release_fd != NULL) {
619
+ *release_fd = fd->fd;
620
+ } else {
621
+ close(fd->fd);
622
+ is_fd_closed = true;
623
+ }
624
+
625
+ fd->orphaned = true;
626
+
627
+ /* Remove the fd from the epoll set */
628
+ if (fd->eps != NULL) {
629
+ epoll_set_remove_fd(fd->eps, fd, is_fd_closed, &error);
630
+ unref_eps = fd->eps;
631
+ fd->eps = NULL;
632
+ }
633
+
634
+ grpc_closure_sched(exec_ctx, fd->on_done_closure, GRPC_ERROR_REF(error));
635
+
636
+ gpr_mu_unlock(&fd->mu);
637
+
638
+ /* We are done with this fd. Release it (i.e add back to freelist) */
639
+ add_fd_to_freelist(fd);
640
+
641
+ if (unref_eps != NULL) {
642
+ /* Unref stale epoll set here, outside the fd lock above.
643
+ The epoll set owns a workqueue which owns an fd, and unreffing
644
+ inside the lock can cause an eventual lock loop that makes TSAN very
645
+ unhappy. */
646
+ EPS_UNREF(exec_ctx, unref_eps, "fd_orphan");
647
+ }
648
+ GRPC_LOG_IF_ERROR("fd_orphan", GRPC_ERROR_REF(error));
649
+ GRPC_ERROR_UNREF(error);
650
+ }
651
+
652
+ /* This polling engine doesn't really need the read notifier functionality. So
653
+ * it just returns a dummy read notifier pollset */
654
+ static grpc_pollset *fd_get_read_notifier_pollset(grpc_exec_ctx *exec_ctx,
655
+ grpc_fd *fd) {
656
+ return &g_read_notifier;
657
+ }
658
+
659
+ static bool fd_is_shutdown(grpc_fd *fd) {
660
+ return grpc_lfev_is_shutdown(&fd->read_closure);
661
+ }
662
+
663
+ /* Might be called multiple times */
664
+ static void fd_shutdown(grpc_exec_ctx *exec_ctx, grpc_fd *fd, grpc_error *why) {
665
+ if (grpc_lfev_set_shutdown(exec_ctx, &fd->read_closure,
666
+ GRPC_ERROR_REF(why))) {
667
+ shutdown(fd->fd, SHUT_RDWR);
668
+ grpc_lfev_set_shutdown(exec_ctx, &fd->write_closure, GRPC_ERROR_REF(why));
669
+ }
670
+ GRPC_ERROR_UNREF(why);
671
+ }
672
+
673
+ static void fd_notify_on_read(grpc_exec_ctx *exec_ctx, grpc_fd *fd,
674
+ grpc_closure *closure) {
675
+ grpc_lfev_notify_on(exec_ctx, &fd->read_closure, closure);
676
+ }
677
+
678
+ static void fd_notify_on_write(grpc_exec_ctx *exec_ctx, grpc_fd *fd,
679
+ grpc_closure *closure) {
680
+ grpc_lfev_notify_on(exec_ctx, &fd->write_closure, closure);
681
+ }
682
+
683
+ static grpc_workqueue *fd_get_workqueue(grpc_fd *fd) { return NULL; }
684
+
685
+ /*******************************************************************************
686
+ * Pollset Definitions
687
+ */
688
+ /* TODO: sreek - Not needed anymore */
689
+ GPR_TLS_DECL(g_current_thread_pollset);
690
+ GPR_TLS_DECL(g_current_thread_worker);
691
+
692
+ static void pollset_worker_init(grpc_pollset_worker *worker) {
693
+ worker->next = worker->prev = NULL;
694
+ gpr_cv_init(&worker->kick_cv);
695
+ }
696
+
697
+ /* Global state management */
698
+ static grpc_error *pollset_global_init(void) {
699
+ gpr_tls_init(&g_current_thread_pollset);
700
+ gpr_tls_init(&g_current_thread_worker);
701
+ return GRPC_ERROR_NONE;
702
+ }
703
+
704
+ static void pollset_global_shutdown(void) {
705
+ gpr_tls_destroy(&g_current_thread_pollset);
706
+ gpr_tls_destroy(&g_current_thread_worker);
707
+ }
708
+
709
+ static grpc_error *pollset_worker_kick(grpc_pollset_worker *worker) {
710
+ gpr_cv_signal(&worker->kick_cv);
711
+ return GRPC_ERROR_NONE;
712
+ }
713
+
714
+ /* Return 1 if the pollset has active threads in pollset_work (pollset must
715
+ * be locked) */
716
+ static int pollset_has_workers(grpc_pollset *p) {
717
+ return p->root_worker.next != &p->root_worker;
718
+ }
719
+
720
+ static void remove_worker(grpc_pollset *p, grpc_pollset_worker *worker) {
721
+ worker->prev->next = worker->next;
722
+ worker->next->prev = worker->prev;
723
+ }
724
+
725
+ static grpc_pollset_worker *pop_front_worker(grpc_pollset *p) {
726
+ if (pollset_has_workers(p)) {
727
+ grpc_pollset_worker *w = p->root_worker.next;
728
+ remove_worker(p, w);
729
+ return w;
730
+ } else {
731
+ return NULL;
732
+ }
733
+ }
734
+
735
+ static void push_back_worker(grpc_pollset *p, grpc_pollset_worker *worker) {
736
+ worker->next = &p->root_worker;
737
+ worker->prev = worker->next->prev;
738
+ worker->prev->next = worker->next->prev = worker;
739
+ }
740
+
741
+ static void push_front_worker(grpc_pollset *p, grpc_pollset_worker *worker) {
742
+ worker->prev = &p->root_worker;
743
+ worker->next = worker->prev->next;
744
+ worker->prev->next = worker->next->prev = worker;
745
+ }
746
+
747
+ /* p->mu must be held before calling this function */
748
+ static grpc_error *pollset_kick(grpc_pollset *p,
749
+ grpc_pollset_worker *specific_worker) {
750
+ GPR_TIMER_BEGIN("pollset_kick", 0);
751
+ grpc_error *error = GRPC_ERROR_NONE;
752
+ const char *err_desc = "Kick Failure";
753
+ grpc_pollset_worker *worker = specific_worker;
754
+ if (worker != NULL) {
755
+ if (worker == GRPC_POLLSET_KICK_BROADCAST) {
756
+ if (pollset_has_workers(p)) {
757
+ GPR_TIMER_BEGIN("pollset_kick.broadcast", 0);
758
+ for (worker = p->root_worker.next; worker != &p->root_worker;
759
+ worker = worker->next) {
760
+ if (gpr_tls_get(&g_current_thread_worker) != (intptr_t)worker) {
761
+ append_error(&error, pollset_worker_kick(worker), err_desc);
762
+ }
763
+ }
764
+ GPR_TIMER_END("pollset_kick.broadcast", 0);
765
+ } else {
766
+ p->kicked_without_pollers = true;
767
+ }
768
+ } else {
769
+ GPR_TIMER_MARK("kicked_specifically", 0);
770
+ if (gpr_tls_get(&g_current_thread_worker) != (intptr_t)worker) {
771
+ append_error(&error, pollset_worker_kick(worker), err_desc);
772
+ }
773
+ }
774
+ } else if (gpr_tls_get(&g_current_thread_pollset) != (intptr_t)p) {
775
+ /* Since worker == NULL, it means that we can kick "any" worker on this
776
+ pollset 'p'. If 'p' happens to be the same pollset this thread is
777
+ currently polling (i.e in pollset_work() function), then there is no need
778
+ to kick any other worker since the current thread can just absorb the
779
+ kick. This is the reason why we enter this case only when
780
+ g_current_thread_pollset is != p */
781
+
782
+ GPR_TIMER_MARK("kick_anonymous", 0);
783
+ worker = pop_front_worker(p);
784
+ if (worker != NULL) {
785
+ GPR_TIMER_MARK("finally_kick", 0);
786
+ push_back_worker(p, worker);
787
+ append_error(&error, pollset_worker_kick(worker), err_desc);
788
+ } else {
789
+ GPR_TIMER_MARK("kicked_no_pollers", 0);
790
+ p->kicked_without_pollers = true;
791
+ }
792
+ }
793
+
794
+ GPR_TIMER_END("pollset_kick", 0);
795
+ GRPC_LOG_IF_ERROR("pollset_kick", GRPC_ERROR_REF(error));
796
+ return error;
797
+ }
798
+
799
+ static void pollset_init(grpc_pollset *pollset, gpr_mu **mu) {
800
+ gpr_mu_init(&pollset->mu);
801
+ *mu = &pollset->mu;
802
+ pollset->eps = NULL;
803
+
804
+ pollset->root_worker.next = pollset->root_worker.prev = &pollset->root_worker;
805
+ pollset->kicked_without_pollers = false;
806
+
807
+ pollset->shutting_down = false;
808
+ pollset->finish_shutdown_called = false;
809
+ pollset->shutdown_done = NULL;
810
+ }
811
+
812
+ static void fd_become_readable(grpc_exec_ctx *exec_ctx, grpc_fd *fd) {
813
+ grpc_lfev_set_ready(exec_ctx, &fd->read_closure);
814
+ }
815
+
816
+ static void fd_become_writable(grpc_exec_ctx *exec_ctx, grpc_fd *fd) {
817
+ grpc_lfev_set_ready(exec_ctx, &fd->write_closure);
818
+ }
819
+
820
+ static void pollset_release_epoll_set(grpc_exec_ctx *exec_ctx, grpc_pollset *ps,
821
+ char *reason) {
822
+ if (ps->eps != NULL) {
823
+ EPS_UNREF(exec_ctx, ps->eps, reason);
824
+ }
825
+ ps->eps = NULL;
826
+ }
827
+
828
+ static void finish_shutdown_locked(grpc_exec_ctx *exec_ctx,
829
+ grpc_pollset *pollset) {
830
+ /* The pollset cannot have any workers if we are at this stage */
831
+ GPR_ASSERT(!pollset_has_workers(pollset));
832
+
833
+ pollset->finish_shutdown_called = true;
834
+
835
+ /* Release the ref and set pollset->eps to NULL */
836
+ pollset_release_epoll_set(exec_ctx, pollset, "ps_shutdown");
837
+ grpc_closure_sched(exec_ctx, pollset->shutdown_done, GRPC_ERROR_NONE);
838
+ }
839
+
840
+ /* pollset->mu lock must be held by the caller before calling this */
841
+ static void pollset_shutdown(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
842
+ grpc_closure *closure) {
843
+ GPR_TIMER_BEGIN("pollset_shutdown", 0);
844
+ GPR_ASSERT(!pollset->shutting_down);
845
+ pollset->shutting_down = true;
846
+ pollset->shutdown_done = closure;
847
+ pollset_kick(pollset, GRPC_POLLSET_KICK_BROADCAST);
848
+
849
+ /* If the pollset has any workers, we cannot call finish_shutdown_locked()
850
+ because it would release the underlying epoll set. In such a case, we
851
+ let the last worker call finish_shutdown_locked() from pollset_work() */
852
+ if (!pollset_has_workers(pollset)) {
853
+ GPR_ASSERT(!pollset->finish_shutdown_called);
854
+ GPR_TIMER_MARK("pollset_shutdown.finish_shutdown_locked", 0);
855
+ finish_shutdown_locked(exec_ctx, pollset);
856
+ }
857
+ GPR_TIMER_END("pollset_shutdown", 0);
858
+ }
859
+
860
+ /* pollset_shutdown is guaranteed to be called before pollset_destroy. So other
861
+ * than destroying the mutexes, there is nothing special that needs to be done
862
+ * here */
863
+ static void pollset_destroy(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset) {
864
+ GPR_ASSERT(!pollset_has_workers(pollset));
865
+ gpr_mu_destroy(&pollset->mu);
866
+ }
867
+
868
+ static bool maybe_do_workqueue_work(grpc_exec_ctx *exec_ctx, epoll_set *eps) {
869
+ if (gpr_mu_trylock(&eps->workqueue_read_mu)) {
870
+ gpr_mpscq_node *n = gpr_mpscq_pop(&eps->workqueue_items);
871
+ gpr_mu_unlock(&eps->workqueue_read_mu);
872
+ if (n != NULL) {
873
+ if (gpr_atm_full_fetch_add(&eps->workqueue_item_count, -1) > 1) {
874
+ workqueue_maybe_wakeup(eps);
875
+ }
876
+ grpc_closure *c = (grpc_closure *)n;
877
+ grpc_error *error = c->error_data.error;
878
+ #ifndef NDEBUG
879
+ c->scheduled = false;
880
+ #endif
881
+ c->cb(exec_ctx, c->cb_arg, error);
882
+ GRPC_ERROR_UNREF(error);
883
+ return true;
884
+ } else if (gpr_atm_no_barrier_load(&eps->workqueue_item_count) > 0) {
885
+ /* n == NULL might mean there's work but it's not available to be popped
886
+ * yet - try to ensure another workqueue wakes up to check shortly if so
887
+ */
888
+ workqueue_maybe_wakeup(eps);
889
+ }
890
+ }
891
+ return false;
892
+ }
893
+
894
+ /* Blocking call */
895
+ static void acquire_epoll_lease(epoll_set *eps) {
896
+ if (g_num_threads_per_eps > 1) {
897
+ gpr_mu_lock(&eps->mu);
898
+ }
899
+ }
900
+
901
+ static void release_epoll_lease(epoll_set *eps) {
902
+ if (g_num_threads_per_eps > 1) {
903
+ gpr_mu_unlock(&eps->mu);
904
+ }
905
+ }
906
+
907
+ #define GRPC_EPOLL_MAX_EVENTS 100
908
+ static void do_epoll_wait(grpc_exec_ctx *exec_ctx, int epoll_fd, epoll_set *eps,
909
+ grpc_error **error) {
910
+ struct epoll_event ep_ev[GRPC_EPOLL_MAX_EVENTS];
911
+ int ep_rv;
912
+ char *err_msg;
913
+ const char *err_desc = "do_epoll_wait";
914
+
915
+ int timeout_ms = -1;
916
+
917
+ GRPC_SCHEDULING_START_BLOCKING_REGION;
918
+ acquire_epoll_lease(eps);
919
+ ep_rv = epoll_wait(epoll_fd, ep_ev, GRPC_EPOLL_MAX_EVENTS, timeout_ms);
920
+ release_epoll_lease(eps);
921
+ GRPC_SCHEDULING_END_BLOCKING_REGION;
922
+
923
+ if (ep_rv < 0) {
924
+ gpr_asprintf(&err_msg,
925
+ "epoll_wait() epoll fd: %d failed with error: %d (%s)",
926
+ epoll_fd, errno, strerror(errno));
927
+ append_error(error, GRPC_OS_ERROR(errno, err_msg), err_desc);
928
+ }
929
+
930
+ #ifdef GRPC_TSAN
931
+ /* See the definition of g_poll_sync for more details */
932
+ gpr_atm_acq_load(&g_epoll_sync);
933
+ #endif /* defined(GRPC_TSAN) */
934
+
935
+ for (int i = 0; i < ep_rv; ++i) {
936
+ void *data_ptr = ep_ev[i].data.ptr;
937
+ if (data_ptr == &eps->workqueue_wakeup_fd) {
938
+ append_error(error,
939
+ grpc_wakeup_fd_consume_wakeup(&eps->workqueue_wakeup_fd),
940
+ err_desc);
941
+ maybe_do_workqueue_work(exec_ctx, eps);
942
+ } else if (data_ptr == &epoll_set_wakeup_fd) {
943
+ gpr_atm_rel_store(&eps->is_shutdown, 1);
944
+ gpr_log(GPR_INFO, "pollset poller: shutdown set");
945
+ } else {
946
+ grpc_fd *fd = data_ptr;
947
+ int cancel = ep_ev[i].events & (EPOLLERR | EPOLLHUP);
948
+ int read_ev = ep_ev[i].events & (EPOLLIN | EPOLLPRI);
949
+ int write_ev = ep_ev[i].events & EPOLLOUT;
950
+ if (read_ev || cancel) {
951
+ fd_become_readable(exec_ctx, fd);
952
+ }
953
+ if (write_ev || cancel) {
954
+ fd_become_writable(exec_ctx, fd);
955
+ }
956
+ }
957
+ }
958
+ }
959
+
960
+ static void epoll_set_work(grpc_exec_ctx *exec_ctx, epoll_set *eps,
961
+ grpc_error **error) {
962
+ int epoll_fd = -1;
963
+ GPR_TIMER_BEGIN("epoll_set_work", 0);
964
+
965
+ /* Since epoll_fd is immutable, it is safe to read it without a lock on the
966
+ epoll set. */
967
+ epoll_fd = eps->epoll_fd;
968
+
969
+ /* If we get some workqueue work to do, it might end up completing an item on
970
+ the completion queue, so there's no need to poll... so we skip that and
971
+ redo the complete loop to verify */
972
+ if (!maybe_do_workqueue_work(exec_ctx, eps)) {
973
+ gpr_atm_no_barrier_fetch_add(&eps->poller_count, 1);
974
+ g_current_thread_epoll_set = eps;
975
+
976
+ do_epoll_wait(exec_ctx, epoll_fd, eps, error);
977
+
978
+ g_current_thread_epoll_set = NULL;
979
+ gpr_atm_no_barrier_fetch_add(&eps->poller_count, -1);
980
+ }
981
+
982
+ GPR_TIMER_END("epoll_set_work", 0);
983
+ }
984
+
985
+ /* pollset->mu lock must be held by the caller before calling this.
986
+ The function pollset_work() may temporarily release the lock (pollset->mu)
987
+ during the course of its execution but it will always re-acquire the lock and
988
+ ensure that it is held by the time the function returns */
989
+ static grpc_error *pollset_work(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
990
+ grpc_pollset_worker **worker_hdl,
991
+ gpr_timespec now, gpr_timespec deadline) {
992
+ GPR_TIMER_BEGIN("pollset_work", 0);
993
+ grpc_error *error = GRPC_ERROR_NONE;
994
+
995
+ grpc_pollset_worker worker;
996
+ pollset_worker_init(&worker);
997
+
998
+ if (worker_hdl) *worker_hdl = &worker;
999
+
1000
+ gpr_tls_set(&g_current_thread_pollset, (intptr_t)pollset);
1001
+ gpr_tls_set(&g_current_thread_worker, (intptr_t)&worker);
1002
+
1003
+ if (pollset->kicked_without_pollers) {
1004
+ /* If the pollset was kicked without pollers, pretend that the current
1005
+ worker got the kick and skip polling. A kick indicates that there is some
1006
+ work that needs attention like an event on the completion queue or an
1007
+ alarm */
1008
+ GPR_TIMER_MARK("pollset_work.kicked_without_pollers", 0);
1009
+ pollset->kicked_without_pollers = 0;
1010
+ } else if (!pollset->shutting_down) {
1011
+ push_front_worker(pollset, &worker);
1012
+
1013
+ gpr_cv_wait(&worker.kick_cv, &pollset->mu,
1014
+ gpr_convert_clock_type(deadline, GPR_CLOCK_REALTIME));
1015
+ /* pollset->mu locked here */
1016
+
1017
+ remove_worker(pollset, &worker);
1018
+ }
1019
+
1020
+ /* If we are the last worker on the pollset (i.e pollset_has_workers() is
1021
+ false at this point) and the pollset is shutting down, we may have to
1022
+ finish the shutdown process by calling finish_shutdown_locked().
1023
+ See pollset_shutdown() for more details.
1024
+
1025
+ Note: Continuing to access pollset here is safe; it is the caller's
1026
+ responsibility to not destroy a pollset when it has outstanding calls to
1027
+ pollset_work() */
1028
+ if (pollset->shutting_down && !pollset_has_workers(pollset) &&
1029
+ !pollset->finish_shutdown_called) {
1030
+ GPR_TIMER_MARK("pollset_work.finish_shutdown_locked", 0);
1031
+ finish_shutdown_locked(exec_ctx, pollset);
1032
+
1033
+ gpr_mu_unlock(&pollset->mu);
1034
+ grpc_exec_ctx_flush(exec_ctx);
1035
+ gpr_mu_lock(&pollset->mu);
1036
+ }
1037
+
1038
+ if (worker_hdl) *worker_hdl = NULL;
1039
+
1040
+ gpr_tls_set(&g_current_thread_pollset, (intptr_t)0);
1041
+ gpr_tls_set(&g_current_thread_worker, (intptr_t)0);
1042
+
1043
+ GPR_TIMER_END("pollset_work", 0);
1044
+
1045
+ GRPC_LOG_IF_ERROR("pollset_work", GRPC_ERROR_REF(error));
1046
+ return error;
1047
+ }
1048
+
1049
+ static void pollset_add_fd(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
1050
+ grpc_fd *fd) {
1051
+ /* Nothing to do */
1052
+ }
1053
+
1054
+ /*******************************************************************************
1055
+ * Pollset-set Definitions
1056
+ */
1057
+ grpc_pollset_set g_dummy_pollset_set;
1058
+ static grpc_pollset_set *pollset_set_create(void) {
1059
+ return &g_dummy_pollset_set;
1060
+ }
1061
+
1062
+ static void pollset_set_destroy(grpc_exec_ctx *exec_ctx,
1063
+ grpc_pollset_set *pss) {
1064
+ /* Nothing to do */
1065
+ }
1066
+
1067
+ static void pollset_set_add_fd(grpc_exec_ctx *exec_ctx, grpc_pollset_set *pss,
1068
+ grpc_fd *fd) {
1069
+ /* Nothing to do */
1070
+ }
1071
+
1072
+ static void pollset_set_del_fd(grpc_exec_ctx *exec_ctx, grpc_pollset_set *pss,
1073
+ grpc_fd *fd) {
1074
+ /* Nothing to do */
1075
+ }
1076
+
1077
+ static void pollset_set_add_pollset(grpc_exec_ctx *exec_ctx,
1078
+ grpc_pollset_set *pss, grpc_pollset *ps) {
1079
+ /* Nothing to do */
1080
+ }
1081
+
1082
+ static void pollset_set_del_pollset(grpc_exec_ctx *exec_ctx,
1083
+ grpc_pollset_set *pss, grpc_pollset *ps) {
1084
+ /* Nothing to do */
1085
+ }
1086
+
1087
+ static void pollset_set_add_pollset_set(grpc_exec_ctx *exec_ctx,
1088
+ grpc_pollset_set *bag,
1089
+ grpc_pollset_set *item) {
1090
+ /* Nothing to do */
1091
+ }
1092
+
1093
+ static void pollset_set_del_pollset_set(grpc_exec_ctx *exec_ctx,
1094
+ grpc_pollset_set *bag,
1095
+ grpc_pollset_set *item) {
1096
+ /* Nothing to do */
1097
+ }
1098
+
1099
+ /*******************************************************************************
1100
+ * Event engine binding
1101
+ */
1102
+
1103
+ static void shutdown_engine(void) {
1104
+ shutdown_poller_threads();
1105
+ shutdown_epoll_sets();
1106
+ fd_global_shutdown();
1107
+ pollset_global_shutdown();
1108
+ epoll_set_global_shutdown();
1109
+ gpr_log(GPR_INFO, "ev-epoll-threadpool engine shutdown complete");
1110
+ }
1111
+
1112
+ static const grpc_event_engine_vtable vtable = {
1113
+ .pollset_size = sizeof(grpc_pollset),
1114
+
1115
+ .fd_create = fd_create,
1116
+ .fd_wrapped_fd = fd_wrapped_fd,
1117
+ .fd_orphan = fd_orphan,
1118
+ .fd_shutdown = fd_shutdown,
1119
+ .fd_is_shutdown = fd_is_shutdown,
1120
+ .fd_notify_on_read = fd_notify_on_read,
1121
+ .fd_notify_on_write = fd_notify_on_write,
1122
+ .fd_get_read_notifier_pollset = fd_get_read_notifier_pollset,
1123
+ .fd_get_workqueue = fd_get_workqueue,
1124
+
1125
+ .pollset_init = pollset_init,
1126
+ .pollset_shutdown = pollset_shutdown,
1127
+ .pollset_destroy = pollset_destroy,
1128
+ .pollset_work = pollset_work,
1129
+ .pollset_kick = pollset_kick,
1130
+ .pollset_add_fd = pollset_add_fd,
1131
+
1132
+ .pollset_set_create = pollset_set_create,
1133
+ .pollset_set_destroy = pollset_set_destroy,
1134
+ .pollset_set_add_pollset = pollset_set_add_pollset,
1135
+ .pollset_set_del_pollset = pollset_set_del_pollset,
1136
+ .pollset_set_add_pollset_set = pollset_set_add_pollset_set,
1137
+ .pollset_set_del_pollset_set = pollset_set_del_pollset_set,
1138
+ .pollset_set_add_fd = pollset_set_add_fd,
1139
+ .pollset_set_del_fd = pollset_set_del_fd,
1140
+
1141
+ .workqueue_ref = workqueue_ref,
1142
+ .workqueue_unref = workqueue_unref,
1143
+ .workqueue_scheduler = workqueue_scheduler,
1144
+
1145
+ .shutdown_engine = shutdown_engine,
1146
+ };
1147
+
1148
+ /*****************************************************************************
1149
+ * Dedicated polling threads and pollsets - Definitions
1150
+ */
1151
+ static void add_fd_to_eps(grpc_fd *fd) {
1152
+ GPR_ASSERT(fd->eps == NULL);
1153
+ GPR_TIMER_BEGIN("add_fd_to_eps", 0);
1154
+
1155
+ grpc_error *error = GRPC_ERROR_NONE;
1156
+ size_t idx = (size_t)gpr_atm_no_barrier_fetch_add(&g_next_eps, 1) % g_num_eps;
1157
+ epoll_set *eps = g_epoll_sets[idx];
1158
+
1159
+ gpr_mu_lock(&fd->mu);
1160
+
1161
+ if (fd->orphaned) {
1162
+ gpr_mu_unlock(&fd->mu);
1163
+ return; /* Early out */
1164
+ }
1165
+
1166
+ epoll_set_add_fd_locked(eps, fd, &error);
1167
+ EPS_ADD_REF(eps, "fd");
1168
+ fd->eps = eps;
1169
+
1170
+ GRPC_POLLING_TRACE("add_fd_to_eps (fd: %d, eps idx = %" PRIdPTR ")", fd->fd,
1171
+ idx);
1172
+ gpr_mu_unlock(&fd->mu);
1173
+
1174
+ GRPC_LOG_IF_ERROR("add_fd_to_eps", error);
1175
+ GPR_TIMER_END("add_fd_to_eps", 0);
1176
+ }
1177
+
1178
+ static bool init_epoll_sets() {
1179
+ grpc_error *error = GRPC_ERROR_NONE;
1180
+ bool is_success = true;
1181
+
1182
+ g_epoll_sets = (epoll_set **)malloc(g_num_eps * sizeof(epoll_set *));
1183
+
1184
+ for (size_t i = 0; i < g_num_eps; i++) {
1185
+ g_epoll_sets[i] = epoll_set_create(&error);
1186
+ if (g_epoll_sets[i] == NULL) {
1187
+ gpr_log(GPR_ERROR, "Error in creating a epoll set");
1188
+ g_num_eps = i; /* Helps cleanup */
1189
+ shutdown_epoll_sets();
1190
+ is_success = false;
1191
+ goto done;
1192
+ }
1193
+
1194
+ EPS_ADD_REF(g_epoll_sets[i], "init_epoll_sets");
1195
+ }
1196
+
1197
+ gpr_atm_no_barrier_store(&g_next_eps, 0);
1198
+ gpr_mu *mu;
1199
+ pollset_init(&g_read_notifier, &mu);
1200
+
1201
+ done:
1202
+ GRPC_LOG_IF_ERROR("init_epoll_sets", error);
1203
+ return is_success;
1204
+ }
1205
+
1206
+ static void shutdown_epoll_sets() {
1207
+ if (!g_epoll_sets) {
1208
+ return;
1209
+ }
1210
+
1211
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
1212
+ for (size_t i = 0; i < g_num_eps; i++) {
1213
+ EPS_UNREF(&exec_ctx, g_epoll_sets[i], "shutdown_epoll_sets");
1214
+ }
1215
+ grpc_exec_ctx_flush(&exec_ctx);
1216
+
1217
+ gpr_free(g_epoll_sets);
1218
+ g_epoll_sets = NULL;
1219
+ pollset_destroy(&exec_ctx, &g_read_notifier);
1220
+ grpc_exec_ctx_finish(&exec_ctx);
1221
+ }
1222
+
1223
+ static void poller_thread_loop(void *arg) {
1224
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
1225
+ grpc_error *error = GRPC_ERROR_NONE;
1226
+ epoll_set *eps = (epoll_set *)arg;
1227
+
1228
+ while (!gpr_atm_acq_load(&eps->is_shutdown)) {
1229
+ epoll_set_work(&exec_ctx, eps, &error);
1230
+ grpc_exec_ctx_flush(&exec_ctx);
1231
+ }
1232
+
1233
+ grpc_exec_ctx_finish(&exec_ctx);
1234
+ GRPC_LOG_IF_ERROR("poller_thread_loop", error);
1235
+ }
1236
+
1237
+ /* g_epoll_sets MUST be initialized before calling this */
1238
+ static void start_poller_threads() {
1239
+ GPR_ASSERT(g_epoll_sets);
1240
+
1241
+ gpr_log(GPR_INFO, "Starting poller threads");
1242
+
1243
+ size_t num_threads = g_num_eps * g_num_threads_per_eps;
1244
+ g_poller_threads = (gpr_thd_id *)malloc(num_threads * sizeof(gpr_thd_id));
1245
+ gpr_thd_options options = gpr_thd_options_default();
1246
+ gpr_thd_options_set_joinable(&options);
1247
+
1248
+ for (size_t i = 0; i < num_threads; i++) {
1249
+ gpr_thd_new(&g_poller_threads[i], poller_thread_loop,
1250
+ (void *)g_epoll_sets[i % g_num_eps], &options);
1251
+ }
1252
+ }
1253
+
1254
+ static void shutdown_poller_threads() {
1255
+ GPR_ASSERT(g_poller_threads);
1256
+ GPR_ASSERT(g_epoll_sets);
1257
+ grpc_error *error = GRPC_ERROR_NONE;
1258
+
1259
+ gpr_log(GPR_INFO, "Shutting down pollers");
1260
+
1261
+ epoll_set *eps = NULL;
1262
+ size_t num_threads = g_num_eps * g_num_threads_per_eps;
1263
+ for (size_t i = 0; i < num_threads; i++) {
1264
+ eps = g_epoll_sets[i];
1265
+ epoll_set_add_wakeup_fd_locked(eps, &epoll_set_wakeup_fd, &error);
1266
+ }
1267
+
1268
+ for (size_t i = 0; i < g_num_eps; i++) {
1269
+ gpr_thd_join(g_poller_threads[i]);
1270
+ }
1271
+
1272
+ GRPC_LOG_IF_ERROR("shutdown_poller_threads", error);
1273
+ gpr_free(g_poller_threads);
1274
+ g_poller_threads = NULL;
1275
+ }
1276
+
1277
+ /****************************************************************************/
1278
+
1279
+ /* It is possible that GLIBC has epoll but the underlying kernel doesn't.
1280
+ * Create a dummy epoll_fd to make sure epoll support is available */
1281
+ static bool is_epoll_available() {
1282
+ int fd = epoll_create1(EPOLL_CLOEXEC);
1283
+ if (fd < 0) {
1284
+ gpr_log(
1285
+ GPR_ERROR,
1286
+ "epoll_create1 failed with error: %d. Not using epoll polling engine",
1287
+ fd);
1288
+ return false;
1289
+ }
1290
+ close(fd);
1291
+ return true;
1292
+ }
1293
+
1294
+ const grpc_event_engine_vtable *grpc_init_epoll_thread_pool_linux(
1295
+ bool requested_explicitly) {
1296
+ if (!requested_explicitly) return NULL;
1297
+
1298
+ if (!grpc_has_wakeup_fd()) {
1299
+ return NULL;
1300
+ }
1301
+
1302
+ if (!is_epoll_available()) {
1303
+ return NULL;
1304
+ }
1305
+
1306
+ fd_global_init();
1307
+
1308
+ if (!GRPC_LOG_IF_ERROR("pollset_global_init", pollset_global_init())) {
1309
+ return NULL;
1310
+ }
1311
+
1312
+ if (!GRPC_LOG_IF_ERROR("epoll_set_global_init", epoll_set_global_init())) {
1313
+ return NULL;
1314
+ }
1315
+
1316
+ if (!init_epoll_sets()) {
1317
+ return NULL;
1318
+ }
1319
+
1320
+ /* TODO (sreek): Maynot be a good idea to start threads here (especially if
1321
+ * this engine doesn't get picked. Consider introducing an engine_init
1322
+ * function in the vtable */
1323
+ start_poller_threads();
1324
+ return &vtable;
1325
+ }
1326
+
1327
+ #else /* defined(GRPC_LINUX_EPOLL) */
1328
+ #if defined(GRPC_POSIX_SOCKET)
1329
+ #include "src/core/lib/iomgr/ev_posix.h"
1330
+ /* If GRPC_LINUX_EPOLL is not defined, it means epoll is not available. Return
1331
+ * NULL */
1332
+ const grpc_event_engine_vtable *grpc_init_epoll_thread_pool_linux(
1333
+ bool requested_explicitly) {
1334
+ return NULL;
1335
+ }
1336
+ #endif /* defined(GRPC_POSIX_SOCKET) */
1337
+ #endif /* !defined(GRPC_LINUX_EPOLL) */