murgatroid99 | 749666e | 2015-01-12 18:25:58 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2014, Google Inc. |
| 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 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 34 | #include <node.h> |
| 35 | #include <nan.h> |
| 36 | |
| 37 | #include "grpc/grpc.h" |
| 38 | #include "grpc/support/time.h" |
| 39 | #include "completion_queue_async_worker.h" |
| 40 | #include "event.h" |
| 41 | #include "tag.h" |
| 42 | |
| 43 | namespace grpc { |
| 44 | namespace node { |
| 45 | |
murgatroid99 | 7ab95fb | 2015-02-02 16:50:07 -0800 | [diff] [blame^] | 46 | const int max_queue_threads = 2; |
| 47 | |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 48 | using v8::Function; |
| 49 | using v8::Handle; |
| 50 | using v8::Object; |
| 51 | using v8::Persistent; |
| 52 | using v8::Value; |
| 53 | |
| 54 | grpc_completion_queue *CompletionQueueAsyncWorker::queue; |
| 55 | |
murgatroid99 | 7ab95fb | 2015-02-02 16:50:07 -0800 | [diff] [blame^] | 56 | int CompletionQueueAsyncWorker::current_threads; |
| 57 | int CompletionQueueAsyncWorker::waiting_next_calls; |
| 58 | |
Craig Tiller | e8e304e | 2015-01-13 14:41:29 -0800 | [diff] [blame] | 59 | CompletionQueueAsyncWorker::CompletionQueueAsyncWorker() |
| 60 | : NanAsyncWorker(NULL) {} |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 61 | |
Craig Tiller | e8e304e | 2015-01-13 14:41:29 -0800 | [diff] [blame] | 62 | CompletionQueueAsyncWorker::~CompletionQueueAsyncWorker() {} |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 63 | |
| 64 | void CompletionQueueAsyncWorker::Execute() { |
| 65 | result = grpc_completion_queue_next(queue, gpr_inf_future); |
| 66 | } |
| 67 | |
Craig Tiller | e8e304e | 2015-01-13 14:41:29 -0800 | [diff] [blame] | 68 | grpc_completion_queue *CompletionQueueAsyncWorker::GetQueue() { return queue; } |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 69 | |
| 70 | void CompletionQueueAsyncWorker::Next() { |
| 71 | NanScope(); |
murgatroid99 | 7ab95fb | 2015-02-02 16:50:07 -0800 | [diff] [blame^] | 72 | if (current_threads < max_queue_threads) { |
| 73 | CompletionQueueAsyncWorker *worker = new CompletionQueueAsyncWorker(); |
| 74 | NanAsyncQueueWorker(worker); |
| 75 | } else { |
| 76 | waiting_next_calls += 1; |
| 77 | } |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | void CompletionQueueAsyncWorker::Init(Handle<Object> exports) { |
| 81 | NanScope(); |
murgatroid99 | 7ab95fb | 2015-02-02 16:50:07 -0800 | [diff] [blame^] | 82 | current_threads = 0; |
| 83 | waiting_next_calls = 0; |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 84 | queue = grpc_completion_queue_create(); |
| 85 | } |
| 86 | |
| 87 | void CompletionQueueAsyncWorker::HandleOKCallback() { |
| 88 | NanScope(); |
murgatroid99 | 7ab95fb | 2015-02-02 16:50:07 -0800 | [diff] [blame^] | 89 | if (waiting_next_calls > 0) { |
| 90 | waiting_next_calls -= 1; |
| 91 | CompletionQueueAsyncWorker *worker = new CompletionQueueAsyncWorker(); |
| 92 | NanAsyncQueueWorker(worker); |
| 93 | } else { |
| 94 | current_threads -= 1; |
| 95 | } |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 96 | NanCallback event_callback(GetTagHandle(result->tag).As<Function>()); |
Craig Tiller | e8e304e | 2015-01-13 14:41:29 -0800 | [diff] [blame] | 97 | Handle<Value> argv[] = {CreateEventObject(result)}; |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 98 | |
| 99 | DestroyTag(result->tag); |
| 100 | grpc_event_finish(result); |
| 101 | result = NULL; |
| 102 | |
| 103 | event_callback.Call(1, argv); |
| 104 | } |
| 105 | |
| 106 | } // namespace node |
| 107 | } // namespace grpc |