Browse Source

Add lock to protect concurrent accesses to knc_state

Vitalii Demianets 11 years ago
parent
commit
04013c71fa
1 changed files with 21 additions and 2 deletions
  1. 21 2
      driver-kncasic.c

+ 21 - 2
driver-kncasic.c

@@ -147,7 +147,10 @@ struct knc_state {
 	int send_buffer_count;
 	int send_buffer_count;
 	int read_buffer_count;
 	int read_buffer_count;
 	/* end SPI thread */
 	/* end SPI thread */
-	
+
+	/* lock to protect resources between different threads */
+	pthread_mutex_t state_lock;
+
 	/* Do not add anything below here!! core[] must be last */
 	/* Do not add anything below here!! core[] must be last */
 	struct knc_core_state core[];
 	struct knc_core_state core[];
 };
 };
@@ -352,6 +355,8 @@ static bool knc_detect_one(void *ctx)
 
 
 	pthread_mutex_init(&knc->spi_qlock, NULL);
 	pthread_mutex_init(&knc->spi_qlock, NULL);
 	pthread_cond_init(&knc->spi_qcond, NULL);
 	pthread_cond_init(&knc->spi_qcond, NULL);
+	pthread_mutex_init(&knc->state_lock, NULL);
+
 	if (thr_info_create(&knc->spi_thr, NULL, knc_spi, (void *)cgpu)) {
 	if (thr_info_create(&knc->spi_thr, NULL, knc_spi, (void *)cgpu)) {
 		applog(LOG_ERR, "%s%i: SPI thread create failed",
 		applog(LOG_ERR, "%s%i: SPI thread create failed",
 			cgpu->drv->name, cgpu->device_id);
 			cgpu->drv->name, cgpu->device_id);
@@ -682,6 +687,7 @@ static int64_t knc_scanwork(struct thr_info *thr)
 	uint32_t last_count = knc->KNC_COUNT_UNIT;
 	uint32_t last_count = knc->KNC_COUNT_UNIT;
 
 
 	applog(LOG_DEBUG, "KnC running scanwork");
 	applog(LOG_DEBUG, "KnC running scanwork");
+	mutex_lock(&knc->state_lock);
 
 
 	gettimeofday(&now, NULL);
 	gettimeofday(&now, NULL);
 
 
@@ -741,7 +747,10 @@ static int64_t knc_scanwork(struct thr_info *thr)
 
 
 	knc_flush(thr);
 	knc_flush(thr);
 
 
-	return (int64_t)(knc->KNC_COUNT_UNIT - last_count) * 0x100000000UL;
+	int64_t nonces_number = (int64_t)(knc->KNC_COUNT_UNIT - last_count) * 0x100000000UL;
+
+	mutex_unlock(&knc->state_lock);
+	return nonces_number;
 }
 }
 
 
 static void knc_flush_work(struct cgpu_info *cgpu)
 static void knc_flush_work(struct cgpu_info *cgpu)
@@ -750,16 +759,22 @@ static void knc_flush_work(struct cgpu_info *cgpu)
 
 
 	applog(LOG_INFO, "KnC running flushwork");
 	applog(LOG_INFO, "KnC running flushwork");
 
 
+	mutex_lock(&knc->state_lock);
+
 	knc->generation++;
 	knc->generation++;
 	knc->scan_adjust=0;
 	knc->scan_adjust=0;
 	if (!knc->generation)
 	if (!knc->generation)
 		knc->generation++;
 		knc->generation++;
+
+	mutex_unlock(&knc->state_lock);
 }
 }
 
 
 static void knc_zero_stats(struct cgpu_info *cgpu)
 static void knc_zero_stats(struct cgpu_info *cgpu)
 {
 {
 	int core;
 	int core;
 	struct knc_state *knc = cgpu->device_data;
 	struct knc_state *knc = cgpu->device_data;
+
+	mutex_lock(&knc->state_lock);
 	for (core = 0; core < knc->cores; core++) {
 	for (core = 0; core < knc->cores; core++) {
 		knc->shares = 0;
 		knc->shares = 0;
 		knc->completed = 0;
 		knc->completed = 0;
@@ -770,6 +785,7 @@ static void knc_zero_stats(struct cgpu_info *cgpu)
 		knc->core[core].shares = 0;
 		knc->core[core].shares = 0;
 		knc->core[core].completed = 0;
 		knc->core[core].completed = 0;
 	}
 	}
+	mutex_unlock(&knc->state_lock);
 }
 }
 
 
 static struct api_data *knc_api_stats(struct cgpu_info *cgpu)
 static struct api_data *knc_api_stats(struct cgpu_info *cgpu)
@@ -780,6 +796,8 @@ static struct api_data *knc_api_stats(struct cgpu_info *cgpu)
 	int asic, core, n;
 	int asic, core, n;
 	char label[256];
 	char label[256];
 
 
+	mutex_lock(&knc->state_lock);
+
 	root = api_add_int(root, "dies", &knc->dies, 1);
 	root = api_add_int(root, "dies", &knc->dies, 1);
 	root = api_add_int(root, "cores", &knc->cores, 1);
 	root = api_add_int(root, "cores", &knc->cores, 1);
 	root = api_add_uint64(root, "shares", &knc->shares, 1);
 	root = api_add_uint64(root, "shares", &knc->shares, 1);
@@ -849,6 +867,7 @@ static struct api_data *knc_api_stats(struct cgpu_info *cgpu)
 		}
 		}
 	}
 	}
 
 
+	mutex_unlock(&knc->state_lock);
 	return root;
 	return root;
 }
 }