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