blob: d1686aa12f6be87a34f5ef37fec39423729cf554 [file] [log] [blame]
David Garcia Quintas2a50dfe2016-05-31 15:09:12 -07001/*
2 *
3 * Copyright 2016, Google Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34#include <grpc/support/alloc.h>
35#include <grpc/support/log.h>
36
37#include "src/core/lib/iomgr/polling_entity.h"
38
David Garcia Quintasc4d51122016-06-06 14:56:02 -070039grpc_polling_entity grpc_polling_entity_create_from_pollset_set(
David Garcia Quintas2a50dfe2016-05-31 15:09:12 -070040 grpc_pollset_set *pollset_set) {
41 grpc_polling_entity pollent;
42 pollent.pollent.pollset_set = pollset_set;
43 pollent.tag = POPS_POLLSET_SET;
44 return pollent;
45}
46
David Garcia Quintas69ff63d2016-06-06 16:39:47 -070047grpc_polling_entity grpc_polling_entity_create_from_pollset(
48 grpc_pollset *pollset) {
David Garcia Quintas2a50dfe2016-05-31 15:09:12 -070049 grpc_polling_entity pollent;
50 pollent.pollent.pollset = pollset;
51 pollent.tag = POPS_POLLSET;
52 return pollent;
53}
54
David Garcia Quintasc4d51122016-06-06 14:56:02 -070055grpc_pollset *grpc_polling_entity_pollset(grpc_polling_entity *pollent) {
David Garcia Quintas2a50dfe2016-05-31 15:09:12 -070056 if (pollent->tag == POPS_POLLSET) {
57 return pollent->pollent.pollset;
58 }
59 return NULL;
60}
61
David Garcia Quintas69ff63d2016-06-06 16:39:47 -070062grpc_pollset_set *grpc_polling_entity_pollset_set(
63 grpc_polling_entity *pollent) {
David Garcia Quintas2a50dfe2016-05-31 15:09:12 -070064 if (pollent->tag == POPS_POLLSET_SET) {
65 return pollent->pollent.pollset_set;
66 }
67 return NULL;
68}
69
David Garcia Quintasc4d51122016-06-06 14:56:02 -070070bool grpc_polling_entity_is_empty(const grpc_polling_entity *pollent) {
David Garcia Quintas2a50dfe2016-05-31 15:09:12 -070071 return pollent->tag == POPS_NONE;
72}
73
David Garcia Quintasc4d51122016-06-06 14:56:02 -070074void grpc_polling_entity_add_to_pollset_set(grpc_exec_ctx *exec_ctx,
David Garcia Quintas69ff63d2016-06-06 16:39:47 -070075 grpc_polling_entity *pollent,
76 grpc_pollset_set *pss_dst) {
David Garcia Quintas2a50dfe2016-05-31 15:09:12 -070077 if (pollent->tag == POPS_POLLSET) {
78 GPR_ASSERT(pollent->pollent.pollset != NULL);
79 grpc_pollset_set_add_pollset(exec_ctx, pss_dst, pollent->pollent.pollset);
80 } else if (pollent->tag == POPS_POLLSET_SET) {
81 GPR_ASSERT(pollent->pollent.pollset_set != NULL);
82 grpc_pollset_set_add_pollset_set(exec_ctx, pss_dst,
83 pollent->pollent.pollset_set);
84 } else {
85 gpr_log(GPR_ERROR, "Invalid grpc_polling_entity tag '%d'", pollent->tag);
86 abort();
87 }
88}
89
David Garcia Quintasc4d51122016-06-06 14:56:02 -070090void grpc_polling_entity_del_from_pollset_set(grpc_exec_ctx *exec_ctx,
David Garcia Quintas69ff63d2016-06-06 16:39:47 -070091 grpc_polling_entity *pollent,
92 grpc_pollset_set *pss_dst) {
David Garcia Quintas2a50dfe2016-05-31 15:09:12 -070093 if (pollent->tag == POPS_POLLSET) {
94 GPR_ASSERT(pollent->pollent.pollset != NULL);
95 grpc_pollset_set_del_pollset(exec_ctx, pss_dst, pollent->pollent.pollset);
96 } else if (pollent->tag == POPS_POLLSET_SET) {
97 GPR_ASSERT(pollent->pollent.pollset_set != NULL);
98 grpc_pollset_set_del_pollset_set(exec_ctx, pss_dst,
99 pollent->pollent.pollset_set);
100 } else {
101 gpr_log(GPR_ERROR, "Invalid grpc_polling_entity tag '%d'", pollent->tag);
102 abort();
103 }
104}