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