| 1 | // -------------------------------------------------------------------------- |
|---|
| 2 | // |
|---|
| 3 | // File |
|---|
| 4 | // Name: WaitForEvent.h |
|---|
| 5 | // Purpose: Generic waiting for events, using an efficient method (platform dependent) |
|---|
| 6 | // Created: 9/3/04 |
|---|
| 7 | // |
|---|
| 8 | // -------------------------------------------------------------------------- |
|---|
| 9 | |
|---|
| 10 | #ifndef WAITFOREVENT__H |
|---|
| 11 | #define WAITFOREVENT__H |
|---|
| 12 | |
|---|
| 13 | #include <cstdlib> |
|---|
| 14 | |
|---|
| 15 | #ifdef HAVE_KQUEUE |
|---|
| 16 | #include <sys/event.h> |
|---|
| 17 | #include <sys/time.h> |
|---|
| 18 | #else |
|---|
| 19 | #include <vector> |
|---|
| 20 | #ifndef WIN32 |
|---|
| 21 | #include <poll.h> |
|---|
| 22 | #endif |
|---|
| 23 | #endif |
|---|
| 24 | |
|---|
| 25 | #include <cstdlib> |
|---|
| 26 | |
|---|
| 27 | #include "CommonException.h" |
|---|
| 28 | |
|---|
| 29 | #include "MemLeakFindOn.h" |
|---|
| 30 | |
|---|
| 31 | class WaitForEvent |
|---|
| 32 | { |
|---|
| 33 | public: |
|---|
| 34 | WaitForEvent(int Timeout = TimeoutInfinite); |
|---|
| 35 | ~WaitForEvent(); |
|---|
| 36 | private: |
|---|
| 37 | // No copying. |
|---|
| 38 | WaitForEvent(const WaitForEvent &); |
|---|
| 39 | WaitForEvent &operator=(const WaitForEvent &); |
|---|
| 40 | public: |
|---|
| 41 | |
|---|
| 42 | enum |
|---|
| 43 | { |
|---|
| 44 | TimeoutInfinite = -1 |
|---|
| 45 | }; |
|---|
| 46 | |
|---|
| 47 | void SetTimeout(int Timeout = TimeoutInfinite); |
|---|
| 48 | |
|---|
| 49 | void *Wait(); |
|---|
| 50 | |
|---|
| 51 | #ifndef HAVE_KQUEUE |
|---|
| 52 | typedef struct |
|---|
| 53 | { |
|---|
| 54 | int fd; |
|---|
| 55 | short events; |
|---|
| 56 | void *item; |
|---|
| 57 | } ItemInfo; |
|---|
| 58 | #endif |
|---|
| 59 | |
|---|
| 60 | // -------------------------------------------------------------------------- |
|---|
| 61 | // |
|---|
| 62 | // Function |
|---|
| 63 | // Name: WaitForEvent::Add(const Type &, int) |
|---|
| 64 | // Purpose: Adds an event to the list of items to wait on. The flags are passed to the object. |
|---|
| 65 | // Created: 9/3/04 |
|---|
| 66 | // |
|---|
| 67 | // -------------------------------------------------------------------------- |
|---|
| 68 | template<typename T> |
|---|
| 69 | void Add(const T *pItem, int Flags = 0) |
|---|
| 70 | { |
|---|
| 71 | ASSERT(pItem != 0); |
|---|
| 72 | #ifdef HAVE_KQUEUE |
|---|
| 73 | struct kevent e; |
|---|
| 74 | pItem->FillInKEvent(e, Flags); |
|---|
| 75 | // Fill in extra flags to say what to do |
|---|
| 76 | e.flags |= EV_ADD; |
|---|
| 77 | e.udata = (void*)pItem; |
|---|
| 78 | if(::kevent(mKQueue, &e, 1, NULL, 0, NULL) == -1) |
|---|
| 79 | { |
|---|
| 80 | THROW_EXCEPTION(CommonException, KEventErrorAdd) |
|---|
| 81 | } |
|---|
| 82 | #else |
|---|
| 83 | // Add item |
|---|
| 84 | ItemInfo i; |
|---|
| 85 | pItem->FillInPoll(i.fd, i.events, Flags); |
|---|
| 86 | i.item = (void*)pItem; |
|---|
| 87 | mItems.push_back(i); |
|---|
| 88 | // Delete any pre-prepared poll info, as it's now out of date |
|---|
| 89 | if(mpPollInfo != 0) |
|---|
| 90 | { |
|---|
| 91 | ::free(mpPollInfo); |
|---|
| 92 | mpPollInfo = 0; |
|---|
| 93 | } |
|---|
| 94 | #endif |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | // -------------------------------------------------------------------------- |
|---|
| 98 | // |
|---|
| 99 | // Function |
|---|
| 100 | // Name: WaitForEvent::Remove(const Type &, int) |
|---|
| 101 | // Purpose: Removes an event from the list of items to wait on. The flags are passed to the object. |
|---|
| 102 | // Created: 9/3/04 |
|---|
| 103 | // |
|---|
| 104 | // -------------------------------------------------------------------------- |
|---|
| 105 | template<typename T> |
|---|
| 106 | void Remove(const T *pItem, int Flags = 0) |
|---|
| 107 | { |
|---|
| 108 | ASSERT(pItem != 0); |
|---|
| 109 | #ifdef HAVE_KQUEUE |
|---|
| 110 | struct kevent e; |
|---|
| 111 | pItem->FillInKEvent(e, Flags); |
|---|
| 112 | // Fill in extra flags to say what to do |
|---|
| 113 | e.flags |= EV_DELETE; |
|---|
| 114 | e.udata = (void*)pItem; |
|---|
| 115 | if(::kevent(mKQueue, &e, 1, NULL, 0, NULL) == -1) |
|---|
| 116 | { |
|---|
| 117 | THROW_EXCEPTION(CommonException, KEventErrorRemove) |
|---|
| 118 | } |
|---|
| 119 | #else |
|---|
| 120 | if(mpPollInfo != 0) |
|---|
| 121 | { |
|---|
| 122 | ::free(mpPollInfo); |
|---|
| 123 | mpPollInfo = 0; |
|---|
| 124 | } |
|---|
| 125 | for(std::vector<ItemInfo>::iterator i(mItems.begin()); i != mItems.end(); ++i) |
|---|
| 126 | { |
|---|
| 127 | if(i->item == pItem) |
|---|
| 128 | { |
|---|
| 129 | mItems.erase(i); |
|---|
| 130 | return; |
|---|
| 131 | } |
|---|
| 132 | } |
|---|
| 133 | #endif |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | private: |
|---|
| 137 | #ifdef HAVE_KQUEUE |
|---|
| 138 | int mKQueue; |
|---|
| 139 | struct timespec mTimeout; |
|---|
| 140 | struct timespec *mpTimeout; |
|---|
| 141 | #else |
|---|
| 142 | int mTimeout; |
|---|
| 143 | std::vector<ItemInfo> mItems; |
|---|
| 144 | struct pollfd *mpPollInfo; |
|---|
| 145 | #endif |
|---|
| 146 | }; |
|---|
| 147 | |
|---|
| 148 | #include "MemLeakFindOff.h" |
|---|
| 149 | |
|---|
| 150 | #endif // WAITFOREVENT__H |
|---|
| 151 | |
|---|
| 152 | |
|---|