Annotation of parser3/src/include/pa_threads.h, revision 1.34

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.34    ! moko       11: #define IDENT_PA_THREADS_H "$Id: pa_threads.h,v 1.33 2017/02/07 22:00:37 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: /// thread ID type
                     17: typedef unsigned int pa_thread_t;
                     18: 
                     19: /// get caller thread ID
                     20: pa_thread_t pa_get_thread_id();
1.1       paf        21: 
1.13      parser     22: class AutoSYNCHRONIZED;
                     23: 
1.7       paf        24: /// simple semaphore object
1.1       paf        25: class Mutex {
1.18      paf        26:        friend class AutoSYNCHRONIZED;
1.13      parser     27: private:
1.1       paf        28:        uint handle;
                     29: public:
                     30:        Mutex();
                     31:        ~Mutex();
1.13      parser     32: private: // for AutoSYNCHRONIZED
1.2       paf        33:        void acquire();
                     34:        void release();
1.1       paf        35: };
                     36: 
1.2       paf        37: extern Mutex global_mutex;
1.1       paf        38: 
1.8       paf        39: /** 
1.7       paf        40:        Helper to ensure paired Mutex::acquire() and Mutex::release().
                     41: 
                     42:        Use it with SYNCHRONIZED macro
                     43: */
1.2       paf        44: class AutoSYNCHRONIZED {
                     45: public:
1.13      parser     46:        AutoSYNCHRONIZED() { global_mutex.acquire(); }
                     47:        ~AutoSYNCHRONIZED() { global_mutex.release(); }
1.7       paf        48: };
                     49: 
1.8       paf        50: /** 
                     51:        put it to first line of a function to ensure thread safety.
1.7       paf        52:        @verbatim
1.13      parser     53:                void someclass::somefunc(...) { SYNCHRONIZED;
1.10      paf        54:                        ...
                     55:                }
1.7       paf        56:        @endverbatim
1.2       paf        57: 
1.13      parser     58:        WARNING: don't use THROW or PTHROW with such thread safety mechanizm -
1.15      parser     59:        longjmp would leave global_mutex acquired, which is wrong!
1.7       paf        60: */
1.16      parser     61: #define SYNCHRONIZED AutoSYNCHRONIZED autoSYNCHRONIZED
1.1       paf        62: 
                     63: #endif

E-mail: