This source file includes following definitions.
- PosixDynamicThreadPoolPeer
- lock
- pending_tasks_available_cv
- pending_tasks
- num_idle_threads
- num_idle_threads_cv
- set_num_idle_threads_cv
- IncrementingTask
- BlockingIncrementingTask
- start_
- SetUp
- TearDown
- WaitForTasksToStart
- WaitForIdleThreads
- CreateNewIncrementingTaskCallback
- CreateNewBlockingIncrementingTaskCallback
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "base/threading/worker_pool_posix.h"
#include <set>
#include "base/bind.h"
#include "base/callback.h"
#include "base/synchronization/condition_variable.h"
#include "base/synchronization/lock.h"
#include "base/threading/platform_thread.h"
#include "base/synchronization/waitable_event.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
class PosixDynamicThreadPool::PosixDynamicThreadPoolPeer {
public:
explicit PosixDynamicThreadPoolPeer(PosixDynamicThreadPool* pool)
: pool_(pool) {}
Lock* lock() { return &pool_->lock_; }
ConditionVariable* pending_tasks_available_cv() {
return &pool_->pending_tasks_available_cv_;
}
const std::queue<PendingTask>& pending_tasks() const {
return pool_->pending_tasks_;
}
int num_idle_threads() const { return pool_->num_idle_threads_; }
ConditionVariable* num_idle_threads_cv() {
return pool_->num_idle_threads_cv_.get();
}
void set_num_idle_threads_cv(ConditionVariable* cv) {
pool_->num_idle_threads_cv_.reset(cv);
}
private:
PosixDynamicThreadPool* pool_;
DISALLOW_COPY_AND_ASSIGN(PosixDynamicThreadPoolPeer);
};
namespace {
void IncrementingTask(Lock* counter_lock,
int* counter,
Lock* unique_threads_lock,
std::set<PlatformThreadId>* unique_threads) {
{
base::AutoLock locked(*unique_threads_lock);
unique_threads->insert(PlatformThread::CurrentId());
}
base::AutoLock locked(*counter_lock);
(*counter)++;
}
struct BlockingIncrementingTaskArgs {
Lock* counter_lock;
int* counter;
Lock* unique_threads_lock;
std::set<PlatformThreadId>* unique_threads;
Lock* num_waiting_to_start_lock;
int* num_waiting_to_start;
ConditionVariable* num_waiting_to_start_cv;
base::WaitableEvent* start;
};
void BlockingIncrementingTask(const BlockingIncrementingTaskArgs& args) {
{
base::AutoLock num_waiting_to_start_locked(*args.num_waiting_to_start_lock);
(*args.num_waiting_to_start)++;
}
args.num_waiting_to_start_cv->Signal();
args.start->Wait();
IncrementingTask(args.counter_lock, args.counter, args.unique_threads_lock,
args.unique_threads);
}
class PosixDynamicThreadPoolTest : public testing::Test {
protected:
PosixDynamicThreadPoolTest()
: pool_(new base::PosixDynamicThreadPool("dynamic_pool", 60*60)),
peer_(pool_.get()),
counter_(0),
num_waiting_to_start_(0),
num_waiting_to_start_cv_(&num_waiting_to_start_lock_),
start_(true, false) {}
virtual void SetUp() OVERRIDE {
peer_.set_num_idle_threads_cv(new ConditionVariable(peer_.lock()));
}
virtual void TearDown() OVERRIDE {
if (pool_.get()) pool_->Terminate();
}
void WaitForTasksToStart(int num_tasks) {
base::AutoLock num_waiting_to_start_locked(num_waiting_to_start_lock_);
while (num_waiting_to_start_ < num_tasks) {
num_waiting_to_start_cv_.Wait();
}
}
void WaitForIdleThreads(int num_idle_threads) {
base::AutoLock pool_locked(*peer_.lock());
while (peer_.num_idle_threads() < num_idle_threads) {
peer_.num_idle_threads_cv()->Wait();
}
}
base::Closure CreateNewIncrementingTaskCallback() {
return base::Bind(&IncrementingTask, &counter_lock_, &counter_,
&unique_threads_lock_, &unique_threads_);
}
base::Closure CreateNewBlockingIncrementingTaskCallback() {
BlockingIncrementingTaskArgs args = {
&counter_lock_, &counter_, &unique_threads_lock_, &unique_threads_,
&num_waiting_to_start_lock_, &num_waiting_to_start_,
&num_waiting_to_start_cv_, &start_
};
return base::Bind(&BlockingIncrementingTask, args);
}
scoped_refptr<base::PosixDynamicThreadPool> pool_;
base::PosixDynamicThreadPool::PosixDynamicThreadPoolPeer peer_;
Lock counter_lock_;
int counter_;
Lock unique_threads_lock_;
std::set<PlatformThreadId> unique_threads_;
Lock num_waiting_to_start_lock_;
int num_waiting_to_start_;
ConditionVariable num_waiting_to_start_cv_;
base::WaitableEvent start_;
};
}
TEST_F(PosixDynamicThreadPoolTest, Basic) {
EXPECT_EQ(0, peer_.num_idle_threads());
EXPECT_EQ(0U, unique_threads_.size());
EXPECT_EQ(0U, peer_.pending_tasks().size());
pool_->PostTask(FROM_HERE, CreateNewIncrementingTaskCallback());
WaitForIdleThreads(1);
EXPECT_EQ(1U, unique_threads_.size()) <<
"There should be only one thread allocated for one task.";
EXPECT_EQ(1, counter_);
}
TEST_F(PosixDynamicThreadPoolTest, ReuseIdle) {
pool_->PostTask(FROM_HERE, CreateNewIncrementingTaskCallback());
WaitForIdleThreads(1);
pool_->PostTask(FROM_HERE, CreateNewBlockingIncrementingTaskCallback());
pool_->PostTask(FROM_HERE, CreateNewBlockingIncrementingTaskCallback());
WaitForTasksToStart(2);
start_.Signal();
WaitForIdleThreads(2);
EXPECT_EQ(2U, unique_threads_.size());
EXPECT_EQ(2, peer_.num_idle_threads());
EXPECT_EQ(3, counter_);
}
TEST_F(PosixDynamicThreadPoolTest, TwoActiveTasks) {
pool_->PostTask(FROM_HERE, CreateNewBlockingIncrementingTaskCallback());
pool_->PostTask(FROM_HERE, CreateNewBlockingIncrementingTaskCallback());
EXPECT_EQ(0, counter_) << "Blocking tasks should not have started yet.";
WaitForTasksToStart(2);
start_.Signal();
WaitForIdleThreads(2);
EXPECT_EQ(2U, unique_threads_.size());
EXPECT_EQ(2, peer_.num_idle_threads()) << "Existing threads are now idle.";
EXPECT_EQ(2, counter_);
}
TEST_F(PosixDynamicThreadPoolTest, Complex) {
pool_->PostTask(FROM_HERE, CreateNewIncrementingTaskCallback());
WaitForIdleThreads(1);
pool_->PostTask(FROM_HERE, CreateNewBlockingIncrementingTaskCallback());
pool_->PostTask(FROM_HERE, CreateNewBlockingIncrementingTaskCallback());
WaitForTasksToStart(2);
start_.Signal();
WaitForIdleThreads(2);
EXPECT_EQ(3, counter_);
EXPECT_EQ(2, peer_.num_idle_threads());
EXPECT_EQ(2U, unique_threads_.size());
{
base::AutoLock locked(*peer_.lock());
while (peer_.num_idle_threads() > 0) {
peer_.pending_tasks_available_cv()->Signal();
peer_.num_idle_threads_cv()->Wait();
}
}
pool_->PostTask(FROM_HERE, CreateNewIncrementingTaskCallback());
WaitForIdleThreads(1);
EXPECT_TRUE(unique_threads_.size() >= 2 && unique_threads_.size() <= 3)
<< "unique_threads_.size() = " << unique_threads_.size();
EXPECT_EQ(1, peer_.num_idle_threads());
EXPECT_EQ(4, counter_);
}
}