1 /*
2 AngelCode Scripting Library
3 Copyright (c) 2003-2017 Andreas Jonsson
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any
7 damages arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any
10 purpose, including commercial applications, and to alter it and
11 redistribute it freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you
14 must not claim that you wrote the original software. If you use
15 this software in a product, an acknowledgment in the product
16 documentation would be appreciated but is not required.
17
18 2. Altered source versions must be plainly marked as such, and
19 must not be misrepresented as being the original software.
20
21 3. This notice may not be removed or altered from any source
22 distribution.
23
24 The original version of this library can be located at:
25 http://www.angelcode.com/angelscript/
26
27 Andreas Jonsson
28 andreas@angelcode.com
29 */
30
31
32
33 //
34 // as_criticalsection.h
35 //
36 // Classes for multi threading support
37 //
38
39 #ifndef AS_CRITICALSECTION_H
40 #define AS_CRITICALSECTION_H
41
42 #include "as_config.h"
43
44 BEGIN_AS_NAMESPACE
45
46 #ifdef AS_NO_THREADS
47
48 #define DECLARECRITICALSECTION(x)
49 #define ENTERCRITICALSECTION(x)
50 #define LEAVECRITICALSECTION(x)
51
tryEnter()52 inline bool tryEnter() { return true; }
53 #define TRYENTERCRITICALSECTION(x) tryEnter()
54
55 #define DECLAREREADWRITELOCK(x)
56 #define ACQUIREEXCLUSIVE(x)
57 #define RELEASEEXCLUSIVE(x)
58 #define ACQUIRESHARED(x)
59 #define RELEASESHARED(x)
60
61 #else
62
63 #define DECLARECRITICALSECTION(x) asCThreadCriticalSection x;
64 #define ENTERCRITICALSECTION(x) x.Enter()
65 #define LEAVECRITICALSECTION(x) x.Leave()
66 #define TRYENTERCRITICALSECTION(x) x.TryEnter()
67
68 #define DECLAREREADWRITELOCK(x) asCThreadReadWriteLock x;
69 #define ACQUIREEXCLUSIVE(x) x.AcquireExclusive()
70 #define RELEASEEXCLUSIVE(x) x.ReleaseExclusive()
71 #define ACQUIRESHARED(x) x.AcquireShared()
72 #define RELEASESHARED(x) x.ReleaseShared()
73
74 #ifdef AS_POSIX_THREADS
75
76 END_AS_NAMESPACE
77 #include <pthread.h>
78 BEGIN_AS_NAMESPACE
79
80 class asCThreadCriticalSection
81 {
82 public:
83 asCThreadCriticalSection();
84 ~asCThreadCriticalSection();
85
86 void Enter();
87 void Leave();
88 bool TryEnter();
89
90 protected:
91 pthread_mutex_t cs;
92 };
93
94 class asCThreadReadWriteLock
95 {
96 public:
97 asCThreadReadWriteLock();
98 ~asCThreadReadWriteLock();
99
100 void AcquireExclusive();
101 void ReleaseExclusive();
102 bool TryAcquireExclusive();
103
104 void AcquireShared();
105 void ReleaseShared();
106 bool TryAcquireShared();
107
108 protected:
109 pthread_rwlock_t lock;
110 };
111
112 #elif defined(AS_WINDOWS_THREADS)
113
114 END_AS_NAMESPACE
115 #ifdef AS_XBOX360
116 #include <xtl.h>
117 #else
118 #ifndef WIN32_LEAN_AND_MEAN
119 #define WIN32_LEAN_AND_MEAN
120 #endif
121 #ifndef _WIN32_WINNT
122 #define _WIN32_WINNT 0x0600 // We need this to get the declaration for Windows Phone compatible Ex functions
123 #endif
124 #include <windows.h>
125 #endif
126 BEGIN_AS_NAMESPACE
127
128 // Undefine macros that cause problems in our code
129 #undef GetObject
130 #undef RegisterClass
131
132 class asCThreadCriticalSection
133 {
134 public:
135 asCThreadCriticalSection();
136 ~asCThreadCriticalSection();
137
138 void Enter();
139 void Leave();
140 bool TryEnter();
141
142 protected:
143 CRITICAL_SECTION cs;
144 };
145
146 class asCThreadReadWriteLock
147 {
148 public:
149 asCThreadReadWriteLock();
150 ~asCThreadReadWriteLock();
151
152 void AcquireExclusive();
153 void ReleaseExclusive();
154
155 void AcquireShared();
156 void ReleaseShared();
157
158 protected:
159 // The Slim Read Write Lock object, SRWLOCK, is more efficient
160 // but it is only available from Windows Vista so we cannot use it and
161 // maintain compatibility with olders versions of Windows.
162
163 // Critical sections and semaphores are available on Windows XP and onwards.
164 // Windows XP is oldest version we support with multithreading.
165
166 // The implementation is based on the following article, that shows
167 // how to implement a fair read/write lock that doesn't risk starving
168 // the writers:
169
170 // http://doc.qt.nokia.com/qq/qq11-mutex.html
171
172 // TODO: Allow use of SRWLOCK through configuration in as_config.h
173
174 CRITICAL_SECTION writeLock;
175 HANDLE readLocks;
176 };
177
178 // This constant really should be a member of asCThreadReadWriteLock,
179 // but it gives a compiler error on MSVC6 so I'm leaving it outside
180 static const asUINT maxReaders = 10;
181
182 #endif
183
184 #endif
185
186 END_AS_NAMESPACE
187
188 #endif
189
190