PollableEvent.h (2376B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim:set ts=2 sw=2 sts=2 et cindent: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef PollableEvent_h__ 8 #define PollableEvent_h__ 9 10 #include "mozilla/Mutex.h" 11 #include "mozilla/TimeStamp.h" 12 13 struct PRFileDesc; 14 15 namespace mozilla { 16 namespace net { 17 18 // class must be called locked 19 class PollableEvent { 20 public: 21 PollableEvent(); 22 ~PollableEvent(); 23 24 // Signal/Clear return false only if they fail 25 bool Signal(); 26 // This is called only when we get non-null out_flags for the socket pair 27 bool Clear(); 28 bool Valid() { return mWriteFD && mReadFD; } 29 30 // We want to detect if writing to one of the socket pair sockets takes 31 // too long to be received by the other socket from the pair. 32 // Hence, we remember the timestamp of the earliest write by a call to 33 // MarkFirstSignalTimestamp() from Signal(). After waking up from poll() 34 // we check how long it took get the 'readable' signal on the socket pair. 35 void MarkFirstSignalTimestamp(); 36 // Called right before we enter poll() to exclude any possible delay between 37 // the earlist call to Signal() and entering poll() caused by processing 38 // of events dispatched to the socket transport thread. 39 void AdjustFirstSignalTimestamp(); 40 // This returns false on following conditions: 41 // - PR_Write has failed 42 // - no out_flags were signalled on the socket pair for too long after 43 // the earliest Signal() 44 bool IsSignallingAlive(TimeDuration const& timeout); 45 46 PRFileDesc* PollableFD() { return mReadFD; } 47 48 private: 49 PRFileDesc* mWriteFD{nullptr}; 50 PRFileDesc* mReadFD{nullptr}; 51 bool mSignaled{false}; 52 // true when PR_Write to the socket pair has failed (status < 1) 53 bool mWriteFailed{false}; 54 // Set true after AdjustFirstSignalTimestamp() was called 55 // Set false after Clear() was called 56 // Ensures shifting the timestamp before entering poll() only once 57 // between Clear()'ings. 58 bool mSignalTimestampAdjusted{false}; 59 // Timestamp of the first call to Signal() (or time we enter poll()) 60 // that happened after the last Clear() call 61 TimeStamp mFirstSignalAfterClear; 62 }; 63 64 } // namespace net 65 } // namespace mozilla 66 67 #endif