--- parser3/src/include/pa_threads.h 2001/01/29 11:53:42 1.2 +++ parser3/src/include/pa_threads.h 2001/10/29 08:23:49 1.18 @@ -1,44 +1,57 @@ -/* - $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.18 2001/10/29 08:23:49 paf Exp $ */ #ifndef PA_THREADS_H #define PA_THREADS_H -#ifdef HAVE_CONFIG_H -# include "pa_config.h" -#endif - +#include "pa_config_includes.h" #include "pa_types.h" +class AutoSYNCHRONIZED; -#ifdef MULTITHREAD - +/// simple semaphore object class Mutex { + friend class AutoSYNCHRONIZED; +private: uint handle; public: Mutex(); ~Mutex(); +private: // for AutoSYNCHRONIZED void acquire(); void release(); }; extern Mutex global_mutex; +/** + Helper to ensure paired Mutex::acquire() and Mutex::release(). + + Use it with SYNCHRONIZED macro +*/ class AutoSYNCHRONIZED { public: AutoSYNCHRONIZED() { global_mutex.acquire(); } ~AutoSYNCHRONIZED() { global_mutex.release(); } -} - -#define SYNCHRONIZED AutoSYNCHRONIZED autoSYNCHRONIZED() - - -#else // not MULTITHREAD-ed - -#define SYNCHRONIZED /* do nothing */ +}; -#endif +/** + put it to first line of a function to ensure thread safety. + @verbatim + void someclass::somefunc(...) { SYNCHRONIZED; + ... + } + @endverbatim + WARNING: don't use THROW or PTHROW with such thread safety mechanizm - + longjmp would leave global_mutex acquired, which is wrong! +*/ +#define SYNCHRONIZED AutoSYNCHRONIZED autoSYNCHRONIZED #endif