Index: runtime/bin/socket.cc |
diff --git a/runtime/bin/socket.cc b/runtime/bin/socket.cc |
index 745306d0e53aef80dffdde95f79f249bda1bfe4d..f7bc5822d3c3924cfd4463cc7c3e3a495da64a2f 100644 |
--- a/runtime/bin/socket.cc |
+++ b/runtime/bin/socket.cc |
@@ -127,42 +127,57 @@ Dart_Handle ListeningSocketRegistry::CreateBindListen(Dart_Handle socket_object, |
} |
-bool ListeningSocketRegistry::CloseSafe(intptr_t socketfd) { |
+bool ListeningSocketRegistry::CloseOneSafe(OSSocket* os_socket) { |
ASSERT(!mutex_->TryLock()); |
+ ASSERT(os_socket != NULL); |
+ ASSERT(os_socket->ref_count > 0); |
+ os_socket->ref_count--; |
+ if (os_socket->ref_count > 0) { |
+ return false; |
+ } |
+ // We free the OS socket by removing it from two datastructures. |
+ sockets_by_fd_.erase(os_socket->socketfd); |
- SocketsIterator it = sockets_by_fd_.find(socketfd); |
- if (it != sockets_by_fd_.end()) { |
- OSSocket *os_socket = it->second; |
+ OSSocket *prev = NULL; |
+ OSSocket *current = sockets_by_port_[os_socket->port]; |
+ while (current != os_socket) { |
+ ASSERT(current != NULL); |
+ prev = current; |
+ current = current->next; |
+ } |
- ASSERT(os_socket->ref_count > 0); |
- os_socket->ref_count--; |
- if (os_socket->ref_count == 0) { |
- // We free the OS socket by removing it from two datastructures. |
- sockets_by_fd_.erase(socketfd); |
- |
- OSSocket *prev = NULL; |
- OSSocket *current = sockets_by_port_[os_socket->port]; |
- while (current != os_socket) { |
- ASSERT(current != NULL); |
- prev = current; |
- current = current->next; |
- } |
+ if ((prev == NULL) && (current->next == NULL)) { |
+ // Remove last element from the list. |
+ sockets_by_port_.erase(os_socket->port); |
+ } else if (prev == NULL) { |
+ // Remove first element of the list. |
+ sockets_by_port_[os_socket->port] = current->next; |
+ } else { |
+ // Remove element from the list which is not the first one. |
+ prev->next = os_socket->next; |
+ } |
- if ((prev == NULL) && (current->next == NULL)) { |
- // Remove last element from the list. |
- sockets_by_port_.erase(os_socket->port); |
- } else if (prev == NULL) { |
- // Remove first element of the list. |
- sockets_by_port_[os_socket->port] = current->next; |
- } else { |
- // Remove element from the list which is not the first one. |
- prev->next = os_socket->next; |
- } |
+ ASSERT(os_socket->ref_count == 0); |
+ delete os_socket; |
+ return true; |
+} |
- delete os_socket; |
- return true; |
- } |
- return false; |
+ |
+void ListeningSocketRegistry::CloseAllSafe() { |
+ MutexLocker ml(mutex_); |
+ SocketsIterator it = sockets_by_fd_.begin(); |
+ while (it != sockets_by_fd_.end()) { |
+ CloseOneSafe(it->second); |
+ it++; |
+ } |
+} |
+ |
+ |
+bool ListeningSocketRegistry::CloseSafe(intptr_t socketfd) { |
+ ASSERT(!mutex_->TryLock()); |
+ SocketsIterator it = sockets_by_fd_.find(socketfd); |
+ if (it != sockets_by_fd_.end()) { |
+ return CloseOneSafe(it->second); |
} else { |
// It should be impossible for the event handler to close something that |
// hasn't been created before. |