--- parser3/src/include/pa_threads.h 2001/01/29 12:13:14 1.3 +++ parser3/src/include/pa_threads.h 2002/08/01 11:26:48 1.24 @@ -1,51 +1,64 @@ -/* - $Id: pa_threads.h,v 1.3 2001/01/29 12:13:14 paf Exp $ +/** @file + Parser: mutex & helpers decls. + + Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com) + Author: Alexandr Petrosian (http://paf.design.ru) */ #ifndef PA_THREADS_H #define PA_THREADS_H -#ifdef HAVE_CONFIG_H -# include "pa_config.h" -#endif +static const char* IDENT_THREADS_H="$Id: pa_threads.h,v 1.24 2002/08/01 11:26:48 paf Exp $"; +#include "pa_config_includes.h" #include "pa_types.h" +/// thread ID type +typedef unsigned int pa_thread_t; + +/// get caller thread ID +pa_thread_t pa_get_thread_id(); -#ifdef MULTITHREAD +class AutoSYNCHRONIZED; +/// simple semaphore object class Mutex { + friend class AutoSYNCHRONIZED; +private: uint handle; public: Mutex(); ~Mutex(); +private: // for AutoSYNCHRONIZED void acquire(); void release(); }; +extern const bool parser_multithreaded; 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(bool athread_safe) : thread_safe(athread_safe) { - if(thread_safe) - global_mutex.acquire(); - } - ~AutoSYNCHRONIZED() { - if(thread_safe) - global_mutex.release(); - } -} - -#define SYNCHRONIZED(thread_safe) AutoSYNCHRONIZED autoSYNCHRONIZED(thread_safe) - - -#else // not MULTITHREAD-ed - -#define SYNCHRONIZED(thread_safe) /* do nothing */ + AutoSYNCHRONIZED() { global_mutex.acquire(); } + ~AutoSYNCHRONIZED() { global_mutex.release(); } +}; -#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