grpc 0.14.1 → 0.15.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 (277) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +1398 -817
  3. data/include/grpc/compression.h +2 -1
  4. data/include/grpc/grpc.h +10 -1
  5. data/include/grpc/grpc_cronet.h +51 -0
  6. data/include/grpc/grpc_posix.h +70 -0
  7. data/include/grpc/impl/codegen/atm.h +2 -2
  8. data/include/grpc/impl/codegen/{atm_win32.h → atm_windows.h} +3 -3
  9. data/include/grpc/impl/codegen/compression_types.h +39 -5
  10. data/include/grpc/impl/codegen/connectivity_state.h +1 -1
  11. data/include/grpc/impl/codegen/grpc_types.h +10 -0
  12. data/include/grpc/impl/codegen/log.h +2 -1
  13. data/include/grpc/impl/codegen/port_platform.h +30 -12
  14. data/include/grpc/impl/codegen/slice_buffer.h +2 -3
  15. data/include/grpc/impl/codegen/sync.h +2 -2
  16. data/include/grpc/impl/codegen/{sync_win32.h → sync_windows.h} +3 -3
  17. data/include/grpc/support/{sync_win32.h → atm_windows.h} +4 -4
  18. data/include/grpc/support/avl.h +5 -0
  19. data/include/grpc/support/{log_win32.h → log_windows.h} +3 -3
  20. data/include/grpc/support/string_util.h +2 -1
  21. data/include/grpc/support/{atm_win32.h → sync_windows.h} +4 -4
  22. data/src/core/ext/census/gen/census.pb.c +179 -0
  23. data/src/core/ext/census/gen/census.pb.h +294 -0
  24. data/src/core/ext/census/grpc_filter.c +11 -7
  25. data/src/core/ext/client_config/channel_connectivity.c +28 -14
  26. data/src/core/ext/client_config/client_channel.c +77 -53
  27. data/src/core/ext/client_config/connector.h +1 -1
  28. data/src/core/ext/client_config/lb_policy.c +9 -6
  29. data/src/core/ext/client_config/lb_policy.h +9 -5
  30. data/src/core/ext/client_config/subchannel.c +58 -39
  31. data/src/core/ext/client_config/subchannel.h +3 -2
  32. data/src/core/ext/client_config/subchannel_call_holder.c +34 -19
  33. data/src/core/ext/client_config/subchannel_call_holder.h +2 -1
  34. data/src/core/ext/client_config/subchannel_index.c +20 -9
  35. data/src/core/ext/lb_policy/grpclb/load_balancer_api.c +7 -7
  36. data/src/core/ext/lb_policy/grpclb/load_balancer_api.h +5 -5
  37. data/src/core/ext/lb_policy/grpclb/proto/grpc/lb/{v0 → v1}/load_balancer.pb.c +29 -30
  38. data/src/core/ext/lb_policy/grpclb/proto/grpc/lb/v1/load_balancer.pb.h +178 -0
  39. data/src/core/ext/lb_policy/pick_first/pick_first.c +65 -45
  40. data/src/core/ext/lb_policy/round_robin/round_robin.c +84 -43
  41. data/src/core/ext/load_reporting/load_reporting.c +133 -0
  42. data/src/core/ext/load_reporting/load_reporting.h +75 -0
  43. data/src/core/ext/load_reporting/load_reporting_filter.c +151 -0
  44. data/src/core/ext/load_reporting/load_reporting_filter.h +41 -0
  45. data/src/core/ext/resolver/dns/native/dns_resolver.c +22 -8
  46. data/src/core/ext/resolver/sockaddr/sockaddr_resolver.c +2 -2
  47. data/src/core/ext/transport/chttp2/client/insecure/channel_create.c +4 -4
  48. data/src/core/ext/transport/chttp2/client/insecure/channel_create_posix.c +95 -0
  49. data/src/core/ext/transport/chttp2/client/secure/secure_channel_create.c +14 -18
  50. data/src/core/ext/transport/chttp2/server/insecure/server_chttp2.c +49 -24
  51. data/src/core/ext/transport/chttp2/server/insecure/server_chttp2_posix.c +82 -0
  52. data/src/core/ext/transport/chttp2/server/secure/server_secure_chttp2.c +104 -60
  53. data/src/core/ext/transport/chttp2/transport/bin_decoder.c +232 -0
  54. data/src/{ruby/ext/grpc/rb_signal.c → core/ext/transport/chttp2/transport/bin_decoder.h} +27 -31
  55. data/src/core/ext/transport/chttp2/transport/chttp2_transport.c +481 -260
  56. data/src/core/ext/transport/chttp2/transport/frame.h +1 -7
  57. data/src/core/ext/transport/chttp2/transport/frame_data.c +44 -27
  58. data/src/core/ext/transport/chttp2/transport/frame_data.h +6 -5
  59. data/src/core/ext/transport/chttp2/transport/frame_goaway.c +23 -17
  60. data/src/core/ext/transport/chttp2/transport/frame_goaway.h +2 -2
  61. data/src/core/ext/transport/chttp2/transport/frame_ping.c +12 -7
  62. data/src/core/ext/transport/chttp2/transport/frame_ping.h +3 -3
  63. data/src/core/ext/transport/chttp2/transport/frame_rst_stream.c +25 -12
  64. data/src/core/ext/transport/chttp2/transport/frame_rst_stream.h +2 -2
  65. data/src/core/ext/transport/chttp2/transport/frame_settings.c +23 -21
  66. data/src/core/ext/transport/chttp2/transport/frame_settings.h +2 -2
  67. data/src/core/ext/transport/chttp2/transport/frame_window_update.c +17 -9
  68. data/src/core/ext/transport/chttp2/transport/frame_window_update.h +2 -2
  69. data/src/core/ext/transport/chttp2/transport/hpack_parser.c +365 -287
  70. data/src/core/ext/transport/chttp2/transport/hpack_parser.h +8 -6
  71. data/src/core/ext/transport/chttp2/transport/hpack_table.c +24 -20
  72. data/src/core/ext/transport/chttp2/transport/hpack_table.h +5 -4
  73. data/src/core/ext/transport/chttp2/transport/incoming_metadata.c +1 -0
  74. data/src/core/ext/transport/chttp2/transport/incoming_metadata.h +1 -0
  75. data/src/core/ext/transport/chttp2/transport/internal.h +34 -32
  76. data/src/core/ext/transport/chttp2/transport/parsing.c +296 -212
  77. data/src/core/ext/transport/chttp2/transport/writing.c +12 -9
  78. data/src/core/lib/channel/channel_args.c +26 -12
  79. data/src/core/lib/channel/channel_args.h +1 -1
  80. data/src/core/lib/channel/channel_stack.c +12 -8
  81. data/src/core/lib/channel/channel_stack.h +27 -11
  82. data/src/core/lib/channel/channel_stack_builder.c +2 -2
  83. data/src/core/lib/channel/compress_filter.c +26 -31
  84. data/src/core/lib/channel/compress_filter.h +4 -4
  85. data/src/core/lib/channel/connected_channel.c +7 -5
  86. data/src/core/lib/channel/http_client_filter.c +34 -8
  87. data/src/core/lib/channel/http_client_filter.h +1 -1
  88. data/src/core/lib/channel/http_server_filter.c +21 -12
  89. data/src/core/lib/compression/{compression_algorithm.c → compression.c} +22 -21
  90. data/src/core/lib/http/httpcli.c +81 -59
  91. data/src/core/lib/http/httpcli.h +11 -15
  92. data/src/core/lib/http/httpcli_security_connector.c +5 -3
  93. data/src/core/lib/http/parser.c +127 -118
  94. data/src/core/lib/http/parser.h +11 -6
  95. data/src/core/lib/iomgr/closure.c +20 -16
  96. data/src/core/lib/iomgr/closure.h +19 -15
  97. data/src/core/lib/iomgr/endpoint.h +1 -1
  98. data/src/core/lib/iomgr/endpoint_pair_posix.c +2 -2
  99. data/src/core/lib/iomgr/error.c +535 -0
  100. data/src/core/lib/iomgr/error.h +192 -0
  101. data/src/core/lib/iomgr/ev_poll_and_epoll_posix.c +190 -83
  102. data/src/core/lib/iomgr/ev_poll_posix.c +1267 -0
  103. data/src/{ruby/ext/grpc/rb_signal.h → core/lib/iomgr/ev_poll_posix.h} +7 -5
  104. data/src/core/lib/iomgr/ev_posix.c +104 -14
  105. data/src/core/lib/iomgr/ev_posix.h +17 -7
  106. data/src/core/lib/iomgr/exec_ctx.c +25 -7
  107. data/src/core/lib/iomgr/exec_ctx.h +27 -8
  108. data/src/core/lib/iomgr/executor.c +2 -2
  109. data/src/core/lib/iomgr/executor.h +1 -1
  110. data/src/core/lib/iomgr/iocp_windows.c +2 -41
  111. data/src/core/lib/iomgr/iocp_windows.h +0 -8
  112. data/src/core/lib/iomgr/iomgr.c +5 -4
  113. data/src/core/lib/iomgr/iomgr_posix.c +5 -1
  114. data/src/core/lib/iomgr/iomgr_windows.c +1 -1
  115. data/src/core/lib/{support → iomgr}/load_file.c +15 -17
  116. data/src/core/lib/{support → iomgr}/load_file.h +8 -7
  117. data/src/core/lib/iomgr/polling_entity.c +104 -0
  118. data/src/core/lib/iomgr/polling_entity.h +81 -0
  119. data/src/core/lib/iomgr/pollset.h +6 -5
  120. data/src/core/lib/iomgr/pollset_set_windows.c +4 -1
  121. data/src/core/lib/iomgr/pollset_windows.c +10 -6
  122. data/src/core/lib/iomgr/resolve_address.h +5 -9
  123. data/src/core/lib/iomgr/resolve_address_posix.c +55 -38
  124. data/src/core/lib/iomgr/resolve_address_windows.c +51 -37
  125. data/src/core/lib/iomgr/sockaddr.h +2 -2
  126. data/src/core/lib/iomgr/{sockaddr_win32.h → sockaddr_windows.h} +3 -3
  127. data/src/core/lib/iomgr/socket_utils_common_posix.c +92 -45
  128. data/src/core/lib/iomgr/socket_utils_posix.h +19 -12
  129. data/src/core/lib/iomgr/socket_windows.c +61 -2
  130. data/src/core/lib/iomgr/socket_windows.h +13 -0
  131. data/src/core/lib/iomgr/tcp_client_posix.c +54 -39
  132. data/src/core/lib/iomgr/tcp_client_windows.c +34 -34
  133. data/src/core/lib/iomgr/tcp_posix.c +43 -39
  134. data/src/core/lib/iomgr/tcp_server.h +5 -3
  135. data/src/core/lib/iomgr/tcp_server_posix.c +103 -64
  136. data/src/core/lib/iomgr/tcp_server_windows.c +114 -101
  137. data/src/core/lib/iomgr/tcp_windows.c +45 -50
  138. data/src/core/lib/iomgr/tcp_windows.h +1 -1
  139. data/src/core/lib/iomgr/timer.c +26 -13
  140. data/src/core/lib/iomgr/udp_server.c +28 -4
  141. data/src/core/lib/iomgr/udp_server.h +5 -1
  142. data/src/core/lib/iomgr/unix_sockets_posix.c +8 -7
  143. data/src/core/lib/iomgr/unix_sockets_posix.h +2 -1
  144. data/src/core/lib/iomgr/unix_sockets_posix_noop.c +4 -2
  145. data/src/core/lib/iomgr/wakeup_fd_eventfd.c +15 -5
  146. data/src/core/lib/iomgr/wakeup_fd_pipe.c +13 -9
  147. data/src/core/lib/iomgr/wakeup_fd_posix.c +6 -6
  148. data/src/core/lib/iomgr/wakeup_fd_posix.h +9 -6
  149. data/src/core/lib/iomgr/workqueue.h +5 -4
  150. data/src/core/lib/iomgr/workqueue_posix.c +40 -26
  151. data/src/core/lib/iomgr/workqueue_windows.c +2 -2
  152. data/src/core/lib/profiling/basic_timers.c +2 -2
  153. data/src/core/lib/security/{security_context.c → context/security_context.c} +1 -1
  154. data/src/core/lib/security/{security_context.h → context/security_context.h} +4 -4
  155. data/src/core/lib/security/credentials/composite/composite_credentials.c +263 -0
  156. data/src/core/lib/security/credentials/composite/composite_credentials.h +72 -0
  157. data/src/core/lib/security/credentials/credentials.c +233 -0
  158. data/src/core/lib/security/{credentials.h → credentials/credentials.h} +19 -157
  159. data/src/core/lib/security/{credentials_metadata.c → credentials/credentials_metadata.c} +1 -1
  160. data/src/core/lib/security/credentials/fake/fake_credentials.c +139 -0
  161. data/src/core/lib/security/credentials/fake/fake_credentials.h +56 -0
  162. data/src/core/lib/security/{credentials_posix.c → credentials/google_default/credentials_posix.c} +1 -1
  163. data/src/core/lib/security/{credentials_win32.c → credentials/google_default/credentials_windows.c} +3 -3
  164. data/src/core/lib/security/{google_default_credentials.c → credentials/google_default/google_default_credentials.c} +93 -35
  165. data/src/core/lib/security/credentials/google_default/google_default_credentials.h +46 -0
  166. data/src/core/lib/security/credentials/iam/iam_credentials.c +85 -0
  167. data/src/core/lib/security/credentials/iam/iam_credentials.h +44 -0
  168. data/src/core/lib/security/{json_token.c → credentials/jwt/json_token.c} +10 -101
  169. data/src/core/lib/security/{json_token.h → credentials/jwt/json_token.h} +3 -33
  170. data/src/core/lib/security/credentials/jwt/jwt_credentials.c +160 -0
  171. data/src/core/lib/security/credentials/jwt/jwt_credentials.h +62 -0
  172. data/src/core/lib/security/{jwt_verifier.c → credentials/jwt/jwt_verifier.c} +35 -15
  173. data/src/core/lib/security/{jwt_verifier.h → credentials/jwt/jwt_verifier.h} +3 -3
  174. data/src/core/lib/security/credentials/oauth2/oauth2_credentials.c +433 -0
  175. data/src/core/lib/security/credentials/oauth2/oauth2_credentials.h +109 -0
  176. data/src/core/lib/security/credentials/plugin/plugin_credentials.c +129 -0
  177. data/src/core/lib/security/credentials/plugin/plugin_credentials.h +45 -0
  178. data/src/core/lib/security/credentials/ssl/ssl_credentials.c +240 -0
  179. data/src/core/lib/security/credentials/ssl/ssl_credentials.h +48 -0
  180. data/src/core/lib/security/{auth_filters.h → transport/auth_filters.h} +3 -3
  181. data/src/core/lib/security/{client_auth_filter.c → transport/client_auth_filter.c} +27 -20
  182. data/src/core/lib/security/{handshake.c → transport/handshake.c} +77 -45
  183. data/src/core/lib/security/{handshake.h → transport/handshake.h} +9 -11
  184. data/src/core/lib/security/{secure_endpoint.c → transport/secure_endpoint.c} +19 -12
  185. data/src/core/lib/security/{secure_endpoint.h → transport/secure_endpoint.h} +3 -3
  186. data/src/core/lib/security/{security_connector.c → transport/security_connector.c} +26 -17
  187. data/src/core/lib/security/{security_connector.h → transport/security_connector.h} +8 -8
  188. data/src/core/lib/security/{server_auth_filter.c → transport/server_auth_filter.c} +24 -16
  189. data/src/core/lib/security/transport/tsi_error.c +40 -0
  190. data/src/core/lib/security/transport/tsi_error.h +42 -0
  191. data/src/core/lib/security/{b64.c → util/b64.c} +1 -1
  192. data/src/core/lib/security/{b64.h → util/b64.h} +3 -3
  193. data/src/core/lib/security/util/json_util.c +61 -0
  194. data/src/core/lib/security/util/json_util.h +55 -0
  195. data/src/core/lib/support/avl.c +11 -0
  196. data/src/core/lib/support/cpu_windows.c +2 -2
  197. data/src/core/lib/support/{env_win32.c → env_windows.c} +3 -3
  198. data/src/core/lib/support/log.c +3 -1
  199. data/src/core/lib/support/log_linux.c +2 -2
  200. data/src/core/lib/support/{log_win32.c → log_windows.c} +4 -4
  201. data/src/core/lib/support/murmur_hash.c +3 -5
  202. data/src/core/lib/support/string.c +10 -0
  203. data/src/core/lib/support/string.h +4 -0
  204. data/src/core/lib/support/{string_util_win32.c → string_util_windows.c} +3 -3
  205. data/src/core/lib/support/{string_win32.c → string_windows.c} +2 -2
  206. data/src/core/lib/support/{string_win32.h → string_windows.h} +5 -5
  207. data/src/core/lib/support/subprocess_windows.c +1 -1
  208. data/src/core/lib/support/{sync_win32.c → sync_windows.c} +2 -2
  209. data/src/core/lib/support/{thd_win32.c → thd_windows.c} +2 -2
  210. data/src/core/lib/support/{time_win32.c → time_windows.c} +2 -2
  211. data/src/core/lib/support/tmpfile_msys.c +1 -1
  212. data/src/core/lib/support/{tmpfile_win32.c → tmpfile_windows.c} +3 -3
  213. data/src/core/lib/surface/alarm.c +2 -2
  214. data/src/core/lib/surface/byte_buffer_reader.c +13 -6
  215. data/src/core/lib/surface/call.c +323 -123
  216. data/src/core/lib/surface/call.h +2 -0
  217. data/src/core/lib/surface/call_log_batch.c +1 -1
  218. data/src/core/lib/surface/channel.c +64 -15
  219. data/src/core/lib/surface/channel.h +9 -0
  220. data/src/core/lib/surface/channel_ping.c +3 -3
  221. data/src/core/lib/surface/completion_queue.c +75 -19
  222. data/src/core/lib/surface/completion_queue.h +7 -2
  223. data/src/core/lib/surface/init.c +2 -1
  224. data/src/core/lib/surface/init_secure.c +4 -4
  225. data/src/core/lib/surface/lame_client.c +12 -8
  226. data/src/core/lib/surface/server.c +213 -120
  227. data/src/core/lib/surface/server.h +1 -0
  228. data/src/core/lib/surface/version.c +1 -1
  229. data/src/core/lib/transport/connectivity_state.c +40 -18
  230. data/src/core/lib/transport/connectivity_state.h +4 -1
  231. data/src/core/lib/transport/metadata.c +23 -23
  232. data/src/core/lib/transport/metadata.h +4 -0
  233. data/src/core/lib/transport/metadata_batch.c +9 -0
  234. data/src/core/lib/transport/metadata_batch.h +3 -0
  235. data/src/core/lib/transport/static_metadata.c +6 -5
  236. data/src/core/lib/transport/static_metadata.h +64 -60
  237. data/src/core/lib/transport/transport.c +24 -12
  238. data/src/core/lib/transport/transport.h +6 -5
  239. data/src/core/lib/transport/transport_impl.h +4 -0
  240. data/src/core/lib/transport/transport_op_string.c +2 -2
  241. data/src/core/plugin_registry/grpc_plugin_registry.c +4 -0
  242. data/src/ruby/bin/math_services.rb +41 -2
  243. data/src/ruby/ext/grpc/rb_call.c +42 -40
  244. data/src/ruby/ext/grpc/rb_channel.c +1 -1
  245. data/src/ruby/ext/grpc/rb_completion_queue.c +59 -6
  246. data/src/ruby/ext/grpc/rb_completion_queue.h +1 -1
  247. data/src/ruby/ext/grpc/rb_grpc.c +1 -3
  248. data/src/ruby/ext/grpc/rb_grpc_imports.generated.c +12 -2
  249. data/src/ruby/ext/grpc/rb_grpc_imports.generated.h +21 -5
  250. data/src/ruby/ext/grpc/rb_loader.c +1 -1
  251. data/src/ruby/ext/grpc/rb_server.c +5 -3
  252. data/src/ruby/lib/grpc.rb +0 -3
  253. data/src/ruby/lib/grpc/errors.rb +3 -2
  254. data/src/ruby/lib/grpc/generic/active_call.rb +32 -42
  255. data/src/ruby/lib/grpc/generic/bidi_call.rb +20 -0
  256. data/src/ruby/lib/grpc/generic/client_stub.rb +31 -54
  257. data/src/ruby/lib/grpc/generic/rpc_desc.rb +4 -4
  258. data/src/ruby/lib/grpc/generic/rpc_server.rb +12 -23
  259. data/src/ruby/lib/grpc/generic/service.rb +8 -8
  260. data/src/ruby/lib/grpc/version.rb +1 -1
  261. data/src/ruby/pb/grpc/health/v1/health_services.rb +30 -2
  262. data/src/ruby/pb/grpc/testing/duplicate/echo_duplicate_services.rb +34 -4
  263. data/src/ruby/pb/grpc/testing/metrics_services.rb +39 -2
  264. data/src/ruby/pb/src/proto/grpc/testing/empty.rb +15 -0
  265. data/src/ruby/pb/src/proto/grpc/testing/messages.rb +84 -0
  266. data/src/ruby/pb/src/proto/grpc/testing/test.rb +14 -0
  267. data/src/ruby/pb/src/proto/grpc/testing/test_services.rb +110 -0
  268. data/src/ruby/pb/test/client.rb +5 -2
  269. data/src/ruby/spec/generic/active_call_spec.rb +3 -2
  270. data/src/ruby/spec/generic/client_stub_spec.rb +27 -24
  271. data/src/ruby/spec/generic/rpc_desc_spec.rb +11 -11
  272. data/src/ruby/spec/generic/rpc_server_spec.rb +42 -61
  273. data/src/ruby/spec/pb/health/checker_spec.rb +3 -5
  274. metadata +86 -48
  275. data/src/core/ext/lb_policy/grpclb/proto/grpc/lb/v0/load_balancer.pb.h +0 -182
  276. data/src/core/lib/security/credentials.c +0 -1296
  277. data/src/ruby/lib/grpc/signals.rb +0 -69
