|
|
| version 1.8, 2001/03/19 16:44:00 | version 1.25, 2002/08/01 11:41:16 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser | Parser: mutex & helpers decls. |
| Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) | |
| Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf) | |
| $Id$ | Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com) |
| Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) | |
| */ | */ |
| #ifndef PA_THREADS_H | #ifndef PA_THREADS_H |
| #define PA_THREADS_H | #define PA_THREADS_H |
| #ifdef HAVE_CONFIG_H | static const char* IDENT_THREADS_H="$Date$"; |
| # include "pa_config.h" | |
| #endif | |
| #include "pa_config_includes.h" | |
| #include "pa_types.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 | /// simple semaphore object |
| class Mutex { | class Mutex { |
| friend class AutoSYNCHRONIZED; | |
| private: | |
| uint handle; | uint handle; |
| public: | public: |
| Mutex(); | Mutex(); |
| ~Mutex(); | ~Mutex(); |
| private: // for AutoSYNCHRONIZED | |
| void acquire(); | void acquire(); |
| void release(); | void release(); |
| }; | }; |
| extern const bool parser_multithreaded; | |
| extern Mutex global_mutex; | extern Mutex global_mutex; |
| /** | /** |
| Line 36 extern Mutex global_mutex; | Line 43 extern Mutex global_mutex; |
| Use it with SYNCHRONIZED macro | Use it with SYNCHRONIZED macro |
| */ | */ |
| class AutoSYNCHRONIZED { | class AutoSYNCHRONIZED { |
| bool thread_safe; | |
| public: | public: |
| AutoSYNCHRONIZED(bool athread_safe) : thread_safe(athread_safe) { | AutoSYNCHRONIZED() { global_mutex.acquire(); } |
| if(thread_safe) | ~AutoSYNCHRONIZED() { global_mutex.release(); } |
| global_mutex.acquire(); | |
| } | |
| ~AutoSYNCHRONIZED() { | |
| if(thread_safe) | |
| global_mutex.release(); | |
| } | |
| }; | }; |
| /** | /** |
| 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 | @verbatim |
| void someclass::somefunc(...) { SYNCHRONIZED(thread_safe); | void someclass::somefunc(...) { SYNCHRONIZED; |
| ... | ... |
| } | } |
| @endverbatim | @endverbatim |
| considering \a thread_safe to be the object field to flag | WARNING: don't use THROW or PTHROW with such thread safety mechanizm - |
| whether safety is really needed in this particular object instance | longjmp would leave global_mutex acquired, which is wrong! |
| */ | */ |
| #define SYNCHRONIZED(athread_safe) AutoSYNCHRONIZED autoSYNCHRONIZED(athread_safe) | #define SYNCHRONIZED AutoSYNCHRONIZED autoSYNCHRONIZED |
| #else // not MULTITHREAD-ed | |
| #define SYNCHRONIZED(athread_safe) /* do nothing */ | |
| #endif | |
| #endif | #endif |