blob: 8d6bc79c96c65245f091c238767f4658d09bfac9 [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
Nicolas "Pixel" Noble0f981e92015-05-03 10:40:56 +020045/* There isn't really any such thing as a pollset under Windows, due to the
46 nature of the IO completion ports. We're still going to provide a minimal
47 set of features for the sake of the rest of grpc. But grpc_pollset_work
48 won't actually do any polling, and return as quickly as possible. */
49
Nicolas Noblea14ebbb2015-06-22 14:17:34 -070050void grpc_pollset_init(grpc_pollset *pollset) {
Nicolas "Pixel" Noble857d2502015-06-25 21:58:18 +020051 memset(pollset, 0, sizeof(*pollset));
Nicolas Noblea14ebbb2015-06-22 14:17:34 -070052 gpr_mu_init(&pollset->mu);
53 gpr_cv_init(&pollset->cv);
54}
Nicolas "Pixel" Noble21f627a2015-02-04 01:31:14 +010055
David Klempnerb5056612015-02-24 14:22:50 -080056void grpc_pollset_shutdown(grpc_pollset *pollset,
57 void (*shutdown_done)(void *arg),
58 void *shutdown_done_arg) {
Nicolas "Pixel" Noble857d2502015-06-25 21:58:18 +020059 gpr_mu_lock(&pollset->mu);
60 pollset->shutting_down = 1;
61 gpr_cv_broadcast(&pollset->cv);
62 gpr_mu_unlock(&pollset->mu);
David Klempnerb5056612015-02-24 14:22:50 -080063 shutdown_done(shutdown_done_arg);
64}
65
Nicolas "Pixel" Noble21f627a2015-02-04 01:31:14 +010066void grpc_pollset_destroy(grpc_pollset *pollset) {
Nicolas "Pixel" Nobleee0c96c2015-02-04 23:35:41 +010067 gpr_mu_destroy(&pollset->mu);
Nicolas Noblea14ebbb2015-06-22 14:17:34 -070068 gpr_cv_destroy(&pollset->cv);
Nicolas "Pixel" Noble21f627a2015-02-04 01:31:14 +010069}
70
71int grpc_pollset_work(grpc_pollset *pollset, gpr_timespec deadline) {
72 gpr_timespec now;
73 now = gpr_now();
74 if (gpr_time_cmp(now, deadline) > 0) {
David Garcia Quintas1c762bd2015-05-31 17:04:43 -070075 return 0 /* GPR_FALSE */;
Nicolas "Pixel" Noble21f627a2015-02-04 01:31:14 +010076 }
Nicolas "Pixel" Noble3857d232015-06-24 19:32:51 +020077 if (grpc_maybe_call_delayed_callbacks(&pollset->mu, 1 /* GPR_TRUE */)) {
David Garcia Quintas1c762bd2015-05-31 17:04:43 -070078 return 1 /* GPR_TRUE */;
Nicolas "Pixel" Noble21f627a2015-02-04 01:31:14 +010079 }
Nicolas "Pixel" Noble3857d232015-06-24 19:32:51 +020080 if (grpc_alarm_check(&pollset->mu, now, &deadline)) {
David Garcia Quintas1c762bd2015-05-31 17:04:43 -070081 return 1 /* GPR_TRUE */;
Nicolas "Pixel" Noble21f627a2015-02-04 01:31:14 +010082 }
Nicolas "Pixel" Noble857d2502015-06-25 21:58:18 +020083 if (!pollset->shutting_down) {
84 gpr_cv_wait(&pollset->cv, &pollset->mu, deadline);
85 }
Nicolas Noblea14ebbb2015-06-22 14:17:34 -070086 return 1 /* GPR_TRUE */;
Nicolas "Pixel" Noble21f627a2015-02-04 01:31:14 +010087}
88
Nicolas Noblea14ebbb2015-06-22 14:17:34 -070089void grpc_pollset_kick(grpc_pollset *p) {
90 gpr_cv_signal(&p->cv);
91}
Nicolas "Pixel" Noble21f627a2015-02-04 01:31:14 +010092
Craig Tiller8674cb12015-06-05 07:09:25 -070093#endif /* GPR_WINSOCK_SOCKET */