Merge branch 'master' into handle-fixes-part1
[mono.git] / mono / metadata / w32handle.c
index 02aa4db826306b53f6affa76beffbd683e4f0ea0..35a5dd93d87663884e7cb43bd2cfdba27547b8dc 100644 (file)
@@ -451,19 +451,17 @@ gpointer mono_w32handle_new_fd (MonoW32HandleType type, int fd,
        fd_index = SLOT_INDEX (fd);
        fd_offset = SLOT_OFFSET (fd);
 
+       mono_os_mutex_lock (&scan_mutex);
        /* Initialize the array entries on demand */
        if (!private_handles [fd_index]) {
-               mono_os_mutex_lock (&scan_mutex);
-
                if (!private_handles [fd_index])
                        private_handles [fd_index] = g_new0 (MonoW32HandleBase, HANDLE_PER_SLOT);
-
-               mono_os_mutex_unlock (&scan_mutex);
        }
 
        handle_data = &private_handles [fd_index][fd_offset];
 
        if (handle_data->type != MONO_W32HANDLE_UNUSED) {
+               mono_os_mutex_unlock (&scan_mutex);
                mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: failed to create %s handle, fd is already in use", __func__, mono_w32handle_ops_typename (type));
                /* FIXME: clean up this handle?  We can't do anything
                 * with the fd, cos thats the new one
@@ -475,6 +473,8 @@ gpointer mono_w32handle_new_fd (MonoW32HandleType type, int fd,
 
        mono_w32handle_init_handle (handle_data, type, handle_specific);
 
+       mono_os_mutex_unlock (&scan_mutex);
+
        return(GUINT_TO_POINTER(fd));
 }