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 4677 : void grpc_pollset_set_init(grpc_pollset_set *pollset_set) {
47 4677 : memset(pollset_set, 0, sizeof(*pollset_set));
48 4677 : gpr_mu_init(&pollset_set->mu);
49 4677 : }
50 :
51 4676 : void grpc_pollset_set_destroy(grpc_pollset_set *pollset_set) {
52 : size_t i;
53 4676 : gpr_mu_destroy(&pollset_set->mu);
54 6085 : for (i = 0; i < pollset_set->fd_count; i++) {
55 1409 : GRPC_FD_UNREF(pollset_set->fds[i], "pollset");
56 : }
57 4676 : gpr_free(pollset_set->pollsets);
58 4676 : gpr_free(pollset_set->fds);
59 4676 : }
60 :
61 13975 : 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 13975 : gpr_mu_lock(&pollset_set->mu);
66 13975 : if (pollset_set->pollset_count == pollset_set->pollset_capacity) {
67 4384 : pollset_set->pollset_capacity =
68 4384 : GPR_MAX(8, 2 * pollset_set->pollset_capacity);
69 4384 : pollset_set->pollsets =
70 4384 : gpr_realloc(pollset_set->pollsets, pollset_set->pollset_capacity *
71 : sizeof(*pollset_set->pollsets));
72 : }
73 13975 : pollset_set->pollsets[pollset_set->pollset_count++] = pollset;
74 24206 : for (i = 0, j = 0; i < pollset_set->fd_count; i++) {
75 10231 : if (grpc_fd_is_orphaned(pollset_set->fds[i])) {
76 139 : GRPC_FD_UNREF(pollset_set->fds[i], "pollset");
77 : } else {
78 10092 : grpc_pollset_add_fd(exec_ctx, pollset, pollset_set->fds[i]);
79 10092 : pollset_set->fds[j++] = pollset_set->fds[i];
80 : }
81 : }
82 13975 : pollset_set->fd_count = j;
83 13975 : gpr_mu_unlock(&pollset_set->mu);
84 13975 : }
85 :
86 13757 : 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 13757 : gpr_mu_lock(&pollset_set->mu);
91 19148 : for (i = 0; i < pollset_set->pollset_count; i++) {
92 19047 : if (pollset_set->pollsets[i] == pollset) {
93 13874 : pollset_set->pollset_count--;
94 13874 : GPR_SWAP(grpc_pollset *, pollset_set->pollsets[i],
95 : pollset_set->pollsets[pollset_set->pollset_count]);
96 13874 : break;
97 : }
98 : }
99 13975 : gpr_mu_unlock(&pollset_set->mu);
100 13915 : }
101 :
102 6042 : 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 6042 : gpr_mu_lock(&pollset_set->mu);
106 6042 : if (pollset_set->fd_count == pollset_set->fd_capacity) {
107 4290 : pollset_set->fd_capacity = GPR_MAX(8, 2 * pollset_set->fd_capacity);
108 8580 : pollset_set->fds = gpr_realloc(
109 8580 : pollset_set->fds, pollset_set->fd_capacity * sizeof(*pollset_set->fds));
110 : }
111 6042 : GRPC_FD_REF(fd, "pollset_set");
112 6048 : pollset_set->fds[pollset_set->fd_count++] = fd;
113 19256 : for (i = 0; i < pollset_set->pollset_count; i++) {
114 13212 : grpc_pollset_add_fd(exec_ctx, pollset_set->pollsets[i], fd);
115 : }
116 6044 : gpr_mu_unlock(&pollset_set->mu);
117 6044 : }
118 :
119 4496 : 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 4496 : gpr_mu_lock(&pollset_set->mu);
123 4830 : for (i = 0; i < pollset_set->fd_count; i++) {
124 4830 : if (pollset_set->fds[i] == fd) {
125 4496 : pollset_set->fd_count--;
126 4496 : GPR_SWAP(grpc_fd *, pollset_set->fds[i],
127 : pollset_set->fds[pollset_set->fd_count]);
128 4496 : GRPC_FD_UNREF(fd, "pollset_set");
129 4496 : break;
130 : }
131 : }
132 4496 : gpr_mu_unlock(&pollset_set->mu);
133 4496 : }
134 :
135 : #endif /* GPR_POSIX_SOCKET */
|