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) { |
| 51 | gpr_mu_init(&pollset->mu); |
| 52 | gpr_cv_init(&pollset->cv); |
| 53 | } |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 54 | |
David Klempner | b505661 | 2015-02-24 14:22:50 -0800 | [diff] [blame] | 55 | void grpc_pollset_shutdown(grpc_pollset *pollset, |
| 56 | void (*shutdown_done)(void *arg), |
| 57 | void *shutdown_done_arg) { |
Nicolas "Pixel" Noble | fcb16e1 | 2015-06-24 23:45:30 +0200 | [diff] [blame^] | 58 | grpc_pollset_kick(pollset); |
David Klempner | b505661 | 2015-02-24 14:22:50 -0800 | [diff] [blame] | 59 | shutdown_done(shutdown_done_arg); |
| 60 | } |
| 61 | |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 62 | void grpc_pollset_destroy(grpc_pollset *pollset) { |
Nicolas "Pixel" Noble | ee0c96c | 2015-02-04 23:35:41 +0100 | [diff] [blame] | 63 | gpr_mu_destroy(&pollset->mu); |
Nicolas Noble | a14ebbb | 2015-06-22 14:17:34 -0700 | [diff] [blame] | 64 | gpr_cv_destroy(&pollset->cv); |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | int grpc_pollset_work(grpc_pollset *pollset, gpr_timespec deadline) { |
| 68 | gpr_timespec now; |
| 69 | now = gpr_now(); |
| 70 | if (gpr_time_cmp(now, deadline) > 0) { |
David Garcia Quintas | 1c762bd | 2015-05-31 17:04:43 -0700 | [diff] [blame] | 71 | return 0 /* GPR_FALSE */; |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 72 | } |
David Garcia Quintas | 1c762bd | 2015-05-31 17:04:43 -0700 | [diff] [blame] | 73 | if (grpc_maybe_call_delayed_callbacks(NULL, 1 /* GPR_TRUE */)) { |
| 74 | return 1 /* GPR_TRUE */; |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 75 | } |
| 76 | if (grpc_alarm_check(NULL, now, &deadline)) { |
David Garcia Quintas | 1c762bd | 2015-05-31 17:04:43 -0700 | [diff] [blame] | 77 | return 1 /* GPR_TRUE */; |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 78 | } |
Nicolas Noble | a14ebbb | 2015-06-22 14:17:34 -0700 | [diff] [blame] | 79 | gpr_cv_wait(&pollset->cv, &pollset->mu, deadline); |
| 80 | return 1 /* GPR_TRUE */; |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 81 | } |
| 82 | |
Nicolas Noble | a14ebbb | 2015-06-22 14:17:34 -0700 | [diff] [blame] | 83 | void grpc_pollset_kick(grpc_pollset *p) { |
| 84 | gpr_cv_signal(&p->cv); |
| 85 | } |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 86 | |
Craig Tiller | 8674cb1 | 2015-06-05 07:09:25 -0700 | [diff] [blame] | 87 | #endif /* GPR_WINSOCK_SOCKET */ |