Fixed a deadlock while closing files.
authorSander Pronk <pronk@cbr.su.se>
Thu, 9 Sep 2010 15:18:50 +0000 (17:18 +0200)
committerSander Pronk <pronk@cbr.su.se>
Thu, 9 Sep 2010 15:18:50 +0000 (17:18 +0200)
src/gmxlib/gmxfio.c

index 02fb27c2fbeb36afe5372c33ba8749d662aeabb5..5ec8ff3151acbe5fe8c3734857705583d38b1859 100644 (file)
@@ -337,18 +337,12 @@ static void gmx_fio_insert(t_fileio *fio)
 }
 
 /* remove a t_fileio into the list. We assume the fio is locked, and we leave 
-   it locked. */
-static void gmx_fio_remove(t_fileio *fio, gmx_bool global_lock)
+   it locked. 
+   NOTE: We also assume that the open_file_mutex has been locked */
+static void gmx_fio_remove(t_fileio *fio)
 {    
     t_fileio *prev;
 
-#ifdef GMX_THREADS
-    /* first lock the big open_files mutex. */
-    /* We don't want two processes operating on this list at the same time */
-    if (global_lock)
-        tMPI_Thread_mutex_lock(&open_file_mutex);
-#endif
-   
     /* lock prev, because we're changing it */ 
     gmx_fio_lock(fio->prev);
 
@@ -363,13 +357,6 @@ static void gmx_fio_remove(t_fileio *fio, gmx_bool global_lock)
 
     /* and make sure we point nowhere in particular */
     fio->next=fio->prev=fio;
-
-#ifdef GMX_THREADS
-    /* now unlock the big open_files mutex.  */
-    if (global_lock)
-        tMPI_Thread_mutex_unlock(&open_file_mutex);
-#endif
-
 }
 
 
@@ -610,14 +597,24 @@ int gmx_fio_close(t_fileio *fio)
 {
     int rc = 0;
 
+#ifdef GMX_THREADS
+    /* first lock the big open_files mutex. */
+    /* We don't want two processes operating on the list at the same time */
+    tMPI_Thread_mutex_lock(&open_file_mutex);
+#endif
+
     gmx_fio_lock(fio);
     /* first remove it from the list */
-    gmx_fio_remove(fio, TRUE);
+    gmx_fio_remove(fio);
     rc=gmx_fio_close_locked(fio);
     gmx_fio_unlock(fio);
 
     sfree(fio);
 
+#ifdef GMX_THREADS
+    tMPI_Thread_mutex_unlock(&open_file_mutex);
+#endif
+
     return rc;
 }
 
@@ -661,7 +658,7 @@ int gmx_fio_fclose(FILE *fp)
         if (cur->fp == fp)
         {
             rc=gmx_fio_close_locked(cur);
-            gmx_fio_remove(cur,FALSE);
+            gmx_fio_remove(cur);
             gmx_fio_stop_getting_next(cur);
             break;
         }