140161064SShuo Chen// excerpts from http://code.google.com/p/muduo/ 240161064SShuo Chen// 340161064SShuo Chen// Use of this source code is governed by a BSD-style license 440161064SShuo Chen// that can be found in the License file. 540161064SShuo Chen// 640161064SShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com) 740161064SShuo Chen 840161064SShuo Chen#include "EventLoopThreadPool.h" 940161064SShuo Chen 1040161064SShuo Chen#include "EventLoop.h" 1140161064SShuo Chen#include "EventLoopThread.h" 1240161064SShuo Chen 1340161064SShuo Chen#include <boost/bind.hpp> 1440161064SShuo Chen 1540161064SShuo Chenusing namespace muduo; 1640161064SShuo Chen 1740161064SShuo ChenEventLoopThreadPool::EventLoopThreadPool(EventLoop* baseLoop) 1840161064SShuo Chen : baseLoop_(baseLoop), 1940161064SShuo Chen started_(false), 2040161064SShuo Chen numThreads_(0), 2140161064SShuo Chen next_(0) 2240161064SShuo Chen{ 2340161064SShuo Chen} 2440161064SShuo Chen 2540161064SShuo ChenEventLoopThreadPool::~EventLoopThreadPool() 2640161064SShuo Chen{ 2740161064SShuo Chen // Don't delete loop, it's stack variable 2840161064SShuo Chen} 2940161064SShuo Chen 3040161064SShuo Chenvoid EventLoopThreadPool::start() 3140161064SShuo Chen{ 3240161064SShuo Chen assert(!started_); 3340161064SShuo Chen baseLoop_->assertInLoopThread(); 3440161064SShuo Chen 3540161064SShuo Chen started_ = true; 3640161064SShuo Chen 3740161064SShuo Chen for (int i = 0; i < numThreads_; ++i) 3840161064SShuo Chen { 3940161064SShuo Chen EventLoopThread* t = new EventLoopThread; 4040161064SShuo Chen threads_.push_back(t); 4140161064SShuo Chen loops_.push_back(t->startLoop()); 4240161064SShuo Chen } 4340161064SShuo Chen} 4440161064SShuo Chen 4540161064SShuo ChenEventLoop* EventLoopThreadPool::getNextLoop() 4640161064SShuo Chen{ 4740161064SShuo Chen baseLoop_->assertInLoopThread(); 4840161064SShuo Chen EventLoop* loop = baseLoop_; 4940161064SShuo Chen 5040161064SShuo Chen if (!loops_.empty()) 5140161064SShuo Chen { 5240161064SShuo Chen // round-robin 5340161064SShuo Chen loop = loops_[next_]; 5440161064SShuo Chen ++next_; 5540161064SShuo Chen if (static_cast<size_t>(next_) >= loops_.size()) 5640161064SShuo Chen { 5740161064SShuo Chen next_ = 0; 5840161064SShuo Chen } 5940161064SShuo Chen } 6040161064SShuo Chen return loop; 6140161064SShuo Chen} 6240161064SShuo Chen 63