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