--- parser3/src/include/pa_threads.h 2001/03/19 15:29:38 1.7 +++ parser3/src/include/pa_threads.h 2020/12/11 14:59:19 1.35 @@ -1,72 +1,65 @@ /** @file - Parser - Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) - Author: Alexander Petrosyan (http://design.ru/paf) + Parser: mutex & helpers decls. - $Id: pa_threads.h,v 1.7 2001/03/19 15:29:38 paf Exp $ + Copyright (c) 2001-2017 Art. Lebedev Studio (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 +#define IDENT_PA_THREADS_H "$Id: pa_threads.h,v 1.35 2020/12/11 14:59:19 moko Exp $" +#include "pa_config_includes.h" #include "pa_types.h" +/// get caller thread ID +uint pa_get_thread_id(); -#ifdef MULTITHREAD +class AutoSYNCHRONIZED; /// simple semaphore object class Mutex { - uint handle; + friend class AutoSYNCHRONIZED; + +#ifdef WIN32 +HANDLE handle; +#else +pthread_mutex_t handle; +#endif + public: Mutex(); ~Mutex(); +private: // for AutoSYNCHRONIZED void acquire(); void release(); }; extern Mutex global_mutex; -/** @brief +/** 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(); - } + AutoSYNCHRONIZED() { global_mutex.acquire(); } + ~AutoSYNCHRONIZED() { global_mutex.release(); } }; -/** @brief - put it to first line of a function to ensure thread safety - +/** + put it to first line of a function to ensure thread safety. @verbatim - void someclass::somefunc(...) { SYNCHRONIZED(thread_safe); - ... - } + void someclass::somefunc(...) { SYNCHRONIZED; + ... + } @endverbatim - considering \a thread_safe to be the object field to flag - whether safety is really needed in this particular object instance + WARNING: don't use THROW or PTHROW with such thread safety mechanizm - + longjmp would leave global_mutex acquired, which is wrong! */ -#define SYNCHRONIZED(athread_safe) AutoSYNCHRONIZED autoSYNCHRONIZED(athread_safe) - -#else // not MULTITHREAD-ed - -#define SYNCHRONIZED(athread_safe) /* do nothing */ - -#endif - +#define SYNCHRONIZED AutoSYNCHRONIZED autoSYNCHRONIZED #endif