| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "mojo/system/local_message_pipe_endpoint.h" | 5 #include "mojo/system/local_message_pipe_endpoint.h" |
| 6 | 6 |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "mojo/system/dispatcher.h" | 10 #include "mojo/system/dispatcher.h" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 } | 24 } |
| 25 | 25 |
| 26 MessagePipeEndpoint::Type LocalMessagePipeEndpoint::GetType() const { | 26 MessagePipeEndpoint::Type LocalMessagePipeEndpoint::GetType() const { |
| 27 return kTypeLocal; | 27 return kTypeLocal; |
| 28 } | 28 } |
| 29 | 29 |
| 30 bool LocalMessagePipeEndpoint::OnPeerClose() { | 30 bool LocalMessagePipeEndpoint::OnPeerClose() { |
| 31 DCHECK(is_open_); | 31 DCHECK(is_open_); |
| 32 DCHECK(is_peer_open_); | 32 DCHECK(is_peer_open_); |
| 33 | 33 |
| 34 MojoWaitFlags old_satisfied_flags = SatisfiedFlags(); | 34 WaitFlagsState old_state = GetWaitFlagsState(); |
| 35 MojoWaitFlags old_satisfiable_flags = SatisfiableFlags(); | |
| 36 is_peer_open_ = false; | 35 is_peer_open_ = false; |
| 37 MojoWaitFlags new_satisfied_flags = SatisfiedFlags(); | 36 WaitFlagsState new_state = GetWaitFlagsState(); |
| 38 MojoWaitFlags new_satisfiable_flags = SatisfiableFlags(); | |
| 39 | 37 |
| 40 if (new_satisfied_flags != old_satisfied_flags || | 38 if (!new_state.equals(old_state)) |
| 41 new_satisfiable_flags != old_satisfiable_flags) { | 39 waiter_list_.AwakeWaitersForStateChange(new_state); |
| 42 waiter_list_.AwakeWaitersForStateChange(new_satisfied_flags, | |
| 43 new_satisfiable_flags); | |
| 44 } | |
| 45 | 40 |
| 46 return true; | 41 return true; |
| 47 } | 42 } |
| 48 | 43 |
| 49 void LocalMessagePipeEndpoint::EnqueueMessage( | 44 void LocalMessagePipeEndpoint::EnqueueMessage( |
| 50 scoped_ptr<MessageInTransit> message) { | 45 scoped_ptr<MessageInTransit> message) { |
| 51 DCHECK(is_open_); | 46 DCHECK(is_open_); |
| 52 DCHECK(is_peer_open_); | 47 DCHECK(is_peer_open_); |
| 53 | 48 |
| 54 bool was_empty = message_queue_.IsEmpty(); | 49 bool was_empty = message_queue_.IsEmpty(); |
| 55 message_queue_.AddMessage(message.Pass()); | 50 message_queue_.AddMessage(message.Pass()); |
| 56 if (was_empty) { | 51 if (was_empty) |
| 57 waiter_list_.AwakeWaitersForStateChange(SatisfiedFlags(), | 52 waiter_list_.AwakeWaitersForStateChange(GetWaitFlagsState()); |
| 58 SatisfiableFlags()); | |
| 59 } | |
| 60 } | 53 } |
| 61 | 54 |
| 62 void LocalMessagePipeEndpoint::Close() { | 55 void LocalMessagePipeEndpoint::Close() { |
| 63 DCHECK(is_open_); | 56 DCHECK(is_open_); |
| 64 is_open_ = false; | 57 is_open_ = false; |
| 65 message_queue_.Clear(); | 58 message_queue_.Clear(); |
| 66 } | 59 } |
| 67 | 60 |
| 68 void LocalMessagePipeEndpoint::CancelAllWaiters() { | 61 void LocalMessagePipeEndpoint::CancelAllWaiters() { |
| 69 DCHECK(is_open_); | 62 DCHECK(is_open_); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 | 110 |
| 118 message = NULL; | 111 message = NULL; |
| 119 | 112 |
| 120 if (enough_space || (flags & MOJO_READ_MESSAGE_FLAG_MAY_DISCARD)) { | 113 if (enough_space || (flags & MOJO_READ_MESSAGE_FLAG_MAY_DISCARD)) { |
| 121 message_queue_.DiscardMessage(); | 114 message_queue_.DiscardMessage(); |
| 122 | 115 |
| 123 // Now it's empty, thus no longer readable. | 116 // Now it's empty, thus no longer readable. |
| 124 if (message_queue_.IsEmpty()) { | 117 if (message_queue_.IsEmpty()) { |
| 125 // It's currently not possible to wait for non-readability, but we should | 118 // It's currently not possible to wait for non-readability, but we should |
| 126 // do the state change anyway. | 119 // do the state change anyway. |
| 127 waiter_list_.AwakeWaitersForStateChange(SatisfiedFlags(), | 120 waiter_list_.AwakeWaitersForStateChange(GetWaitFlagsState()); |
| 128 SatisfiableFlags()); | |
| 129 } | 121 } |
| 130 } | 122 } |
| 131 | 123 |
| 132 if (!enough_space) | 124 if (!enough_space) |
| 133 return MOJO_RESULT_RESOURCE_EXHAUSTED; | 125 return MOJO_RESULT_RESOURCE_EXHAUSTED; |
| 134 | 126 |
| 135 return MOJO_RESULT_OK; | 127 return MOJO_RESULT_OK; |
| 136 } | 128 } |
| 137 | 129 |
| 138 MojoResult LocalMessagePipeEndpoint::AddWaiter(Waiter* waiter, | 130 MojoResult LocalMessagePipeEndpoint::AddWaiter(Waiter* waiter, |
| 139 MojoWaitFlags flags, | 131 MojoWaitFlags flags, |
| 140 MojoResult wake_result) { | 132 MojoResult wake_result) { |
| 141 DCHECK(is_open_); | 133 DCHECK(is_open_); |
| 142 | 134 |
| 143 if ((flags & SatisfiedFlags())) | 135 WaitFlagsState state = GetWaitFlagsState(); |
| 136 if (state.satisfies(flags)) |
| 144 return MOJO_RESULT_ALREADY_EXISTS; | 137 return MOJO_RESULT_ALREADY_EXISTS; |
| 145 if (!(flags & SatisfiableFlags())) | 138 if (!state.can_satisfy(flags)) |
| 146 return MOJO_RESULT_FAILED_PRECONDITION; | 139 return MOJO_RESULT_FAILED_PRECONDITION; |
| 147 | 140 |
| 148 waiter_list_.AddWaiter(waiter, flags, wake_result); | 141 waiter_list_.AddWaiter(waiter, flags, wake_result); |
| 149 return MOJO_RESULT_OK; | 142 return MOJO_RESULT_OK; |
| 150 } | 143 } |
| 151 | 144 |
| 152 void LocalMessagePipeEndpoint::RemoveWaiter(Waiter* waiter) { | 145 void LocalMessagePipeEndpoint::RemoveWaiter(Waiter* waiter) { |
| 153 DCHECK(is_open_); | 146 DCHECK(is_open_); |
| 154 waiter_list_.RemoveWaiter(waiter); | 147 waiter_list_.RemoveWaiter(waiter); |
| 155 } | 148 } |
| 156 | 149 |
| 157 MojoWaitFlags LocalMessagePipeEndpoint::SatisfiedFlags() { | 150 WaitFlagsState LocalMessagePipeEndpoint::GetWaitFlagsState() { |
| 158 MojoWaitFlags satisfied_flags = 0; | 151 WaitFlagsState rv; |
| 159 if (!message_queue_.IsEmpty()) | 152 if (!message_queue_.IsEmpty()) { |
| 160 satisfied_flags |= MOJO_WAIT_FLAG_READABLE; | 153 rv.satisfied_flags |= MOJO_WAIT_FLAG_READABLE; |
| 161 if (is_peer_open_) | 154 rv.satisfiable_flags |= MOJO_WAIT_FLAG_READABLE; |
| 162 satisfied_flags |= MOJO_WAIT_FLAG_WRITABLE; | 155 } |
| 163 return satisfied_flags; | 156 if (is_peer_open_) { |
| 164 } | 157 rv.satisfied_flags |= MOJO_WAIT_FLAG_WRITABLE; |
| 165 | 158 rv.satisfiable_flags |= MOJO_WAIT_FLAG_READABLE | MOJO_WAIT_FLAG_WRITABLE; |
| 166 MojoWaitFlags LocalMessagePipeEndpoint::SatisfiableFlags() { | 159 } |
| 167 MojoWaitFlags satisfiable_flags = 0; | 160 return rv; |
| 168 if (!message_queue_.IsEmpty() || is_peer_open_) | |
| 169 satisfiable_flags |= MOJO_WAIT_FLAG_READABLE; | |
| 170 if (is_peer_open_) | |
| 171 satisfiable_flags |= MOJO_WAIT_FLAG_WRITABLE; | |
| 172 return satisfiable_flags; | |
| 173 } | 161 } |
| 174 | 162 |
| 175 } // namespace system | 163 } // namespace system |
| 176 } // namespace mojo | 164 } // namespace mojo |
| OLD | NEW |