1a1bde736SShuo Chen// excerpts from http://code.google.com/p/muduo/ 2a1bde736SShuo Chen// 3a1bde736SShuo Chen// Use of this source code is governed by a BSD-style license 4a1bde736SShuo Chen// that can be found in the License file. 5a1bde736SShuo Chen// 6a1bde736SShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com) 7a1bde736SShuo Chen 8a1bde736SShuo Chen#include "EventLoopThreadPool.h" 9a1bde736SShuo Chen 10a1bde736SShuo Chen#include "EventLoop.h" 11a1bde736SShuo Chen#include "EventLoopThread.h" 12a1bde736SShuo Chen 13a1bde736SShuo Chen#include <boost/bind.hpp> 14a1bde736SShuo Chen 15a1bde736SShuo Chenusing namespace muduo; 16a1bde736SShuo Chen 17a1bde736SShuo ChenEventLoopThreadPool::EventLoopThreadPool(EventLoop* baseLoop) 18a1bde736SShuo Chen : baseLoop_(baseLoop), 19a1bde736SShuo Chen started_(false), 20a1bde736SShuo Chen numThreads_(0), 21a1bde736SShuo Chen next_(0) 22a1bde736SShuo Chen{ 23a1bde736SShuo Chen} 24a1bde736SShuo Chen 25a1bde736SShuo ChenEventLoopThreadPool::~EventLoopThreadPool() 26a1bde736SShuo Chen{ 27a1bde736SShuo Chen // Don't delete loop, it's stack variable 28a1bde736SShuo Chen} 29a1bde736SShuo Chen 30a1bde736SShuo Chenvoid EventLoopThreadPool::start() 31a1bde736SShuo Chen{ 32a1bde736SShuo Chen assert(!started_); 33a1bde736SShuo Chen baseLoop_->assertInLoopThread(); 34a1bde736SShuo Chen 35a1bde736SShuo Chen started_ = true; 36a1bde736SShuo Chen 37a1bde736SShuo Chen for (int i = 0; i < numThreads_; ++i) 38a1bde736SShuo Chen { 39a1bde736SShuo Chen EventLoopThread* t = new EventLoopThread; 40a1bde736SShuo Chen threads_.push_back(t); 41a1bde736SShuo Chen loops_.push_back(t->startLoop()); 42a1bde736SShuo Chen } 43a1bde736SShuo Chen} 44a1bde736SShuo Chen 45a1bde736SShuo ChenEventLoop* EventLoopThreadPool::getNextLoop() 46a1bde736SShuo Chen{ 47a1bde736SShuo Chen baseLoop_->assertInLoopThread(); 48a1bde736SShuo Chen EventLoop* loop = baseLoop_; 49a1bde736SShuo Chen 50a1bde736SShuo Chen if (!loops_.empty()) 51a1bde736SShuo Chen { 52a1bde736SShuo Chen // round-robin 53a1bde736SShuo Chen loop = loops_[next_]; 54a1bde736SShuo Chen ++next_; 55a1bde736SShuo Chen if (static_cast<size_t>(next_) >= loops_.size()) 56a1bde736SShuo Chen { 57a1bde736SShuo Chen next_ = 0; 58a1bde736SShuo Chen } 59a1bde736SShuo Chen } 60a1bde736SShuo Chen return loop; 61a1bde736SShuo Chen} 62a1bde736SShuo Chen 63