Use the curses_lock to protect the curses_active variable and test it under lock.
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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
diff --git a/main.c b/main.c
index 77ac515..c5327b4 100644
--- a/main.c
+++ b/main.c
@@ -1391,6 +1391,27 @@ static char statusline[256];
static int cpucursor, gpucursor, logstart, logcursor;
static struct cgpu_info *gpus, *cpus;
+static inline void unlock_curses(void)
+{
+ mutex_unlock(&curses_lock);
+}
+
+static inline void lock_curses(void)
+{
+ mutex_lock(&curses_lock);
+}
+
+static bool curses_active_locked(void)
+{
+ bool ret;
+
+ lock_curses();
+ ret = curses_active;
+ if (!ret)
+ unlock_curses();
+ return ret;
+}
+
static void text_print_status(int thr_id)
{
struct cgpu_info *cgpu = thr_info[thr_id].cgpu;
@@ -1483,13 +1504,11 @@ static void curses_print_status(int thr_id)
static void print_status(int thr_id)
{
- if (!curses_active)
- text_print_status(thr_id);
- else {
- mutex_lock(&curses_lock);
+ if (curses_active_locked()) {
curses_print_status(thr_id);
- mutex_unlock(&curses_lock);
- }
+ unlock_curses();
+ } else
+ text_print_status(thr_id);
}
/* Check for window resize. Called with curses mutex locked */
@@ -1523,14 +1542,13 @@ static void wlogprint(const char *f, ...)
{
va_list ap;
- mutex_lock(&curses_lock);
-
- va_start(ap, f);
- vw_printw(logwin, f, ap);
- va_end(ap);
- wrefresh(logwin);
-
- mutex_unlock(&curses_lock);
+ if (curses_active_locked()) {
+ va_start(ap, f);
+ vw_printw(logwin, f, ap);
+ va_end(ap);
+ wrefresh(logwin);
+ unlock_curses();
+ }
}
void log_curses(int prio, const char *f, va_list ap)
@@ -1538,23 +1556,23 @@ void log_curses(int prio, const char *f, va_list ap)
if (opt_quiet && prio != LOG_ERR)
return;
- if (curses_active) {
+ if (curses_active_locked()) {
if (!opt_loginput) {
- mutex_lock(&curses_lock);
vw_printw(logwin, f, ap);
wrefresh(logwin);
- mutex_unlock(&curses_lock);
}
+ unlock_curses();
} else
vprintf(f, ap);
}
static void clear_logwin(void)
{
- mutex_lock(&curses_lock);
- wclear(logwin);
- wrefresh(logwin);
- mutex_unlock(&curses_lock);
+ if (curses_active_locked()) {
+ wclear(logwin);
+ wrefresh(logwin);
+ unlock_curses();
+ }
}
static bool submit_upstream_work(const struct work *work)
@@ -1765,7 +1783,8 @@ static void workio_cmd_free(struct workio_cmd *wc)
static void disable_curses(void)
{
- if (test_and_clear(&curses_active)) {
+ if (curses_active_locked()) {
+ curses_active = false;
leaveok(logwin, false);
leaveok(statuswin, false);
leaveok(mainwin, false);
@@ -1775,8 +1794,7 @@ static void disable_curses(void)
delwin(statuswin);
delwin(mainwin);
endwin();
- refresh();
-
+ refresh();
#ifdef WIN32
// Move the cursor to after curses output.
HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);
@@ -1789,6 +1807,7 @@ static void disable_curses(void)
SetConsoleCursorPosition(hout, coord);
}
#endif
+ unlock_curses();
}
}
@@ -2302,24 +2321,25 @@ static void display_pool_summary(struct pool *pool)
{
double efficiency = 0.0;
- mutex_lock(&curses_lock);
- wlog("Pool: %s\n", pool->rpc_url);
- wlog("%s long-poll support\n", pool->hdr_path ? "Has" : "Does not have");
- wlog(" Queued work requests: %d\n", pool->getwork_requested);
- wlog(" Share submissions: %d\n", pool->accepted + pool->rejected);
- wlog(" Accepted shares: %d\n", pool->accepted);
- wlog(" Rejected shares: %d\n", pool->rejected);
- if (pool->accepted || pool->rejected)
- wlog(" Reject ratio: %.1f\n", (double)(pool->rejected * 100) / (double)(pool->accepted + pool->rejected));
- efficiency = pool->getwork_requested ? pool->accepted * 100.0 / pool->getwork_requested : 0.0;
- wlog(" Efficiency (accepted / queued): %.0f%%\n", efficiency);
-
- wlog(" Discarded work due to new blocks: %d\n", pool->discarded_work);
- wlog(" Stale submissions discarded due to new blocks: %d\n", pool->stale_shares);
- 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);
- wrefresh(logwin);
- mutex_unlock(&curses_lock);
+ if (curses_active_locked()) {
+ wlog("Pool: %s\n", pool->rpc_url);
+ wlog("%s long-poll support\n", pool->hdr_path ? "Has" : "Does not have");
+ wlog(" Queued work requests: %d\n", pool->getwork_requested);
+ wlog(" Share submissions: %d\n", pool->accepted + pool->rejected);
+ wlog(" Accepted shares: %d\n", pool->accepted);
+ wlog(" Rejected shares: %d\n", pool->rejected);
+ if (pool->accepted || pool->rejected)
+ wlog(" Reject ratio: %.1f\n", (double)(pool->rejected * 100) / (double)(pool->accepted + pool->rejected));
+ efficiency = pool->getwork_requested ? pool->accepted * 100.0 / pool->getwork_requested : 0.0;
+ wlog(" Efficiency (accepted / queued): %.0f%%\n", efficiency);
+
+ wlog(" Discarded work due to new blocks: %d\n", pool->discarded_work);
+ wlog(" Stale submissions discarded due to new blocks: %d\n", pool->stale_shares);
+ 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);
+ wrefresh(logwin);
+ unlock_curses();
+ }
}
/* We can't remove the memory used for this struct pool because there may
@@ -4138,15 +4158,14 @@ static void *watchdog_thread(void *userdata)
hashmeter(-1, &zero_tv, 0);
- if (curses_active) {
- mutex_lock(&curses_lock);
+ if (curses_active_locked()) {
for (i = 0; i < mining_threads; i++)
curses_print_status(i);
if (!change_logwinsize()) {
redrawwin(statuswin);
redrawwin(logwin);
}
- mutex_unlock(&curses_lock);
+ unlock_curses();
}
gettimeofday(&now, NULL);
@@ -4480,8 +4499,11 @@ out:
static void enable_curses(void) {
int x,y;
- if (curses_active)
+ lock_curses();
+ if (curses_active) {
+ unlock_curses();
return;
+ }
mainwin = initscr();
getmaxyx(mainwin, y, x);
@@ -4493,7 +4515,8 @@ static void enable_curses(void) {
leaveok(logwin, true);
cbreak();
noecho();
- test_and_set(&curses_active);
+ curses_active = true;
+ unlock_curses();
}
int main (int argc, char *argv[])
diff --git a/miner.h b/miner.h
index 0414bf9..a33f452 100644
--- a/miner.h
+++ b/miner.h
@@ -325,7 +325,6 @@ extern bool use_syslog;
extern struct thr_info *thr_info;
extern int longpoll_thr_id;
extern struct work_restart *work_restart;
-extern pthread_mutex_t control_lock;
#ifdef HAVE_OPENCL
typedef struct {
@@ -423,8 +422,6 @@ extern bool tq_push(struct thread_q *tq, void *data);
extern void *tq_pop(struct thread_q *tq, const struct timespec *abstime);
extern void tq_freeze(struct thread_q *tq);
extern void tq_thaw(struct thread_q *tq);
-extern bool test_and_set(bool *var);
-extern bool test_and_clear(bool *var);
extern bool successful_connect;
extern enum cl_kernel chosen_kernel;
diff --git a/util.c b/util.c
index c7d6c7e..def60a8 100644
--- a/util.c
+++ b/util.c
@@ -43,28 +43,6 @@
bool successful_connect = false;
-bool test_and_set(bool *var)
-{
- bool ret;
-
- mutex_lock(&control_lock);
- ret = *var;
- *var = true;
- mutex_unlock(&control_lock);
- return ret;
-}
-
-bool test_and_clear(bool *var)
-{
- bool ret;
-
- mutex_lock(&control_lock);
- ret = *var;
- *var = false;
- mutex_unlock(&control_lock);
- return ret;
-}
-
struct data_buffer {
void *buf;
size_t len;