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 #include "src/core/lib/iomgr/polling_entity.h"
20
21 #include <grpc/support/alloc.h>
22 #include <grpc/support/port_platform.h>
23
24 #include "absl/log/check.h"
25 #include "absl/strings/str_format.h"
26 #include "src/core/util/crash.h"
27
grpc_polling_entity_create_from_pollset_set(grpc_pollset_set * pollset_set)28 grpc_polling_entity grpc_polling_entity_create_from_pollset_set(
29 grpc_pollset_set* pollset_set) {
30 grpc_polling_entity pollent;
31 if (pollset_set == nullptr) {
32 pollent.tag = GRPC_POLLS_NONE;
33 } else {
34 pollent.pollent.pollset_set = pollset_set;
35 pollent.tag = GRPC_POLLS_POLLSET_SET;
36 }
37 return pollent;
38 }
39
grpc_polling_entity_create_from_pollset(grpc_pollset * pollset)40 grpc_polling_entity grpc_polling_entity_create_from_pollset(
41 grpc_pollset* pollset) {
42 grpc_polling_entity pollent;
43 pollent.pollent.pollset = pollset;
44 pollent.tag = GRPC_POLLS_POLLSET;
45 return pollent;
46 }
47
grpc_polling_entity_pollset(grpc_polling_entity * pollent)48 grpc_pollset* grpc_polling_entity_pollset(grpc_polling_entity* pollent) {
49 if (pollent->tag == GRPC_POLLS_POLLSET) {
50 return pollent->pollent.pollset;
51 }
52 return nullptr;
53 }
54
grpc_polling_entity_pollset_set(grpc_polling_entity * pollent)55 grpc_pollset_set* grpc_polling_entity_pollset_set(
56 grpc_polling_entity* pollent) {
57 if (pollent->tag == GRPC_POLLS_POLLSET_SET) {
58 return pollent->pollent.pollset_set;
59 }
60 return nullptr;
61 }
62
grpc_polling_entity_is_empty(const grpc_polling_entity * pollent)63 bool grpc_polling_entity_is_empty(const grpc_polling_entity* pollent) {
64 return pollent->tag == GRPC_POLLS_NONE;
65 }
66
grpc_polling_entity_add_to_pollset_set(grpc_polling_entity * pollent,grpc_pollset_set * pss_dst)67 void grpc_polling_entity_add_to_pollset_set(grpc_polling_entity* pollent,
68 grpc_pollset_set* pss_dst) {
69 if (pollent->tag == GRPC_POLLS_POLLSET) {
70 // CFStream does not use file destriptors. When CFStream is used, the fd
71 // pollset is possible to be null.
72 if (pollent->pollent.pollset != nullptr) {
73 grpc_pollset_set_add_pollset(pss_dst, pollent->pollent.pollset);
74 }
75 } else if (pollent->tag == GRPC_POLLS_POLLSET_SET) {
76 CHECK_NE(pollent->pollent.pollset_set, nullptr);
77 grpc_pollset_set_add_pollset_set(pss_dst, pollent->pollent.pollset_set);
78 } else if (pollent->tag == GRPC_POLLS_NONE) {
79 // Do nothing.
80 } else {
81 grpc_core::Crash(
82 absl::StrFormat("Invalid grpc_polling_entity tag '%d'", pollent->tag));
83 }
84 }
85
grpc_polling_entity_del_from_pollset_set(grpc_polling_entity * pollent,grpc_pollset_set * pss_dst)86 void grpc_polling_entity_del_from_pollset_set(grpc_polling_entity* pollent,
87 grpc_pollset_set* pss_dst) {
88 if (pollent->tag == GRPC_POLLS_POLLSET) {
89 #ifdef GRPC_CFSTREAM
90 if (pollent->pollent.pollset != nullptr) {
91 grpc_pollset_set_del_pollset(pss_dst, pollent->pollent.pollset);
92 }
93 #else
94 CHECK_NE(pollent->pollent.pollset, nullptr);
95 grpc_pollset_set_del_pollset(pss_dst, pollent->pollent.pollset);
96 #endif
97 } else if (pollent->tag == GRPC_POLLS_POLLSET_SET) {
98 CHECK_NE(pollent->pollent.pollset_set, nullptr);
99 grpc_pollset_set_del_pollset_set(pss_dst, pollent->pollent.pollset_set);
100 } else if (pollent->tag == GRPC_POLLS_NONE) {
101 // Do nothing.
102 } else {
103 grpc_core::Crash(
104 absl::StrFormat("Invalid grpc_polling_entity tag '%d'", pollent->tag));
105 }
106 }
107
grpc_polling_entity_string(grpc_polling_entity * pollent)108 std::string grpc_polling_entity_string(grpc_polling_entity* pollent) {
109 if (pollent->tag == GRPC_POLLS_POLLSET) {
110 return absl::StrFormat("pollset:%p", pollent->pollent.pollset);
111 } else if (pollent->tag == GRPC_POLLS_POLLSET_SET) {
112 return absl::StrFormat("pollset_set:%p", pollent->pollent.pollset_set);
113 } else {
114 return absl::StrFormat("invalid_tag:%d", pollent->tag);
115 }
116 }
117