Line data Source code
1 : /*
2 : *
3 : * Copyright 2015, 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/port_platform.h>
35 :
36 : #ifdef GPR_POSIX_SOCKET
37 :
38 : #include <stdlib.h>
39 : #include <string.h>
40 :
41 : #include <grpc/support/alloc.h>
42 : #include <grpc/support/useful.h>
43 :
44 : #include "src/core/iomgr/pollset_set.h"
45 :
46 7145 : void grpc_pollset_set_init(grpc_pollset_set *pollset_set) {
47 7145 : memset(pollset_set, 0, sizeof(*pollset_set));
48 7145 : gpr_mu_init(&pollset_set->mu);
49 7145 : }
50 :
51 7084 : void grpc_pollset_set_destroy(grpc_pollset_set *pollset_set) {
52 : size_t i;
53 7084 : gpr_mu_destroy(&pollset_set->mu);
54 9223 : for (i = 0; i < pollset_set->fd_count; i++) {
55 2139 : GRPC_FD_UNREF(pollset_set->fds[i], "pollset");
56 : }
57 7084 : gpr_free(pollset_set->pollsets);
58 7084 : gpr_free(pollset_set->fds);
59 7084 : }
60 :
61 15095 : void grpc_pollset_set_add_pollset(grpc_exec_ctx *exec_ctx,
62 : grpc_pollset_set *pollset_set,
63 : grpc_pollset *pollset) {
64 : size_t i, j;
65 15095 : gpr_mu_lock(&pollset_set->mu);
66 15095 : if (pollset_set->pollset_count == pollset_set->pollset_capacity) {
67 6586 : pollset_set->pollset_capacity =
68 6586 : GPR_MAX(8, 2 * pollset_set->pollset_capacity);
69 6586 : pollset_set->pollsets =
70 6586 : gpr_realloc(pollset_set->pollsets, pollset_set->pollset_capacity *
71 : sizeof(*pollset_set->pollsets));
72 : }
73 15095 : pollset_set->pollsets[pollset_set->pollset_count++] = pollset;
74 25759 : for (i = 0, j = 0; i < pollset_set->fd_count; i++) {
75 10664 : if (grpc_fd_is_orphaned(pollset_set->fds[i])) {
76 157 : GRPC_FD_UNREF(pollset_set->fds[i], "pollset");
77 : } else {
78 10507 : grpc_pollset_add_fd(exec_ctx, pollset, pollset_set->fds[i]);
79 10507 : pollset_set->fds[j++] = pollset_set->fds[i];
80 : }
81 : }
82 15095 : pollset_set->fd_count = j;
83 15095 : gpr_mu_unlock(&pollset_set->mu);
84 15095 : }
85 :
86 14435 : void grpc_pollset_set_del_pollset(grpc_exec_ctx *exec_ctx,
87 : grpc_pollset_set *pollset_set,
88 : grpc_pollset *pollset) {
89 : size_t i;
90 14435 : gpr_mu_lock(&pollset_set->mu);
91 20041 : for (i = 0; i < pollset_set->pollset_count; i++) {
92 20051 : if (pollset_set->pollsets[i] == pollset) {
93 14936 : pollset_set->pollset_count--;
94 14936 : GPR_SWAP(grpc_pollset *, pollset_set->pollsets[i],
95 : pollset_set->pollsets[pollset_set->pollset_count]);
96 14936 : break;
97 : }
98 : }
99 14926 : gpr_mu_unlock(&pollset_set->mu);
100 14971 : }
101 :
102 13652 : void grpc_pollset_set_add_fd(grpc_exec_ctx *exec_ctx,
103 : grpc_pollset_set *pollset_set, grpc_fd *fd) {
104 : size_t i;
105 13652 : gpr_mu_lock(&pollset_set->mu);
106 13652 : if (pollset_set->fd_count == pollset_set->fd_capacity) {
107 6563 : pollset_set->fd_capacity = GPR_MAX(8, 2 * pollset_set->fd_capacity);
108 13126 : pollset_set->fds = gpr_realloc(
109 13083 : pollset_set->fds, pollset_set->fd_capacity * sizeof(*pollset_set->fds));
110 : }
111 13652 : GRPC_FD_REF(fd, "pollset_set");
112 13652 : pollset_set->fds[pollset_set->fd_count++] = fd;
113 33233 : for (i = 0; i < pollset_set->pollset_count; i++) {
114 19581 : grpc_pollset_add_fd(exec_ctx, pollset_set->pollsets[i], fd);
115 : }
116 13652 : gpr_mu_unlock(&pollset_set->mu);
117 13652 : }
118 :
119 11263 : void grpc_pollset_set_del_fd(grpc_exec_ctx *exec_ctx,
120 : grpc_pollset_set *pollset_set, grpc_fd *fd) {
121 : size_t i;
122 11263 : gpr_mu_lock(&pollset_set->mu);
123 11650 : for (i = 0; i < pollset_set->fd_count; i++) {
124 11650 : if (pollset_set->fds[i] == fd) {
125 11263 : pollset_set->fd_count--;
126 11263 : GPR_SWAP(grpc_fd *, pollset_set->fds[i],
127 : pollset_set->fds[pollset_set->fd_count]);
128 11263 : GRPC_FD_UNREF(fd, "pollset_set");
129 11263 : break;
130 : }
131 : }
132 11263 : gpr_mu_unlock(&pollset_set->mu);
133 11263 : }
134 :
135 : #endif /* GPR_POSIX_SOCKET */
|