PageRenderTime 21ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/mordor/iomanager_epoll.h

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