murgatroid99 | 749666e | 2015-01-12 18:25:58 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
murgatroid99 | 749666e | 2015-01-12 18:25:58 -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 | |
murgatroid99 | 013d203 | 2016-11-01 13:05:24 -0700 | [diff] [blame] | 34 | /* I don't like using #ifndef, but I don't see a better way to do this */ |
| 35 | #ifndef GRPC_UV |
| 36 | |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 37 | #include <node.h> |
| 38 | #include <nan.h> |
| 39 | |
| 40 | #include "grpc/grpc.h" |
murgatroid99 | 7765906 | 2015-02-11 09:26:25 -0800 | [diff] [blame] | 41 | #include "grpc/support/log.h" |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 42 | #include "grpc/support/time.h" |
murgatroid99 | 013d203 | 2016-11-01 13:05:24 -0700 | [diff] [blame] | 43 | #include "completion_queue.h" |
murgatroid99 | 016bb50 | 2015-02-09 15:55:10 -0800 | [diff] [blame] | 44 | #include "call.h" |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 45 | |
| 46 | namespace grpc { |
| 47 | namespace node { |
| 48 | |
murgatroid99 | 013d203 | 2016-11-01 13:05:24 -0700 | [diff] [blame] | 49 | namespace { |
| 50 | |
| 51 | /* A worker that asynchronously calls completion_queue_next, and queues onto the |
| 52 | node event loop a call to the function stored in the event's tag. */ |
| 53 | class CompletionQueueAsyncWorker : public Nan::AsyncWorker { |
| 54 | public: |
| 55 | CompletionQueueAsyncWorker(); |
| 56 | |
| 57 | ~CompletionQueueAsyncWorker(); |
| 58 | /* Calls completion_queue_next with the provided deadline, and stores the |
| 59 | event if there was one or sets an error message if there was not */ |
| 60 | void Execute(); |
| 61 | |
| 62 | /* Returns the completion queue attached to this class */ |
| 63 | static grpc_completion_queue *GetQueue(); |
| 64 | |
| 65 | /* Convenience function to create a worker with the given arguments and queue |
| 66 | it to run asynchronously */ |
| 67 | static void Next(); |
| 68 | |
| 69 | /* Initialize the CompletionQueueAsyncWorker class */ |
| 70 | static void Init(v8::Local<v8::Object> exports); |
| 71 | |
| 72 | protected: |
| 73 | /* Called when Execute has succeeded (completed without setting an error |
| 74 | message). Calls the saved callback with the event that came from |
| 75 | completion_queue_next */ |
| 76 | void HandleOKCallback(); |
| 77 | |
| 78 | void HandleErrorCallback(); |
| 79 | |
| 80 | private: |
Michael Lumish | fe6b328 | 2017-02-07 11:20:16 -0800 | [diff] [blame] | 81 | static void TryAddWorker(); |
| 82 | |
murgatroid99 | 013d203 | 2016-11-01 13:05:24 -0700 | [diff] [blame] | 83 | grpc_event result; |
| 84 | |
| 85 | static grpc_completion_queue *queue; |
| 86 | |
| 87 | // Number of grpc_completion_queue_next calls in the thread pool |
| 88 | static int current_threads; |
| 89 | // Number of grpc_completion_queue_next calls waiting to enter the thread pool |
| 90 | static int waiting_next_calls; |
| 91 | }; |
| 92 | |
murgatroid99 | 7ab95fb | 2015-02-02 16:50:07 -0800 | [diff] [blame] | 93 | const int max_queue_threads = 2; |
| 94 | |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 95 | using v8::Function; |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 96 | using v8::Local; |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 97 | using v8::Object; |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 98 | using v8::Value; |
| 99 | |
| 100 | grpc_completion_queue *CompletionQueueAsyncWorker::queue; |
| 101 | |
murgatroid99 | e339f6f | 2015-09-23 11:38:39 -0700 | [diff] [blame] | 102 | // Invariants: current_threads <= max_queue_threads |
| 103 | // (current_threads == max_queue_threads) || (waiting_next_calls == 0) |
| 104 | |
murgatroid99 | 7ab95fb | 2015-02-02 16:50:07 -0800 | [diff] [blame] | 105 | int CompletionQueueAsyncWorker::current_threads; |
| 106 | int CompletionQueueAsyncWorker::waiting_next_calls; |
| 107 | |
Craig Tiller | e8e304e | 2015-01-13 14:41:29 -0800 | [diff] [blame] | 108 | CompletionQueueAsyncWorker::CompletionQueueAsyncWorker() |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 109 | : Nan::AsyncWorker(NULL) {} |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 110 | |
Craig Tiller | e8e304e | 2015-01-13 14:41:29 -0800 | [diff] [blame] | 111 | CompletionQueueAsyncWorker::~CompletionQueueAsyncWorker() {} |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 112 | |
| 113 | void CompletionQueueAsyncWorker::Execute() { |
Craig Tiller | 354398f | 2015-07-13 09:16:03 -0700 | [diff] [blame] | 114 | result = |
Nicolas "Pixel" Noble | 8e6bab5 | 2015-08-07 01:40:49 +0200 | [diff] [blame] | 115 | grpc_completion_queue_next(queue, gpr_inf_future(GPR_CLOCK_REALTIME), NULL); |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame] | 116 | if (!result.success) { |
murgatroid99 | 937ac9b | 2015-08-13 11:00:13 -0700 | [diff] [blame] | 117 | SetErrorMessage("The async function encountered an error"); |
murgatroid99 | d4d67ad | 2015-02-09 10:43:21 -0800 | [diff] [blame] | 118 | } |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 119 | } |
| 120 | |
Craig Tiller | e8e304e | 2015-01-13 14:41:29 -0800 | [diff] [blame] | 121 | grpc_completion_queue *CompletionQueueAsyncWorker::GetQueue() { return queue; } |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 122 | |
Michael Lumish | fe6b328 | 2017-02-07 11:20:16 -0800 | [diff] [blame] | 123 | void CompletionQueueAsyncWorker::TryAddWorker() { |
| 124 | if (current_threads < max_queue_threads && waiting_next_calls > 0) { |
murgatroid99 | e339f6f | 2015-09-23 11:38:39 -0700 | [diff] [blame] | 125 | current_threads += 1; |
Michael Lumish | fe6b328 | 2017-02-07 11:20:16 -0800 | [diff] [blame] | 126 | waiting_next_calls -= 1; |
murgatroid99 | 7ab95fb | 2015-02-02 16:50:07 -0800 | [diff] [blame] | 127 | CompletionQueueAsyncWorker *worker = new CompletionQueueAsyncWorker(); |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 128 | Nan::AsyncQueueWorker(worker); |
murgatroid99 | 7ab95fb | 2015-02-02 16:50:07 -0800 | [diff] [blame] | 129 | } |
murgatroid99 | e339f6f | 2015-09-23 11:38:39 -0700 | [diff] [blame] | 130 | GPR_ASSERT(current_threads <= max_queue_threads); |
| 131 | GPR_ASSERT((current_threads == max_queue_threads) || |
| 132 | (waiting_next_calls == 0)); |
Michael Lumish | fe6b328 | 2017-02-07 11:20:16 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | void CompletionQueueAsyncWorker::Next() { |
| 136 | waiting_next_calls += 1; |
| 137 | TryAddWorker(); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 138 | } |
| 139 | |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 140 | void CompletionQueueAsyncWorker::Init(Local<Object> exports) { |
| 141 | Nan::HandleScope scope; |
murgatroid99 | 7ab95fb | 2015-02-02 16:50:07 -0800 | [diff] [blame] | 142 | current_threads = 0; |
| 143 | waiting_next_calls = 0; |
Nicolas "Pixel" Noble | 8e6bab5 | 2015-08-07 01:40:49 +0200 | [diff] [blame] | 144 | queue = grpc_completion_queue_create(NULL); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | void CompletionQueueAsyncWorker::HandleOKCallback() { |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 148 | Nan::HandleScope scope; |
Michael Lumish | fe6b328 | 2017-02-07 11:20:16 -0800 | [diff] [blame] | 149 | current_threads -= 1; |
| 150 | TryAddWorker(); |
murgatroid99 | 79962f3 | 2017-04-10 14:37:43 -0700 | [diff] [blame^] | 151 | CompleteTag(result.tag, NULL); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 152 | |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame] | 153 | DestroyTag(result.tag); |
murgatroid99 | d4d67ad | 2015-02-09 10:43:21 -0800 | [diff] [blame] | 154 | } |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 155 | |
murgatroid99 | d4d67ad | 2015-02-09 10:43:21 -0800 | [diff] [blame] | 156 | void CompletionQueueAsyncWorker::HandleErrorCallback() { |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 157 | Nan::HandleScope scope; |
Michael Lumish | fe6b328 | 2017-02-07 11:20:16 -0800 | [diff] [blame] | 158 | current_threads -= 1; |
| 159 | TryAddWorker(); |
murgatroid99 | 79962f3 | 2017-04-10 14:37:43 -0700 | [diff] [blame^] | 160 | CompleteTag(result.tag, ErrorMessage()); |
murgatroid99 | 7765906 | 2015-02-11 09:26:25 -0800 | [diff] [blame] | 161 | |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame] | 162 | DestroyTag(result.tag); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 163 | } |
| 164 | |
murgatroid99 | 013d203 | 2016-11-01 13:05:24 -0700 | [diff] [blame] | 165 | } // namespace |
| 166 | |
| 167 | grpc_completion_queue *GetCompletionQueue() { |
| 168 | return CompletionQueueAsyncWorker::GetQueue(); |
| 169 | } |
| 170 | |
| 171 | void CompletionQueueNext() { |
| 172 | CompletionQueueAsyncWorker::Next(); |
| 173 | } |
| 174 | |
| 175 | void CompletionQueueInit(Local<Object> exports) { |
| 176 | CompletionQueueAsyncWorker::Init(exports); |
| 177 | } |
| 178 | |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 179 | } // namespace node |
| 180 | } // namespace grpc |
murgatroid99 | 013d203 | 2016-11-01 13:05:24 -0700 | [diff] [blame] | 181 | |
| 182 | #endif /* GRPC_UV */ |