Commit fd87825bf9f867e3d70338eeaf07df834e27db7b

Con Kolivas 2013-03-12T22:17:08

Convert the ch_lock to cg_lock.

diff --git a/api.c b/api.c
index 93dc51b..a2c7058 100644
--- a/api.c
+++ b/api.c
@@ -3197,7 +3197,7 @@ static void minecoin(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __may
 #endif
 		root = api_add_const(root, "Hash Method", SHA256STR, false);
 
-	mutex_lock(&ch_lock);
+	cg_rlock(&ch_lock);
 	if (current_fullhash && *current_fullhash) {
 		root = api_add_timeval(root, "Current Block Time", &block_timeval, true);
 		root = api_add_string(root, "Current Block Hash", current_fullhash, true);
@@ -3206,7 +3206,7 @@ static void minecoin(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __may
 		root = api_add_timeval(root, "Current Block Time", &t, true);
 		root = api_add_const(root, "Current Block Hash", BLANK, false);
 	}
-	mutex_unlock(&ch_lock);
+	cg_runlock(&ch_lock);
 
 	root = api_add_bool(root, "LP", &have_longpoll, false);
 	root = api_add_diff(root, "Network Difficulty", &current_diff, true);
diff --git a/cgminer.c b/cgminer.c
index b47618a..3f8ba54 100644
--- a/cgminer.c
+++ b/cgminer.c
@@ -176,7 +176,7 @@ pthread_mutex_t cgusb_lock;
 pthread_mutex_t hash_lock;
 static pthread_mutex_t *stgd_lock;
 pthread_mutex_t console_lock;
-pthread_mutex_t ch_lock;
+cglock_t ch_lock;
 static pthread_rwlock_t blk_lock;
 static pthread_mutex_t sshare_lock;
 
@@ -225,8 +225,11 @@ const
 bool curses_active;
 
 static char current_block[40];
+
+/* Protected by ch_lock */
 static char *current_hash;
 char *current_fullhash;
+
 static char datestamp[40];
 static char blocktime[32];
 struct timeval block_timeval;
@@ -1998,8 +2001,10 @@ static void curses_print_status(void)
 			pool->has_gbt ? "GBT" : "LP", pool->rpc_user);
 	}
 	wclrtoeol(statuswin);
+	cg_rlock(&ch_lock);
 	mvwprintw(statuswin, 5, 0, " Block: %s...  Diff:%s  Started: %s  Best share: %s   ",
 		  current_hash, block_diff, blocktime, best_share);
+	cg_runlock(&ch_lock);
 	mvwhline(statuswin, 6, 0, '-', 80);
 	mvwhline(statuswin, statusy - 1, 0, '-', 80);
 	mvwprintw(statuswin, devcursor - 1, 1, "[P]ool management %s[S]ettings [D]isplay options [Q]uit",
@@ -3555,27 +3560,20 @@ static void set_curblock(char *hexstr, unsigned char *hash)
 {
 	unsigned char hash_swap[32];
 	unsigned char block_hash_swap[32];
-	char *old_hash;
 
 	strcpy(current_block, hexstr);
 	swap256(hash_swap, hash);
 	swap256(block_hash_swap, hash + 4);
 
-	/* Don't free current_hash directly to avoid dereferencing when read
-	 * elsewhere - and update block_timeval inside the same lock */
-	mutex_lock(&ch_lock);
+	cg_wlock(&ch_lock);
 	gettimeofday(&block_timeval, NULL);
-	old_hash = current_hash;
+	free(current_hash);
 	current_hash = bin2hex(hash_swap + 2, 8);
-	free(old_hash);
-	old_hash = current_fullhash;
+	free(current_fullhash);
 	current_fullhash = bin2hex(block_hash_swap, 32);
-	free(old_hash);
-	mutex_unlock(&ch_lock);
-
 	get_timestamp(blocktime, &block_timeval);
-
 	applog(LOG_INFO, "New block: %s... diff %s", current_hash, block_diff);
+	cg_wunlock(&ch_lock);
 }
 
 /* Search to see if this string is from a block that has been seen before */
@@ -7038,7 +7036,7 @@ int main(int argc, char *argv[])
 	cglock_init(&control_lock);
 	mutex_init(&stats_lock);
 	mutex_init(&sharelog_lock);
-	mutex_init(&ch_lock);
+	cglock_init(&ch_lock);
 	mutex_init(&sshare_lock);
 	rwlock_init(&blk_lock);
 	rwlock_init(&netacc_lock);
diff --git a/miner.h b/miner.h
index 7d06c85..76c2a30 100644
--- a/miner.h
+++ b/miner.h
@@ -796,7 +796,7 @@ extern pthread_mutex_t cgusb_lock;
 extern cglock_t control_lock;
 extern pthread_mutex_t hash_lock;
 extern pthread_mutex_t console_lock;
-extern pthread_mutex_t ch_lock;
+extern cglock_t ch_lock;
 extern pthread_rwlock_t mining_thr_lock;
 extern pthread_rwlock_t devices_lock;