Changeset 8:dba41f33ddce in xplcommon
- Timestamp:
- 12/29/12 08:01:50 (12 years ago)
- Branch:
- default
- Phase:
- public
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
src/xplcommon/WaitableEvent.h
r7 r8 44 44 * An event on which one can wait with a waiter. 45 45 */ 46 class WaitableEvent : public Waitable 46 class WaitableEvent : public Waitable, public Failable 47 47 { 48 48 public: 49 49 /** 50 50 * Construct the event. 51 * 52 * @param waiter the waiter to be used for waiting. If given, the 53 * check() function will always return immediately. If not given, 54 * the check() function may block and returns only when the event 55 * is fired. 51 56 */ 52 57 WaitableEvent(Waiter* waiter = 0); -
src/xplcommon/posix/WaitableEvent.cc
r6 r8 48 48 49 49 WaitableEvent::WaitableEvent(Waiter* waiter) : 50 Waitable(waiter, eventfd(0, EFD_NONBLOCK), POLLIN) 50 Waitable(waiter, eventfd(0, (waiter==0) ? 0 : EFD_NONBLOCK), POLLIN), 51 fired(false) 51 52 { 52 53 assert(fd>=0); … … 67 68 if (write(fd, &one, sizeof(one))<0) { 68 69 if (errno!=EAGAIN) { 69 perror("xplcommon::posix::WaitableEvent::fire: write"); 70 assert(0); 70 setErrorCode(errno); 71 71 } 72 72 } … … 77 77 bool WaitableEvent::check() 78 78 { 79 if ( fired) {79 if (doCheck()) { 80 80 fired = false; 81 81 return true; … … 97 97 { 98 98 if ((events&POLLIN)==POLLIN) { 99 uint64_t value = 0; 100 if (read(fd, &value, sizeof(value))<0) { 101 if (errno!=EAGAIN) { 102 perror("xplcommon::posix::WaitableEvent::handleEvents: read"); 103 assert(0); 104 } 105 } else { 106 fired = value>0; 99 doCheck(); 100 } 101 } 102 103 //------------------------------------------------------------------------------ 104 105 bool WaitableEvent::doCheck(bool fromHandleEvents) 106 { 107 if (failed()) return false; 108 if (!fromHandleEvents && fired) return true; 109 110 uint64_t value = 0; 111 if (read(fd, &value, sizeof(value))<0) { 112 if (errno!=EAGAIN) { 113 setErrorCode(errno); 107 114 } 115 } else { 116 fired = fired || value>0; 108 117 } 118 119 return fired; 109 120 } 110 121 -
src/xplcommon/posix/WaitableEvent.h
r6 r8 34 34 #include "Waitable.h" 35 35 36 #include "../Failable.h" 37 36 38 //------------------------------------------------------------------------------ 37 39 … … 43 45 * An event on which one can wait with a waiter. 44 46 */ 45 class WaitableEvent : public Waitable 47 class WaitableEvent : public Waitable, public Failable 46 48 { 47 49 private: … … 83 85 */ 84 86 virtual void handleEvents(short events); 87 88 private: 89 /** 90 * Perform the real check. If we already know that we are fired, 91 * just return true. Otherwise try to read from the event fd. 92 */ 93 bool doCheck(bool fromHandleEvents = false); 85 94 }; 86 95 -
test/testevent.cc
r4 r8 89 89 //------------------------------------------------------------------------------ 90 90 91 class BlockingServerThread : public Thread 92 { 93 private: 94 WaitableEvent event; 95 96 public: 97 WaitableEvent& getEvent(); 98 99 virtual void run(); 100 }; 101 102 //------------------------------------------------------------------------------ 103 104 inline WaitableEvent& BlockingServerThread::getEvent() 105 { 106 return event; 107 } 108 109 //------------------------------------------------------------------------------ 110 111 void BlockingServerThread::run() 112 { 113 printf("BlockingServerThread::run: waiting...\n"); 114 if (event.check()) { 115 printf("BlockingServerThread::run: waiting done\n"); 116 } else { 117 printf("BlockingServerThread::run: waiting failed\n"); 118 } 119 printf("BlockingServerThread::run: finished\n"); 120 } 121 122 123 //------------------------------------------------------------------------------ 124 //------------------------------------------------------------------------------ 125 91 126 class ClientThread : public Thread 92 127 { … … 120 155 //------------------------------------------------------------------------------ 121 156 122 int main()157 template <class Server> void runTest() 123 158 { 124 Server ThreadserverThread;159 Server serverThread; 125 160 ClientThread clientThread(serverThread.getEvent()); 126 161 … … 134 169 serverThread.join(); 135 170 printf("Both threads returned\n"); 171 } 172 173 //------------------------------------------------------------------------------ 174 //------------------------------------------------------------------------------ 175 176 int main() 177 { 178 printf("Running the event test with a non-blocking WaitableEvent\n\n"); 179 runTest<ServerThread>(); 180 181 printf("\n\nRunning the event test with a blocking WaitableEvent\n\n"); 182 runTest<BlockingServerThread>(); 136 183 137 184 return 0;
Note:
See TracChangeset
for help on using the changeset viewer.