summaryrefslogtreecommitdiffstats
path: root/netwerk/base/PollableEvent.h
blob: feb637f6cc85210850e7cac79c01b816593a6eec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef PollableEvent_h__
#define PollableEvent_h__

#include "mozilla/Mutex.h"
#include "mozilla/TimeStamp.h"

struct PRFileDesc;

namespace mozilla {
namespace net {

// class must be called locked
class PollableEvent {
 public:
  PollableEvent();
  ~PollableEvent();

  // Signal/Clear return false only if they fail
  bool Signal();
  // This is called only when we get non-null out_flags for the socket pair
  bool Clear();
  bool Valid() { return mWriteFD && mReadFD; }

  // We want to detect if writing to one of the socket pair sockets takes
  // too long to be received by the other socket from the pair.
  // Hence, we remember the timestamp of the earliest write by a call to
  // MarkFirstSignalTimestamp() from Signal().  After waking up from poll()
  // we check how long it took get the 'readable' signal on the socket pair.
  void MarkFirstSignalTimestamp();
  // Called right before we enter poll() to exclude any possible delay between
  // the earlist call to Signal() and entering poll() caused by processing
  // of events dispatched to the socket transport thread.
  void AdjustFirstSignalTimestamp();
  // This returns false on following conditions:
  // - PR_Write has failed
  // - no out_flags were signalled on the socket pair for too long after
  //   the earliest Signal()
  bool IsSignallingAlive(TimeDuration const& timeout);

  PRFileDesc* PollableFD() { return mReadFD; }

 private:
  PRFileDesc* mWriteFD;
  PRFileDesc* mReadFD;
  bool mSignaled;
  // true when PR_Write to the socket pair has failed (status < 1)
  bool mWriteFailed;
  // Set true after AdjustFirstSignalTimestamp() was called
  // Set false after Clear() was called
  // Ensures shifting the timestamp before entering poll() only once
  // between Clear()'ings.
  bool mSignalTimestampAdjusted;
  // Timestamp of the first call to Signal() (or time we enter poll())
  // that happened after the last Clear() call
  TimeStamp mFirstSignalAfterClear;
};

}  // namespace net
}  // namespace mozilla

#endif