blob: 6182eb353289a283e360d5fb620a2656e14e9603 [file] [log] [blame]
Craig Tillerf2d39b72015-01-21 15:06:08 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, Google Inc.
Craig Tillerf2d39b72015-01-21 15:06:08 -08004 * 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
Craig Tillerd14a1a52015-01-21 15:26:29 -080034#include <grpc/support/port_platform.h>
Craig Tillerf2d39b72015-01-21 15:06:08 -080035
Nicolas "Pixel" Noble21f627a2015-02-04 01:31:14 +010036#ifdef GPR_WINSOCK_SOCKET
Craig Tillerf2d39b72015-01-21 15:06:08 -080037
Nicolas "Pixel" Noble21f627a2015-02-04 01:31:14 +010038#include <grpc/support/thd.h>
39
40#include "src/core/iomgr/alarm_internal.h"
Nicolas "Pixel" Noble21f627a2015-02-04 01:31:14 +010041#include "src/core/iomgr/iomgr_internal.h"
Nicolas "Pixel" Noblefcb16e12015-06-24 23:45:30 +020042#include "src/core/iomgr/pollset.h"
Nicolas "Pixel" Noble21f627a2015-02-04 01:31:14 +010043#include "src/core/iomgr/pollset_windows.h"
44
Craig Tillera82950e2015-09-22 12:33:20 -070045static void remove_worker(grpc_pollset *p, grpc_pollset_worker *worker) {
Craig Tillerd1885e02015-07-30 14:32:31 -070046 worker->prev->next = worker->next;
47 worker->next->prev = worker->prev;
48}
49
Craig Tillera82950e2015-09-22 12:33:20 -070050static int has_workers(grpc_pollset *p) {
Craig Tillerd1885e02015-07-30 14:32:31 -070051 return p->root_worker.next != &p->root_worker;
52}
53
Craig Tillera82950e2015-09-22 12:33:20 -070054static grpc_pollset_worker *pop_front_worker(grpc_pollset *p) {
55 if (has_workers(p)) {
56 grpc_pollset_worker *w = p->root_worker.next;
57 remove_worker(p, w);
58 return w;
59 } else {
60 return NULL;
61 }
Craig Tillerd1885e02015-07-30 14:32:31 -070062}
63
Craig Tillera82950e2015-09-22 12:33:20 -070064static void push_back_worker(grpc_pollset *p, grpc_pollset_worker *worker) {
Craig Tillerd1885e02015-07-30 14:32:31 -070065 worker->next = &p->root_worker;
66 worker->prev = worker->next->prev;
67 worker->prev->next = worker->next->prev = worker;
68}
69
Craig Tillera82950e2015-09-22 12:33:20 -070070static void push_front_worker(grpc_pollset *p, grpc_pollset_worker *worker) {
Craig Tillerd1885e02015-07-30 14:32:31 -070071 worker->prev = &p->root_worker;
72 worker->next = worker->prev->next;
73 worker->prev->next = worker->next->prev = worker;
74}
75
Nicolas "Pixel" Noble0f981e92015-05-03 10:40:56 +020076/* There isn't really any such thing as a pollset under Windows, due to the
77 nature of the IO completion ports. We're still going to provide a minimal
78 set of features for the sake of the rest of grpc. But grpc_pollset_work
79 won't actually do any polling, and return as quickly as possible. */
80
Craig Tillera82950e2015-09-22 12:33:20 -070081void grpc_pollset_init(grpc_pollset *pollset) {
82 memset(pollset, 0, sizeof(*pollset));
83 gpr_mu_init(&pollset->mu);
Craig Tillerd1885e02015-07-30 14:32:31 -070084 pollset->root_worker.next = pollset->root_worker.prev = &pollset->root_worker;
85 pollset->kicked_without_pollers = 0;
Nicolas Noblea14ebbb2015-06-22 14:17:34 -070086}
Nicolas "Pixel" Noble21f627a2015-02-04 01:31:14 +010087
Craig Tiller82f9bd82015-09-23 09:31:51 -070088void grpc_pollset_shutdown(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
89 grpc_closure *closure) {
Craig Tillera82950e2015-09-22 12:33:20 -070090 gpr_mu_lock(&pollset->mu);
Nicolas "Pixel" Noble857d2502015-06-25 21:58:18 +020091 pollset->shutting_down = 1;
Craig Tillera82950e2015-09-22 12:33:20 -070092 grpc_pollset_kick(pollset, GRPC_POLLSET_KICK_BROADCAST);
93 gpr_mu_unlock(&pollset->mu);
Craig Tiller82f9bd82015-09-23 09:31:51 -070094 grpc_exec_ctx_enqueue(exec_ctx, closure, 1);
David Klempnerb5056612015-02-24 14:22:50 -080095}
96
Craig Tillera82950e2015-09-22 12:33:20 -070097void grpc_pollset_destroy(grpc_pollset *pollset) {
98 gpr_mu_destroy(&pollset->mu);
Nicolas "Pixel" Noble21f627a2015-02-04 01:31:14 +010099}
100
Craig Tiller565b18b2015-09-23 10:09:42 -0700101void grpc_pollset_work(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
102 grpc_pollset_worker *worker, gpr_timespec now,
103 gpr_timespec deadline) {
Craig Tillerd1885e02015-07-30 14:32:31 -0700104 int added_worker = 0;
Craig Tillerd1885e02015-07-30 14:32:31 -0700105 worker->next = worker->prev = NULL;
Craig Tillera82950e2015-09-22 12:33:20 -0700106 gpr_cv_init(&worker->cv);
Craig Tiller82f9bd82015-09-23 09:31:51 -0700107 if (grpc_alarm_check(exec_ctx, now, &deadline)) {
Craig Tillera82950e2015-09-22 12:33:20 -0700108 goto done;
109 }
110 if (!pollset->kicked_without_pollers && !pollset->shutting_down) {
111 push_front_worker(pollset, worker);
112 added_worker = 1;
113 gpr_cv_wait(&worker->cv, &pollset->mu, deadline);
114 } else {
115 pollset->kicked_without_pollers = 0;
116 }
Craig Tillerd1885e02015-07-30 14:32:31 -0700117done:
Craig Tiller258f8de2015-09-23 12:32:40 -0700118 if (!grpc_closure_list_empty(exec_ctx->closure_list)) {
119 gpr_mu_unlock(&pollset->mu);
120 grpc_exec_ctx_flush(exec_ctx);
121 gpr_mu_lock(&pollset->mu);
122 }
Craig Tillera82950e2015-09-22 12:33:20 -0700123 gpr_cv_destroy(&worker->cv);
124 if (added_worker) {
125 remove_worker(pollset, worker);
126 }
Nicolas "Pixel" Noble21f627a2015-02-04 01:31:14 +0100127}
128
Craig Tillera82950e2015-09-22 12:33:20 -0700129void grpc_pollset_kick(grpc_pollset *p, grpc_pollset_worker *specific_worker) {
130 if (specific_worker != NULL) {
131 if (specific_worker == GRPC_POLLSET_KICK_BROADCAST) {
132 for (specific_worker = p->root_worker.next;
133 specific_worker != &p->root_worker;
134 specific_worker = specific_worker->next) {
135 gpr_cv_signal(&specific_worker->cv);
136 }
137 p->kicked_without_pollers = 1;
138 } else {
139 gpr_cv_signal(&specific_worker->cv);
Craig Tillerd1885e02015-07-30 14:32:31 -0700140 }
Craig Tillera82950e2015-09-22 12:33:20 -0700141 } else {
142 specific_worker = pop_front_worker(p);
143 if (specific_worker != NULL) {
144 push_back_worker(p, specific_worker);
145 gpr_cv_signal(&specific_worker->cv);
146 } else {
147 p->kicked_without_pollers = 1;
Craig Tillerd1885e02015-07-30 14:32:31 -0700148 }
Craig Tillera82950e2015-09-22 12:33:20 -0700149 }
Craig Tillerd1885e02015-07-30 14:32:31 -0700150}
Nicolas "Pixel" Noble21f627a2015-02-04 01:31:14 +0100151
Craig Tiller8674cb12015-06-05 07:09:25 -0700152#endif /* GPR_WINSOCK_SOCKET */