Browse Source

Sanity check to prove locking.

nfactor-troky
Con Kolivas 14 years ago
parent
commit
98f609b5be
  1. 80
      main.c
  2. 14
      miner.h
  3. 20
      util.c

80
main.c

@ -284,10 +284,10 @@ static bool pool_tset(struct pool *pool, bool *var)
{ {
bool ret; bool ret;
pthread_mutex_lock(&pool->pool_lock); mutex_lock(&pool->pool_lock);
ret = *var; ret = *var;
*var = true; *var = true;
pthread_mutex_unlock(&pool->pool_lock); mutex_unlock(&pool->pool_lock);
return ret; return ret;
} }
@ -295,10 +295,10 @@ static bool pool_tclear(struct pool *pool, bool *var)
{ {
bool ret; bool ret;
pthread_mutex_lock(&pool->pool_lock); mutex_lock(&pool->pool_lock);
ret = *var; ret = *var;
*var = false; *var = false;
pthread_mutex_unlock(&pool->pool_lock); mutex_unlock(&pool->pool_lock);
return ret; return ret;
} }
@ -306,9 +306,9 @@ static struct pool *current_pool(void)
{ {
struct pool *pool; struct pool *pool;
pthread_mutex_lock(&control_lock); mutex_lock(&control_lock);
pool = currentpool; pool = currentpool;
pthread_mutex_unlock(&control_lock); mutex_unlock(&control_lock);
return pool; return pool;
} }
@ -831,9 +831,9 @@ static void print_status(int thr_id)
if (!curses_active) if (!curses_active)
text_print_status(thr_id); text_print_status(thr_id);
else { else {
pthread_mutex_lock(&curses_lock); mutex_lock(&curses_lock);
curses_print_status(thr_id); curses_print_status(thr_id);
pthread_mutex_unlock(&curses_lock); mutex_unlock(&curses_lock);
} }
} }
@ -865,24 +865,24 @@ static void wlogprint(const char *f, ...)
{ {
va_list ap; va_list ap;
pthread_mutex_lock(&curses_lock); mutex_lock(&curses_lock);
va_start(ap, f); va_start(ap, f);
vw_printw(logwin, f, ap); vw_printw(logwin, f, ap);
va_end(ap); va_end(ap);
wrefresh(logwin); wrefresh(logwin);
pthread_mutex_unlock(&curses_lock); mutex_unlock(&curses_lock);
} }
void log_curses(const char *f, va_list ap) void log_curses(const char *f, va_list ap)
{ {
if (curses_active) { if (curses_active) {
if (!opt_loginput) { if (!opt_loginput) {
pthread_mutex_lock(&curses_lock); mutex_lock(&curses_lock);
vw_printw(logwin, f, ap); vw_printw(logwin, f, ap);
wrefresh(logwin); wrefresh(logwin);
pthread_mutex_unlock(&curses_lock); mutex_unlock(&curses_lock);
} }
} else } else
vprintf(f, ap); vprintf(f, ap);
@ -890,10 +890,10 @@ void log_curses(const char *f, va_list ap)
static void clear_logwin(void) static void clear_logwin(void)
{ {
pthread_mutex_lock(&curses_lock); mutex_lock(&curses_lock);
wclear(logwin); wclear(logwin);
wrefresh(logwin); wrefresh(logwin);
pthread_mutex_unlock(&curses_lock); mutex_unlock(&curses_lock);
} }
static bool submit_upstream_work(const struct work *work) static bool submit_upstream_work(const struct work *work)
@ -1287,7 +1287,7 @@ static bool workio_submit_work(struct workio_cmd *wc)
static void inc_staged(struct pool *pool, int inc, bool lp) static void inc_staged(struct pool *pool, int inc, bool lp)
{ {
pthread_mutex_lock(&stgd_lock); mutex_lock(&stgd_lock);
if (lp) { if (lp) {
lp_staged += inc; lp_staged += inc;
total_staged += inc; total_staged += inc;
@ -1295,23 +1295,23 @@ static void inc_staged(struct pool *pool, int inc, bool lp)
--lp_staged; --lp_staged;
else else
total_staged += inc; total_staged += inc;
pthread_mutex_unlock(&stgd_lock); mutex_unlock(&stgd_lock);
} }
static void dec_staged(int inc) static void dec_staged(int inc)
{ {
pthread_mutex_lock(&stgd_lock); mutex_lock(&stgd_lock);
total_staged -= inc; total_staged -= inc;
pthread_mutex_unlock(&stgd_lock); mutex_unlock(&stgd_lock);
} }
static int requests_staged(void) static int requests_staged(void)
{ {
int ret; int ret;
pthread_mutex_lock(&stgd_lock); mutex_lock(&stgd_lock);
ret = total_staged; ret = total_staged;
pthread_mutex_unlock(&stgd_lock); mutex_unlock(&stgd_lock);
return ret; return ret;
} }
@ -1319,9 +1319,9 @@ static int real_staged(void)
{ {
int ret; int ret;
pthread_mutex_lock(&stgd_lock); mutex_lock(&stgd_lock);
ret = total_staged - lp_staged; ret = total_staged - lp_staged;
pthread_mutex_unlock(&stgd_lock); mutex_unlock(&stgd_lock);
return ret; return ret;
} }
@ -1354,7 +1354,7 @@ static void switch_pools(struct pool *selected)
struct pool *pool, *last_pool; struct pool *pool, *last_pool;
int i, pool_no; int i, pool_no;
pthread_mutex_lock(&control_lock); mutex_lock(&control_lock);
last_pool = currentpool; last_pool = currentpool;
pool_no = currentpool->pool_no; pool_no = currentpool->pool_no;
@ -1399,7 +1399,7 @@ static void switch_pools(struct pool *selected)
currentpool = pools[pool_no]; currentpool = pools[pool_no];
pool = currentpool; pool = currentpool;
pthread_mutex_unlock(&control_lock); mutex_unlock(&control_lock);
if (pool != last_pool) { if (pool != last_pool) {
applog(LOG_WARNING, "Switching to %s", pool->rpc_url); applog(LOG_WARNING, "Switching to %s", pool->rpc_url);
@ -1407,9 +1407,9 @@ static void switch_pools(struct pool *selected)
} }
/* Reset the queued amount to allow more to be queued for the new pool */ /* Reset the queued amount to allow more to be queued for the new pool */
pthread_mutex_lock(&qd_lock); mutex_lock(&qd_lock);
total_queued = 0; total_queued = 0;
pthread_mutex_unlock(&qd_lock); mutex_unlock(&qd_lock);
inc_staged(pool, 1, true); inc_staged(pool, 1, true);
} }
@ -1521,7 +1521,7 @@ static void display_pool_summary(struct pool *pool)
{ {
double efficiency = 0.0; double efficiency = 0.0;
pthread_mutex_lock(&curses_lock); mutex_lock(&curses_lock);
wlog("Pool: %s\n", pool->rpc_url); wlog("Pool: %s\n", pool->rpc_url);
wlog(" Queued work requests: %d\n", pool->getwork_requested); wlog(" Queued work requests: %d\n", pool->getwork_requested);
wlog(" Share submissions: %d\n", pool->accepted + pool->rejected); wlog(" Share submissions: %d\n", pool->accepted + pool->rejected);
@ -1537,7 +1537,7 @@ static void display_pool_summary(struct pool *pool)
wlog(" Unable to get work from server occasions: %d\n", pool->localgen_occasions); wlog(" Unable to get work from server occasions: %d\n", pool->localgen_occasions);
wlog(" Submitting work remotely delay occasions: %d\n\n", pool->remotefail_occasions); wlog(" Submitting work remotely delay occasions: %d\n\n", pool->remotefail_occasions);
wrefresh(logwin); wrefresh(logwin);
pthread_mutex_unlock(&curses_lock); mutex_unlock(&curses_lock);
} }
/* We can't remove the memory used for this struct pool because there may /* We can't remove the memory used for this struct pool because there may
@ -2031,7 +2031,7 @@ static void hashmeter(int thr_id, struct timeval *diff,
} }
/* Totals are updated by all threads so can race without locking */ /* Totals are updated by all threads so can race without locking */
pthread_mutex_lock(&hash_lock); mutex_lock(&hash_lock);
gettimeofday(&temp_tv_end, NULL); gettimeofday(&temp_tv_end, NULL);
timeval_subtract(&total_diff, &temp_tv_end, &total_tv_end); timeval_subtract(&total_diff, &temp_tv_end, &total_tv_end);
@ -2063,24 +2063,24 @@ static void hashmeter(int thr_id, struct timeval *diff,
local_mhashes_done = 0; local_mhashes_done = 0;
out_unlock: out_unlock:
pthread_mutex_unlock(&hash_lock); mutex_unlock(&hash_lock);
} }
/* This is overkill, but at least we'll know accurately how much work is /* This is overkill, but at least we'll know accurately how much work is
* queued to prevent ever being left without work */ * queued to prevent ever being left without work */
static void inc_queued(void) static void inc_queued(void)
{ {
pthread_mutex_lock(&qd_lock); mutex_lock(&qd_lock);
total_queued++; total_queued++;
pthread_mutex_unlock(&qd_lock); mutex_unlock(&qd_lock);
} }
static void dec_queued(void) static void dec_queued(void)
{ {
pthread_mutex_lock(&qd_lock); mutex_lock(&qd_lock);
if (total_queued > 0) if (total_queued > 0)
total_queued--; total_queued--;
pthread_mutex_unlock(&qd_lock); mutex_unlock(&qd_lock);
dec_staged(1); dec_staged(1);
} }
@ -2088,9 +2088,9 @@ static int requests_queued(void)
{ {
int ret; int ret;
pthread_mutex_lock(&qd_lock); mutex_lock(&qd_lock);
ret = total_queued; ret = total_queued;
pthread_mutex_unlock(&qd_lock); mutex_unlock(&qd_lock);
return ret; return ret;
} }
@ -3083,8 +3083,6 @@ static void stop_longpoll(void)
have_longpoll = false; have_longpoll = false;
} }
static void quit(int status, const char *format, ...);
static void start_longpoll(void) static void start_longpoll(void)
{ {
struct thr_info *thr = &thr_info[longpoll_thr_id]; struct thr_info *thr = &thr_info[longpoll_thr_id];
@ -3244,7 +3242,7 @@ static void *watchdog_thread(void *userdata)
if (curses_active) { if (curses_active) {
statwin ^= true; statwin ^= true;
pthread_mutex_lock(&curses_lock); mutex_lock(&curses_lock);
for (i = 0; i < mining_threads; i++) for (i = 0; i < mining_threads; i++)
curses_print_status(i); curses_print_status(i);
if (statwin) if (statwin)
@ -3253,7 +3251,7 @@ static void *watchdog_thread(void *userdata)
check_logwinsize(); check_logwinsize();
redrawwin(logwin); redrawwin(logwin);
} }
pthread_mutex_unlock(&curses_lock); mutex_unlock(&curses_lock);
} }
if (unlikely(work_restart[watchdog_thr_id].restart)) { if (unlikely(work_restart[watchdog_thr_id].restart)) {
@ -3378,7 +3376,7 @@ static void print_summary(void)
printf("\n"); printf("\n");
} }
static void quit(int status, const char *format, ...) void quit(int status, const char *format, ...)
{ {
va_list ap; va_list ap;

14
miner.h

@ -184,6 +184,20 @@ static inline void swap256(void *dest_p, const void *src_p)
dest[7] = src[0]; dest[7] = src[0];
} }
extern void quit(int status, const char *format, ...);
static inline void mutex_lock(pthread_mutex_t *lock)
{
if (unlikely(pthread_mutex_lock(lock)))
quit(1, "WTF MUTEX ERROR ON LOCK!");
}
static inline void mutex_unlock(pthread_mutex_t *lock)
{
if (unlikely(pthread_mutex_unlock(lock)))
quit(1, "WTF MUTEX ERROR ON UNLOCK!");
}
struct pool; struct pool;
extern bool opt_debug; extern bool opt_debug;

