Skip to content

Commit

Permalink
edit format
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaozhengcoder committed Nov 3, 2018
1 parent 356d873 commit df3899f
Show file tree
Hide file tree
Showing 10 changed files with 295 additions and 272 deletions.
20 changes: 10 additions & 10 deletions muduo_tutorial/recipes/reactor/s01/Channel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,32 @@ const int Channel::kWriteEvent = POLLOUT;
Channel::Channel(EventLoop *loop, int fdArg)
: loop_(loop), fd_(fdArg), events_(0), revents_(0), index_(-1) {}

void Channel::update()
void Channel::update()
{
loop_->updateChannel(this); // EventLoop* loop_; //void
// EventLoop::updateChannel(Channel* channel)
// EventLoop::updateChannel(Channel* channel)
}

void Channel::handleEvent()
void Channel::handleEvent()
{
if (revents_ & POLLNVAL)
if (revents_ & POLLNVAL)
{
LOG_WARN << "Channel::handle_event() POLLNVAL";
}

if (revents_ & (POLLERR | POLLNVAL))
if (revents_ & (POLLERR | POLLNVAL))
{
if (errorCallback_)
errorCallback_();
errorCallback_();
}
if (revents_ & (POLLIN | POLLPRI | POLLRDHUP))
if (revents_ & (POLLIN | POLLPRI | POLLRDHUP))
{
if (readCallback_)
readCallback_();
readCallback_();
}
if (revents_ & POLLOUT)
if (revents_ & POLLOUT)
{
if (writeCallback_)
writeCallback_();
writeCallback_();
}
}
104 changes: 57 additions & 47 deletions muduo_tutorial/recipes/reactor/s01/Channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,52 +24,62 @@ class EventLoop;
/// an eventfd, a timerfd, or a signalfd
class Channel : boost::noncopyable
{
public:
typedef boost::function<void()> EventCallback;

Channel(EventLoop* loop, int fd);

void handleEvent();
void setReadCallback(const EventCallback& cb)
{ readCallback_ = cb; }
void setWriteCallback(const EventCallback& cb)
{ writeCallback_ = cb; }
void setErrorCallback(const EventCallback& cb)
{ errorCallback_ = cb; }

int fd() const { return fd_; }
int events() const { return events_; }
void set_revents(int revt) { revents_ = revt; }
bool isNoneEvent() const { return events_ == kNoneEvent; }

void enableReading() { events_ |= kReadEvent; update(); }
// void enableWriting() { events_ |= kWriteEvent; update(); }
// void disableWriting() { events_ &= ~kWriteEvent; update(); }
// void disableAll() { events_ = kNoneEvent; update(); }

// for Poller
int index() { return index_; }
void set_index(int idx) { index_ = idx; }

EventLoop* ownerLoop() { return loop_; }

private:
void update();

static const int kNoneEvent;
static const int kReadEvent;
static const int kWriteEvent;

EventLoop* loop_;
const int fd_;
int events_;
int revents_;
int index_; // used by Poller.

EventCallback readCallback_;
EventCallback writeCallback_;
EventCallback errorCallback_;
public:
typedef boost::function<void()> EventCallback;

Channel(EventLoop *loop, int fd);

void handleEvent();
void setReadCallback(const EventCallback &cb)
{
readCallback_ = cb;
}
void setWriteCallback(const EventCallback &cb)
{
writeCallback_ = cb;
}
void setErrorCallback(const EventCallback &cb)
{
errorCallback_ = cb;
}

int fd() const { return fd_; }
int events() const { return events_; }
void set_revents(int revt) { revents_ = revt; }
bool isNoneEvent() const { return events_ == kNoneEvent; }

void enableReading()
{
events_ |= kReadEvent;
update();
}
// void enableWriting() { events_ |= kWriteEvent; update(); }
// void disableWriting() { events_ &= ~kWriteEvent; update(); }
// void disableAll() { events_ = kNoneEvent; update(); }

// for Poller
int index() { return index_; }
void set_index(int idx) { index_ = idx; }

EventLoop *ownerLoop() { return loop_; }

private:
void update();

static const int kNoneEvent;
static const int kReadEvent;
static const int kWriteEvent;

EventLoop *loop_;
const int fd_;
int events_;
int revents_;
int index_; // used by Poller.

EventCallback readCallback_;
EventCallback writeCallback_;
EventCallback errorCallback_;
};

}
#endif // MUDUO_NET_CHANNEL_H
} // namespace muduo
#endif // MUDUO_NET_CHANNEL_H
83 changes: 41 additions & 42 deletions muduo_tutorial/recipes/reactor/s01/EventLoop.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,73 +16,72 @@

using namespace muduo;

