1 #ifndef CRLOCKS_H
2 #define CRLOCKS_H
3 
4 #include "lvautoptr.h"
5 
6 class CRMutex {
7 public:
~CRMutex()8     virtual ~CRMutex() {}
9     virtual void acquire() = 0;
10     virtual void release() = 0;
11 };
12 
13 class CRMonitor : public CRMutex {
14 public:
15     virtual void wait() = 0;
16     virtual void notify() = 0;
17     virtual void notifyAll() = 0;
18 };
19 
20 class CRRunnable {
21 public:
22     virtual void run() = 0;
~CRRunnable()23     virtual ~CRRunnable() {}
24 };
25 
26 class CRThread {
27 public:
~CRThread()28     virtual ~CRThread() {}
29     virtual void start() = 0;
30     virtual void join() = 0;
31 };
32 
33 class CRExecutor {
34 public:
~CRExecutor()35     virtual ~CRExecutor() {}
36     virtual void execute(CRRunnable * task) = 0;
37 };
38 
39 typedef LVAutoPtr<CRThread> CRThreadRef;
40 typedef LVAutoPtr<CRMonitor> CRMonitorRef;
41 typedef LVAutoPtr<CRMutex> CRMutexRef;
42 
43 class CRGuard {
44     CRMutex * mutex;
45 public:
CRGuard(CRMutexRef & _mutex)46     CRGuard(CRMutexRef & _mutex) : mutex(_mutex.get()) { if(mutex) mutex->acquire(); }
CRGuard(CRMonitorRef & _mutex)47     CRGuard(CRMonitorRef & _mutex) : mutex(_mutex.get()) { if(mutex) mutex->acquire(); }
CRGuard(CRMutex * _mutex)48     CRGuard(CRMutex * _mutex) : mutex(_mutex) { if(mutex) mutex->acquire(); }
CRGuard(CRMonitor * _mutex)49     CRGuard(CRMonitor * _mutex) : mutex(_mutex) { if(mutex) mutex->acquire(); }
~CRGuard()50     ~CRGuard() { if (mutex) mutex->release(); }
51 };
52 
53 
54 extern CRMutex * _refMutex;
55 extern CRMutex * _fontMutex;
56 extern CRMutex * _fontManMutex;
57 extern CRMutex * _fontGlyphCacheMutex;
58 extern CRMutex * _fontLocalGlyphCacheMutex;
59 extern CRMutex * _crengineMutex;
60 
61 // use REF_GUARD to acquire LVProtectedRef mutex
62 #define REF_GUARD CRGuard _refGuard(_refMutex); CR_UNUSED(_refGuard);
63 // use FONT_GUARD to acquire font operations mutex
64 #define FONT_GUARD CRGuard _fontGuard(_fontMutex); CR_UNUSED(_fontGuard);
65 // use FONT_MAN_GUARD to acquire font manager mutex
66 #define FONT_MAN_GUARD CRGuard _fontManGuard(_fontManMutex); CR_UNUSED(_fontManGuard);
67 // use FONT_GLYPH_CACHE_GUARD to acquire font global glyph cache operations mutex
68 #define FONT_GLYPH_CACHE_GUARD CRGuard _fontGlyphCacheGuard(_fontGlyphCacheMutex); CR_UNUSED(_fontGlyphCacheGuard);
69 // use FONT_LOCAL_GLYPH_CACHE_GUARD to acquire font global glyph cache operations mutex
70 #define FONT_LOCAL_GLYPH_CACHE_GUARD CRGuard _fontLocalGlyphCacheGuard(_fontLocalGlyphCacheMutex); CR_UNUSED(_fontLocalGlyphCacheGuard);
71 // use CRENGINE_GUARD to acquire crengine drawing lock
72 #define CRENGINE_GUARD CRGuard _crengineGuard(_crengineMutex); CR_UNUSED(_crengineMutex);
73 
74 /// call to create mutexes for different parts of CoolReader engine
75 void CRSetupEngineConcurrency();
76 
77 #endif // CRLOCKS_H
78