Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #if !defined(DART_IO_DISABLED) | 5 #if !defined(DART_IO_DISABLED) |
| 6 | 6 |
| 7 #include "bin/socket.h" | 7 #include "bin/socket.h" |
| 8 | 8 |
| 9 #include "bin/dartutils.h" | 9 #include "bin/dartutils.h" |
| 10 #include "bin/io_buffer.h" | 10 #include "bin/io_buffer.h" |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 120 sockets_by_port_[allocated_port] = os_socket; | 120 sockets_by_port_[allocated_port] = os_socket; |
| 121 sockets_by_fd_[socketfd] = os_socket; | 121 sockets_by_fd_[socketfd] = os_socket; |
| 122 | 122 |
| 123 // We set as a side-effect the port on the dart socket_object. | 123 // We set as a side-effect the port on the dart socket_object. |
| 124 Socket::SetSocketIdNativeField(socket_object, socketfd); | 124 Socket::SetSocketIdNativeField(socket_object, socketfd); |
| 125 | 125 |
| 126 return Dart_True(); | 126 return Dart_True(); |
| 127 } | 127 } |
| 128 | 128 |
| 129 | 129 |
| 130 bool ListeningSocketRegistry::CloseOneSafe(OSSocket* os_socket) { | |
|
siva
2016/08/09 21:41:43
ASSERT(mutex_ is owned by current thread);
ASSERT(
zra
2016/08/09 22:19:27
For the mutexes under bin there is no "is owned by
| |
| 131 ASSERT(os_socket->ref_count > 0); | |
| 132 os_socket->ref_count--; | |
| 133 if (os_socket->ref_count > 0) { | |
| 134 return false; | |
| 135 } | |
| 136 // We free the OS socket by removing it from two datastructures. | |
| 137 sockets_by_fd_.erase(os_socket->socketfd); | |
| 138 | |
| 139 OSSocket *prev = NULL; | |
| 140 OSSocket *current = sockets_by_port_[os_socket->port]; | |
| 141 while (current != os_socket) { | |
| 142 ASSERT(current != NULL); | |
| 143 prev = current; | |
| 144 current = current->next; | |
| 145 } | |
| 146 | |
| 147 if ((prev == NULL) && (current->next == NULL)) { | |
| 148 // Remove last element from the list. | |
| 149 sockets_by_port_.erase(os_socket->port); | |
| 150 } else if (prev == NULL) { | |
| 151 // Remove first element of the list. | |
| 152 sockets_by_port_[os_socket->port] = current->next; | |
| 153 } else { | |
| 154 // Remove element from the list which is not the first one. | |
| 155 prev->next = os_socket->next; | |
| 156 } | |
| 157 | |
|
siva
2016/08/09 21:41:43
ASSERT(os_socket->ref_count == 0);
zra
2016/08/09 22:19:27
Done.
| |
| 158 delete os_socket; | |
| 159 return true; | |
| 160 } | |
| 161 | |
| 162 | |
| 163 void ListeningSocketRegistry::CloseAllSafe() { | |
| 164 MutexLocker ml(mutex_); | |
| 165 SocketsIterator it = sockets_by_fd_.begin(); | |
| 166 while (it != sockets_by_fd_.end()) { | |
| 167 CloseOneSafe(it->second); | |
| 168 it++; | |
| 169 } | |
| 170 } | |
| 171 | |
| 172 | |
| 130 bool ListeningSocketRegistry::CloseSafe(intptr_t socketfd) { | 173 bool ListeningSocketRegistry::CloseSafe(intptr_t socketfd) { |
| 131 ASSERT(!mutex_->TryLock()); | 174 ASSERT(!mutex_->TryLock()); |
| 132 | 175 |
| 133 SocketsIterator it = sockets_by_fd_.find(socketfd); | 176 SocketsIterator it = sockets_by_fd_.find(socketfd); |
| 134 if (it != sockets_by_fd_.end()) { | 177 if (it != sockets_by_fd_.end()) { |
| 135 OSSocket *os_socket = it->second; | 178 OSSocket *os_socket = it->second; |
| 136 | 179 return CloseOneSafe(os_socket); |
|
siva
2016/08/09 21:41:43
why not
CloseOneSafe(it->second);
zra
2016/08/09 22:19:27
Done.
| |
| 137 ASSERT(os_socket->ref_count > 0); | |
| 138 os_socket->ref_count--; | |
| 139 if (os_socket->ref_count == 0) { | |
| 140 // We free the OS socket by removing it from two datastructures. | |
| 141 sockets_by_fd_.erase(socketfd); | |
| 142 | |
| 143 OSSocket *prev = NULL; | |
| 144 OSSocket *current = sockets_by_port_[os_socket->port]; | |
| 145 while (current != os_socket) { | |
| 146 ASSERT(current != NULL); | |
| 147 prev = current; | |
| 148 current = current->next; | |
| 149 } | |
| 150 | |
| 151 if ((prev == NULL) && (current->next == NULL)) { | |
| 152 // Remove last element from the list. | |
| 153 sockets_by_port_.erase(os_socket->port); | |
| 154 } else if (prev == NULL) { | |
| 155 // Remove first element of the list. | |
| 156 sockets_by_port_[os_socket->port] = current->next; | |
| 157 } else { | |
| 158 // Remove element from the list which is not the first one. | |
| 159 prev->next = os_socket->next; | |
| 160 } | |
| 161 | |
| 162 delete os_socket; | |
| 163 return true; | |
| 164 } | |
| 165 return false; | |
| 166 } else { | 180 } else { |
| 167 // It should be impossible for the event handler to close something that | 181 // It should be impossible for the event handler to close something that |
| 168 // hasn't been created before. | 182 // hasn't been created before. |
| 169 UNREACHABLE(); | 183 UNREACHABLE(); |
| 170 return false; | 184 return false; |
| 171 } | 185 } |
| 172 } | 186 } |
| 173 | 187 |
| 174 | 188 |
| 175 void FUNCTION_NAME(InternetAddress_Parse)(Dart_NativeArguments args) { | 189 void FUNCTION_NAME(InternetAddress_Parse)(Dart_NativeArguments args) { |
| (...skipping 707 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 883 if (Dart_IsError(err)) { | 897 if (Dart_IsError(err)) { |
| 884 Dart_PropagateError(err); | 898 Dart_PropagateError(err); |
| 885 } | 899 } |
| 886 return socket; | 900 return socket; |
| 887 } | 901 } |
| 888 | 902 |
| 889 } // namespace bin | 903 } // namespace bin |
| 890 } // namespace dart | 904 } // namespace dart |
| 891 | 905 |
| 892 #endif // !defined(DART_IO_DISABLED) | 906 #endif // !defined(DART_IO_DISABLED) |
| OLD | NEW |