aboutsummaryrefslogtreecommitdiff
path: root/Src/nu/AutoLock.h
blob: 92c9524bf7046444e0e0421fabd84477aaf97973 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#pragma warning (disable:4786)
#ifndef AUTOLOCKH
#define AUTOLOCKH

#ifdef _WIN32
#include <windows.h>
#else
#include <CoreServices/CoreServices.h>
#endif

/*
NULLSOFT_LOCK_OUTPUT_STATUS turns on/off debugging output
this can be VERY useful if you are trying to find a deadlock
each time the guard is locked or unlocked, it outputs a list of
any threads using the mutex, and their function stack
*/ 
//#define NULLSOFT_LOCK_OUTPUT_STATS

#ifdef NULLSOFT_LOCK_OUTPUT_STATS

#include <string> // we save each function name as a string
#include <deque> // we make a list of the recursive function stack for each thread
#include <map> // and map
#include <iostream> // we output to std::cerr
#include <windows.h>


/*****
Description:
	This class uses scoping to wrap a critical section (lightweight in-process mutex)
	The constructor enters the mutex and the destructor leaves it.  This allows it to
	take advantage of automatic scoping in C++, because C++ automatically calls the destructor
	when an object leaves scope. 
 
	This is _especially_ useful when you have multiple return paths, since you don't have to
	repeat mutex-leaving code.
 
To use:
	Make a LockGuard for a resource you want to protect.  The guard is shared, so make it part 
	of your class, or a global, or whatever.  The LockGuard is essentially a "token", equivalent
	to your mutex handle or critical section handle.
	
	Make an AutoLock object on the stack to lock.  It will unlock automatically when the object
	leaves scope.  
	
		Note: You'll want to make an object on the stack - don't use a heap object (new/delete)
		unless you have weird requirements and know what you are doing.
 
	Example:
 
	class MyClass
	{
		LockGuard fileGuard;
		fstream file;
		void DumpSomeData() // 
		{
			AutoLock lock(fileGuard);
			file << GetData();
		}
 
		void CALLBACK NewData() // potentially called by another thread
		{
			AutoLock lock(fileGuard)
			file << newData;
		}
	};
 
 
	Tip: You can use "false scoping" to tweak the mutex lifetime, for example:
 
	void DoStuff()
	{
		a = GetData();
		{ // false scope
			AutoLock lock(dataGuard);
			DoCalculationsWith(a);
		} // mutex will release here
		SetData(a);
	}
	
	Tip: A common mistake is making a temporary object.
	i.e.
	CORRECT:  AutoLock lock(fileGuard); // an AutoLock object called "lock" is put on the stack
	INCORRECT: AutoLock(fileGuard); // An unnamed temporary is created which will be destroyed IMMEDIATELY
 
*******/
#define MANUALLOCKNAME(x) x
#define LOCKNAME(x) ,x
#define GUARDNAME(x) (x)
namespace Nullsoft
{
	namespace Utility
	{
		/* the token which represents a resource to be locked */
		class LockGuard
		{
		public:
			inline LockGuard(char *name = "Unnamed Guard") : lockName(name), owner(0)
			{
				InitializeCriticalSection(&cerr_cs);
				InitializeCriticalSection(&map_cs);
				InitializeCriticalSection(&m_cs);
			}

			inline LockGuard(DWORD spin_count,char *name = "Unnamed Guard") : lockName(name), owner(0)
			{
				InitializeCriticalSection(&cerr_cs);
				InitializeCriticalSection(&map_cs);
				InitializeCriticalSection(&m_cs);
			}

			inline ~LockGuard()
			{
				DeleteCriticalSection(&cerr_cs);
				DeleteCriticalSection(&map_cs);
				DeleteCriticalSection(&m_cs);
			}

			inline void Lock()
			{
				EnterCriticalSection(&m_cs);
			}

			inline void Unlock()
			{
				LeaveCriticalSection(&m_cs);
			}

			int ThreadCount()
			{
				EnterCriticalSection(&map_cs);
				int count = 0;
				for (ThreadMap::iterator itr = threads.begin(); itr != threads.end(); itr++)
				{
					if (!itr->second.empty())
						count++;
				}

				LeaveCriticalSection(&map_cs);
				return count;
			}

			void Display()
			{
				EnterCriticalSection(&map_cs);
				EnterCriticalSection(&cerr_cs);

				if (ThreadCount() > 1 && owner)
				{
					wchar_t disp[256];
					wsprintfW(disp, L"Guard: %S\r\n", lockName.c_str());
					OutputDebugStringW(disp);
					for (ThreadMap::iterator itr = threads.begin(); itr != threads.end(); itr++)
					{
						if (itr->second.empty())
							continue;


						wsprintfW(disp, L"  Thread ID: %x", itr->first);
						if (owner == itr->first)
							wcscat(disp, L" [holding the mutex] *****\r\n");
						else
							wcscat(disp, L" [blocked]\r\n");
						OutputDebugStringW(disp);
						for (FunctionStack::iterator fitr = itr->second.begin(); fitr != itr->second.end(); fitr++)
						{
							wsprintfW(disp, L"    %S();\r\n", fitr->c_str());
							OutputDebugStringW(disp);
						}
					}
				}
				LeaveCriticalSection(&cerr_cs);
				LeaveCriticalSection(&map_cs);
			}

