LCOV - code coverage report
Current view: top level - src/cpp/common - completion_queue.cc (source / functions) Hit Total Coverage
Test: tmp.zDYK9MVh93 Lines: 33 33 100.0 %
Date: 2015-10-10 Functions: 8 8 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright 2015, Google Inc.
       3             :  * All rights reserved.
       4             :  *
       5             :  * Redistribution and use in source and binary forms, with or without
       6             :  * modification, are permitted provided that the following conditions are
       7             :  * met:
       8             :  *
       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
      14             :  * distribution.
      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.
      18             :  *
      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.
      30             :  *
      31             :  */
      32             : 
      33             : #include <grpc++/completion_queue.h>
      34             : 
      35             : #include <memory>
      36             : 
      37             : #include <grpc/grpc.h>
      38             : #include <grpc/support/log.h>
      39             : #include <grpc++/support/time.h>
      40             : 
      41             : namespace grpc {
      42             : 
      43      159759 : CompletionQueue::CompletionQueue() {
      44      159764 :   cq_ = grpc_completion_queue_create(nullptr);
      45      159760 : }
      46             : 
      47      159512 : CompletionQueue::CompletionQueue(grpc_completion_queue* take) : cq_(take) {}
      48             : 
      49      319271 : CompletionQueue::~CompletionQueue() { grpc_completion_queue_destroy(cq_); }
      50             : 
      51      159735 : void CompletionQueue::Shutdown() { grpc_completion_queue_shutdown(cq_); }
      52             : 
      53     8637743 : CompletionQueue::NextStatus CompletionQueue::AsyncNextInternal(
      54             :     void** tag, bool* ok, gpr_timespec deadline) {
      55             :   for (;;) {
      56     8637743 :     auto ev = grpc_completion_queue_next(cq_, deadline, nullptr);
      57     8638998 :     switch (ev.type) {
      58             :       case GRPC_QUEUE_TIMEOUT:
      59     8446485 :         return TIMEOUT;
      60             :       case GRPC_QUEUE_SHUTDOWN:
      61      160020 :         return SHUTDOWN;
      62             :       case GRPC_OP_COMPLETE:
      63     6229016 :         auto cq_tag = static_cast<CompletionQueueTag*>(ev.tag);
      64     6229016 :         *ok = ev.success != 0;
      65     6229016 :         *tag = cq_tag;
      66     6229016 :         if (cq_tag->FinalizeResult(tag, ok)) {
      67     3787091 :           return GOT_EVENT;
      68             :         }
      69     2438068 :         break;
      70             :     }
      71     2438343 :   }
      72             : }
      73             : 
      74      608394 : bool CompletionQueue::Pluck(CompletionQueueTag* tag) {
      75      608394 :   auto deadline = gpr_inf_future(GPR_CLOCK_REALTIME);
      76      608406 :   auto ev = grpc_completion_queue_pluck(cq_, tag, deadline, nullptr);
      77      608395 :   bool ok = ev.success != 0;
      78      608395 :   void* ignored = tag;
      79      608395 :   GPR_ASSERT(tag->FinalizeResult(&ignored, &ok));
      80      608317 :   GPR_ASSERT(ignored == tag);
      81             :   // Ignore mutations by FinalizeResult: Pluck returns the C API status
      82      608317 :   return ev.success != 0;
      83             : }
      84             : 
      85      100980 : void CompletionQueue::TryPluck(CompletionQueueTag* tag) {
      86      100980 :   auto deadline = gpr_time_0(GPR_CLOCK_REALTIME);
      87      101030 :   auto ev = grpc_completion_queue_pluck(cq_, tag, deadline, nullptr);
      88      202060 :   if (ev.type == GRPC_QUEUE_TIMEOUT) return;
      89           5 :   bool ok = ev.success != 0;
      90           5 :   void* ignored = tag;
      91             :   // the tag must be swallowed if using TryPluck
      92           5 :   GPR_ASSERT(!tag->FinalizeResult(&ignored, &ok));
      93             : }
      94             : 
      95             : }  // namespace grpc

Generated by: LCOV version 1.10