Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -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 | |
Nicolas "Pixel" Noble | ff2828b | 2015-04-03 03:16:46 +0200 | [diff] [blame] | 34 | #include <grpc++/impl/sync.h> |
| 35 | #include <grpc++/impl/thd.h> |
Vijay Pai | e8a7e30 | 2015-08-24 10:52:33 -0700 | [diff] [blame] | 36 | #include "src/cpp/server/fixed_size_thread_pool.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 37 | |
| 38 | namespace grpc { |
| 39 | |
Vijay Pai | 69f2410 | 2015-07-15 12:33:37 -0700 | [diff] [blame] | 40 | void FixedSizeThreadPool::ThreadFunc() { |
vjpai | d5933b6 | 2015-03-25 14:55:56 -0700 | [diff] [blame] | 41 | for (;;) { |
| 42 | // Wait until work is available or we are shutting down. |
Nicolas "Pixel" Noble | ff2828b | 2015-04-03 03:16:46 +0200 | [diff] [blame] | 43 | grpc::unique_lock<grpc::mutex> lock(mu_); |
vjpai | d5933b6 | 2015-03-25 14:55:56 -0700 | [diff] [blame] | 44 | if (!shutdown_ && callbacks_.empty()) { |
| 45 | cv_.wait(lock); |
| 46 | } |
| 47 | // Drain callbacks before considering shutdown to ensure all work |
| 48 | // gets completed. |
| 49 | if (!callbacks_.empty()) { |
| 50 | auto cb = callbacks_.front(); |
| 51 | callbacks_.pop(); |
| 52 | lock.unlock(); |
| 53 | cb(); |
| 54 | } else if (shutdown_) { |
| 55 | return; |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
Vijay Pai | 69f2410 | 2015-07-15 12:33:37 -0700 | [diff] [blame] | 60 | FixedSizeThreadPool::FixedSizeThreadPool(int num_threads) : shutdown_(false) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 61 | for (int i = 0; i < num_threads; i++) { |
Vijay Pai | 69f2410 | 2015-07-15 12:33:37 -0700 | [diff] [blame] | 62 | threads_.push_back( |
| 63 | new grpc::thread(&FixedSizeThreadPool::ThreadFunc, this)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | |
Vijay Pai | 69f2410 | 2015-07-15 12:33:37 -0700 | [diff] [blame] | 67 | FixedSizeThreadPool::~FixedSizeThreadPool() { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 68 | { |
Nicolas "Pixel" Noble | ff2828b | 2015-04-03 03:16:46 +0200 | [diff] [blame] | 69 | grpc::lock_guard<grpc::mutex> lock(mu_); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 70 | shutdown_ = true; |
| 71 | cv_.notify_all(); |
| 72 | } |
Vijay Pai | 82dd80a | 2015-03-24 10:36:08 -0700 | [diff] [blame] | 73 | for (auto t = threads_.begin(); t != threads_.end(); t++) { |
Yang Gao | 69fe075 | 2015-06-01 14:32:38 -0700 | [diff] [blame] | 74 | (*t)->join(); |
| 75 | delete *t; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | |
vjpai | 72a4417 | 2015-07-16 21:44:44 -0700 | [diff] [blame] | 79 | void FixedSizeThreadPool::Add(const std::function<void()>& callback) { |
Nicolas "Pixel" Noble | ff2828b | 2015-04-03 03:16:46 +0200 | [diff] [blame] | 80 | grpc::lock_guard<grpc::mutex> lock(mu_); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 81 | callbacks_.push(callback); |
Craig Tiller | b1663e7 | 2015-02-01 22:09:38 -0800 | [diff] [blame] | 82 | cv_.notify_one(); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 83 | } |
| 84 | |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 85 | } // namespace grpc |