Skip to content

Commit

Permalink
TimerQueue half done.
Browse files Browse the repository at this point in the history
  • Loading branch information
chenshuo committed Mar 13, 2010
1 parent 856d45c commit f0f6632
Show file tree
Hide file tree
Showing 19 changed files with 300 additions and 58 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 2.6)
project(muduo CXX)

set(CMAKE_CXX_FLAGS
"-g -Wall -Wextra -Werror -Wconversion -Wold-style-cast -Wpointer-arith -Wshadow -Wno-unused-parameter -Wwrite-strings -rdynamic"
"-g -Wall -Wextra -Werror -Wconversion -Wold-style-cast -Wpointer-arith -Wshadow -Wno-unused-parameter -Wwrite-strings -rdynamic -lpthread"
)
include_directories(${PROJECT_SOURCE_DIR})
add_subdirectory(muduo/base)
Expand Down
2 changes: 1 addition & 1 deletion Doxyfile
Original file line number Diff line number Diff line change
Expand Up @@ -1400,7 +1400,7 @@ GROUP_GRAPHS = YES
# collaboration diagrams in a style similar to the OMG's Unified Modeling
# Language.

UML_LOOK = YES
UML_LOOK = NO

# If set to YES, the inheritance and collaboration graphs will show the
# relations between templates and their instances.
Expand Down
5 changes: 2 additions & 3 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

SOURCE_DIR=`pwd`
BUILD_DIR=${BUILD_DIR:-../build}
cd $BUILD_DIR
cmake $SOURCE_DIR
make
mkdir -p $BUILD_DIR
cd $BUILD_DIR && cmake $SOURCE_DIR && make

4 changes: 3 additions & 1 deletion muduo/base/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
set(base_SRCS
UtcTime.cc)
Thread.cc
UtcTime.cc
)

add_library(muduo_base ${base_SRCS})

Expand Down
66 changes: 66 additions & 0 deletions muduo/base/Mutex.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#ifndef MUDUO_BASE_MUTEX_H
#define MUDUO_BASE_MUTEX_H

#include <pthread.h>

#include <boost/noncopyable.hpp>

namespace muduo
{

class MutexLock : boost::noncopyable
{
public:
MutexLock()
{
pthread_mutex_init(&mutex_, NULL);
}

~MutexLock()
{
pthread_mutex_destroy(&mutex_);
}

void lock()
{
pthread_mutex_lock(&mutex_);
}

void unlock()
{
pthread_mutex_unlock(&mutex_);
}

pthread_mutex_t* getPthreadMutex() /* non-const */
{
return &mutex_;
}

private:

pthread_mutex_t mutex_;
};

class MutexLockGuard : boost::noncopyable
{
public:
explicit MutexLockGuard(MutexLock& mutex) : mutex_(mutex)
{
mutex_.lock();
}

~MutexLockGuard()
{
mutex_.unlock();
}

private:

MutexLock& mutex_;
};

}

#define MutexLockGuard(x) error

#endif
55 changes: 55 additions & 0 deletions muduo/base/Thread.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <muduo/base/Thread.h>

#include <unistd.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <linux/unistd.h>

namespace
{
__thread pid_t t_tid = 0;

pid_t gettid()
{
return static_cast<pid_t>(::syscall(SYS_gettid));
}

void* startThread(void* cb)
{
muduo::Thread::ThreadFunc* func = static_cast<muduo::Thread::ThreadFunc*>(cb);
t_tid = gettid();
(*func)();
return NULL;
}
}

using namespace muduo;

Thread::Thread(const ThreadFunc& func)
: ptid_(0),
func_(func)
{
}

Thread::~Thread()
{
}

void Thread::start()
{
pthread_create(&ptid_, NULL, &startThread, &func_);
}

void Thread::join()
{
pthread_join(ptid_, NULL);
}

pid_t CurrentThread::tid()
{
if (t_tid == 0) {
t_tid = gettid();
}
return t_tid;
}

34 changes: 34 additions & 0 deletions muduo/base/Thread.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#ifndef MUDUO_BASE_THREAD_H
#define MUDUO_BASE_THREAD_H

#include <pthread.h>
#include <boost/function.hpp>

namespace muduo
{

class Thread
{
public:
typedef boost::function<void ()> ThreadFunc;

explicit Thread(const ThreadFunc&);
~Thread();

void start();
void join();

private:

pthread_t ptid_;
ThreadFunc func_;
};

namespace CurrentThread
{
pid_t tid();
}

}

