2
* Copyright 2015, Google Inc.
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions are
9
* * Redistributions of source code must retain the above copyright
10
* notice, this list of conditions and the following disclaimer.
11
* * Redistributions in binary form must reproduce the above
12
* copyright notice, this list of conditions and the following disclaimer
13
* in the documentation and/or other materials provided with the
15
* * Neither the name of Google Inc. nor the names of its
16
* contributors may be used to endorse or promote products derived from
17
* this software without specific prior written permission.
19
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
#include <grpc++/completion_queue.h>
37
#include <grpc/grpc.h>
38
#include <grpc/support/log.h>
39
#include <grpc++/time.h>
43
CompletionQueue::CompletionQueue() { cq_ = grpc_completion_queue_create(); }
45
CompletionQueue::CompletionQueue(grpc_completion_queue* take) : cq_(take) {}
47
CompletionQueue::~CompletionQueue() { grpc_completion_queue_destroy(cq_); }
49
void CompletionQueue::Shutdown() { grpc_completion_queue_shutdown(cq_); }
51
CompletionQueue::NextStatus CompletionQueue::AsyncNextInternal(
52
void** tag, bool* ok, gpr_timespec deadline) {
54
auto ev = grpc_completion_queue_next(cq_, deadline);
56
case GRPC_QUEUE_TIMEOUT:
58
case GRPC_QUEUE_SHUTDOWN:
60
case GRPC_OP_COMPLETE:
61
auto cq_tag = static_cast<CompletionQueueTag*>(ev.tag);
62
*ok = ev.success != 0;
64
if (cq_tag->FinalizeResult(tag, ok)) {
72
bool CompletionQueue::Pluck(CompletionQueueTag* tag) {
73
auto ev = grpc_completion_queue_pluck(cq_, tag, gpr_inf_future);
74
bool ok = ev.success != 0;
76
GPR_ASSERT(tag->FinalizeResult(&ignored, &ok));
77
GPR_ASSERT(ignored == tag);
78
// Ignore mutations by FinalizeResult: Pluck returns the C API status
79
return ev.success != 0;
82
void CompletionQueue::TryPluck(CompletionQueueTag* tag) {
83
auto ev = grpc_completion_queue_pluck(cq_, tag, gpr_time_0);
84
if (ev.type == GRPC_QUEUE_TIMEOUT) return;
85
bool ok = ev.success != 0;
87
// the tag must be swallowed if using TryPluck
88
GPR_ASSERT(!tag->FinalizeResult(&ignored, &ok));