Craig Tiller | f2d39b7 | 2015-01-21 15:06:08 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Craig Tiller | f2d39b7 | 2015-01-21 15:06:08 -0800 | [diff] [blame] | 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 | |
Craig Tiller | d14a1a5 | 2015-01-21 15:26:29 -0800 | [diff] [blame] | 34 | #include <grpc/support/port_platform.h> |
Craig Tiller | f2d39b7 | 2015-01-21 15:06:08 -0800 | [diff] [blame] | 35 | |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 36 | #ifdef GPR_WINSOCK_SOCKET |
Craig Tiller | f2d39b7 | 2015-01-21 15:06:08 -0800 | [diff] [blame] | 37 | |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 38 | #include <grpc/support/thd.h> |
| 39 | |
| 40 | #include "src/core/iomgr/alarm_internal.h" |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 41 | #include "src/core/iomgr/iomgr_internal.h" |
Nicolas "Pixel" Noble | fcb16e1 | 2015-06-24 23:45:30 +0200 | [diff] [blame] | 42 | #include "src/core/iomgr/pollset.h" |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 43 | #include "src/core/iomgr/pollset_windows.h" |
| 44 | |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 45 | /* 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 Noble | a14ebbb | 2015-06-22 14:17:34 -0700 | [diff] [blame] | 50 | void grpc_pollset_init(grpc_pollset *pollset) { |
Nicolas "Pixel" Noble | 857d250 | 2015-06-25 21:58:18 +0200 | [diff] [blame^] | 51 | memset(pollset, 0, sizeof(*pollset)); |
Nicolas Noble | a14ebbb | 2015-06-22 14:17:34 -0700 | [diff] [blame] | 52 | gpr_mu_init(&pollset->mu); |
| 53 | gpr_cv_init(&pollset->cv); |
| 54 | } |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 55 | |
David Klempner | b505661 | 2015-02-24 14:22:50 -0800 | [diff] [blame] | 56 | void grpc_pollset_shutdown(grpc_pollset *pollset, |
| 57 | void (*shutdown_done)(void *arg), |
| 58 | void *shutdown_done_arg) { |
Nicolas "Pixel" Noble | 857d250 | 2015-06-25 21:58:18 +0200 | [diff] [blame^] | 59 | gpr_mu_lock(&pollset->mu); |
| 60 | pollset->shutting_down = 1; |
| 61 | gpr_cv_broadcast(&pollset->cv); |
| 62 | gpr_mu_unlock(&pollset->mu); |
David Klempner | b505661 | 2015-02-24 14:22:50 -0800 | [diff] [blame] | 63 | shutdown_done(shutdown_done_arg); |
| 64 | } |
| 65 | |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 66 | void grpc_pollset_destroy(grpc_pollset *pollset) { |
Nicolas "Pixel" Noble | ee0c96c | 2015-02-04 23:35:41 +0100 | [diff] [blame] | 67 | gpr_mu_destroy(&pollset->mu); |
Nicolas Noble | a14ebbb | 2015-06-22 14:17:34 -0700 | [diff] [blame] | 68 | gpr_cv_destroy(&pollset->cv); |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | int 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 Quintas | 1c762bd | 2015-05-31 17:04:43 -0700 | [diff] [blame] | 75 | return 0 /* GPR_FALSE */; |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 76 | } |
Nicolas "Pixel" Noble | 3857d23 | 2015-06-24 19:32:51 +0200 | [diff] [blame] | 77 | if (grpc_maybe_call_delayed_callbacks(&pollset->mu, 1 /* GPR_TRUE */)) { |
David Garcia Quintas | 1c762bd | 2015-05-31 17:04:43 -0700 | [diff] [blame] | 78 | return 1 /* GPR_TRUE */; |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 79 | } |
Nicolas "Pixel" Noble | 3857d23 | 2015-06-24 19:32:51 +0200 | [diff] [blame] | 80 | if (grpc_alarm_check(&pollset->mu, now, &deadline)) { |
David Garcia Quintas | 1c762bd | 2015-05-31 17:04:43 -0700 | [diff] [blame] | 81 | return 1 /* GPR_TRUE */; |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 82 | } |
Nicolas "Pixel" Noble | 857d250 | 2015-06-25 21:58:18 +0200 | [diff] [blame^] | 83 | if (!pollset->shutting_down) { |
| 84 | gpr_cv_wait(&pollset->cv, &pollset->mu, deadline); |
| 85 | } |
Nicolas Noble | a14ebbb | 2015-06-22 14:17:34 -0700 | [diff] [blame] | 86 | return 1 /* GPR_TRUE */; |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 87 | } |
| 88 | |
Nicolas Noble | a14ebbb | 2015-06-22 14:17:34 -0700 | [diff] [blame] | 89 | void grpc_pollset_kick(grpc_pollset *p) { |
| 90 | gpr_cv_signal(&p->cv); |
| 91 | } |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 92 | |
Craig Tiller | 8674cb1 | 2015-06-05 07:09:25 -0700 | [diff] [blame] | 93 | #endif /* GPR_WINSOCK_SOCKET */ |