1 // Copyright 2014 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef MOJO_CORE_HANDLE_SIGNALS_STATE_H_ 6 #define MOJO_CORE_HANDLE_SIGNALS_STATE_H_ 7 8 #include "mojo/core/system_impl_export.h" 9 #include "mojo/public/c/system/types.h" 10 11 namespace mojo { 12 namespace core { 13 14 // A convenience wrapper around the MojoHandleSignalsState struct. 15 // 16 // NOTE: This is duplicated in the public C++ SDK to avoid circular 17 // dependencies between the EDK and the public SDK. 18 struct MOJO_SYSTEM_IMPL_EXPORT HandleSignalsState final 19 : public MojoHandleSignalsState { HandleSignalsStatefinal20 HandleSignalsState() { 21 satisfied_signals = MOJO_HANDLE_SIGNAL_NONE; 22 satisfiable_signals = MOJO_HANDLE_SIGNAL_NONE; 23 } 24 HandleSignalsStatefinal25 HandleSignalsState(MojoHandleSignals satisfied, 26 MojoHandleSignals satisfiable) { 27 satisfied_signals = satisfied; 28 satisfiable_signals = satisfiable; 29 } 30 31 bool operator==(const HandleSignalsState& other) const { 32 return satisfied_signals == other.satisfied_signals && 33 satisfiable_signals == other.satisfiable_signals; 34 } 35 36 // TODO(rockot): Remove this in favor of operator==. equalsfinal37 bool equals(const HandleSignalsState& other) const { 38 return satisfied_signals == other.satisfied_signals && 39 satisfiable_signals == other.satisfiable_signals; 40 } 41 satisfies_anyfinal42 bool satisfies_any(MojoHandleSignals signals) const { 43 return !!(satisfied_signals & signals); 44 } 45 satisfies_allfinal46 bool satisfies_all(MojoHandleSignals signals) const { 47 return (satisfied_signals & signals) == signals; 48 } 49 can_satisfy_anyfinal50 bool can_satisfy_any(MojoHandleSignals signals) const { 51 return !!(satisfiable_signals & signals); 52 } 53 54 // The handle is currently readable. May apply to a message pipe handle or 55 // data pipe consumer handle. readablefinal56 bool readable() const { return satisfies_any(MOJO_HANDLE_SIGNAL_READABLE); } 57 58 // The handle is currently writable. May apply to a message pipe handle or 59 // data pipe producer handle. writablefinal60 bool writable() const { return satisfies_any(MOJO_HANDLE_SIGNAL_WRITABLE); } 61 62 // The handle's peer is closed. May apply to any message pipe or data pipe 63 // handle. peer_closedfinal64 bool peer_closed() const { 65 return satisfies_any(MOJO_HANDLE_SIGNAL_PEER_CLOSED); 66 } 67 68 // The handle's peer exists in a remote execution context (e.g. in another 69 // process.) peer_remotefinal70 bool peer_remote() const { 71 return satisfies_any(MOJO_HANDLE_SIGNAL_PEER_REMOTE); 72 } 73 74 // Indicates whether the handle has exceeded some quota limit. quota_exceededfinal75 bool quota_exceeded() const { 76 return satisfies_any(MOJO_HANDLE_SIGNAL_QUOTA_EXCEEDED); 77 } 78 79 // The handle will never be |readable()| again. never_readablefinal80 bool never_readable() const { 81 return !can_satisfy_any(MOJO_HANDLE_SIGNAL_READABLE); 82 } 83 84 // The handle will never be |writable()| again. never_writablefinal85 bool never_writable() const { 86 return !can_satisfy_any(MOJO_HANDLE_SIGNAL_WRITABLE); 87 } 88 89 // The handle can never indicate |peer_closed()|. Never true for message pipe 90 // or data pipe handles (they can always signal peer closure), but always true 91 // for other types of handles (they have no peer.) never_peer_closedfinal92 bool never_peer_closed() const { 93 return !can_satisfy_any(MOJO_HANDLE_SIGNAL_PEER_CLOSED); 94 } 95 96 // The handle will never indicate |peer_remote()| again. True iff the peer is 97 // known to be closed. never_peer_remotefinal98 bool never_peer_remote() const { 99 return !can_satisfy_any(MOJO_HANDLE_SIGNAL_PEER_REMOTE); 100 } 101 102 // (Copy and assignment allowed.) 103 }; 104 105 static_assert(sizeof(HandleSignalsState) == sizeof(MojoHandleSignalsState), 106 "HandleSignalsState should add no overhead"); 107 108 } // namespace core 109 } // namespace mojo 110 111 #endif // MOJO_CORE_HANDLE_SIGNALS_STATE_H_ 112