Bladeren bron

Use condition variable properly ... it is possible that the driver was
initialized before we starts waiting.

Cédric Augonnet 16 jaren geleden
bovenliggende
commit
cc3f65515d
5 gewijzigde bestanden met toevoegingen van 16 en 3 verwijderingen
  1. 11 3
      src/core/workers.c
  2. 2 0
      src/core/workers.h
  3. 1 0
      src/drivers/core/driver_core.c
  4. 1 0
      src/drivers/cuda/driver_cuda.c
  5. 1 0
      src/drivers/gordon/driver_gordon.c

+ 11 - 3
src/core/workers.c

@@ -243,11 +243,13 @@ static void init_workers(struct machine_config_s *config)
 #ifdef USE_CPUS
 			case CORE_WORKER:
 				workerarg->set = NULL;
+				workerarg->worker_is_initialized = 0;
 				pthread_create(&workerarg->worker_thread, 
 						NULL, core_worker, workerarg);
 
 				pthread_mutex_lock(&workerarg->mutex);
-				pthread_cond_wait(&workerarg->ready_cond, &workerarg->mutex);
+				if (!workerarg->worker_is_initialized)
+					pthread_cond_wait(&workerarg->ready_cond, &workerarg->mutex);
 				pthread_mutex_unlock(&workerarg->mutex);
 
 				break;
@@ -255,11 +257,13 @@ static void init_workers(struct machine_config_s *config)
 #ifdef USE_CUDA
 			case CUDA_WORKER:
 				workerarg->set = NULL;
+				workerarg->worker_is_initialized = 0;
 				pthread_create(&workerarg->worker_thread, 
 						NULL, cuda_worker, workerarg);
 
 				pthread_mutex_lock(&workerarg->mutex);
-				pthread_cond_wait(&workerarg->ready_cond, &workerarg->mutex);
+				if (!workerarg->worker_is_initialized)
+					pthread_cond_wait(&workerarg->ready_cond, &workerarg->mutex);
 				pthread_mutex_unlock(&workerarg->mutex);
 
 				break;
@@ -273,11 +277,15 @@ static void init_workers(struct machine_config_s *config)
 					gordon_worker_set.nworkers = ngordon_spus; 
 					gordon_worker_set.workers = &config->workers[worker];
 
+					gordon_worker_set.set_is_initialized = 0;
+
 					pthread_create(&gordon_worker_set.worker_thread, NULL, 
 							gordon_worker, &gordon_worker_set);
 
 					pthread_mutex_lock(&gordon_worker_set.mutex);
-					pthread_cond_wait(&gordon_worker_set.ready_cond, &gordon_worker_set.mutex);
+					if (!gordon_worker_set.set_is_initialized)
+						pthread_cond_wait(&gordon_worker_set.ready_cond,
+									&gordon_worker_set.mutex);
 					pthread_mutex_unlock(&gordon_worker_set.mutex);
 
 					gordon_inited = 1;

+ 2 - 0
src/core/workers.h

@@ -75,6 +75,7 @@ struct worker_s {
 	struct worker_set_s *set; /* in case this worker belongs to a set */
 	struct job_list_s *terminated_jobs; /* list of pending jobs which were executed */
 	unsigned worker_is_running;
+	unsigned worker_is_initialized;
 };
 
 /* in case a single CPU worker may control multiple 
@@ -87,6 +88,7 @@ struct worker_set_s {
 	void *retval;
 	struct worker_s *workers;
         pthread_cond_t ready_cond; /* indicate when the set is ready */
+	unsigned set_is_initialized;
 };
 
 struct machine_config_s {

+ 1 - 0
src/drivers/core/driver_core.c

@@ -114,6 +114,7 @@ void *core_worker(void *arg)
 	
         /* tell the main thread that we are ready */
 	pthread_mutex_lock(&core_arg->mutex);
+	core_arg->worker_is_initialized = 1;
 	pthread_cond_signal(&core_arg->ready_cond);
 	pthread_mutex_unlock(&core_arg->mutex);
 

+ 1 - 0
src/drivers/cuda/driver_cuda.c

@@ -343,6 +343,7 @@ void *cuda_worker(void *arg)
 
 	/* tell the main thread that this one is ready */
 	pthread_mutex_lock(&args->mutex);
+	args->worker_is_initialized = 1;
 	pthread_cond_signal(&args->ready_cond);
 	pthread_mutex_unlock(&args->mutex);
 

+ 1 - 0
src/drivers/gordon/driver_gordon.c

@@ -474,6 +474,7 @@ void *gordon_worker(void *arg)
 	
 	/* tell the core that gordon is ready */
 	pthread_mutex_lock(&gordon_set_arg->mutex);
+	gordon_set_arg->set_is_initialized = 1;
 	pthread_cond_signal(&gordon_set_arg->ready_cond);
 	pthread_mutex_unlock(&gordon_set_arg->mutex);