Annotation of parser3/src/include/pa_threads.h, revision 1.7
1.7 ! paf 1: /** @file
1.5 paf 2: Parser
3: Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
1.6 paf 4: Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
1.5 paf 5:
1.7 ! paf 6: $Id: pa_threads.h,v 1.6 2001/03/11 08:16:33 paf Exp $
1.1 paf 7: */
8:
9: #ifndef PA_THREADS_H
10: #define PA_THREADS_H
11:
12: #ifdef HAVE_CONFIG_H
13: # include "pa_config.h"
14: #endif
15:
16: #include "pa_types.h"
17:
18:
19: #ifdef MULTITHREAD
20:
1.7 ! paf 21: /// simple semaphore object
1.1 paf 22: class Mutex {
23: uint handle;
24: public:
25: Mutex();
26: ~Mutex();
1.2 paf 27: void acquire();
28: void release();
1.1 paf 29: };
30:
1.2 paf 31: extern Mutex global_mutex;
1.1 paf 32:
1.7 ! paf 33: /** @brief
! 34: Helper to ensure paired Mutex::acquire() and Mutex::release().
! 35:
! 36: Use it with SYNCHRONIZED macro
! 37: */
1.2 paf 38: class AutoSYNCHRONIZED {
1.3 paf 39: bool thread_safe;
1.2 paf 40: public:
1.3 paf 41: AutoSYNCHRONIZED(bool athread_safe) : thread_safe(athread_safe) {
42: if(thread_safe)
43: global_mutex.acquire();
44: }
45: ~AutoSYNCHRONIZED() {
46: if(thread_safe)
47: global_mutex.release();
48: }
1.7 ! paf 49: };
! 50:
! 51: /** @brief
! 52: put it to first line of a function to ensure thread safety
! 53:
! 54: @verbatim
! 55: void someclass::somefunc(...) { SYNCHRONIZED(thread_safe);
! 56: ...
! 57: }
! 58: @endverbatim
1.2 paf 59:
1.7 ! paf 60: considering \a thread_safe to be the object field to flag
! 61: whether safety is really needed in this particular object instance
! 62: */
1.4 paf 63: #define SYNCHRONIZED(athread_safe) AutoSYNCHRONIZED autoSYNCHRONIZED(athread_safe)
1.2 paf 64:
65: #else // not MULTITHREAD-ed
66:
1.4 paf 67: #define SYNCHRONIZED(athread_safe) /* do nothing */
1.1 paf 68:
69: #endif
1.2 paf 70:
1.1 paf 71:
72: #endif
E-mail: