Replace two-statement mutex_lockguard macro with single RAII declaration

The old macro expanded to two statements (mutex_lock + DEFER unlock).
In an unbraced if/else only the lock would be conditional while the
DEFER always executed — a latent double-unlock / unbalanced-unlock bug.

Add ScopedMutexLock to both debug and ndebug headers:
 - debug: stores __FILE__/__func__/__LINE__ from the call site and
   passes them to _mutex_lock/_mutex_unlock for diagnostics
 - ndebug: lightweight wrapper around mutex_lock/mutex_unlock

Redefine mutex_lockguard to create a single uniquely-named
ScopedMutexLock variable (via __COUNTER__), making it safe in any
statement context. Remove the now-unused scope_guard include.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Antonio S. Musumeci 2026-04-09 21:42:24 -04:00
parent 13fc1b27df
commit 8baf274cde
3 changed files with 62 additions and 7 deletions

View file

@ -130,3 +130,30 @@ _mutex_unlock(pthread_mutex_t &mutex_,
if(rv != 0)
_print_error_and_abort(__func__,rv,file_,func_,linenum_);
}
class ScopedMutexLock
{
private:
pthread_mutex_t &_mutex;
const char *_file;
const char *_func;
int _line;
public:
ScopedMutexLock(pthread_mutex_t &mutex_,
const char *file_,
const char *func_,
int line_)
: _mutex(mutex_), _file(file_), _func(func_), _line(line_)
{
_mutex_lock(_mutex,_file,_func,_line);
}
~ScopedMutexLock()
{
_mutex_unlock(_mutex,_file,_func,_line);
}
ScopedMutexLock(const ScopedMutexLock&) = delete;
ScopedMutexLock& operator=(const ScopedMutexLock&) = delete;
};