140161064SShuo Chen// excerpts from http://code.google.com/p/muduo/
240161064SShuo Chen//
340161064SShuo Chen// Use of this source code is governed by a BSD-style license
440161064SShuo Chen// that can be found in the License file.
540161064SShuo Chen//
640161064SShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com)
740161064SShuo Chen
840161064SShuo Chen#include "EventLoopThread.h"
940161064SShuo Chen
1040161064SShuo Chen#include "EventLoop.h"
1140161064SShuo Chen
1240161064SShuo Chen#include <boost/bind.hpp>
1340161064SShuo Chen
1440161064SShuo Chenusing namespace muduo;
1540161064SShuo Chen
1640161064SShuo ChenEventLoopThread::EventLoopThread()
1740161064SShuo Chen  : loop_(NULL),
1840161064SShuo Chen    exiting_(false),
1940161064SShuo Chen    thread_(boost::bind(&EventLoopThread::threadFunc, this)),
2040161064SShuo Chen    mutex_(),
2140161064SShuo Chen    cond_(mutex_)
2240161064SShuo Chen{
2340161064SShuo Chen}
2440161064SShuo Chen
2540161064SShuo ChenEventLoopThread::~EventLoopThread()
2640161064SShuo Chen{
2740161064SShuo Chen  exiting_ = true;
2840161064SShuo Chen  loop_->quit();
2940161064SShuo Chen  thread_.join();
3040161064SShuo Chen}
3140161064SShuo Chen
3240161064SShuo ChenEventLoop* EventLoopThread::startLoop()
3340161064SShuo Chen{
3440161064SShuo Chen  assert(!thread_.started());
3540161064SShuo Chen  thread_.start();
3640161064SShuo Chen
3740161064SShuo Chen  {
3840161064SShuo Chen    MutexLockGuard lock(mutex_);
3940161064SShuo Chen    while (loop_ == NULL)
4040161064SShuo Chen    {
4140161064SShuo Chen      cond_.wait();
4240161064SShuo Chen    }
4340161064SShuo Chen  }
4440161064SShuo Chen
4540161064SShuo Chen  return loop_;
4640161064SShuo Chen}
4740161064SShuo Chen
4840161064SShuo Chenvoid EventLoopThread::threadFunc()
4940161064SShuo Chen{
5040161064SShuo Chen  EventLoop loop;
5140161064SShuo Chen
5240161064SShuo Chen  {
5340161064SShuo Chen    MutexLockGuard lock(mutex_);
5440161064SShuo Chen    loop_ = &loop;
5540161064SShuo Chen    cond_.notify();
5640161064SShuo Chen  }
5740161064SShuo Chen
5840161064SShuo Chen  loop.loop();
5940161064SShuo Chen  //assert(exiting_);
6040161064SShuo Chen}
6140161064SShuo Chen
62