__thread EventLoop* t_loopInThisThread = 0;
__thread EventLoop *t_loopInThisThread = 0;
const int kPollTimeMs = 10000;

EventLoop::EventLoop()
: looping_(false),
quit_(false),
threadId_(CurrentThread::tid()),
poller_(new Poller(this))
: looping_(false),
quit_(false),
threadId_(CurrentThread::tid()),
poller_(new Poller(this))
{
LOG_TRACE << "EventLoop created " << this << " in thread " << threadId_;
if (t_loopInThisThread)
{
LOG_FATAL << "Another EventLoop " << t_loopInThisThread
<< " exists in this thread " << threadId_;
}
else
{
t_loopInThisThread = this;
}
LOG_TRACE << "EventLoop created " << this << " in thread " << threadId_;
if (t_loopInThisThread)
{
LOG_FATAL << "Another EventLoop " << t_loopInThisThread
<< " exists in this thread " << threadId_;
}
else
{
t_loopInThisThread = this;
}
}

EventLoop::~EventLoop()
{
assert(!looping_);
t_loopInThisThread = NULL;
assert(!looping_);
t_loopInThisThread = NULL;
}

void EventLoop::loop()
{
assert(!looping_);
assertInLoopThread();
looping_ = true;
quit_ = false;
assert(!looping_);
assertInLoopThread();
looping_ = true;
quit_ = false;

while (!quit_)
{
activeChannels_.clear();
poller_->poll(kPollTimeMs, &activeChannels_);

for (ChannelList::iterator it = activeChannels_.begin();
it != activeChannels_.end(); ++it)
while (!quit_)
{
(*it)->handleEvent();
activeChannels_.clear();
poller_->poll(kPollTimeMs, &activeChannels_);

for (ChannelList::iterator it = activeChannels_.begin();
it != activeChannels_.end(); ++it)
{
(*it)->handleEvent();
}
}
}

LOG_TRACE << "EventLoop " << this << " stop looping";
looping_ = false;
LOG_TRACE << "EventLoop " << this << " stop looping";
looping_ = false;
}

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

void EventLoop::updateChannel(Channel* channel)
void EventLoop::updateChannel(Channel *channel)
{
assert(channel->ownerLoop() == this);
assertInLoopThread();
poller_->updateChannel(channel);
assert(channel->ownerLoop() == this);
assertInLoopThread();
poller_->updateChannel(channel);
}

void EventLoop::abortNotInLoopThread()
{
LOG_FATAL << "EventLoop::abortNotInLoopThread - EventLoop " << this
<< " was created in threadId_ = " << threadId_
<< ", current thread id = " << CurrentThread::tid();
LOG_FATAL << "EventLoop::abortNotInLoopThread - EventLoop " << this
<< " was created in threadId_ = " << threadId_
<< ", current thread id = " << CurrentThread::tid();
}

62 changes: 30 additions & 32 deletions muduo_tutorial/recipes/reactor/s01/EventLoop.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,49 +21,47 @@ class Poller;

class EventLoop : boost::noncopyable
{
public:
public:
EventLoop();

EventLoop();
// force out-line dtor, for scoped_ptr members.
~EventLoop();

// force out-line dtor, for scoped_ptr members.
~EventLoop();
///
/// Loops forever.
///
/// Must be called in the same thread as creation of the object.
///
void loop();

///
/// Loops forever.
///
/// Must be called in the same thread as creation of the object.
///
void loop();
void quit();

void quit();
// internal use only
void updateChannel(Channel *channel);
// void removeChannel(Channel* channel);

// internal use only
void updateChannel(Channel* channel);
// void removeChannel(Channel* channel);

void assertInLoopThread()
{
if (!isInLoopThread())
void assertInLoopThread()
{
abortNotInLoopThread();
if (!isInLoopThread())
{
abortNotInLoopThread();
}
}
}

bool isInLoopThread() const { return threadId_ == CurrentThread::tid(); }

private:
bool isInLoopThread() const { return threadId_ == CurrentThread::tid(); }

void abortNotInLoopThread();
private:
void abortNotInLoopThread();

typedef std::vector<Channel*> ChannelList;
typedef std::vector<Channel *> ChannelList;

bool looping_; /* atomic */
bool quit_; /* atomic */
const pid_t threadId_;
boost::scoped_ptr<Poller> poller_;
ChannelList activeChannels_;
bool looping_; /* atomic */
bool quit_; /* atomic */
const pid_t threadId_;
boost::scoped_ptr<Poller> poller_;
ChannelList activeChannels_;
};

}
} // namespace muduo

#endif // MUDUO_NET_EVENTLOOP_H
#endif // MUDUO_NET_EVENTLOOP_H
Loading

0 comments on commit df3899f

Please sign in to comment.