Commit b8ed231f6452b57d727ed44ef603434cd2320584

Con Kolivas 2013-03-12T22:40:23

Add intermediate variants of cglocks that can be up or downgraded to read or write locks and use them for stratum work generation.

diff --git a/cgminer.c b/cgminer.c
index cdfb889..d527ed7 100644
--- a/cgminer.c
+++ b/cgminer.c
@@ -5334,11 +5334,15 @@ static void gen_stratum_work(struct pool *pool, struct work *work)
 	size_t alloc_len;
 	int i;
 
-	cg_wlock(&pool->data_lock);
+	/* Use intermediate lock to update the one pool variable */
+	cg_ilock(&pool->data_lock);
 
 	/* Generate coinbase */
 	work->nonce2 = bin2hex((const unsigned char *)&pool->nonce2, pool->n2size);
 	pool->nonce2++;
+
+	/* Downgrade to a read lock to read off the pool variables */
+	cg_dlock(&pool->data_lock);
 	alloc_len = pool->swork.cb_len;
 	align_len(&alloc_len);
 	coinbase = calloc(alloc_len, 1);
@@ -5387,7 +5391,7 @@ static void gen_stratum_work(struct pool *pool, struct work *work)
 	work->job_id = strdup(pool->swork.job_id);
 	work->nonce1 = strdup(pool->nonce1);
 	work->ntime = strdup(pool->swork.ntime);
-	cg_wunlock(&pool->data_lock);
+	cg_runlock(&pool->data_lock);
 
 	applog(LOG_DEBUG, "Generated stratum merkle %s", merkle_hash);
 	applog(LOG_DEBUG, "Generated stratum header %s", header);
diff --git a/miner.h b/miner.h
index 5962cce..36a9c11 100644
--- a/miner.h
+++ b/miner.h
@@ -710,6 +710,7 @@ static inline void cglock_init(cglock_t *lock)
 	rwlock_init(&lock->rwlock);
 }
 
+/* Read lock variant of cglock */
 static inline void cg_rlock(cglock_t *lock)
 {
 	mutex_lock(&lock->mutex);
@@ -717,10 +718,29 @@ static inline void cg_rlock(cglock_t *lock)
 	mutex_unlock(&lock->mutex);
 }
 
+/* Intermediate variant of cglock */
+static inline void cg_ilock(cglock_t *lock)
+{
+	mutex_lock(&lock->mutex);
+}
+
+/* Upgrade intermediate variant to a write lock */
+static inline void cg_ulock(cglock_t *lock)
+{
+	wr_lock(&lock->rwlock);
+}
+
+/* Write lock variant of cglock */
 static inline void cg_wlock(cglock_t *lock)
 {
 	mutex_lock(&lock->mutex);
 	wr_lock(&lock->rwlock);
+}
+
+/* Downgrade intermediate variant to a read lock */
+static inline void cg_dlock(cglock_t *lock)
+{
+	rd_lock(&lock->rwlock);
 	mutex_unlock(&lock->mutex);
 }
 
@@ -732,6 +752,7 @@ static inline void cg_runlock(cglock_t *lock)
 static inline void cg_wunlock(cglock_t *lock)
 {
 	wr_unlock(&lock->rwlock);
+	mutex_unlock(&lock->mutex);
 }
 
 struct pool;