Commit 5d122cad93f0d6e57e5ad36b0f9b00ff39d9ff18

Con Kolivas 2013-02-03T23:41:41

Provide wrappers for grabbing of thr value under the mining_thr_lock.

diff --git a/api.c b/api.c
index 1a2f312..84a947c 100644
--- a/api.c
+++ b/api.c
@@ -1800,9 +1800,7 @@ static void pgaenable(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char
 #endif
 
 	for (i = 0; i < mining_threads; i++) {
-		mutex_lock(&mining_thr_lock);
-		thr = mining_thr[i];
-		mutex_unlock(&mining_thr_lock);
+		thr = get_thread(i);
 		pga = thr->cgpu->cgminer_id;
 		if (pga == dev) {
 			cgpu->deven = DEV_ENABLED;
@@ -2107,9 +2105,7 @@ static void gpuenable(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char
 	}
 
 	for (i = 0; i < gpu_threads; i++) {
-		mutex_lock(&mining_thr_lock);
-		thr = mining_thr[i];
-		mutex_unlock(&mining_thr_lock);
+		thr = get_thread(i);
 		gpu = thr->cgpu->device_id;
 		if (gpu == id) {
 			if (thr->cgpu->status != LIFE_WELL) {
diff --git a/cgminer.c b/cgminer.c
index cfc2cfa..f8efa62 100644
--- a/cgminer.c
+++ b/cgminer.c
@@ -365,6 +365,23 @@ static void applog_and_exit(const char *fmt, ...)
 static pthread_mutex_t sharelog_lock;
 static FILE *sharelog_file = NULL;
 
+struct thr_info *get_thread(int thr_id)
+{
+	struct thr_info *thr;
+
+	mutex_lock(&mining_thr_lock);
+	thr = mining_thr[thr_id];
+	mutex_unlock(&mining_thr_lock);
+	return thr;
+}
+
+static struct cgpu_info *get_thr_cgpu(int thr_id)
+{
+	struct thr_info *thr = get_thread(thr_id);
+
+	return thr->cgpu;
+}
+
 static void sharelog(const char*disposition, const struct work*work)
 {
 	char *target, *hash, *data;
@@ -379,9 +396,7 @@ static void sharelog(const char*disposition, const struct work*work)
 		return;
 
 	thr_id = work->thr_id;
-	mutex_lock(&mining_thr_lock);
-	cgpu = mining_thr[thr_id]->cgpu;
-	mutex_unlock(&mining_thr_lock);
+	cgpu = get_thr_cgpu(thr_id);
 	pool = work->pool;
 	t = (unsigned long int)(work->tv_work_found.tv_sec);
 	target = bin2hex(work->target, sizeof(work->target));
@@ -1726,11 +1741,7 @@ out:
 
 int dev_from_id(int thr_id)
 {
-	struct cgpu_info *cgpu;
-
-	mutex_lock(&mining_thr_lock);
-	cgpu = mining_thr[thr_id]->cgpu;
-	mutex_unlock(&mining_thr_lock);
+	struct cgpu_info *cgpu = get_thr_cgpu(thr_id);
 
 	return cgpu->device_id;
 }
@@ -1907,10 +1918,7 @@ static void text_print_status(int thr_id)
 	struct cgpu_info *cgpu;
 	char logline[256];
 
-	mutex_lock(&mining_thr_lock);
-	cgpu = mining_thr[thr_id]->cgpu;
-	mutex_unlock(&mining_thr_lock);
-
+	cgpu = get_thr_cgpu(thr_id);
 	if (cgpu) {
 		get_statline(logline, cgpu);
 		printf("%s\n", logline);
@@ -1979,9 +1987,7 @@ static void curses_print_devstatus(int thr_id)
 	char displayed_hashes[16], displayed_rolling[16];
 	uint64_t dh64, dr64;
 
-	mutex_lock(&mining_thr_lock);
-	cgpu = mining_thr[thr_id]->cgpu;
-	mutex_unlock(&mining_thr_lock);
+	cgpu = get_thr_cgpu(thr_id);
 
 	if (devcursor + cgpu->cgminer_id > LINES - 2 || opt_compact)
 		return;
@@ -2232,9 +2238,7 @@ share_result(json_t *val, json_t *res, json_t *err, const struct work *work,
 	struct pool *pool = work->pool;
 	struct cgpu_info *cgpu;
 
-	mutex_lock(&mining_thr_lock);
-	cgpu = mining_thr[work->thr_id]->cgpu;
-	mutex_unlock(&mining_thr_lock);
+	cgpu = get_thr_cgpu(work->thr_id);
 
 	if (json_is_true(res) || (work->gbt && json_is_null(res))) {
 		mutex_lock(&stats_lock);
@@ -2391,9 +2395,7 @@ static bool submit_upstream_work(struct work *work, CURL *curl, bool resubmit)
 	char hashshow[64 + 4] = "";
 	char worktime[200] = "";
 
-	mutex_lock(&mining_thr_lock);
-	cgpu = mining_thr[thr_id]->cgpu;
-	mutex_unlock(&mining_thr_lock);
+	cgpu = get_thr_cgpu(thr_id);
 
 #ifdef __BIG_ENDIAN__
         int swapcounter = 0;
@@ -2781,9 +2783,7 @@ static void __kill_work(void)
 	applog(LOG_DEBUG, "Stopping mining threads");
 	/* Stop the mining threads*/
 	for (i = 0; i < mining_threads; i++) {
-		mutex_lock(&mining_thr_lock);
-		thr = mining_thr[i];
-		mutex_unlock(&mining_thr_lock);
+		thr = get_thread(i);
 		thr_info_freeze(thr);
 		thr->pause = true;
 	}
@@ -2793,9 +2793,7 @@ static void __kill_work(void)
 	applog(LOG_DEBUG, "Killing off mining threads");
 	/* Kill the mining threads*/
 	for (i = 0; i < mining_threads; i++) {
-		mutex_lock(&mining_thr_lock);
-		thr = mining_thr[i];
-		mutex_unlock(&mining_thr_lock);
+		thr = get_thread(i);
 		thr_info_cancel(thr);
 	}
 
@@ -4443,9 +4441,7 @@ static void hashmeter(int thr_id, struct timeval *diff,
 	local_mhashes = (double)hashes_done / 1000000.0;
 	/* Update the last time this thread reported in */
 	if (thr_id >= 0) {
-		mutex_lock(&mining_thr_lock);
-		thr = mining_thr[thr_id];
-		mutex_unlock(&mining_thr_lock);
+		thr = get_thread(thr_id);
 		gettimeofday(&(thr->last), NULL);
 		thr->cgpu->device_last_well = time(NULL);
 	}
@@ -5907,9 +5903,7 @@ static void *watchdog_thread(void __maybe_unused *userdata)
 			for (i = 0; i < mining_threads; i++) {
 				struct thr_info *thr;
 
-				mutex_lock(&mining_thr_lock);
-				thr = mining_thr[i];
-				mutex_unlock(&mining_thr_lock);
+				thr = get_thread(i);
 
 				/* Don't touch disabled devices */
 				if (thr->cgpu->deven == DEV_DISABLED)
@@ -6855,9 +6849,7 @@ begin_bench:
 		cgpu->status = LIFE_INIT;
 
 		for (j = 0; j < cgpu->threads; ++j, ++k) {
-			mutex_lock(&mining_thr_lock);
-			thr = mining_thr[k];
-			mutex_unlock(&mining_thr_lock);
+			thr = get_thread(k);
 			thr->id = k;
 			thr->cgpu = cgpu;
 			thr->device_thread = j;
diff --git a/driver-opencl.c b/driver-opencl.c
index 0521ac1..9e9e401 100644
--- a/driver-opencl.c
+++ b/driver-opencl.c
@@ -618,10 +618,7 @@ void pause_dynamic_threads(int gpu)
 	for (i = 1; i < cgpu->threads; i++) {
 		struct thr_info *thr;
 
-		mutex_lock(&mining_thr_lock);
-		thr = mining_thr[i];
-		mutex_unlock(&mining_thr_lock);
-
+		thr = get_thread(i);
 		if (!thr->pause && cgpu->dynamic) {
 			applog(LOG_WARNING, "Disabling extra threads due to dynamic mode.");
 			applog(LOG_WARNING, "Tune dynamic intensity with --gpu-dyninterval");
@@ -709,9 +706,7 @@ retry:
 		else
 			wlog("%d\n", gpus[gpu].intensity);
 		for (i = 0; i < mining_threads; i++) {
-			mutex_lock(&mining_thr_lock);
-			thr = mining_thr[i];
-			mutex_unlock(&mining_thr_lock);
+			thr = get_thread(i);
 			if (thr->cgpu != cgpu)
 				continue;
 			get_datestamp(checkin, &thr->last);
@@ -766,9 +761,7 @@ retry:
 		}
 		gpus[selected].deven = DEV_ENABLED;
 		for (i = 0; i < mining_threads; ++i) {
-			mutex_lock(&mining_thr_lock);
-			thr = mining_thr[i];
-			mutex_unlock(&mining_thr_lock);
+			thr = get_thread(i);
 			cgpu = thr->cgpu;
 			if (cgpu->drv->drv_id != DRIVER_OPENCL)
 				continue;
@@ -1155,18 +1148,14 @@ select_cgpu:
 	gpu = cgpu->device_id;
 
 	for (thr_id = 0; thr_id < mining_threads; ++thr_id) {
-		mutex_lock(&mining_thr_lock);
-		thr = mining_thr[thr_id];
-		mutex_unlock(&mining_thr_lock);
+		thr = get_thread(thr_id);
 		cgpu = thr->cgpu;
 		if (cgpu->drv->drv_id != DRIVER_OPENCL)
 			continue;
 		if (dev_from_id(thr_id) != gpu)
 			continue;
 
-		mutex_lock(&mining_thr_lock);
-		thr = mining_thr[thr_id];
-		mutex_unlock(&mining_thr_lock);
+		thr = get_thread(thr_id);
 		if (!thr) {
 			applog(LOG_WARNING, "No reference to thread %d exists", thr_id);
 			continue;
@@ -1184,9 +1173,7 @@ select_cgpu:
 	for (thr_id = 0; thr_id < mining_threads; ++thr_id) {
 		int virtual_gpu;
 
-		mutex_lock(&mining_thr_lock);
-		thr = mining_thr[thr_id];
-		mutex_unlock(&mining_thr_lock);
+		thr = get_thread(thr_id);
 		cgpu = thr->cgpu;
 		if (cgpu->drv->drv_id != DRIVER_OPENCL)
 			continue;
@@ -1223,9 +1210,7 @@ select_cgpu:
 	get_datestamp(cgpu->init, &now);
 
 	for (thr_id = 0; thr_id < mining_threads; ++thr_id) {
-		mutex_lock(&mining_thr_lock);
-		thr = mining_thr[thr_id];
-		mutex_unlock(&mining_thr_lock);
+		thr = get_thread(thr_id);
 		cgpu = thr->cgpu;
 		if (cgpu->drv->drv_id != DRIVER_OPENCL)
 			continue;
diff --git a/miner.h b/miner.h
index 87d58fe..a862af6 100644
--- a/miner.h
+++ b/miner.h
@@ -1101,6 +1101,7 @@ extern void clean_work(struct work *work);
 extern void free_work(struct work *work);
 extern void __copy_work(struct work *work, struct work *base_work);
 extern struct work *copy_work(struct work *base_work);
+extern struct thr_info *get_thread(int thr_id);
 
 enum api_data_type {
 	API_ESCAPE,