Commit 2586bda59a3c668c56987472c2f6785296e762aa

Con Kolivas 2012-05-31T09:40:32

Use only one longpoll as much as possible by using a pthread conditional broadcast that each longpoll thread waits on and checks if it's the current pool before opening its longpoll connection.

diff --git a/cgminer.c b/cgminer.c
index f460796..42c771e 100644
--- a/cgminer.c
+++ b/cgminer.c
@@ -165,6 +165,9 @@ static pthread_rwlock_t blk_lock;
 
 pthread_rwlock_t netacc_lock;
 
+static pthread_mutex_t lp_lock;
+static pthread_cond_t lp_cond;
+
 double total_mhashes_done;
 static struct timeval total_tv_start, total_tv_end;
 
@@ -2283,6 +2286,10 @@ void switch_pools(struct pool *selected)
 	mutex_lock(&qd_lock);
 	total_queued = 0;
 	mutex_unlock(&qd_lock);
+
+	mutex_lock(&lp_lock);
+	pthread_cond_broadcast(&lp_cond);
+	mutex_unlock(&lp_lock);
 }
 
 static void discard_work(struct work *work)
@@ -3993,6 +4000,14 @@ static struct pool *select_longpoll_pool(struct pool *cp)
 	return NULL;
 }
 
+static void wait_lpcurrent(struct pool *pool)
+{
+	while (pool != current_pool()) {
+		mutex_lock(&lp_lock);
+		pthread_cond_wait(&lp_cond, &lp_lock);
+	}
+}
+
 static void *longpoll_thread(void *userdata)
 {
 	struct pool *cp = (struct pool *)userdata;
@@ -4023,6 +4038,8 @@ retry_pool:
 	/* Any longpoll from any pool is enough for this to be true */
 	have_longpoll = true;
 
+	wait_lpcurrent(cp);
+
 	if (cp == pool)
 		applog(LOG_WARNING, "Long-polling activated for %s", pool->lp_url);
 	else
@@ -4031,6 +4048,8 @@ retry_pool:
 	while (42) {
 		json_t *val, *soval;
 
+		wait_lpcurrent(cp);
+
 		gettimeofday(&start, NULL);
 
 		/* Longpoll connections can be persistent for a very long time
@@ -4726,6 +4745,10 @@ int main(int argc, char *argv[])
 	rwlock_init(&blk_lock);
 	rwlock_init(&netacc_lock);
 
+	mutex_init(&lp_lock);
+	if (unlikely(pthread_cond_init(&lp_cond, NULL)))
+		quit(1, "Failed to pthread_cond_init lp_cond");
+
 	sprintf(packagename, "%s %s", PACKAGE, VERSION);
 
 #ifdef WANT_CPUMINE