DO NOT MERGE: adb: use asocket's close function when closing.

close_all_sockets was assuming that all registered local sockets used
local_socket_close as their close function. However, this is not true
for JDWP sockets.

Backport of 53eb31d8 and 9b587de.

Bug: http://b/28347842
Change-Id: I05e071b3d5c7e55c7a59400c5c9bb0635eeef29f
diff --git a/adb/Android.mk b/adb/Android.mk
index af7d7e5..974867e 100644
--- a/adb/Android.mk
+++ b/adb/Android.mk
@@ -82,7 +82,9 @@
 	LOCAL_STATIC_LIBRARIES += libcutils
 endif
 
-include $(BUILD_HOST_EXECUTABLE)
+ifneq ($(HOST_OS),windows)
+  include $(BUILD_HOST_EXECUTABLE)
+endif
 
 $(call dist-for-goals,dist_files sdk,$(LOCAL_BUILT_MODULE))
 
diff --git a/adb/mutex_list.h b/adb/mutex_list.h
index ff72751..15e383c 100644
--- a/adb/mutex_list.h
+++ b/adb/mutex_list.h
@@ -6,7 +6,6 @@
 #ifndef ADB_MUTEX
 #error ADB_MUTEX not defined when including this file
 #endif
-ADB_MUTEX(socket_list_lock)
 ADB_MUTEX(transport_lock)
 #if ADB_HOST
 ADB_MUTEX(local_transports_lock)
diff --git a/adb/sockets.c b/adb/sockets.c
index faa9564..29df706 100644
--- a/adb/sockets.c
+++ b/adb/sockets.c
@@ -30,9 +30,11 @@
 #define  TRACE_TAG  TRACE_SOCKETS
 #include "adb.h"
 
-ADB_MUTEX_DEFINE( socket_list_lock );
+#if defined(__BIONIC__)
+#define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP PTHREAD_RECURSIVE_MUTEX_INITIALIZER
+#endif
 
-static void local_socket_close_locked(asocket *s);
+static pthread_mutex_t socket_list_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
 
 int sendfailmsg(int fd, const char *reason)
 {
@@ -71,7 +73,7 @@
     asocket *s;
     asocket *result = NULL;
 
-    adb_mutex_lock(&socket_list_lock);
+    pthread_mutex_lock(&socket_list_lock);
     for (s = local_socket_list.next; s != &local_socket_list; s = s->next) {
         if (s->id != local_id)
             continue;
@@ -80,7 +82,7 @@
         }
         break;
     }
-    adb_mutex_unlock(&socket_list_lock);
+    pthread_mutex_unlock(&socket_list_lock);
 
     return result;
 }
@@ -97,7 +99,7 @@
 
 void install_local_socket(asocket *s)
 {
-    adb_mutex_lock(&socket_list_lock);
+    pthread_mutex_lock(&socket_list_lock);
 
     s->id = local_socket_next_id++;
 
@@ -107,7 +109,7 @@
 
     insert_local_socket(s, &local_socket_list);
 
-    adb_mutex_unlock(&socket_list_lock);
+    pthread_mutex_unlock(&socket_list_lock);
 }
 
 void remove_socket(asocket *s)
@@ -130,15 +132,15 @@
         /* this is a little gross, but since s->close() *will* modify
         ** the list out from under you, your options are limited.
         */
-    adb_mutex_lock(&socket_list_lock);
+    pthread_mutex_lock(&socket_list_lock);
 restart:
     for(s = local_socket_list.next; s != &local_socket_list; s = s->next){
         if(s->transport == t || (s->peer && s->peer->transport == t)) {
-            local_socket_close_locked(s);
+            s->close(s);
             goto restart;
         }
     }
-    adb_mutex_unlock(&socket_list_lock);
+    pthread_mutex_unlock(&socket_list_lock);
 }
 
 static int local_socket_enqueue(asocket *s, apacket *p)
@@ -202,13 +204,6 @@
 //    D("LS(%d): ready()\n", s->id);
 }
 
-static void local_socket_close(asocket *s)
-{
-    adb_mutex_lock(&socket_list_lock);
-    local_socket_close_locked(s);
-    adb_mutex_unlock(&socket_list_lock);
-}
-
 // be sure to hold the socket list lock when calling this
 static void local_socket_destroy(asocket  *s)
 {
@@ -238,8 +233,9 @@
 }
 
 
-static void local_socket_close_locked(asocket *s)
+static void local_socket_close(asocket *s)
 {
+    pthread_mutex_lock(&socket_list_lock);
     D("entered. LS(%d) fd=%d\n", s->id, s->fd);
     if(s->peer) {
         D("LS(%d): closing peer. peer->id=%d peer->fd=%d\n",
@@ -251,12 +247,7 @@
         if (s->peer->shutdown)
           s->peer->shutdown(s->peer);
         s->peer->peer = 0;
-        // tweak to avoid deadlock
-        if (s->peer->close == local_socket_close) {
-            local_socket_close_locked(s->peer);
-        } else {
-            s->peer->close(s->peer);
-        }
+        s->peer->close(s->peer);
         s->peer = 0;
     }
 
@@ -267,6 +258,7 @@
         int   id = s->id;
         local_socket_destroy(s);
         D("LS(%d): closed\n", id);
+        pthread_mutex_unlock(&socket_list_lock);
         return;
     }
 
@@ -278,6 +270,7 @@
     remove_socket(s);
     D("LS(%d): put on socket_closing_list fd=%d\n", s->id, s->fd);
     insert_local_socket(s, &local_socket_closing_list);
+    pthread_mutex_unlock(&socket_list_lock);
 }
 
 static void local_socket_event_func(int fd, unsigned ev, void *_s)