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