165c497a3SShuo Chen// excerpts from http://code.google.com/p/muduo/
265c497a3SShuo Chen//
365c497a3SShuo Chen// Use of this source code is governed by a BSD-style license
465c497a3SShuo Chen// that can be found in the License file.
565c497a3SShuo Chen//
665c497a3SShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com)
765c497a3SShuo Chen
865c497a3SShuo Chen#include "EventLoopThread.h"
965c497a3SShuo Chen
1065c497a3SShuo Chen#include "EventLoop.h"
1165c497a3SShuo Chen
1265c497a3SShuo Chen#include <boost/bind.hpp>
1365c497a3SShuo Chen
1465c497a3SShuo Chenusing namespace muduo;
1565c497a3SShuo Chen
1665c497a3SShuo ChenEventLoopThread::EventLoopThread()
1765c497a3SShuo Chen  : loop_(NULL),
1865c497a3SShuo Chen    exiting_(false),
1965c497a3SShuo Chen    thread_(boost::bind(&EventLoopThread::threadFunc, this)),
2065c497a3SShuo Chen    mutex_(),
2165c497a3SShuo Chen    cond_(mutex_)
2265c497a3SShuo Chen{
2365c497a3SShuo Chen}
2465c497a3SShuo Chen
2565c497a3SShuo ChenEventLoopThread::~EventLoopThread()
2665c497a3SShuo Chen{
2765c497a3SShuo Chen  exiting_ = true;
2865c497a3SShuo Chen  loop_->quit();
2965c497a3SShuo Chen  thread_.join();
3065c497a3SShuo Chen}
3165c497a3SShuo Chen
3265c497a3SShuo ChenEventLoop* EventLoopThread::startLoop()
3365c497a3SShuo Chen{
3465c497a3SShuo Chen  assert(!thread_.started());
3565c497a3SShuo Chen  thread_.start();
3665c497a3SShuo Chen
3765c497a3SShuo Chen  {
3865c497a3SShuo Chen    MutexLockGuard lock(mutex_);
3965c497a3SShuo Chen    while (loop_ == NULL)
4065c497a3SShuo Chen    {
4165c497a3SShuo Chen      cond_.wait();
4265c497a3SShuo Chen    }
4365c497a3SShuo Chen  }
4465c497a3SShuo Chen
4565c497a3SShuo Chen  return loop_;
4665c497a3SShuo Chen}
4765c497a3SShuo Chen
4865c497a3SShuo Chenvoid EventLoopThread::threadFunc()
4965c497a3SShuo Chen{
5065c497a3SShuo Chen  EventLoop loop;
5165c497a3SShuo Chen
5265c497a3SShuo Chen  {
5365c497a3SShuo Chen    MutexLockGuard lock(mutex_);
5465c497a3SShuo Chen    loop_ = &loop;
5565c497a3SShuo Chen    cond_.notify();
5665c497a3SShuo Chen  }
5765c497a3SShuo Chen
5865c497a3SShuo Chen  loop.loop();
5965c497a3SShuo Chen  //assert(exiting_);
6065c497a3SShuo Chen}
6165c497a3SShuo Chen
62