Add intermediate variants of cglocks that can be up or downgraded to read or write locks and use them for stratum work generation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
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;