/mordor/iomanager_epoll.h
C Header | 88 lines | 63 code | 18 blank | 7 comment | 0 complexity | 1594e7b7a235f017f1197a28c01e7480 MD5 | raw file
1#ifndef __MORDOR_IOMANAGER_EPOLL_H__ 2#define __MORDOR_IOMANAGER_EPOLL_H__ 3// Copyright (c) 2009 - Mozy, Inc. 4 5#include "scheduler.h" 6#include "timer.h" 7#include "version.h" 8 9#ifndef LINUX 10#error IOManagerEPoll is Linux only 11#endif 12 13namespace Mordor { 14 15class Fiber; 16 17class IOManager : public Scheduler, public TimerManager 18{ 19public: 20 enum Event { 21 NONE = 0x0000, 22 READ = 0x0001, 23 WRITE = 0x0004, 24 CLOSE = 0x2000 25 }; 26 27private: 28 struct AsyncState : boost::noncopyable 29 { 30 AsyncState(); 31 ~AsyncState(); 32 33 struct EventContext 34 { 35 EventContext() : scheduler(NULL) {} 36 Scheduler *scheduler; 37 boost::shared_ptr<Fiber> fiber; 38 boost::function<void ()> dg; 39 }; 40 41 EventContext &contextForEvent(Event event); 42 bool triggerEvent(Event event, size_t &pendingEventCount); 43 void resetContext(EventContext &); 44 45 int m_fd; 46 EventContext m_in, m_out, m_close; 47 Event m_events; 48 boost::mutex m_mutex; 49 50 private: 51 void asyncResetContext(EventContext&); 52 }; 53 54public: 55 /// @param autoStart whether call the start() automatically in constructor 56 /// @note @p autoStart provides a more friendly behavior for derived class 57 /// that inherits from IOManager 58 IOManager(size_t threads = 1, bool useCaller = true, bool autoStart = true, size_t batchSize = 1); 59 ~IOManager(); 60 61 bool stopping(); 62 63 void registerEvent(int fd, Event events, 64 boost::function<void ()> dg = NULL); 65 /// Will not cause the event to fire 66 /// @return If the event was successfully unregistered before firing normally 67 bool unregisterEvent(int fd, Event events); 68 /// Will cause the event to fire 69 bool cancelEvent(int fd, Event events); 70 71protected: 72 bool stopping(unsigned long long &nextTimeout); 73 void idle(); 74 void tickle(); 75 76 void onTimerInsertedAtFront() { tickle(); } 77 78private: 79 int m_epfd; 80 int m_tickleFds[2]; 81 size_t m_pendingEventCount; 82 boost::mutex m_mutex; 83 std::vector<AsyncState *> m_pendingEvents; 84}; 85 86} 87 88#endif