From 7510ca24a0ab79a6cf9eb76f13117b4e4d18051e Mon Sep 17 00:00:00 2001 From: Karl Williamson Date: Tue, 8 Dec 2020 18:30:56 -0700 Subject: Make many-reader mutexes more resilient These mutexes rely on a counter being accurate to work. If for some reason that I can't imagine happening, the count goes below 0, this commit resets it to zero, which may be enough to cause the program to continue. --- thread.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'thread.h') diff --git a/thread.h b/thread.h index 4c4966f79d..99679b22f5 100644 --- a/thread.h +++ b/thread.h @@ -298,6 +298,7 @@ MUTEX_LOCK(&(mutex)->lock); \ (mutex)->readers_count--; \ if ((mutex)->readers_count <= 0) { \ + assert((mutex)->readers_count == 0); \ COND_SIGNAL(&(mutex)->wakeup); \ (mutex)->readers_count = 0; \ } \ @@ -308,8 +309,11 @@ STMT_START { \ MUTEX_LOCK(&(mutex)->lock); \ do { \ - if ((mutex)->readers_count == 0) \ + if ((mutex)->readers_count <= 0) { \ + assert((mutex)->readers_count == 0); \ + (mutex)->readers_count = 0; \ break; \ + } \ COND_WAIT(&(mutex)->wakeup, &(mutex)->lock); \ } \ while (1); \ -- cgit v1.2.1