#endif
3 changes: 1 addition & 2 deletions muduo/base/UtcTime.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ inline double timeDifference(UtcTime high, UtcTime low)
///
/// Add @c seconds to given timestamp.
///
/// @param high, low
/// @return (high-low) in seconds
/// @return timestamp+seconds as UtcTime
///
inline UtcTime addTime(UtcTime timestamp, double seconds)
{
Expand Down
29 changes: 27 additions & 2 deletions muduo/net/Channel.cc
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
#include <muduo/net/Channel.h>

#include <poll.h>

using namespace muduo;
using namespace muduo::net;

Channel::Channel(EventLoop* loop, Socket sock)
const int Channel::kReadEvent = POLLIN;

Channel::Channel(EventLoop* loop, int fd__)
: loop_(loop),
sock_(sock),
fd_(fd__),
events_(0)
{
}
Expand All @@ -14,3 +18,24 @@ Channel::~Channel()
{
}

void Channel::handle_event()
{
if ((revents_ & POLLHUP) && !(revents_ & POLLIN))
{
//FIXME handleClose();
}

if (revents_ & (POLLERR|POLLNVAL))
{
if (errorCallback_) errorCallback_();
}
if (revents_ & (POLLIN | POLLPRI | POLLRDHUP))
{
if (readCallback_) readCallback_();
}
if (revents_ & POLLOUT)
{
if (writeCallback_) writeCallback_();
}
}

31 changes: 23 additions & 8 deletions muduo/net/Channel.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef NET_CHANNEL_H
#define NET_CHANNEL_H

#include <boost/function.hpp>
#include <boost/noncopyable.hpp>

#include <muduo/net/Socket.h>
Expand All @@ -12,30 +13,44 @@ namespace net

class EventLoop;

///
/// A selectable I/O channel.
/// The class doesn't own the file descriptor.
///
class Channel : boost::noncopyable
{
public:
Channel(EventLoop* loop, Socket sock);
typedef boost::function<void()> EventCallback;
static const int kNoneEvent;
static const int kReadEvent;
static const int kWriteEvent;
static const int kErrorvent;

Channel(EventLoop* loop, int fd);
~Channel();

void handle(int revents);
void handle_event();
void setReadCallback(const EventCallback& cb) { readCallback_ = cb; }
void setWriteCallback(const EventCallback& cb) { writeCallback_ = cb; }
void setErrorCallback(const EventCallback& cb) { errorCallback_ = cb; }

/*
int fd() { return fd_; }
void set_fd(int _fd) { fd_ = _fd; }
int events() { return events_; }
void set_events(int events0) { events_ = events0; }
*/
void set_events(int evt) { events_ = evt; }
void set_revents(int revt) { revents_ = revt; }

EventLoop* getLoop() { return loop_; }

// void set_loop(EventLoop* loop) { loop_ = loop; }

private:
EventLoop* loop_;
Socket sock_;
const int fd_;
int events_;
int revents_;
EventCallback readCallback_;
EventCallback writeCallback_;
EventCallback errorCallback_;
};

}
Expand Down
34 changes: 15 additions & 19 deletions muduo/net/EventLoop.cc
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include <muduo/net/EventLoop.h>

#include <muduo/base/UtcTime.h>
#include <muduo/base/Mutex.h>
#include <muduo/base/Thread.h>
#include <muduo/net/Channel.h>
//#include <net/internal/Log.h>
#include <muduo/net/Poller.h>
#include <muduo/net/TimerQueue.h>

Expand All @@ -11,8 +11,10 @@ using namespace muduo::net;

EventLoop::EventLoop()
: poller_(Poller::newDefaultPoller()),
timerQueue_(new TimerQueue),
quit_(false)
timerQueue_(new TimerQueue(this)),
looping_(false),
quit_(false),
thread_(CurrentThread::tid())
{
init();
}
Expand All @@ -23,33 +25,27 @@ EventLoop::~EventLoop()

void EventLoop::loop()
{
assert(!looping_);
looping_ = true;
while (!quit_)
{
poller_->poll(1000);
}
/*
while (!quit_)
{
UtcTime now(UtcTime::now());
UtcTime next(timerQueue_->tick(now));
int timeout = next.valid() ? static_cast<int>((timeDifference(next, now))*1000) : 1000;
if (timeout <= 0)
activeChannels_.clear();
poller_->poll(1000, &activeChannels_);
for (ChannelList::iterator it = activeChannels_.begin();
it != activeChannels_.end(); ++it)
{
timeout = 1;
(*it)->handle_event();
}
NetLogInfo << "polling " << timeout << NetSend;
poller_->poll(timeout);
}
*/
looping_ = false;
}

void EventLoop::quit()
{
quit_ = true;
}

void EventLoop::addChannel(Channel* channel)
void EventLoop::updateChannel(Channel* channel)
{
assert(channel->getLoop() == this);
// channel->set_loop(this);
Expand Down
Loading

0 comments on commit f0f6632

Please sign in to comment.