wakeup_fd_cv.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. *
  3. * Copyright 2016 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. /*
  19. * wakeup_fd_cv uses condition variables to implement wakeup fds.
  20. *
  21. * It is intended for use only in cases when eventfd() and pipe() are not
  22. * available. It can only be used with the "poll" engine.
  23. *
  24. * Implementation:
  25. * A global table of cv wakeup fds is mantained. A cv wakeup fd is a negative
  26. * file descriptor. poll() is then run in a background thread with only the
  27. * real socket fds while we wait on a condition variable trigged by either the
  28. * poll() completion or a wakeup_fd() call.
  29. *
  30. */
  31. #ifndef GRPC_CORE_LIB_IOMGR_WAKEUP_FD_CV_H
  32. #define GRPC_CORE_LIB_IOMGR_WAKEUP_FD_CV_H
  33. #include <grpc/support/port_platform.h>
  34. #include <grpc/support/sync.h>
  35. #include "src/core/lib/iomgr/ev_posix.h"
  36. #define GRPC_FD_TO_IDX(fd) (-(fd)-1)
  37. #define GRPC_IDX_TO_FD(idx) (-(idx)-1)
  38. typedef struct grpc_cv_node {
  39. gpr_cv* cv;
  40. struct grpc_cv_node* next;
  41. struct grpc_cv_node* prev;
  42. } grpc_cv_node;
  43. typedef struct grpc_fd_node {
  44. int is_set;
  45. grpc_cv_node* cvs;
  46. struct grpc_fd_node* next_free;
  47. } grpc_fd_node;
  48. typedef struct grpc_cv_fd_table {
  49. gpr_mu mu;
  50. gpr_refcount pollcount;
  51. gpr_cv shutdown_cv;
  52. grpc_fd_node* cvfds;
  53. grpc_fd_node* free_fds;
  54. unsigned int size;
  55. grpc_poll_function_type poll;
  56. } grpc_cv_fd_table;
  57. extern const grpc_wakeup_fd_vtable grpc_cv_wakeup_fd_vtable;
  58. #endif /* GRPC_CORE_LIB_IOMGR_WAKEUP_FD_CV_H */