@@ -0,0 +1,1267 @@
1
+ /*
2
+ *
3
+ * Copyright 2016, 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 <grpc/support/port_platform.h>
35
+
36
+ #ifdef GPR_POSIX_SOCKET
37
+
38
+ #include "src/core/lib/iomgr/ev_poll_posix.h"
39
+
40
+ #include <assert.h>
41
+ #include <errno.h>
42
+ #include <poll.h>
43
+ #include <string.h>
44
+ #include <sys/socket.h>
45
+ #include <unistd.h>
46
+
47
+ #include <grpc/support/alloc.h>
48
+ #include <grpc/support/log.h>
49
+ #include <grpc/support/string_util.h>
50
+ #include <grpc/support/tls.h>
51
+ #include <grpc/support/useful.h>
52
+
53
+ #include "src/core/lib/iomgr/iomgr_internal.h"
54
+ #include "src/core/lib/iomgr/wakeup_fd_posix.h"
55
+ #include "src/core/lib/profiling/timers.h"
56
+ #include "src/core/lib/support/block_annotate.h"
57
+
58
+ /*******************************************************************************
59
+ * FD declarations
60
+ */
61
+
62
+ typedef struct grpc_fd_watcher {
63
+ struct grpc_fd_watcher *next;
64
+ struct grpc_fd_watcher *prev;
65
+ grpc_pollset *pollset;
66
+ grpc_pollset_worker *worker;
67
+ grpc_fd *fd;
68
+ } grpc_fd_watcher;
69
+
70
+ struct grpc_fd {
71
+ int fd;
72
+ /* refst format:
73
+ bit0: 1=active/0=orphaned
74
+ bit1-n: refcount
75
+ meaning that mostly we ref by two to avoid altering the orphaned bit,
76
+ and just unref by 1 when we're ready to flag the object as orphaned */
77
+ gpr_atm refst;
78
+
79
+ gpr_mu mu;
80
+ int shutdown;
81
+ int closed;
82
+ int released;
83
+
84
+ /* The watcher list.
85
+
86
+ The following watcher related fields are protected by watcher_mu.
87
+
88
+ An fd_watcher is an ephemeral object created when an fd wants to
89
+ begin polling, and destroyed after the poll.
90
+
91
+ It denotes the fd's interest in whether to read poll or write poll
92
+ or both or neither on this fd.
93
+
94
+ If a watcher is asked to poll for reads or writes, the read_watcher
95
+ or write_watcher fields are set respectively. A watcher may be asked
96
+ to poll for both, in which case both fields will be set.
97
+
98
+ read_watcher and write_watcher may be NULL if no watcher has been
99
+ asked to poll for reads or writes.
100
+
101
+ If an fd_watcher is not asked to poll for reads or writes, it's added
102
+ to a linked list of inactive watchers, rooted at inactive_watcher_root.
103
+ If at a later time there becomes need of a poller to poll, one of
104
+ the inactive pollers may be kicked out of their poll loops to take
105
+ that responsibility. */
106
+ grpc_fd_watcher inactive_watcher_root;
107
+ grpc_fd_watcher *read_watcher;
108
+ grpc_fd_watcher *write_watcher;
109
+
110
+ grpc_closure *read_closure;
111
+ grpc_closure *write_closure;
112
+
113
+ grpc_closure *on_done_closure;
114
+
115
+ grpc_iomgr_object iomgr_object;
116
+
117
+ /* The pollset that last noticed and notified that the fd is readable */
118
+ grpc_pollset *read_notifier_pollset;
119
+ };
120
+
121
+ /* Begin polling on an fd.
122
+ Registers that the given pollset is interested in this fd - so that if read
123
+ or writability interest changes, the pollset can be kicked to pick up that
124
+ new interest.
125
+ Return value is:
126
+ (fd_needs_read? read_mask : 0) | (fd_needs_write? write_mask : 0)
127
+ i.e. a combination of read_mask and write_mask determined by the fd's current
128
+ interest in said events.
129
+ Polling strategies that do not need to alter their behavior depending on the
130
+ fd's current interest (such as epoll) do not need to call this function.
131
+ MUST NOT be called with a pollset lock taken */
132
+ static uint32_t fd_begin_poll(grpc_fd *fd, grpc_pollset *pollset,
133
+ grpc_pollset_worker *worker, uint32_t read_mask,
134
+ uint32_t write_mask, grpc_fd_watcher *rec);
135
+ /* Complete polling previously started with fd_begin_poll
136
+ MUST NOT be called with a pollset lock taken
137
+ if got_read or got_write are 1, also does the become_{readable,writable} as
138
+ appropriate. */
139
+ static void fd_end_poll(grpc_exec_ctx *exec_ctx, grpc_fd_watcher *rec,
140
+ int got_read, int got_write,
141
+ grpc_pollset *read_notifier_pollset);
142
+
143
+ /* Return 1 if this fd is orphaned, 0 otherwise */
144
+ static bool fd_is_orphaned(grpc_fd *fd);
145
+
146
+ /* Reference counting for fds */
147
+ /*#define GRPC_FD_REF_COUNT_DEBUG*/
148
+ #ifdef GRPC_FD_REF_COUNT_DEBUG
149
+ static void fd_ref(grpc_fd *fd, const char *reason, const char *file, int line);
150
+ static void fd_unref(grpc_fd *fd, const char *reason, const char *file,
151
+ int line);
152
+ #define GRPC_FD_REF(fd, reason) fd_ref(fd, reason, __FILE__, __LINE__)
153
+ #define GRPC_FD_UNREF(fd, reason) fd_unref(fd, reason, __FILE__, __LINE__)
154
+ #else
155
+ static void fd_ref(grpc_fd *fd);
156
+ static void fd_unref(grpc_fd *fd);
157
+ #define GRPC_FD_REF(fd, reason) fd_ref(fd)
158
+ #define GRPC_FD_UNREF(fd, reason) fd_unref(fd)
159
+ #endif
160
+
161
+ #define CLOSURE_NOT_READY ((grpc_closure *)0)
162
+ #define CLOSURE_READY ((grpc_closure *)1)
163
+
164
+ /*******************************************************************************
165
+ * pollset declarations
166
+ */
167
+
168
+ typedef struct grpc_cached_wakeup_fd {
169
+ grpc_wakeup_fd fd;
170
+ struct grpc_cached_wakeup_fd *next;
171
+ } grpc_cached_wakeup_fd;
172
+
173
+ struct grpc_pollset_worker {
174
+ grpc_cached_wakeup_fd *wakeup_fd;
175
+ int reevaluate_polling_on_wakeup;
176
+ int kicked_specifically;
177
+ struct grpc_pollset_worker *next;
178
+ struct grpc_pollset_worker *prev;
179
+ };
180
+
181
+ struct grpc_pollset {
182
+ gpr_mu mu;
183
+ grpc_pollset_worker root_worker;
184
+ int shutting_down;
185
+ int called_shutdown;
186
+ int kicked_without_pollers;
187
+ grpc_closure *shutdown_done;
188
+ grpc_closure_list idle_jobs;
189
+ /* all polled fds */
190
+ size_t fd_count;
191
+ size_t fd_capacity;
192
+ grpc_fd **fds;
193
+ /* Local cache of eventfds for workers */
194
+ grpc_cached_wakeup_fd *local_wakeup_cache;
195
+ };
196
+
197
+ /* Add an fd to a pollset */
198
+ static void pollset_add_fd(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
199
+ struct grpc_fd *fd);
200
+
201
+ static void pollset_set_add_fd(grpc_exec_ctx *exec_ctx,
202
+ grpc_pollset_set *pollset_set, grpc_fd *fd);
203
+
204
+ /* Convert a timespec to milliseconds:
205
+ - very small or negative poll times are clamped to zero to do a
206
+ non-blocking poll (which becomes spin polling)
207
+ - other small values are rounded up to one millisecond
208
+ - longer than a millisecond polls are rounded up to the next nearest
209
+ millisecond to avoid spinning
210
+ - infinite timeouts are converted to -1 */
211
+ static int poll_deadline_to_millis_timeout(gpr_timespec deadline,
212
+ gpr_timespec now);
213
+
214
+ /* Allow kick to wakeup the currently polling worker */
215
+ #define GRPC_POLLSET_CAN_KICK_SELF 1
216
+ /* Force the wakee to repoll when awoken */
217
+ #define GRPC_POLLSET_REEVALUATE_POLLING_ON_WAKEUP 2
218
+ /* As per pollset_kick, with an extended set of flags (defined above)
219
+ -- mostly for fd_posix's use. */
220
+ static grpc_error *pollset_kick_ext(grpc_pollset *p,
221
+ grpc_pollset_worker *specific_worker,
222
+ uint32_t flags) GRPC_MUST_USE_RESULT;
223
+
224
+ /* Return 1 if the pollset has active threads in pollset_work (pollset must
225
+ * be locked) */
226
+ static int pollset_has_workers(grpc_pollset *pollset);
227
+
228
+ /*******************************************************************************
229
+ * pollset_set definitions
230
+ */
231
+
232
+ struct grpc_pollset_set {
233
+ gpr_mu mu;
234
+
235
+ size_t pollset_count;
236
+ size_t pollset_capacity;
237
+ grpc_pollset **pollsets;
238
+
239
+ size_t pollset_set_count;
240
+ size_t pollset_set_capacity;
241
+ struct grpc_pollset_set **pollset_sets;
242
+
243
+ size_t fd_count;
244
+ size_t fd_capacity;
245
+ grpc_fd **fds;
246
+ };
247
+
248
+ /*******************************************************************************
249
+ * fd_posix.c
250
+ */
251
+
252
+ #ifdef GRPC_FD_REF_COUNT_DEBUG
253
+ #define REF_BY(fd, n, reason) ref_by(fd, n, reason, __FILE__, __LINE__)
254
+ #define UNREF_BY(fd, n, reason) unref_by(fd, n, reason, __FILE__, __LINE__)
255
+ static void ref_by(grpc_fd *fd, int n, const char *reason, const char *file,
256
+ int line) {
257
+ gpr_log(GPR_DEBUG, "FD %d %p ref %d %d -> %d [%s; %s:%d]", fd->fd, fd, n,
258
+ gpr_atm_no_barrier_load(&fd->refst),
259
+ gpr_atm_no_barrier_load(&fd->refst) + n, reason, file, line);
260
+ #else
261
+ #define REF_BY(fd, n, reason) ref_by(fd, n)
262
+ #define UNREF_BY(fd, n, reason) unref_by(fd, n)
263
+ static void ref_by(grpc_fd *fd, int n) {
264
+ #endif
265
+ GPR_ASSERT(gpr_atm_no_barrier_fetch_add(&fd->refst, n) > 0);
266
+ }
267
+
268
+ #ifdef GRPC_FD_REF_COUNT_DEBUG
269
+ static void unref_by(grpc_fd *fd, int n, const char *reason, const char *file,
270
+ int line) {
271
+ gpr_atm old;
272
+ gpr_log(GPR_DEBUG, "FD %d %p unref %d %d -> %d [%s; %s:%d]", fd->fd, fd, n,
273
+ gpr_atm_no_barrier_load(&fd->refst),
274
+ gpr_atm_no_barrier_load(&fd->refst) - n, reason, file, line);
275
+ #else
276
+ static void unref_by(grpc_fd *fd, int n) {
277
+ gpr_atm old;
278
+ #endif
279
+ old = gpr_atm_full_fetch_add(&fd->refst, -n);
280
+ if (old == n) {
281
+ gpr_mu_destroy(&fd->mu);
282
+ grpc_iomgr_unregister_object(&fd->iomgr_object);
283
+ gpr_free(fd);
284
+ } else {
285
+ GPR_ASSERT(old > n);
286
+ }
287
+ }
288
+
289
+ static grpc_fd *fd_create(int fd, const char *name) {
290
+ grpc_fd *r = gpr_malloc(sizeof(*r));
291
+ gpr_mu_init(&r->mu);
292
+ gpr_atm_rel_store(&r->refst, 1);
293
+ r->shutdown = 0;
294
+ r->read_closure = CLOSURE_NOT_READY;
295
+ r->write_closure = CLOSURE_NOT_READY;
296
+ r->fd = fd;
297
+ r->inactive_watcher_root.next = r->inactive_watcher_root.prev =
298
+ &r->inactive_watcher_root;
299
+ r->read_watcher = r->write_watcher = NULL;
300
+ r->on_done_closure = NULL;
301
+ r->closed = 0;
302
+ r->released = 0;
303
+ r->read_notifier_pollset = NULL;
304
+
305
+ char *name2;
306
+ gpr_asprintf(&name2, "%s fd=%d", name, fd);
307
+ grpc_iomgr_register_object(&r->iomgr_object, name2);
308
+ gpr_free(name2);
309
+ #ifdef GRPC_FD_REF_COUNT_DEBUG
310
+ gpr_log(GPR_DEBUG, "FD %d %p create %s", fd, r, name);
311
+ #endif
312
+ return r;
313
+ }
314
+
315
+ static bool fd_is_orphaned(grpc_fd *fd) {
316
+ return (gpr_atm_acq_load(&fd->refst) & 1) == 0;
317
+ }
318
+
319
+ /* Return the read-notifier pollset */
320
+ static grpc_pollset *fd_get_read_notifier_pollset(grpc_exec_ctx *exec_ctx,
321
+ grpc_fd *fd) {
322
+ grpc_pollset *notifier = NULL;
323
+
324
+ gpr_mu_lock(&fd->mu);
325
+ notifier = fd->read_notifier_pollset;
326
+ gpr_mu_unlock(&fd->mu);
327
+
328
+ return notifier;
329
+ }
330
+
331
+ static grpc_error *pollset_kick_locked(grpc_fd_watcher *watcher) {
332
+ gpr_mu_lock(&watcher->pollset->mu);
333
+ GPR_ASSERT(watcher->worker);
334
+ grpc_error *err = pollset_kick_ext(watcher->pollset, watcher->worker,
335
+ GRPC_POLLSET_REEVALUATE_POLLING_ON_WAKEUP);
336
+ gpr_mu_unlock(&watcher->pollset->mu);
337
+ return err;
338
+ }
339
+
340
+ static void maybe_wake_one_watcher_locked(grpc_fd *fd) {
341
+ if (fd->inactive_watcher_root.next != &fd->inactive_watcher_root) {
342
+ pollset_kick_locked(fd->inactive_watcher_root.next);
343
+ } else if (fd->read_watcher) {
344
+ pollset_kick_locked(fd->read_watcher);
345
+ } else if (fd->write_watcher) {
346
+ pollset_kick_locked(fd->write_watcher);
347
+ }
348
+ }
349
+
350
+ static void wake_all_watchers_locked(grpc_fd *fd) {
351
+ grpc_fd_watcher *watcher;
352
+ for (watcher = fd->inactive_watcher_root.next;
353
+ watcher != &fd->inactive_watcher_root; watcher = watcher->next) {
354
+ pollset_kick_locked(watcher);
355
+ }
356
+ if (fd->read_watcher) {
357
+ pollset_kick_locked(fd->read_watcher);
358
+ }
359
+ if (fd->write_watcher && fd->write_watcher != fd->read_watcher) {
360
+ pollset_kick_locked(fd->write_watcher);
361
+ }
362
+ }
363
+
364
+ static int has_watchers(grpc_fd *fd) {
365
+ return fd->read_watcher != NULL || fd->write_watcher != NULL ||
366
+ fd->inactive_watcher_root.next != &fd->inactive_watcher_root;
367
+ }
368
+
369
+ static void close_fd_locked(grpc_exec_ctx *exec_ctx, grpc_fd *fd) {
370
+ fd->closed = 1;
371
+ if (!fd->released) {
372
+ close(fd->fd);
373
+ }
374
+ grpc_exec_ctx_sched(exec_ctx, fd->on_done_closure, GRPC_ERROR_NONE, NULL);
375
+ }
376
+
377
+ static int fd_wrapped_fd(grpc_fd *fd) {
378
+ if (fd->released || fd->closed) {
379
+ return -1;
380
+ } else {
381
+ return fd->fd;
382
+ }
383
+ }
384
+
385
+ static void fd_orphan(grpc_exec_ctx *exec_ctx, grpc_fd *fd,
386
+ grpc_closure *on_done, int *release_fd,
387
+ const char *reason) {
388
+ fd->on_done_closure = on_done;
389
+ fd->released = release_fd != NULL;
390
+ if (!fd->released) {
391
+ shutdown(fd->fd, SHUT_RDWR);
392
+ } else {
393
+ *release_fd = fd->fd;
394
+ }
395
+ gpr_mu_lock(&fd->mu);
396
+ REF_BY(fd, 1, reason); /* remove active status, but keep referenced */
397
+ if (!has_watchers(fd)) {
398
+ close_fd_locked(exec_ctx, fd);
399
+ } else {
400
+ wake_all_watchers_locked(fd);
401
+ }
402
+ gpr_mu_unlock(&fd->mu);
403
+ UNREF_BY(fd, 2, reason); /* drop the reference */
404
+ }
405
+
406
+ /* increment refcount by two to avoid changing the orphan bit */
407
+ #ifdef GRPC_FD_REF_COUNT_DEBUG
408
+ static void fd_ref(grpc_fd *fd, const char *reason, const char *file,
409
+ int line) {
410
+ ref_by(fd, 2, reason, file, line);
411
+ }
412
+
413
+ static void fd_unref(grpc_fd *fd, const char *reason, const char *file,
414
+ int line) {
415
+ unref_by(fd, 2, reason, file, line);
416
+ }
417
+ #else
418
+ static void fd_ref(grpc_fd *fd) { ref_by(fd, 2); }
419
+
420
+ static void fd_unref(grpc_fd *fd) { unref_by(fd, 2); }
421
+ #endif
422
+
423
+ static grpc_error *fd_shutdown_error(bool shutdown) {
424
+ if (!shutdown) {
425
+ return GRPC_ERROR_NONE;
426
+ } else {
427
+ return GRPC_ERROR_CREATE("FD shutdown");
428
+ }
429
+ }
430
+
431
+ static void notify_on_locked(grpc_exec_ctx *exec_ctx, grpc_fd *fd,
432
+ grpc_closure **st, grpc_closure *closure) {
433
+ if (fd->shutdown) {
434
+ grpc_exec_ctx_sched(exec_ctx, closure, GRPC_ERROR_CREATE("FD shutdown"),
435
+ NULL);
436
+ } else if (*st == CLOSURE_NOT_READY) {
437
+ /* not ready ==> switch to a waiting state by setting the closure */
438
+ *st = closure;
439
+ } else if (*st == CLOSURE_READY) {
440
+ /* already ready ==> queue the closure to run immediately */
441
+ *st = CLOSURE_NOT_READY;
442
+ grpc_exec_ctx_sched(exec_ctx, closure, fd_shutdown_error(fd->shutdown),
443
+ NULL);
444
+ maybe_wake_one_watcher_locked(fd);
445
+ } else {
446
+ /* upcallptr was set to a different closure. This is an error! */
447
+ gpr_log(GPR_ERROR,
448
+ "User called a notify_on function with a previous callback still "
449
+ "pending");
450
+ abort();
451
+ }
452
+ }
453
+
454
+ /* returns 1 if state becomes not ready */
455
+ static int set_ready_locked(grpc_exec_ctx *exec_ctx, grpc_fd *fd,
456
+ grpc_closure **st) {
457
+ if (*st == CLOSURE_READY) {
458
+ /* duplicate ready ==> ignore */
459
+ return 0;
460
+ } else if (*st == CLOSURE_NOT_READY) {
461
+ /* not ready, and not waiting ==> flag ready */
462
+ *st = CLOSURE_READY;
463
+ return 0;
464
+ } else {
465
+ /* waiting ==> queue closure */
466
+ grpc_exec_ctx_sched(exec_ctx, *st, fd_shutdown_error(fd->shutdown), NULL);
467
+ *st = CLOSURE_NOT_READY;
468
+ return 1;
469
+ }
470
+ }
471
+
472
+ static void set_read_notifier_pollset_locked(
473
+ grpc_exec_ctx *exec_ctx, grpc_fd *fd, grpc_pollset *read_notifier_pollset) {
474
+ fd->read_notifier_pollset = read_notifier_pollset;
475
+ }
476
+
477
+ static void fd_shutdown(grpc_exec_ctx *exec_ctx, grpc_fd *fd) {
478
+ gpr_mu_lock(&fd->mu);
479
+ /* only shutdown once */
480
+ if (!fd->shutdown) {
481
+ fd->shutdown = 1;
482
+ /* signal read/write closed to OS so that future operations fail */
483
+ shutdown(fd->fd, SHUT_RDWR);
484
+ set_ready_locked(exec_ctx, fd, &fd->read_closure);
485
+ set_ready_locked(exec_ctx, fd, &fd->write_closure);
486
+ }
487
+ gpr_mu_unlock(&fd->mu);
488
+ }
489
+
490
+ static bool fd_is_shutdown(grpc_fd *fd) {
491
+ gpr_mu_lock(&fd->mu);
492
+ bool r = fd->shutdown;
493
+ gpr_mu_unlock(&fd->mu);
494
+ return r;
495
+ }
496
+
497
+ static void fd_notify_on_read(grpc_exec_ctx *exec_ctx, grpc_fd *fd,
498
+ grpc_closure *closure) {
499
+ gpr_mu_lock(&fd->mu);
500
+ notify_on_locked(exec_ctx, fd, &fd->read_closure, closure);
501
+ gpr_mu_unlock(&fd->mu);
502
+ }
503
+
504
+ static void fd_notify_on_write(grpc_exec_ctx *exec_ctx, grpc_fd *fd,
505
+ grpc_closure *closure) {
506
+ gpr_mu_lock(&fd->mu);
507
+ notify_on_locked(exec_ctx, fd, &fd->write_closure, closure);
508
+ gpr_mu_unlock(&fd->mu);
509
+ }
510
+
511
+ static uint32_t fd_begin_poll(grpc_fd *fd, grpc_pollset *pollset,
512
+ grpc_pollset_worker *worker, uint32_t read_mask,
513
+ uint32_t write_mask, grpc_fd_watcher *watcher) {
514
+ uint32_t mask = 0;
515
+ grpc_closure *cur;
516
+ int requested;
517
+ /* keep track of pollers that have requested our events, in case they change
518
+ */
519
+ GRPC_FD_REF(fd, "poll");
520
+
521
+ gpr_mu_lock(&fd->mu);
522
+
523
+ /* if we are shutdown, then don't add to the watcher set */
524
+ if (fd->shutdown) {
525
+ watcher->fd = NULL;
526
+ watcher->pollset = NULL;
527
+ watcher->worker = NULL;
528
+ gpr_mu_unlock(&fd->mu);
529
+ GRPC_FD_UNREF(fd, "poll");
530
+ return 0;
531
+ }
532
+
533
+ /* if there is nobody polling for read, but we need to, then start doing so */
534
+ cur = fd->read_closure;
535
+ requested = cur != CLOSURE_READY;
536
+ if (read_mask && fd->read_watcher == NULL && requested) {
537
+ fd->read_watcher = watcher;
538
+ mask |= read_mask;
539
+ }
540
+ /* if there is nobody polling for write, but we need to, then start doing so
541
+ */
542
+ cur = fd->write_closure;
543
+ requested = cur != CLOSURE_READY;
544
+ if (write_mask && fd->write_watcher == NULL && requested) {
545
+ fd->write_watcher = watcher;
546
+ mask |= write_mask;
547
+ }
548
+ /* if not polling, remember this watcher in case we need someone to later */
549
+ if (mask == 0 && worker != NULL) {
550
+ watcher->next = &fd->inactive_watcher_root;
551
+ watcher->prev = watcher->next->prev;
552
+ watcher->next->prev = watcher->prev->next = watcher;
553
+ }
554
+ watcher->pollset = pollset;
555
+ watcher->worker = worker;
556
+ watcher->fd = fd;
557
+ gpr_mu_unlock(&fd->mu);
558
+
559
+ return mask;
560
+ }
561
+
562
+ static void fd_end_poll(grpc_exec_ctx *exec_ctx, grpc_fd_watcher *watcher,
563
+ int got_read, int got_write,
564
+ grpc_pollset *read_notifier_pollset) {
565
+ int was_polling = 0;
566
+ int kick = 0;
567
+ grpc_fd *fd = watcher->fd;
568
+
569
+ if (fd == NULL) {
570
+ return;
571
+ }
572
+
573
+ gpr_mu_lock(&fd->mu);
574
+
575
+ if (watcher == fd->read_watcher) {
576
+ /* remove read watcher, kick if we still need a read */
577
+ was_polling = 1;
578
+ if (!got_read) {
579
+ kick = 1;
580
+ }
581
+ fd->read_watcher = NULL;
582
+ }
583
+ if (watcher == fd->write_watcher) {
584
+ /* remove write watcher, kick if we still need a write */
585
+ was_polling = 1;
586
+ if (!got_write) {
587
+ kick = 1;
588
+ }
589
+ fd->write_watcher = NULL;
590
+ }
591
+ if (!was_polling && watcher->worker != NULL) {
592
+ /* remove from inactive list */
593
+ watcher->next->prev = watcher->prev;
594
+ watcher->prev->next = watcher->next;
595
+ }
596
+ if (got_read) {
597
+ if (set_ready_locked(exec_ctx, fd, &fd->read_closure)) {
598
+ kick = 1;
599
+ }
600
+ if (read_notifier_pollset != NULL) {
601
+ set_read_notifier_pollset_locked(exec_ctx, fd, read_notifier_pollset);
602
+ }
603
+ }
604
+ if (got_write) {
605
+ if (set_ready_locked(exec_ctx, fd, &fd->write_closure)) {
606
+ kick = 1;
607
+ }
608
+ }
609
+ if (kick) {
610
+ maybe_wake_one_watcher_locked(fd);
611
+ }
612
+ if (fd_is_orphaned(fd) && !has_watchers(fd) && !fd->closed) {
613
+ close_fd_locked(exec_ctx, fd);
614
+ }
615
+ gpr_mu_unlock(&fd->mu);
616
+
617
+ GRPC_FD_UNREF(fd, "poll");
618
+ }
619
+
620
+ /*******************************************************************************
621
+ * pollset_posix.c
622
+ */
623
+
624
+ GPR_TLS_DECL(g_current_thread_poller);
625
+ GPR_TLS_DECL(g_current_thread_worker);
626
+
627
+ static void remove_worker(grpc_pollset *p, grpc_pollset_worker *worker) {
628
+ worker->prev->next = worker->next;
629
+ worker->next->prev = worker->prev;
630
+ }
631
+
632
+ static int pollset_has_workers(grpc_pollset *p) {
633
+ return p->root_worker.next != &p->root_worker;
634
+ }
635
+
636
+ static grpc_pollset_worker *pop_front_worker(grpc_pollset *p) {
637
+ if (pollset_has_workers(p)) {
638
+ grpc_pollset_worker *w = p->root_worker.next;
639
+ remove_worker(p, w);
640
+ return w;
641
+ } else {
642
+ return NULL;
643
+ }
644
+ }
645
+
646
+ static void push_back_worker(grpc_pollset *p, grpc_pollset_worker *worker) {
647
+ worker->next = &p->root_worker;
648
+ worker->prev = worker->next->prev;
649
+ worker->prev->next = worker->next->prev = worker;
650
+ }
651
+
652
+ static void push_front_worker(grpc_pollset *p, grpc_pollset_worker *worker) {
653
+ worker->prev = &p->root_worker;
654
+ worker->next = worker->prev->next;
655
+ worker->prev->next = worker->next->prev = worker;
656
+ }
657
+
658
+ static void kick_append_error(grpc_error **composite, grpc_error *error) {
659
+ if (error == GRPC_ERROR_NONE) return;
660
+ if (*composite == GRPC_ERROR_NONE) {
661
+ *composite = GRPC_ERROR_CREATE("Kick Failure");
662
+ }
663
+ *composite = grpc_error_add_child(*composite, error);
664
+ }
665
+
666
+ static grpc_error *pollset_kick_ext(grpc_pollset *p,
667
+ grpc_pollset_worker *specific_worker,
668
+ uint32_t flags) {
669
+ GPR_TIMER_BEGIN("pollset_kick_ext", 0);
670
+ grpc_error *error = GRPC_ERROR_NONE;
671
+
672
+ /* pollset->mu already held */
673
+ if (specific_worker != NULL) {
674
+ if (specific_worker == GRPC_POLLSET_KICK_BROADCAST) {
675
+ GPR_TIMER_BEGIN("pollset_kick_ext.broadcast", 0);
676
+ GPR_ASSERT((flags & GRPC_POLLSET_REEVALUATE_POLLING_ON_WAKEUP) == 0);
677
+ for (specific_worker = p->root_worker.next;
678
+ specific_worker != &p->root_worker;
679
+ specific_worker = specific_worker->next) {
680
+ kick_append_error(
681
+ &error, grpc_wakeup_fd_wakeup(&specific_worker->wakeup_fd->fd));
682
+ }
683
+ p->kicked_without_pollers = true;
684
+ GPR_TIMER_END("pollset_kick_ext.broadcast", 0);
685
+ } else if (gpr_tls_get(&g_current_thread_worker) !=
686
+ (intptr_t)specific_worker) {
687
+ GPR_TIMER_MARK("different_thread_worker", 0);
688
+ if ((flags & GRPC_POLLSET_REEVALUATE_POLLING_ON_WAKEUP) != 0) {
689
+ specific_worker->reevaluate_polling_on_wakeup = true;
690
+ }
691
+ specific_worker->kicked_specifically = true;
692
+ kick_append_error(&error,
693
+ grpc_wakeup_fd_wakeup(&specific_worker->wakeup_fd->fd));
694
+ } else if ((flags & GRPC_POLLSET_CAN_KICK_SELF) != 0) {
695
+ GPR_TIMER_MARK("kick_yoself", 0);
696
+ if ((flags & GRPC_POLLSET_REEVALUATE_POLLING_ON_WAKEUP) != 0) {
697
+ specific_worker->reevaluate_polling_on_wakeup = true;
698
+ }
699
+ specific_worker->kicked_specifically = true;
700
+ kick_append_error(&error,
701
+ grpc_wakeup_fd_wakeup(&specific_worker->wakeup_fd->fd));
702
+ }
703
+ } else if (gpr_tls_get(&g_current_thread_poller) != (intptr_t)p) {
704
+ GPR_ASSERT((flags & GRPC_POLLSET_REEVALUATE_POLLING_ON_WAKEUP) == 0);
705
+ GPR_TIMER_MARK("kick_anonymous", 0);
706
+ specific_worker = pop_front_worker(p);
707
+ if (specific_worker != NULL) {
708
+ if (gpr_tls_get(&g_current_thread_worker) == (intptr_t)specific_worker) {
709
+ GPR_TIMER_MARK("kick_anonymous_not_self", 0);
710
+ push_back_worker(p, specific_worker);
711
+ specific_worker = pop_front_worker(p);
712
+ if ((flags & GRPC_POLLSET_CAN_KICK_SELF) == 0 &&
713
+ gpr_tls_get(&g_current_thread_worker) ==
714
+ (intptr_t)specific_worker) {
715
+ push_back_worker(p, specific_worker);
716
+ specific_worker = NULL;
717
+ }
718
+ }
719
+ if (specific_worker != NULL) {
720
+ GPR_TIMER_MARK("finally_kick", 0);
721
+ push_back_worker(p, specific_worker);
722
+ kick_append_error(
723
+ &error, grpc_wakeup_fd_wakeup(&specific_worker->wakeup_fd->fd));
724
+ }
725
+ } else {
726
+ GPR_TIMER_MARK("kicked_no_pollers", 0);
727
+ p->kicked_without_pollers = true;
728
+ }
729
+ }
730
+
731
+ GPR_TIMER_END("pollset_kick_ext", 0);
732
+ GRPC_LOG_IF_ERROR("pollset_kick_ext", GRPC_ERROR_REF(error));
733
+ return error;
734
+ }
735
+
736
+ static grpc_error *pollset_kick(grpc_pollset *p,
737
+ grpc_pollset_worker *specific_worker) {
738
+ return pollset_kick_ext(p, specific_worker, 0);
739
+ }
740
+
741
+ /* global state management */
742
+
743
+ static grpc_error *pollset_global_init(void) {
744
+ gpr_tls_init(&g_current_thread_poller);
745
+ gpr_tls_init(&g_current_thread_worker);
746
+ return grpc_wakeup_fd_init(&grpc_global_wakeup_fd);
747
+ }
748
+
749
+ static void pollset_global_shutdown(void) {
750
+ grpc_wakeup_fd_destroy(&grpc_global_wakeup_fd);
751
+ gpr_tls_destroy(&g_current_thread_poller);
752
+ gpr_tls_destroy(&g_current_thread_worker);
753
+ }
754
+
755
+ static grpc_error *kick_poller(void) {
756
+ return grpc_wakeup_fd_wakeup(&grpc_global_wakeup_fd);
757
+ }
758
+
759
+ /* main interface */
760
+
761
+ static void pollset_init(grpc_pollset *pollset, gpr_mu **mu) {
762
+ gpr_mu_init(&pollset->mu);
763
+ *mu = &pollset->mu;
764
+ pollset->root_worker.next = pollset->root_worker.prev = &pollset->root_worker;
765
+ pollset->shutting_down = 0;
766
+ pollset->called_shutdown = 0;
767
+ pollset->kicked_without_pollers = 0;
768
+ pollset->idle_jobs.head = pollset->idle_jobs.tail = NULL;
769
+ pollset->local_wakeup_cache = NULL;
770
+ pollset->kicked_without_pollers = 0;
771
+ pollset->fd_count = 0;
772
+ pollset->fd_capacity = 0;
773
+ pollset->fds = NULL;
774
+ }
775
+
776
+ static void pollset_destroy(grpc_pollset *pollset) {
777
+ GPR_ASSERT(!pollset_has_workers(pollset));
778
+ GPR_ASSERT(pollset->idle_jobs.head == pollset->idle_jobs.tail);
779
+ while (pollset->local_wakeup_cache) {
780
+ grpc_cached_wakeup_fd *next = pollset->local_wakeup_cache->next;
781
+ grpc_wakeup_fd_destroy(&pollset->local_wakeup_cache->fd);
782
+ gpr_free(pollset->local_wakeup_cache);
783
+ pollset->local_wakeup_cache = next;
784
+ }
785
+ gpr_free(pollset->fds);
786
+ gpr_mu_destroy(&pollset->mu);
787
+ }
788
+
789
+ static void pollset_reset(grpc_pollset *pollset) {
790
+ GPR_ASSERT(pollset->shutting_down);
791
+ GPR_ASSERT(!pollset_has_workers(pollset));
792
+ GPR_ASSERT(pollset->idle_jobs.head == pollset->idle_jobs.tail);
793
+ GPR_ASSERT(pollset->fd_count == 0);
794
+ pollset->shutting_down = 0;
795
+ pollset->called_shutdown = 0;
796
+ pollset->kicked_without_pollers = 0;
797
+ }
798
+
799
+ static void pollset_add_fd(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
800
+ grpc_fd *fd) {
801
+ gpr_mu_lock(&pollset->mu);
802
+ size_t i;
803
+ /* TODO(ctiller): this is O(num_fds^2); maybe switch to a hash set here */
804
+ for (i = 0; i < pollset->fd_count; i++) {
805
+ if (pollset->fds[i] == fd) goto exit;
806
+ }
807
+ if (pollset->fd_count == pollset->fd_capacity) {
808
+ pollset->fd_capacity =
809
+ GPR_MAX(pollset->fd_capacity + 8, pollset->fd_count * 3 / 2);
810
+ pollset->fds =
811
+ gpr_realloc(pollset->fds, sizeof(grpc_fd *) * pollset->fd_capacity);
812
+ }
813
+ pollset->fds[pollset->fd_count++] = fd;
814
+ GRPC_FD_REF(fd, "multipoller");
815
+ pollset_kick(pollset, NULL);
816
+ exit:
817
+ gpr_mu_unlock(&pollset->mu);
818
+ }
819
+
820
+ static void finish_shutdown(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset) {
821
+ GPR_ASSERT(grpc_closure_list_empty(pollset->idle_jobs));
822
+ size_t i;
823
+ for (i = 0; i < pollset->fd_count; i++) {
824
+ GRPC_FD_UNREF(pollset->fds[i], "multipoller");
825
+ }
826
+ pollset->fd_count = 0;
827
+ grpc_exec_ctx_sched(exec_ctx, pollset->shutdown_done, GRPC_ERROR_NONE, NULL);
828
+ }
829
+
830
+ static void work_combine_error(grpc_error **composite, grpc_error *error) {
831
+ if (error == GRPC_ERROR_NONE) return;
832
+ if (*composite == GRPC_ERROR_NONE) {
833
+ *composite = GRPC_ERROR_CREATE("pollset_work");
834
+ }
835
+ *composite = grpc_error_add_child(*composite, error);
836
+ }
837
+
838
+ static grpc_error *pollset_work(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
839
+ grpc_pollset_worker **worker_hdl,
840
+ gpr_timespec now, gpr_timespec deadline) {
841
+ grpc_pollset_worker worker;
842
+ *worker_hdl = &worker;
843
+ grpc_error *error = GRPC_ERROR_NONE;
844
+
845
+ /* pollset->mu already held */
846
+ int added_worker = 0;
847
+ int locked = 1;
848
+ int queued_work = 0;
849
+ int keep_polling = 0;
850
+ GPR_TIMER_BEGIN("pollset_work", 0);
851
+ /* this must happen before we (potentially) drop pollset->mu */
852
+ worker.next = worker.prev = NULL;
853
+ worker.reevaluate_polling_on_wakeup = 0;
854
+ if (pollset->local_wakeup_cache != NULL) {
855
+ worker.wakeup_fd = pollset->local_wakeup_cache;
856
+ pollset->local_wakeup_cache = worker.wakeup_fd->next;
857
+ } else {
858
+ worker.wakeup_fd = gpr_malloc(sizeof(*worker.wakeup_fd));
859
+ error = grpc_wakeup_fd_init(&worker.wakeup_fd->fd);
860
+ if (error != GRPC_ERROR_NONE) {
861
+ GRPC_LOG_IF_ERROR("pollset_work", GRPC_ERROR_REF(error));
862
+ return error;
863
+ }
864
+ }
865
+ worker.kicked_specifically = 0;
866
+ /* If there's work waiting for the pollset to be idle, and the
867
+ pollset is idle, then do that work */
868
+ if (!pollset_has_workers(pollset) &&
869
+ !grpc_closure_list_empty(pollset->idle_jobs)) {
870
+ GPR_TIMER_MARK("pollset_work.idle_jobs", 0);
871
+ grpc_exec_ctx_enqueue_list(exec_ctx, &pollset->idle_jobs, NULL);
872
+ goto done;
873
+ }
874
+ /* If we're shutting down then we don't execute any extended work */
875
+ if (pollset->shutting_down) {
876
+ GPR_TIMER_MARK("pollset_work.shutting_down", 0);
877
+ goto done;
878
+ }
879
+ /* Start polling, and keep doing so while we're being asked to
880
+ re-evaluate our pollers (this allows poll() based pollers to
881
+ ensure they don't miss wakeups) */
882
+ keep_polling = 1;
883
+ gpr_tls_set(&g_current_thread_poller, (intptr_t)pollset);
884
+ while (keep_polling) {
885
+ keep_polling = 0;
886
+ if (!pollset->kicked_without_pollers) {
887
+ if (!added_worker) {
888
+ push_front_worker(pollset, &worker);
889
+ added_worker = 1;
890
+ gpr_tls_set(&g_current_thread_worker, (intptr_t)&worker);
891
+ }
892
+ GPR_TIMER_BEGIN("maybe_work_and_unlock", 0);
893
+ #define POLLOUT_CHECK (POLLOUT | POLLHUP | POLLERR)
894
+ #define POLLIN_CHECK (POLLIN | POLLHUP | POLLERR)
895
+
896
+ int timeout;
897
+ int r;
898
+ size_t i, fd_count;
899
+ nfds_t pfd_count;
900
+ /* TODO(ctiller): inline some elements to avoid an allocation */
901
+ grpc_fd_watcher *watchers;
902
+ struct pollfd *pfds;
903
+
904
+ timeout = poll_deadline_to_millis_timeout(deadline, now);
905
+ /* TODO(ctiller): perform just one malloc here if we exceed the inline
906
+ * case */
907
+ pfds = gpr_malloc(sizeof(*pfds) * (pollset->fd_count + 2));
908
+ watchers = gpr_malloc(sizeof(*watchers) * (pollset->fd_count + 2));
909
+ fd_count = 0;
910
+ pfd_count = 2;
911
+ pfds[0].fd = GRPC_WAKEUP_FD_GET_READ_FD(&grpc_global_wakeup_fd);
912
+ pfds[0].events = POLLIN;
913
+ pfds[0].revents = 0;
914
+ pfds[1].fd = GRPC_WAKEUP_FD_GET_READ_FD(&worker.wakeup_fd->fd);
915
+ pfds[1].events = POLLIN;
916
+ pfds[1].revents = 0;
917
+ for (i = 0; i < pollset->fd_count; i++) {
918
+ if (fd_is_orphaned(pollset->fds[i])) {
919
+ GRPC_FD_UNREF(pollset->fds[i], "multipoller");
920
+ } else {
921
+ pollset->fds[fd_count++] = pollset->fds[i];
922
+ watchers[pfd_count].fd = pollset->fds[i];
923
+ GRPC_FD_REF(watchers[pfd_count].fd, "multipoller_start");
924
+ pfds[pfd_count].fd = pollset->fds[i]->fd;
925
+ pfds[pfd_count].revents = 0;
926
+ pfd_count++;
927
+ }
928
+ }
929
+ pollset->fd_count = fd_count;
930
+ gpr_mu_unlock(&pollset->mu);
931
+
932
+ for (i = 2; i < pfd_count; i++) {
933
+ grpc_fd *fd = watchers[i].fd;
934
+ pfds[i].events = (short)fd_begin_poll(fd, pollset, &worker, POLLIN,
935
+ POLLOUT, &watchers[i]);
936
+ GRPC_FD_UNREF(fd, "multipoller_start");
937
+ }
938
+
939
+ /* TODO(vpai): Consider first doing a 0 timeout poll here to avoid
940
+ even going into the blocking annotation if possible */
941
+ GRPC_SCHEDULING_START_BLOCKING_REGION;
942
+ r = grpc_poll_function(pfds, pfd_count, timeout);
943
+ GRPC_SCHEDULING_END_BLOCKING_REGION;
944
+
945
+ if (r < 0) {
946
+ if (errno != EINTR) {
947
+ work_combine_error(&error, GRPC_OS_ERROR(errno, "poll"));
948
+ }
949
+ for (i = 2; i < pfd_count; i++) {
950
+ fd_end_poll(exec_ctx, &watchers[i], 0, 0, NULL);
951
+ }
952
+ } else if (r == 0) {
953
+ for (i = 2; i < pfd_count; i++) {
954
+ fd_end_poll(exec_ctx, &watchers[i], 0, 0, NULL);
955
+ }
956
+ } else {
957
+ if (pfds[0].revents & POLLIN_CHECK) {
958
+ work_combine_error(
959
+ &error, grpc_wakeup_fd_consume_wakeup(&grpc_global_wakeup_fd));
960
+ }
961
+ if (pfds[1].revents & POLLIN_CHECK) {
962
+ work_combine_error(
963
+ &error, grpc_wakeup_fd_consume_wakeup(&worker.wakeup_fd->fd));
964
+ }
965
+ for (i = 2; i < pfd_count; i++) {
966
+ if (watchers[i].fd == NULL) {
967
+ fd_end_poll(exec_ctx, &watchers[i], 0, 0, NULL);
968
+ } else {
969
+ fd_end_poll(exec_ctx, &watchers[i], pfds[i].revents & POLLIN_CHECK,
970
+ pfds[i].revents & POLLOUT_CHECK, pollset);
971
+ }
972
+ }
973
+ }
974
+
975
+ gpr_free(pfds);
976
+ gpr_free(watchers);
977
+ GPR_TIMER_END("maybe_work_and_unlock", 0);
978
+ locked = 0;
979
+ } else {
980
+ GPR_TIMER_MARK("pollset_work.kicked_without_pollers", 0);
981
+ pollset->kicked_without_pollers = 0;
982
+ }
983
+ /* Finished execution - start cleaning up.
984
+ Note that we may arrive here from outside the enclosing while() loop.
985
+ In that case we won't loop though as we haven't added worker to the
986
+ worker list, which means nobody could ask us to re-evaluate polling). */
987
+ done:
988
+ if (!locked) {
989
+ queued_work |= grpc_exec_ctx_flush(exec_ctx);
990
+ gpr_mu_lock(&pollset->mu);
991
+ locked = 1;
992
+ }
993
+ /* If we're forced to re-evaluate polling (via pollset_kick with
994
+ GRPC_POLLSET_REEVALUATE_POLLING_ON_WAKEUP) then we land here and force
995
+ a loop */
996
+ if (worker.reevaluate_polling_on_wakeup && error == GRPC_ERROR_NONE) {
997
+ worker.reevaluate_polling_on_wakeup = 0;
998
+ pollset->kicked_without_pollers = 0;
999
+ if (queued_work || worker.kicked_specifically) {
1000
+ /* If there's queued work on the list, then set the deadline to be
1001
+ immediate so we get back out of the polling loop quickly */
1002
+ deadline = gpr_inf_past(GPR_CLOCK_MONOTONIC);
1003
+ }
1004
+ keep_polling = 1;
1005
+ }
1006
+ if (keep_polling) {
1007
+ now = gpr_now(now.clock_type);
1008
+ }
1009
+ }
1010
+ gpr_tls_set(&g_current_thread_poller, 0);
1011
+ if (added_worker) {
1012
+ remove_worker(pollset, &worker);
1013
+ gpr_tls_set(&g_current_thread_worker, 0);
1014
+ }
1015
+ /* release wakeup fd to the local pool */
1016
+ worker.wakeup_fd->next = pollset->local_wakeup_cache;
1017
+ pollset->local_wakeup_cache = worker.wakeup_fd;
1018
+ /* check shutdown conditions */
1019
+ if (pollset->shutting_down) {
1020
+ if (pollset_has_workers(pollset)) {
1021
+ pollset_kick(pollset, NULL);
1022
+ } else if (!pollset->called_shutdown) {
1023
+ pollset->called_shutdown = 1;
1024
+ gpr_mu_unlock(&pollset->mu);
1025
+ finish_shutdown(exec_ctx, pollset);
1026
+ grpc_exec_ctx_flush(exec_ctx);
1027
+ /* Continuing to access pollset here is safe -- it is the caller's
1028
+ * responsibility to not destroy when it has outstanding calls to
1029
+ * pollset_work.
1030
+ * TODO(dklempner): Can we refactor the shutdown logic to avoid this? */
1031
+ gpr_mu_lock(&pollset->mu);
1032
+ } else if (!grpc_closure_list_empty(pollset->idle_jobs)) {
1033
+ grpc_exec_ctx_enqueue_list(exec_ctx, &pollset->idle_jobs, NULL);
1034
+ gpr_mu_unlock(&pollset->mu);
1035
+ grpc_exec_ctx_flush(exec_ctx);
1036
+ gpr_mu_lock(&pollset->mu);
1037
+ }
1038
+ }
1039
+ *worker_hdl = NULL;
1040
+ GPR_TIMER_END("pollset_work", 0);
1041
+ GRPC_LOG_IF_ERROR("pollset_work", GRPC_ERROR_REF(error));
1042
+ return error;
1043
+ }
1044
+
1045
+ static void pollset_shutdown(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
1046
+ grpc_closure *closure) {
1047
+ GPR_ASSERT(!pollset->shutting_down);
1048
+ pollset->shutting_down = 1;
1049
+ pollset->shutdown_done = closure;
1050
+ pollset_kick(pollset, GRPC_POLLSET_KICK_BROADCAST);
1051
+ if (!pollset_has_workers(pollset)) {
1052
+ grpc_exec_ctx_enqueue_list(exec_ctx, &pollset->idle_jobs, NULL);
1053
+ }
1054
+ if (!pollset->called_shutdown && !pollset_has_workers(pollset)) {
1055
+ pollset->called_shutdown = 1;
1056
+ finish_shutdown(exec_ctx, pollset);
1057
+ }
1058
+ }
1059
+
1060
+ static int poll_deadline_to_millis_timeout(gpr_timespec deadline,
1061
+ gpr_timespec now) {
1062
+ gpr_timespec timeout;
1063
+ static const int64_t max_spin_polling_us = 10;
1064
+ if (gpr_time_cmp(deadline, gpr_inf_future(deadline.clock_type)) == 0) {
1065
+ return -1;
1066
+ }
1067
+ if (gpr_time_cmp(deadline, gpr_time_add(now, gpr_time_from_micros(
1068
+ max_spin_polling_us,
1069
+ GPR_TIMESPAN))) <= 0) {
1070
+ return 0;
1071
+ }
1072
+ timeout = gpr_time_sub(deadline, now);
1073
+ return gpr_time_to_millis(gpr_time_add(
1074
+ timeout, gpr_time_from_nanos(GPR_NS_PER_MS - 1, GPR_TIMESPAN)));
1075
+ }
1076
+
1077
+ /*******************************************************************************
1078
+ * pollset_set_posix.c
1079
+ */
1080
+
1081
+ static grpc_pollset_set *pollset_set_create(void) {
1082
+ grpc_pollset_set *pollset_set = gpr_malloc(sizeof(*pollset_set));
1083
+ memset(pollset_set, 0, sizeof(*pollset_set));
1084
+ gpr_mu_init(&pollset_set->mu);
1085
+ return pollset_set;
1086
+ }
1087
+
1088
+ static void pollset_set_destroy(grpc_pollset_set *pollset_set) {
1089
+ size_t i;
1090
+ gpr_mu_destroy(&pollset_set->mu);
1091
+ for (i = 0; i < pollset_set->fd_count; i++) {
1092
+ GRPC_FD_UNREF(pollset_set->fds[i], "pollset_set");
1093
+ }
1094
+ gpr_free(pollset_set->pollsets);
1095
+ gpr_free(pollset_set->pollset_sets);
1096
+ gpr_free(pollset_set->fds);
1097
+ gpr_free(pollset_set);
1098
+ }
1099
+
1100
+ static void pollset_set_add_pollset(grpc_exec_ctx *exec_ctx,
1101
+ grpc_pollset_set *pollset_set,
1102
+ grpc_pollset *pollset) {
1103
+ size_t i, j;
1104
+ gpr_mu_lock(&pollset_set->mu);
1105
+ if (pollset_set->pollset_count == pollset_set->pollset_capacity) {
1106
+ pollset_set->pollset_capacity =
1107
+ GPR_MAX(8, 2 * pollset_set->pollset_capacity);
1108
+ pollset_set->pollsets =
1109
+ gpr_realloc(pollset_set->pollsets, pollset_set->pollset_capacity *
1110
+ sizeof(*pollset_set->pollsets));
1111
+ }
1112
+ pollset_set->pollsets[pollset_set->pollset_count++] = pollset;
1113
+ for (i = 0, j = 0; i < pollset_set->fd_count; i++) {
1114
+ if (fd_is_orphaned(pollset_set->fds[i])) {
1115
+ GRPC_FD_UNREF(pollset_set->fds[i], "pollset_set");
1116
+ } else {
1117
+ pollset_add_fd(exec_ctx, pollset, pollset_set->fds[i]);
1118
+ pollset_set->fds[j++] = pollset_set->fds[i];
1119
+ }
1120
+ }
1121
+ pollset_set->fd_count = j;
1122
+ gpr_mu_unlock(&pollset_set->mu);
1123
+ }
1124
+
1125
+ static void pollset_set_del_pollset(grpc_exec_ctx *exec_ctx,
1126
+ grpc_pollset_set *pollset_set,
1127
+ grpc_pollset *pollset) {
1128
+ size_t i;
1129
+ gpr_mu_lock(&pollset_set->mu);
1130
+ for (i = 0; i < pollset_set->pollset_count; i++) {
1131
+ if (pollset_set->pollsets[i] == pollset) {
1132
+ pollset_set->pollset_count--;
1133
+ GPR_SWAP(grpc_pollset *, pollset_set->pollsets[i],
1134
+ pollset_set->pollsets[pollset_set->pollset_count]);
1135
+ break;
1136
+ }
1137
+ }
1138
+ gpr_mu_unlock(&pollset_set->mu);
1139
+ }
1140
+
1141
+ static void pollset_set_add_pollset_set(grpc_exec_ctx *exec_ctx,
1142
+ grpc_pollset_set *bag,
1143
+ grpc_pollset_set *item) {
1144
+ size_t i, j;
1145
+ gpr_mu_lock(&bag->mu);
1146
+ if (bag->pollset_set_count == bag->pollset_set_capacity) {
1147
+ bag->pollset_set_capacity = GPR_MAX(8, 2 * bag->pollset_set_capacity);
1148
+ bag->pollset_sets =
1149
+ gpr_realloc(bag->pollset_sets,
1150
+ bag->pollset_set_capacity * sizeof(*bag->pollset_sets));
1151
+ }
1152
+ bag->pollset_sets[bag->pollset_set_count++] = item;
1153
+ for (i = 0, j = 0; i < bag->fd_count; i++) {
1154
+ if (fd_is_orphaned(bag->fds[i])) {
1155
+ GRPC_FD_UNREF(bag->fds[i], "pollset_set");
1156
+ } else {
1157
+ pollset_set_add_fd(exec_ctx, item, bag->fds[i]);
1158
+ bag->fds[j++] = bag->fds[i];
1159
+ }
1160
+ }
1161
+ bag->fd_count = j;
1162
+ gpr_mu_unlock(&bag->mu);
1163
+ }
1164
+
1165
+ static void pollset_set_del_pollset_set(grpc_exec_ctx *exec_ctx,
1166
+ grpc_pollset_set *bag,
1167
+ grpc_pollset_set *item) {
1168
+ size_t i;
1169
+ gpr_mu_lock(&bag->mu);
1170
+ for (i = 0; i < bag->pollset_set_count; i++) {
1171
+ if (bag->pollset_sets[i] == item) {
1172
+ bag->pollset_set_count--;
1173
+ GPR_SWAP(grpc_pollset_set *, bag->pollset_sets[i],
1174
+ bag->pollset_sets[bag->pollset_set_count]);
1175
+ break;
1176
+ }
1177
+ }
1178
+ gpr_mu_unlock(&bag->mu);
1179
+ }
1180
+
1181
+ static void pollset_set_add_fd(grpc_exec_ctx *exec_ctx,
1182
+ grpc_pollset_set *pollset_set, grpc_fd *fd) {
1183
+ size_t i;
1184
+ gpr_mu_lock(&pollset_set->mu);
1185
+ if (pollset_set->fd_count == pollset_set->fd_capacity) {
1186
+ pollset_set->fd_capacity = GPR_MAX(8, 2 * pollset_set->fd_capacity);
1187
+ pollset_set->fds = gpr_realloc(
1188
+ pollset_set->fds, pollset_set->fd_capacity * sizeof(*pollset_set->fds));
1189
+ }
1190
+ GRPC_FD_REF(fd, "pollset_set");
1191
+ pollset_set->fds[pollset_set->fd_count++] = fd;
1192
+ for (i = 0; i < pollset_set->pollset_count; i++) {
1193
+ pollset_add_fd(exec_ctx, pollset_set->pollsets[i], fd);
1194
+ }
1195
+ for (i = 0; i < pollset_set->pollset_set_count; i++) {
1196
+ pollset_set_add_fd(exec_ctx, pollset_set->pollset_sets[i], fd);
1197
+ }
1198
+ gpr_mu_unlock(&pollset_set->mu);
1199
+ }
1200
+
1201
+ static void pollset_set_del_fd(grpc_exec_ctx *exec_ctx,
1202
+ grpc_pollset_set *pollset_set, grpc_fd *fd) {
1203
+ size_t i;
1204
+ gpr_mu_lock(&pollset_set->mu);
1205
+ for (i = 0; i < pollset_set->fd_count; i++) {
1206
+ if (pollset_set->fds[i] == fd) {
1207
+ pollset_set->fd_count--;
1208
+ GPR_SWAP(grpc_fd *, pollset_set->fds[i],
1209
+ pollset_set->fds[pollset_set->fd_count]);
1210
+ GRPC_FD_UNREF(fd, "pollset_set");
1211
+ break;
1212
+ }
1213
+ }
1214
+ for (i = 0; i < pollset_set->pollset_set_count; i++) {
1215
+ pollset_set_del_fd(exec_ctx, pollset_set->pollset_sets[i], fd);
1216
+ }
1217
+ gpr_mu_unlock(&pollset_set->mu);
1218
+ }
1219
+
1220
+ /*******************************************************************************
1221
+ * event engine binding
1222
+ */
1223
+
1224
+ static void shutdown_engine(void) { pollset_global_shutdown(); }
1225
+
1226
+ static const grpc_event_engine_vtable vtable = {
1227
+ .pollset_size = sizeof(grpc_pollset),
1228
+
1229
+ .fd_create = fd_create,
1230
+ .fd_wrapped_fd = fd_wrapped_fd,
1231
+ .fd_orphan = fd_orphan,
1232
+ .fd_shutdown = fd_shutdown,
1233
+ .fd_is_shutdown = fd_is_shutdown,
1234
+ .fd_notify_on_read = fd_notify_on_read,
1235
+ .fd_notify_on_write = fd_notify_on_write,
1236
+ .fd_get_read_notifier_pollset = fd_get_read_notifier_pollset,
1237
+
1238
+ .pollset_init = pollset_init,
1239
+ .pollset_shutdown = pollset_shutdown,
1240
+ .pollset_reset = pollset_reset,
1241
+ .pollset_destroy = pollset_destroy,
1242
+ .pollset_work = pollset_work,
1243
+ .pollset_kick = pollset_kick,
1244
+ .pollset_add_fd = pollset_add_fd,
1245
+
1246
+ .pollset_set_create = pollset_set_create,
1247
+ .pollset_set_destroy = pollset_set_destroy,
1248
+ .pollset_set_add_pollset = pollset_set_add_pollset,
1249
+ .pollset_set_del_pollset = pollset_set_del_pollset,
1250
+ .pollset_set_add_pollset_set = pollset_set_add_pollset_set,
1251
+ .pollset_set_del_pollset_set = pollset_set_del_pollset_set,
1252
+ .pollset_set_add_fd = pollset_set_add_fd,
1253
+ .pollset_set_del_fd = pollset_set_del_fd,
1254
+
1255
+ .kick_poller = kick_poller,
1256
+
1257
+ .shutdown_engine = shutdown_engine,
1258
+ };
1259
+
1260
+ const grpc_event_engine_vtable *grpc_init_poll_posix(void) {
1261
+ if (!GRPC_LOG_IF_ERROR("pollset_global_init", pollset_global_init())) {
1262
+ return NULL;
1263
+ }
1264
+ return &vtable;
1265
+ }
1266
+
1267
+ #endif