20
util.c

@ -47,10 +47,10 @@ bool test_and_set(bool *var)
{ {
bool ret; bool ret;
pthread_mutex_lock(&control_lock); mutex_lock(&control_lock);
ret = *var; ret = *var;
*var = true; *var = true;
pthread_mutex_unlock(&control_lock); mutex_unlock(&control_lock);
return ret; return ret;
} }
@ -58,10 +58,10 @@ bool test_and_clear(bool *var)
{ {
bool ret; bool ret;
pthread_mutex_lock(&control_lock); mutex_lock(&control_lock);
ret = *var; ret = *var;
*var = false; *var = false;
pthread_mutex_unlock(&control_lock); mutex_unlock(&control_lock);
return ret; return ret;
} }
@ -600,12 +600,12 @@ void tq_free(struct thread_q *tq)
static void tq_freezethaw(struct thread_q *tq, bool frozen) static void tq_freezethaw(struct thread_q *tq, bool frozen)
{ {
pthread_mutex_lock(&tq->mutex); mutex_lock(&tq->mutex);
tq->frozen = frozen; tq->frozen = frozen;
pthread_cond_signal(&tq->cond); pthread_cond_signal(&tq->cond);
pthread_mutex_unlock(&tq->mutex); mutex_unlock(&tq->mutex);
} }
void tq_freeze(struct thread_q *tq) void tq_freeze(struct thread_q *tq)
@ -630,7 +630,7 @@ bool tq_push(struct thread_q *tq, void *data)
ent->data = data; ent->data = data;
INIT_LIST_HEAD(&ent->q_node); INIT_LIST_HEAD(&ent->q_node);
pthread_mutex_lock(&tq->mutex); mutex_lock(&tq->mutex);
if (!tq->frozen) { if (!tq->frozen) {
list_add_tail(&ent->q_node, &tq->q); list_add_tail(&ent->q_node, &tq->q);
@ -640,7 +640,7 @@ bool tq_push(struct thread_q *tq, void *data)
} }
pthread_cond_signal(&tq->cond); pthread_cond_signal(&tq->cond);
pthread_mutex_unlock(&tq->mutex); mutex_unlock(&tq->mutex);
return rc; return rc;
} }
@ -651,7 +651,7 @@ void *tq_pop(struct thread_q *tq, const struct timespec *abstime)
void *rval = NULL; void *rval = NULL;
int rc; int rc;
pthread_mutex_lock(&tq->mutex); mutex_lock(&tq->mutex);
if (!list_empty(&tq->q)) if (!list_empty(&tq->q))
goto pop; goto pop;
@ -673,7 +673,7 @@ pop:
free(ent); free(ent);
out: out:
pthread_mutex_unlock(&tq->mutex); mutex_unlock(&tq->mutex);
return rval; return rval;
} }

Loading…
Cancel
Save