Browse Source

Merge commit 'fd87825' into cg_merges_20130513

Conflicts:
	miner.c
Luke Dashjr 12 years ago
parent
commit
47f757ff2d
3 changed files with 15 additions and 16 deletions
  1. 2 2
      api.c
  2. 12 13
      miner.c
  3. 1 1
      miner.h

+ 2 - 2
api.c

@@ -2884,7 +2884,7 @@ static void minecoin(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __may
 #endif
 #endif
 		root = api_add_const(root, "Hash Method", SHA256STR, false);
 		root = api_add_const(root, "Hash Method", SHA256STR, false);
 
 
-	mutex_lock(&ch_lock);
+	cg_rlock(&ch_lock);
 	if (current_fullhash && *current_fullhash) {
 	if (current_fullhash && *current_fullhash) {
 		root = api_add_timeval(root, "Current Block Time", &block_timeval, true);
 		root = api_add_timeval(root, "Current Block Time", &block_timeval, true);
 		root = api_add_string(root, "Current Block Hash", current_fullhash, true);
 		root = api_add_string(root, "Current Block Hash", current_fullhash, true);
@@ -2893,7 +2893,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_timeval(root, "Current Block Time", &t, true);
 		root = api_add_const(root, "Current Block Hash", BLANK, false);
 		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_bool(root, "LP", &have_longpoll, false);
 	root = api_add_diff(root, "Network Difficulty", &current_diff, true);
 	root = api_add_diff(root, "Network Difficulty", &current_diff, true);

+ 12 - 13
miner.c

@@ -212,7 +212,7 @@ static int total_control_threads;
 pthread_mutex_t hash_lock;
 pthread_mutex_t hash_lock;
 static pthread_mutex_t *stgd_lock;
 static pthread_mutex_t *stgd_lock;
 pthread_mutex_t console_lock;
 pthread_mutex_t console_lock;
-pthread_mutex_t ch_lock;
+cglock_t ch_lock;
 static pthread_rwlock_t blk_lock;
 static pthread_rwlock_t blk_lock;
 static pthread_mutex_t sshare_lock;
 static pthread_mutex_t sshare_lock;
 
 
@@ -266,9 +266,12 @@ const
 bool curses_active;
 bool curses_active;
 
 
 static char current_block[40];
 static char current_block[40];
+
+/* Protected by ch_lock */
 static char *current_hash;
 static char *current_hash;
 static uint32_t current_block_id;
 static uint32_t current_block_id;
 char *current_fullhash;
 char *current_fullhash;
+
 static char datestamp[40];
 static char datestamp[40];
 static char blocktime[32];
 static char blocktime[32];
 struct timeval block_timeval;
 struct timeval block_timeval;
@@ -1860,12 +1863,12 @@ void have_block_height(uint32_t block_id, uint32_t blkheight)
 	if (known_blkheight == blkheight)
 	if (known_blkheight == blkheight)
 		return;
 		return;
 	applog(LOG_DEBUG, "Learned that block id %08" PRIx32 " is height %" PRIu32, be32toh(block_id), blkheight);
 	applog(LOG_DEBUG, "Learned that block id %08" PRIx32 " is height %" PRIu32, be32toh(block_id), blkheight);
-	mutex_lock(&ch_lock);
+	cg_wlock(&ch_lock);
 	known_blkheight = blkheight;
 	known_blkheight = blkheight;
 	known_blkheight_blkid = block_id;
 	known_blkheight_blkid = block_id;
 	if (block_id == current_block_id)
 	if (block_id == current_block_id)
 		__update_block_title(NULL);
 		__update_block_title(NULL);
-	mutex_unlock(&ch_lock);
+	cg_wunlock(&ch_lock);
 }
 }
 
 
 static bool work_decode(struct pool *pool, struct work *work, json_t *val)
 static bool work_decode(struct pool *pool, struct work *work, json_t *val)
@@ -2486,8 +2489,10 @@ static void curses_print_status(void)
 			pool->sockaddr_url, pool->diff, have_longpoll ? "": "out", pool->rpc_user);
 			pool->sockaddr_url, pool->diff, have_longpoll ? "": "out", pool->rpc_user);
 	}
 	}
 	wclrtoeol(statuswin);
 	wclrtoeol(statuswin);
