ev_posix.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. *
  3. * Copyright 2015 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #ifndef GRPC_CORE_LIB_IOMGR_EV_POSIX_H
  19. #define GRPC_CORE_LIB_IOMGR_EV_POSIX_H
  20. #include <grpc/support/port_platform.h>
  21. #include <poll.h>
  22. #include "src/core/lib/debug/trace.h"
  23. #include "src/core/lib/iomgr/exec_ctx.h"
  24. #include "src/core/lib/iomgr/pollset.h"
  25. #include "src/core/lib/iomgr/pollset_set.h"
  26. #include "src/core/lib/iomgr/wakeup_fd_posix.h"
  27. extern grpc_core::TraceFlag grpc_fd_trace; /* Disabled by default */
  28. extern grpc_core::TraceFlag grpc_polling_trace; /* Disabled by default */
  29. #define GRPC_FD_TRACE(format, ...) \
  30. if (grpc_fd_trace.enabled()) { \
  31. gpr_log(GPR_INFO, "(fd-trace) " format, __VA_ARGS__); \
  32. }
  33. typedef struct grpc_fd grpc_fd;
  34. typedef struct grpc_event_engine_vtable {
  35. size_t pollset_size;
  36. bool can_track_err;
  37. grpc_fd* (*fd_create)(int fd, const char* name, bool track_err);
  38. int (*fd_wrapped_fd)(grpc_fd* fd);
  39. void (*fd_orphan)(grpc_fd* fd, grpc_closure* on_done, int* release_fd,
  40. const char* reason);
  41. void (*fd_shutdown)(grpc_fd* fd, grpc_error* why);
  42. void (*fd_notify_on_read)(grpc_fd* fd, grpc_closure* closure);
  43. void (*fd_notify_on_write)(grpc_fd* fd, grpc_closure* closure);
  44. void (*fd_notify_on_error)(grpc_fd* fd, grpc_closure* closure);
  45. bool (*fd_is_shutdown)(grpc_fd* fd);
  46. grpc_pollset* (*fd_get_read_notifier_pollset)(grpc_fd* fd);
  47. void (*pollset_init)(grpc_pollset* pollset, gpr_mu** mu);
  48. void (*pollset_shutdown)(grpc_pollset* pollset, grpc_closure* closure);
  49. void (*pollset_destroy)(grpc_pollset* pollset);
  50. grpc_error* (*pollset_work)(grpc_pollset* pollset,
  51. grpc_pollset_worker** worker,
  52. grpc_millis deadline);
  53. grpc_error* (*pollset_kick)(grpc_pollset* pollset,
  54. grpc_pollset_worker* specific_worker);
  55. void (*pollset_add_fd)(grpc_pollset* pollset, struct grpc_fd* fd);
  56. grpc_pollset_set* (*pollset_set_create)(void);
  57. void (*pollset_set_destroy)(grpc_pollset_set* pollset_set);
  58. void (*pollset_set_add_pollset)(grpc_pollset_set* pollset_set,
  59. grpc_pollset* pollset);
  60. void (*pollset_set_del_pollset)(grpc_pollset_set* pollset_set,
  61. grpc_pollset* pollset);
  62. void (*pollset_set_add_pollset_set)(grpc_pollset_set* bag,
  63. grpc_pollset_set* item);
  64. void (*pollset_set_del_pollset_set)(grpc_pollset_set* bag,
  65. grpc_pollset_set* item);
  66. void (*pollset_set_add_fd)(grpc_pollset_set* pollset_set, grpc_fd* fd);
  67. void (*pollset_set_del_fd)(grpc_pollset_set* pollset_set, grpc_fd* fd);
  68. void (*shutdown_engine)(void);
  69. } grpc_event_engine_vtable;
  70. void grpc_event_engine_init(void);
  71. void grpc_event_engine_shutdown(void);
  72. /* Return the name of the poll strategy */
  73. const char* grpc_get_poll_strategy_name();
  74. /* Returns true if polling engine can track errors separately, false otherwise.
  75. * If this is true, fd can be created with track_err set. After this, error
  76. * events will be reported using fd_notify_on_error. If it is not set, errors
  77. * will continue to be reported through fd_notify_on_read and
  78. * fd_notify_on_write.
  79. */
  80. bool grpc_event_engine_can_track_errors();
  81. /* Create a wrapped file descriptor.
  82. Requires fd is a non-blocking file descriptor.
  83. \a track_err if true means that error events would be tracked separately
  84. using grpc_fd_notify_on_error. Currently, valid only for linux systems.
  85. This takes ownership of closing fd. */
  86. grpc_fd* grpc_fd_create(int fd, const char* name, bool track_err);
  87. /* Return the wrapped fd, or -1 if it has been released or closed. */
  88. int grpc_fd_wrapped_fd(grpc_fd* fd);
  89. /* Releases fd to be asynchronously destroyed.
  90. on_done is called when the underlying file descriptor is definitely close()d.
  91. If on_done is NULL, no callback will be made.
  92. If release_fd is not NULL, it's set to fd and fd will not be closed.
  93. Requires: *fd initialized; no outstanding notify_on_read or
  94. notify_on_write.
  95. MUST NOT be called with a pollset lock taken */
  96. void grpc_fd_orphan(grpc_fd* fd, grpc_closure* on_done, int* release_fd,
  97. const char* reason);
  98. /* Has grpc_fd_shutdown been called on an fd? */
  99. bool grpc_fd_is_shutdown(grpc_fd* fd);
  100. /* Cause any current and future callbacks to fail. */
  101. void grpc_fd_shutdown(grpc_fd* fd, grpc_error* why);
  102. /* Register read interest, causing read_cb to be called once when fd becomes
  103. readable, on deadline specified by deadline, or on shutdown triggered by
  104. grpc_fd_shutdown.
  105. read_cb will be called with read_cb_arg when *fd becomes readable.
  106. read_cb is Called with status of GRPC_CALLBACK_SUCCESS if readable,
  107. GRPC_CALLBACK_TIMED_OUT if the call timed out,
  108. and CANCELLED if the call was cancelled.
  109. Requires:This method must not be called before the read_cb for any previous
  110. call runs. Edge triggered events are used whenever they are supported by the
  111. underlying platform. This means that users must drain fd in read_cb before
  112. calling notify_on_read again. Users are also expected to handle spurious
  113. events, i.e read_cb is called while nothing can be readable from fd */
  114. void grpc_fd_notify_on_read(grpc_fd* fd, grpc_closure* closure);
  115. /* Exactly the same semantics as above, except based on writable events. */
  116. void grpc_fd_notify_on_write(grpc_fd* fd, grpc_closure* closure);
  117. /* Exactly the same semantics as above, except based on error events. track_err
  118. * needs to have been set on grpc_fd_create */
  119. void grpc_fd_notify_on_error(grpc_fd* fd, grpc_closure* closure);
  120. /* Return the read notifier pollset from the fd */
  121. grpc_pollset* grpc_fd_get_read_notifier_pollset(grpc_fd* fd);
  122. /* pollset_posix functions */
  123. /* Add an fd to a pollset */
  124. void grpc_pollset_add_fd(grpc_pollset* pollset, struct grpc_fd* fd);
  125. /* pollset_set_posix functions */
  126. void grpc_pollset_set_add_fd(grpc_pollset_set* pollset_set, grpc_fd* fd);
  127. void grpc_pollset_set_del_fd(grpc_pollset_set* pollset_set, grpc_fd* fd);
  128. /* override to allow tests to hook poll() usage */
  129. typedef int (*grpc_poll_function_type)(struct pollfd*, nfds_t, int);
  130. extern grpc_poll_function_type grpc_poll_function;
  131. /* WARNING: The following two functions should be used for testing purposes
  132. * ONLY */
  133. void grpc_set_event_engine_test_only(const grpc_event_engine_vtable*);
  134. const grpc_event_engine_vtable* grpc_get_event_engine_test_only();
  135. #endif /* GRPC_CORE_LIB_IOMGR_EV_POSIX_H */