Annotation of parser3/src/include/pa_threads.h, revision 1.35
1.7 paf 1: /** @file
1.9 paf 2: Parser: mutex & helpers decls.
3:
1.33 moko 4: Copyright (c) 2001-2017 Art. Lebedev Studio (http://www.artlebedev.com)
1.23 paf 5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.1 paf 6: */
7:
8: #ifndef PA_THREADS_H
9: #define PA_THREADS_H
1.24 paf 10:
1.35 ! moko 11: #define IDENT_PA_THREADS_H "$Id: pa_threads.h,v 1.34 2020/12/10 23:07:04 moko Exp $"
1.1 paf 12:
1.11 paf 13: #include "pa_config_includes.h"
1.1 paf 14: #include "pa_types.h"
1.21 paf 15:
16: /// get caller thread ID
1.35 ! moko 17: uint pa_get_thread_id();
1.1 paf 18:
1.13 parser 19: class AutoSYNCHRONIZED;
20:
1.7 paf 21: /// simple semaphore object
1.1 paf 22: class Mutex {
1.18 paf 23: friend class AutoSYNCHRONIZED;
1.35 ! moko 24:
! 25: #ifdef WIN32
! 26: HANDLE handle;
! 27: #else
! 28: pthread_mutex_t handle;
! 29: #endif
! 30:
1.1 paf 31: public:
32: Mutex();
33: ~Mutex();
1.13 parser 34: private: // for AutoSYNCHRONIZED
1.2 paf 35: void acquire();
36: void release();
1.1 paf 37: };
38:
1.2 paf 39: extern Mutex global_mutex;
1.1 paf 40:
1.8 paf 41: /**
1.7 paf 42: Helper to ensure paired Mutex::acquire() and Mutex::release().
43: Use it with SYNCHRONIZED macro
44: */
1.35 ! moko 45:
1.2 paf 46: class AutoSYNCHRONIZED {
47: public:
1.13 parser 48: AutoSYNCHRONIZED() { global_mutex.acquire(); }
49: ~AutoSYNCHRONIZED() { global_mutex.release(); }
1.7 paf 50: };
51:
1.8 paf 52: /**
53: put it to first line of a function to ensure thread safety.
1.7 paf 54: @verbatim
1.13 parser 55: void someclass::somefunc(...) { SYNCHRONIZED;
1.10 paf 56: ...
57: }
1.7 paf 58: @endverbatim
1.2 paf 59:
1.13 parser 60: WARNING: don't use THROW or PTHROW with such thread safety mechanizm -
1.15 parser 61: longjmp would leave global_mutex acquired, which is wrong!
1.7 paf 62: */
1.16 parser 63: #define SYNCHRONIZED AutoSYNCHRONIZED autoSYNCHRONIZED
1.1 paf 64:
65: #endif
E-mail: