--- parser3/src/include/pa_threads.h 2001/01/29 11:53:42 1.2 +++ parser3/src/include/pa_threads.h 2001/03/20 06:45:18 1.10 @@ -1,5 +1,11 @@ -/* - $Id: pa_threads.h,v 1.2 2001/01/29 11:53:42 paf Exp $ +/** @file + Parser: mutex & helpers decls. + + Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) + + Author: Alexander Petrosyan (http://design.ru/paf) + + $Id: pa_threads.h,v 1.10 2001/03/20 06:45:18 paf Exp $ */ #ifndef PA_THREADS_H @@ -14,6 +20,7 @@ #ifdef MULTITHREAD +/// simple semaphore object class Mutex { uint handle; public: @@ -25,18 +32,40 @@ public: extern Mutex global_mutex; +/** + Helper to ensure paired Mutex::acquire() and Mutex::release(). + + Use it with SYNCHRONIZED macro +*/ class AutoSYNCHRONIZED { + bool thread_safe; public: - AutoSYNCHRONIZED() { global_mutex.acquire(); } - ~AutoSYNCHRONIZED() { global_mutex.release(); } -} + AutoSYNCHRONIZED(bool athread_safe) : thread_safe(athread_safe) { + if(thread_safe) + global_mutex.acquire(); + } + ~AutoSYNCHRONIZED() { + if(thread_safe) + global_mutex.release(); + } +}; -#define SYNCHRONIZED AutoSYNCHRONIZED autoSYNCHRONIZED() +/** + put it to first line of a function to ensure thread safety. + @verbatim + void someclass::somefunc(...) { SYNCHRONIZED(thread_safe); + ... + } + @endverbatim + considering @a thread_safe to be the object field to flag + whether safety is really needed in this particular object instance +*/ +#define SYNCHRONIZED(athread_safe) AutoSYNCHRONIZED autoSYNCHRONIZED(athread_safe) #else // not MULTITHREAD-ed -#define SYNCHRONIZED /* do nothing */ +#define SYNCHRONIZED(athread_safe) /* do nothing */ #endif