+	cg_rlock(&ch_lock);
 	mvwprintw(statuswin, 5, 0, " Block: %s  Diff:%s (%s)  Started: %s",
 	mvwprintw(statuswin, 5, 0, " Block: %s  Diff:%s (%s)  Started: %s",
 		  current_hash, block_diff, net_hashrate, blocktime);
 		  current_hash, block_diff, net_hashrate, blocktime);
+	cg_runlock(&ch_lock);
 	mvwhline(statuswin, 6, 0, '-', 80);
 	mvwhline(statuswin, 6, 0, '-', 80);
 	mvwhline(statuswin, statusy - 1, 0, '-', 80);
 	mvwhline(statuswin, statusy - 1, 0, '-', 80);
 	mvwprintw(statuswin, devcursor - 1, 1, "[P]ool management %s[S]ettings [D]isplay options [Q]uit",
 	mvwprintw(statuswin, devcursor - 1, 1, "[P]ool management %s[S]ettings [D]isplay options [Q]uit",
@@ -4600,26 +4605,20 @@ static char *blkhashstr(unsigned char *hash)
 static void set_curblock(char *hexstr, unsigned char *hash)
 static void set_curblock(char *hexstr, unsigned char *hash)
 {
 {
 	unsigned char hash_swap[32];
 	unsigned char hash_swap[32];
-	char *old_hash;
 
 
 	current_block_id = ((uint32_t*)hash)[0];
 	current_block_id = ((uint32_t*)hash)[0];
 	strcpy(current_block, hexstr);
 	strcpy(current_block, hexstr);
 	swap256(hash_swap, hash);
 	swap256(hash_swap, hash);
 	swap32tole(hash_swap, hash_swap, 32 / 4);
 	swap32tole(hash_swap, hash_swap, 32 / 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);
 	gettimeofday(&block_timeval, NULL);
 	__update_block_title(hash_swap);
 	__update_block_title(hash_swap);
-	old_hash = current_fullhash;
+	free(current_fullhash);
 	current_fullhash = bin2hex(hash_swap, 32);
 	current_fullhash = bin2hex(hash_swap, 32);
-	free(old_hash);
-	mutex_unlock(&ch_lock);
-
 	get_timestamp(blocktime, &block_timeval);
 	get_timestamp(blocktime, &block_timeval);
-
 	applog(LOG_INFO, "New block: %s diff %s (%s)", current_hash, block_diff, net_hashrate);
 	applog(LOG_INFO, "New block: %s diff %s (%s)", current_hash, block_diff, net_hashrate);
+	cg_wunlock(&ch_lock);
 }
 }
 
 
 /* Search to see if this string is from a block that has been seen before */
 /* Search to see if this string is from a block that has been seen before */
@@ -8287,7 +8286,7 @@ int main(int argc, char *argv[])
 	cglock_init(&control_lock);
 	cglock_init(&control_lock);
 	mutex_init(&stats_lock);
 	mutex_init(&stats_lock);
 	mutex_init(&sharelog_lock);
 	mutex_init(&sharelog_lock);
-	mutex_init(&ch_lock);
+	cglock_init(&ch_lock);
 	mutex_init(&sshare_lock);
 	mutex_init(&sshare_lock);
 	rwlock_init(&blk_lock);
 	rwlock_init(&blk_lock);
 	rwlock_init(&netacc_lock);
 	rwlock_init(&netacc_lock);

+ 1 - 1
miner.h

@@ -850,7 +850,7 @@ extern int opt_expiry;
 extern cglock_t control_lock;
 extern cglock_t control_lock;
 extern pthread_mutex_t hash_lock;
 extern pthread_mutex_t hash_lock;
 extern pthread_mutex_t console_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 mining_thr_lock;
 extern pthread_rwlock_t devices_lock;
 extern pthread_rwlock_t devices_lock;