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