			void In(DWORD thread, char *functionName)
			{
				EnterCriticalSection(&map_cs);
				threads[thread].push_back(functionName);
				LeaveCriticalSection(&map_cs);
			}

			void Out(DWORD thread)
			{
				EnterCriticalSection(&map_cs);
				threads[thread].pop_back();
				LeaveCriticalSection(&map_cs);
			}
			std::string lockName;
			CRITICAL_SECTION cerr_cs, map_cs;
			typedef std::deque<std::string> FunctionStack; // this typedef reduce ugly c++ <>::<>::<> overkill
			typedef std::map<DWORD, FunctionStack> ThreadMap;
			ThreadMap threads;

			DWORD owner;
		private:
			CRITICAL_SECTION m_cs;

		};

		/* an AutoLock locks a resource (represented by a LockGuard) for the duration of its lifetime */
		class AutoLock
		{
		public:
			/*
			@param functionName The function name which wants the mutex
				we pass it in as a char * even though it'll be converted to a std::string
				to reduce overhead when OUTPUT_STATS is off
			 */
			inline AutoLock(LockGuard &_guard, char *functionName = "function name not passed") : guard(&_guard)
			{
				ManualLock(functionName);
			}
			inline void ManualLock(char *functionName = "manual lock")
			{
				thisThread = GetCurrentThreadId();
				guard->In(thisThread, functionName);
				guard->Display();
				guard->Lock();
				guard->owner = thisThread;
				guard->Display();
			}

			inline void ManualUnlock()
			{
				guard->Display();
				guard->Unlock();

				InterlockedCompareExchange((LONG volatile *)&guard->owner, 0, (LONG)thisThread);
				/* above line is functionally equivalent to:
				if (guard->owner == thisThread) 
					guard->owner=0;
				*/
				guard->Out(thisThread);
				guard->Display();
			}

			inline ~AutoLock()
			{
				ManualUnlock();
			}

			LockGuard *guard;
			DWORD thisThread;
		};

	}
}


#else
#define MANUALLOCKNAME(x)
#define LOCKNAME(x)
#define GUARDNAME(x)
namespace Nullsoft
{
	namespace Utility
	{
		/* the token which represents a resource to be locked */
		class LockGuard
		{
		public:
			inline LockGuard(char *guardName = "")
			{
      #ifdef _WIN32
					InitializeCriticalSection(&m_cs);
          #else
           MPCreateCriticalRegion(&cr);
          #endif
			}
#if _WIN32_WINNT >= 0x403
			inline LockGuard(DWORD spin_count, char *guardName = "")
			{
				if (spin_count)
					InitializeCriticalSectionAndSpinCount(&m_cs, spin_count);
				else
					InitializeCriticalSection(&m_cs);
			}
#endif
			inline ~LockGuard()
			{
      #ifdef _WIN32
				DeleteCriticalSection(&m_cs);
			#else
      MPDeleteCriticalRegion(cr);
      #endif
      }
			inline void Lock()
			{
      #ifdef _WIN32
				EnterCriticalSection(&m_cs);
			#else
        MPEnterCriticalRegion(cr, kDurationForever);
      #endif
      }

			inline void Unlock()
			{
      #ifdef _WIN32
				LeaveCriticalSection(&m_cs);
			#else
        MPExitCriticalRegion(cr);
      #endif
      }
		private:
    #ifdef _WIN32
			CRITICAL_SECTION m_cs;
		#else
			MPCriticalRegionID cr;
    #endif
			LockGuard(const LockGuard &copy) { } // make copy constructor private so it can't be used
			LockGuard &operator =(const LockGuard &copy) {} // same with operator=
    };

		/* an AutoLock locks a resource (represented by a LockGuard) for the duration of its lifetime */
		class AutoLock
		{
		public:
			inline AutoLock(LockGuard &_guard) : guard(&_guard)
			{
				guard->Lock();
			}
			
			inline AutoLock(LockGuard *_guard) : guard(_guard)
			{
				guard->Lock();
			}
			inline void ManualLock()
			{
				guard->Lock();
			}

			inline void ManualUnlock()
			{
				guard->Unlock();

			}
			inline ~AutoLock()
			{
				guard->Unlock();
			}
			LockGuard *guard;
		};

		// will lock anything that implements Lock() and Unlock()
		template <class LockGuard_t>
		class AutoLockT
		{
		public:
			inline AutoLockT(LockGuard_t &_guard) : guard(&_guard)
			{
				guard->Lock();
			}
			
			inline AutoLockT(LockGuard_t *_guard) : guard(_guard)
			{
				guard->Lock();
			}
			inline void ManualLock()
			{
				guard->Lock();
			}

			inline void ManualUnlock()
			{
				guard->Unlock();

			}
			inline ~AutoLockT()
			{
				guard->Unlock();
			}
			LockGuard_t *guard;
		};
	}
}
#endif

#endif