12745a763SShuo Chen// excerpts from http://code.google.com/p/muduo/ 22745a763SShuo Chen// 32745a763SShuo Chen// Use of this source code is governed by a BSD-style license 42745a763SShuo Chen// that can be found in the License file. 52745a763SShuo Chen// 62745a763SShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com) 72745a763SShuo Chen 82745a763SShuo Chen#include "EventLoopThread.h" 92745a763SShuo Chen 102745a763SShuo Chen#include "EventLoop.h" 112745a763SShuo Chen 122745a763SShuo Chen#include <boost/bind.hpp> 132745a763SShuo Chen 142745a763SShuo Chenusing namespace muduo; 152745a763SShuo Chen 162745a763SShuo ChenEventLoopThread::EventLoopThread() 172745a763SShuo Chen : loop_(NULL), 182745a763SShuo Chen exiting_(false), 192745a763SShuo Chen thread_(boost::bind(&EventLoopThread::threadFunc, this)), 202745a763SShuo Chen mutex_(), 212745a763SShuo Chen cond_(mutex_) 222745a763SShuo Chen{ 232745a763SShuo Chen} 242745a763SShuo Chen 252745a763SShuo ChenEventLoopThread::~EventLoopThread() 262745a763SShuo Chen{ 272745a763SShuo Chen exiting_ = true; 282745a763SShuo Chen loop_->quit(); 292745a763SShuo Chen thread_.join(); 302745a763SShuo Chen} 312745a763SShuo Chen 322745a763SShuo ChenEventLoop* EventLoopThread::startLoop() 332745a763SShuo Chen{ 342745a763SShuo Chen assert(!thread_.started()); 352745a763SShuo Chen thread_.start(); 362745a763SShuo Chen 372745a763SShuo Chen { 382745a763SShuo Chen MutexLockGuard lock(mutex_); 392745a763SShuo Chen while (loop_ == NULL) 402745a763SShuo Chen { 412745a763SShuo Chen cond_.wait(); 422745a763SShuo Chen } 432745a763SShuo Chen } 442745a763SShuo Chen 452745a763SShuo Chen return loop_; 462745a763SShuo Chen} 472745a763SShuo Chen 482745a763SShuo Chenvoid EventLoopThread::threadFunc() 492745a763SShuo Chen{ 502745a763SShuo Chen EventLoop loop; 512745a763SShuo Chen 522745a763SShuo Chen { 532745a763SShuo Chen MutexLockGuard lock(mutex_); 542745a763SShuo Chen loop_ = &loop; 552745a763SShuo Chen cond_.notify(); 562745a763SShuo Chen } 572745a763SShuo Chen 582745a763SShuo Chen loop.loop(); 592745a763SShuo Chen //assert(exiting_); 602745a763SShuo Chen} 612745a763SShuo Chen 62