Просмотр исходного кода

fix atomicity of mutex and cond initialization in simgrid runs

Samuel Thibault лет назад: 9
Родитель
Сommit
4a06c0f32f
1 измененных файлов с 31 добавлено и 7 удалено
  1. 31 7
      src/common/thread.c

+ 31 - 7
src/common/thread.c

@@ -109,7 +109,18 @@ int starpu_pthread_mutex_lock(starpu_pthread_mutex_t *mutex)
 {
 	_STARPU_TRACE_LOCKING_MUTEX();
 
-	if (!*mutex) STARPU_PTHREAD_MUTEX_INIT(mutex, NULL);
+	/* Note: this is actually safe, because simgrid only preempts within
+	 * simgrid functions */
+	if (!*mutex) {
+		/* Here we may get preempted */
+		xbt_mutex_t new_mutex = xbt_mutex_init();
+		if (!*mutex)
+			*mutex = new_mutex;
+		else
+			/* Somebody already initialized it while we were
+			 * calling xbt_mutex_init, this one is now useless */
+			xbt_mutex_destroy(new_mutex);
+	}
 
 	xbt_mutex_acquire(*mutex);
 
@@ -226,18 +237,32 @@ int starpu_pthread_cond_init(starpu_pthread_cond_t *cond, starpu_pthread_condatt
 	return 0;
 }
 
+static void _starpu_pthread_cond_auto_init(starpu_pthread_cond_t *cond)
+{
+	/* Note: this is actually safe, because simgrid only preempts within
+	 * simgrid functions */
+	if (!*cond) {
+		/* Here we may get preempted */
+		xbt_cond_t new_cond = xbt_cond_init();
+		if (!*cond)
+			*cond = new_cond;
+		else
+			/* Somebody already initialized it while we were
+			 * calling xbt_cond_init, this one is now useless */
+			xbt_cond_destroy(new_cond);
+	}
+}
+
 int starpu_pthread_cond_signal(starpu_pthread_cond_t *cond)
 {
-	if (!*cond)
-		STARPU_PTHREAD_COND_INIT(cond, NULL);
+	_starpu_pthread_cond_auto_init(cond);
 	xbt_cond_signal(*cond);
 	return 0;
 }
 
 int starpu_pthread_cond_broadcast(starpu_pthread_cond_t *cond)
 {
-	if (!*cond)
-		STARPU_PTHREAD_COND_INIT(cond, NULL);
+	_starpu_pthread_cond_auto_init(cond);
 	xbt_cond_broadcast(*cond);
 	return 0;
 }
@@ -246,8 +271,7 @@ int starpu_pthread_cond_wait(starpu_pthread_cond_t *cond, starpu_pthread_mutex_t
 {
 	_STARPU_TRACE_COND_WAIT_BEGIN();
 
-	if (!*cond)
-		STARPU_PTHREAD_COND_INIT(cond, NULL);
+	_starpu_pthread_cond_auto_init(cond);
 	xbt_cond_wait(*cond, *mutex);
 
 	_STARPU_TRACE_COND_WAIT_END();