Annotation of parser3/src/include/pa_threads.h, revision 1.12
1.7 paf 1: /** @file
1.9 paf 2: Parser: mutex & helpers decls.
3:
1.5 paf 4: Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
1.9 paf 5:
1.6 paf 6: Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
1.5 paf 7:
1.12 ! paf 8: $Id: pa_threads.h,v 1.11 2001/03/23 13:08:09 paf Exp $
1.1 paf 9: */
10:
11: #ifndef PA_THREADS_H
12: #define PA_THREADS_H
13:
1.11 paf 14: #include "pa_config_includes.h"
1.1 paf 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.8 paf 33: /**
1.7 paf 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:
1.8 paf 51: /**
52: put it to first line of a function to ensure thread safety.
1.7 paf 53: @verbatim
1.10 paf 54: void someclass::somefunc(...) { SYNCHRONIZED(thread_safe);
55: ...
56: }
1.7 paf 57: @endverbatim
1.2 paf 58:
1.10 paf 59: considering @a thread_safe to be the object field to flag
1.7 paf 60: whether safety is really needed in this particular object instance
61: */
1.4 paf 62: #define SYNCHRONIZED(athread_safe) AutoSYNCHRONIZED autoSYNCHRONIZED(athread_safe)
1.2 paf 63:
1.12 ! paf 64: #define SYNCH_LOCK global_mutex.acqire()
! 65: #define SYNCH_UNLOCK global_mutex.release()
! 66:
1.2 paf 67: #else // not MULTITHREAD-ed
68:
1.4 paf 69: #define SYNCHRONIZED(athread_safe) /* do nothing */
1.12 ! paf 70:
! 71: #define SYNCH_LOCK
! 72: #define SYNCH_UNLOCK
1.1 paf 73:
74: #endif
1.2 paf 75:
1.1 paf 76:
77: #endif
E-mail: