Annotation of parser3/src/include/pa_threads.h, revision 1.10
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.10 ! paf 8: $Id: pa_threads.h,v 1.9 2001/03/19 17:42:13 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: @verbatim
1.10 ! paf 56: void someclass::somefunc(...) { SYNCHRONIZED(thread_safe);
! 57: ...
! 58: }
1.7 paf 59: @endverbatim
1.2 paf 60:
1.10 ! paf 61: considering @a thread_safe to be the object field to flag
1.7 paf 62: whether safety is really needed in this particular object instance
63: */
1.4 paf 64: #define SYNCHRONIZED(athread_safe) AutoSYNCHRONIZED autoSYNCHRONIZED(athread_safe)
1.2 paf 65:
66: #else // not MULTITHREAD-ed
67:
1.4 paf 68: #define SYNCHRONIZED(athread_safe) /* do nothing */
1.1 paf 69:
70: #endif
1.2 paf 71:
1.1 paf 72:
73: #endif
E-mail: