blob: 2bdc44be2ea20d967e752139786df2d7248b2bcf [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * 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" Nobleff2828b2015-04-03 03:16:46 +020034#include <grpc++/impl/sync.h>
35#include <grpc++/impl/thd.h>
Vijay Paie8a7e302015-08-24 10:52:33 -070036#include "src/cpp/server/fixed_size_thread_pool.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080037
38namespace grpc {
39
Vijay Pai69f24102015-07-15 12:33:37 -070040void FixedSizeThreadPool::ThreadFunc() {
vjpaid5933b62015-03-25 14:55:56 -070041 for (;;) {
42 // Wait until work is available or we are shutting down.
Nicolas "Pixel" Nobleff2828b2015-04-03 03:16:46 +020043 grpc::unique_lock<grpc::mutex> lock(mu_);
vjpaid5933b62015-03-25 14:55:56 -070044 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 Pai69f24102015-07-15 12:33:37 -070060FixedSizeThreadPool::FixedSizeThreadPool(int num_threads) : shutdown_(false) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080061 for (int i = 0; i < num_threads; i++) {
Vijay Pai69f24102015-07-15 12:33:37 -070062 threads_.push_back(
63 new grpc::thread(&FixedSizeThreadPool::ThreadFunc, this));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080064 }
65}
66
Vijay Pai69f24102015-07-15 12:33:37 -070067FixedSizeThreadPool::~FixedSizeThreadPool() {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080068 {
Nicolas "Pixel" Nobleff2828b2015-04-03 03:16:46 +020069 grpc::lock_guard<grpc::mutex> lock(mu_);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080070 shutdown_ = true;
71 cv_.notify_all();
72 }
Vijay Pai82dd80a2015-03-24 10:36:08 -070073 for (auto t = threads_.begin(); t != threads_.end(); t++) {
Yang Gao69fe0752015-06-01 14:32:38 -070074 (*t)->join();
75 delete *t;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080076 }
77}
78
vjpai72a44172015-07-16 21:44:44 -070079void FixedSizeThreadPool::Add(const std::function<void()>& callback) {
Nicolas "Pixel" Nobleff2828b2015-04-03 03:16:46 +020080 grpc::lock_guard<grpc::mutex> lock(mu_);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080081 callbacks_.push(callback);
Craig Tillerb1663e72015-02-01 22:09:38 -080082 cv_.notify_one();
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080083}
84
Craig Tiller190d3602015-02-18 09:23:38 -080085} // namespace grpc