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

          Line data    Source code
       1             : /*
       2             :  *
       3             :  * Copyright 2015, 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             : 
      34             : #include <grpc++/impl/sync.h>
      35             : #include <grpc++/impl/thd.h>
      36             : 
      37             : #include "src/cpp/server/dynamic_thread_pool.h"
      38             : 
      39             : namespace grpc {
      40       24324 : DynamicThreadPool::DynamicThread::DynamicThread(DynamicThreadPool* pool)
      41             :     : pool_(pool),
      42             :       thd_(new grpc::thread(&DynamicThreadPool::DynamicThread::ThreadFunc,
      43       24324 :                             this)) {}
      44       48648 : DynamicThreadPool::DynamicThread::~DynamicThread() {
      45       24324 :   thd_->join();
      46       24324 :   thd_.reset();
      47       24324 : }
      48             : 
      49       24322 : void DynamicThreadPool::DynamicThread::ThreadFunc() {
      50       24322 :   pool_->ThreadFunc();
      51             :   // Now that we have killed ourselves, we should reduce the thread count
      52       24323 :   grpc::unique_lock<grpc::mutex> lock(pool_->mu_);
      53       24324 :   pool_->nthreads_--;
      54             :   // Move ourselves to dead list
      55       24324 :   pool_->dead_threads_.push_back(this);
      56             : 
      57       24324 :   if ((pool_->shutdown_) && (pool_->nthreads_ == 0)) {
      58         178 :     pool_->shutdown_cv_.notify_one();
      59       24324 :   }
      60       24323 : }
      61             : 
      62      187235 : void DynamicThreadPool::ThreadFunc() {
      63             :   for (;;) {
      64             :     // Wait until work is available or we are shutting down.
      65      187235 :     grpc::unique_lock<grpc::mutex> lock(mu_);
      66      187270 :     if (!shutdown_ && callbacks_.empty()) {
      67             :       // If there are too many threads waiting, then quit this thread
      68       95058 :       if (threads_waiting_ >= reserve_threads_) {
      69       23612 :         break;
      70             :       }
      71       71446 :       threads_waiting_++;
      72       71446 :       cv_.wait(lock);
      73       71447 :       threads_waiting_--;
      74             :     }
      75             :     // Drain callbacks before considering shutdown to ensure all work
      76             :     // gets completed.
      77      163659 :     if (!callbacks_.empty()) {
      78      159756 :       auto cb = callbacks_.front();
      79      159756 :       callbacks_.pop();
      80      159756 :       lock.unlock();
      81      159756 :       cb();
      82        3907 :     } else if (shutdown_) {
      83         712 :       break;
      84             :     }
      85      162913 :   }
      86       24324 : }
      87             : 
      88         178 : DynamicThreadPool::DynamicThreadPool(int reserve_threads)
      89             :     : shutdown_(false),
      90             :       reserve_threads_(reserve_threads),
      91             :       nthreads_(0),
      92         178 :       threads_waiting_(0) {
      93         890 :   for (int i = 0; i < reserve_threads_; i++) {
      94         712 :     grpc::lock_guard<grpc::mutex> lock(mu_);
      95         712 :     nthreads_++;
      96         712 :     new DynamicThread(this);
      97         712 :   }
      98         178 : }
      99             : 
     100        1650 : void DynamicThreadPool::ReapThreads(std::list<DynamicThread*>* tlist) {
     101       25974 :   for (auto t = tlist->begin(); t != tlist->end(); t = tlist->erase(t)) {
     102       24324 :     delete *t;
     103             :   }
     104        1650 : }
     105             : 
     106         534 : DynamicThreadPool::~DynamicThreadPool() {
     107         178 :   grpc::unique_lock<grpc::mutex> lock(mu_);
     108         178 :   shutdown_ = true;
     109         178 :   cv_.notify_all();
     110         534 :   while (nthreads_ != 0) {
     111         178 :     shutdown_cv_.wait(lock);
     112             :   }
     113         178 :   ReapThreads(&dead_threads_);
     114         356 : }
     115             : 
     116      159756 : void DynamicThreadPool::Add(const std::function<void()>& callback) {
     117      159756 :   grpc::lock_guard<grpc::mutex> lock(mu_);
     118             :   // Add works to the callbacks list
     119      159756 :   callbacks_.push(callback);
     120             :   // Increase pool size or notify as needed
     121      159756 :   if (threads_waiting_ == 0) {
     122             :     // Kick off a new thread
     123       23612 :     nthreads_++;
     124       23612 :     new DynamicThread(this);
     125             :   } else {
     126      136144 :     cv_.notify_one();
     127             :   }
     128             :   // Also use this chance to harvest dead threads
     129      159756 :   if (!dead_threads_.empty()) {
     130        1472 :     ReapThreads(&dead_threads_);
     131      159756 :   }
     132      159756 : }
     133             : 
     134             : }  // namespace grpc

Generated by: LCOV version 1.10