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