polling_entity.cc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. #include <grpc/support/port_platform.h>
  19. #include <grpc/support/alloc.h>
  20. #include <grpc/support/log.h>
  21. #include "src/core/lib/iomgr/polling_entity.h"
  22. grpc_polling_entity grpc_polling_entity_create_from_pollset_set(
  23. grpc_pollset_set* pollset_set) {
  24. grpc_polling_entity pollent;
  25. pollent.pollent.pollset_set = pollset_set;
  26. pollent.tag = GRPC_POLLS_POLLSET_SET;
  27. return pollent;
  28. }
  29. grpc_polling_entity grpc_polling_entity_create_from_pollset(
  30. grpc_pollset* pollset) {
  31. grpc_polling_entity pollent;
  32. pollent.pollent.pollset = pollset;
  33. pollent.tag = GRPC_POLLS_POLLSET;
  34. return pollent;
  35. }
  36. grpc_pollset* grpc_polling_entity_pollset(grpc_polling_entity* pollent) {
  37. if (pollent->tag == GRPC_POLLS_POLLSET) {
  38. return pollent->pollent.pollset;
  39. }
  40. return nullptr;
  41. }
  42. grpc_pollset_set* grpc_polling_entity_pollset_set(
  43. grpc_polling_entity* pollent) {
  44. if (pollent->tag == GRPC_POLLS_POLLSET_SET) {
  45. return pollent->pollent.pollset_set;
  46. }
  47. return nullptr;
  48. }
  49. bool grpc_polling_entity_is_empty(const grpc_polling_entity* pollent) {
  50. return pollent->tag == GRPC_POLLS_NONE;
  51. }
  52. void grpc_polling_entity_add_to_pollset_set(grpc_polling_entity* pollent,
  53. grpc_pollset_set* pss_dst) {
  54. if (pollent->tag == GRPC_POLLS_POLLSET) {
  55. // CFStream does not use file destriptors. When CFStream is used, the fd
  56. // pollset is possible to be null.
  57. if (pollent->pollent.pollset != nullptr) {
  58. grpc_pollset_set_add_pollset(pss_dst, pollent->pollent.pollset);
  59. }
  60. } else if (pollent->tag == GRPC_POLLS_POLLSET_SET) {
  61. GPR_ASSERT(pollent->pollent.pollset_set != nullptr);
  62. grpc_pollset_set_add_pollset_set(pss_dst, pollent->pollent.pollset_set);
  63. } else {
  64. gpr_log(GPR_ERROR, "Invalid grpc_polling_entity tag '%d'", pollent->tag);
  65. abort();
  66. }
  67. }
  68. void grpc_polling_entity_del_from_pollset_set(grpc_polling_entity* pollent,
  69. grpc_pollset_set* pss_dst) {
  70. if (pollent->tag == GRPC_POLLS_POLLSET) {
  71. #ifdef GRPC_CFSTREAM
  72. if (pollent->pollent.pollset != nullptr) {
  73. grpc_pollset_set_del_pollset(pss_dst, pollent->pollent.pollset);
  74. }
  75. #else
  76. GPR_ASSERT(pollent->pollent.pollset != nullptr);
  77. grpc_pollset_set_del_pollset(pss_dst, pollent->pollent.pollset);
  78. #endif
  79. } else if (pollent->tag == GRPC_POLLS_POLLSET_SET) {
  80. GPR_ASSERT(pollent->pollent.pollset_set != nullptr);
  81. grpc_pollset_set_del_pollset_set(pss_dst, pollent->pollent.pollset_set);
  82. } else {
  83. gpr_log(GPR_ERROR, "Invalid grpc_polling_entity tag '%d'", pollent->tag);
  84. abort();
  85. }
  86. }