1c461658bSShuo Chen// excerpts from http://code.google.com/p/muduo/ 2c461658bSShuo Chen// 3c461658bSShuo Chen// Use of this source code is governed by a BSD-style license 4c461658bSShuo Chen// that can be found in the License file. 5c461658bSShuo Chen// 6c461658bSShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com) 7c461658bSShuo Chen 8c461658bSShuo Chen#include "EventLoopThread.h" 9c461658bSShuo Chen 10c461658bSShuo Chen#include "EventLoop.h" 11c461658bSShuo Chen 12c461658bSShuo Chen#include <boost/bind.hpp> 13c461658bSShuo Chen 14c461658bSShuo Chenusing namespace muduo; 15c461658bSShuo Chen 16c461658bSShuo ChenEventLoopThread::EventLoopThread() 17c461658bSShuo Chen : loop_(NULL), 18c461658bSShuo Chen exiting_(false), 19c461658bSShuo Chen thread_(boost::bind(&EventLoopThread::threadFunc, this)), 20c461658bSShuo Chen mutex_(), 21c461658bSShuo Chen cond_(mutex_) 22c461658bSShuo Chen{ 23c461658bSShuo Chen} 24c461658bSShuo Chen 25c461658bSShuo ChenEventLoopThread::~EventLoopThread() 26c461658bSShuo Chen{ 27c461658bSShuo Chen exiting_ = true; 28c461658bSShuo Chen loop_->quit(); 29c461658bSShuo Chen thread_.join(); 30c461658bSShuo Chen} 31c461658bSShuo Chen 32c461658bSShuo ChenEventLoop* EventLoopThread::startLoop() 33c461658bSShuo Chen{ 34c461658bSShuo Chen assert(!thread_.started()); 35c461658bSShuo Chen thread_.start(); 36c461658bSShuo Chen 37c461658bSShuo Chen { 38c461658bSShuo Chen MutexLockGuard lock(mutex_); 39c461658bSShuo Chen while (loop_ == NULL) 40c461658bSShuo Chen { 41c461658bSShuo Chen cond_.wait(); 42c461658bSShuo Chen } 43c461658bSShuo Chen } 44c461658bSShuo Chen 45c461658bSShuo Chen return loop_; 46c461658bSShuo Chen} 47c461658bSShuo Chen 48c461658bSShuo Chenvoid EventLoopThread::threadFunc() 49c461658bSShuo Chen{ 50c461658bSShuo Chen EventLoop loop; 51c461658bSShuo Chen 52c461658bSShuo Chen { 53c461658bSShuo Chen MutexLockGuard lock(mutex_); 54c461658bSShuo Chen loop_ = &loop; 55c461658bSShuo Chen cond_.notify(); 56c461658bSShuo Chen } 57c461658bSShuo Chen 58c461658bSShuo Chen loop.loop(); 59c461658bSShuo Chen //assert(exiting_); 60c461658bSShuo Chen} 61c461658bSShuo Chen 62