My favorites | Sign in
Project Home Downloads Issues Source
Repository:
Checkout   Browse   Changes   Clones    
 
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
#region Copyright 2011 by Roger Knapp, Licensed under the Apache License, Version 2.0
/* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#endregion
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;

namespace CSharpTest.Net.Synchronization
{
/// <summary>
/// Creates a tracking/assertion wrapper around an implementation of an ILockStrategy to verify lock state before
/// and after acquisition and release of both reader and writer locks.
/// </summary>
public class DebugLocking<T> : DebugLocking
where T : ILockStrategy, new()
{
/// <summary> Constructs the lock tracking object </summary>
public DebugLocking() : base(new T())
{ }
/// <summary> Constructs the lock tracking object </summary>
public DebugLocking(bool captureStack, int limitTimeout, int limitNestedReaders, bool concurrentReads, int limitNestedWriters)
: base(new T(), captureStack, limitTimeout, limitNestedReaders, concurrentReads, limitNestedWriters)
{ }
}

/// <summary>
/// Creates a tracking/assertion wrapper around an implementation of an ILockStrategy to verify lock state before
/// and after acquisition and release of both reader and writer locks.
/// </summary>
public class DebugLocking : ILockStrategy
{
bool _disposed;

readonly bool _captureStack;
readonly int _limitTimeout;
readonly int _limitNestedReaders;
readonly bool _concurrentReads;
readonly int _limitNestedWriters;

readonly ILockStrategy _lock;

DebugLockTracker _writer;
readonly Dictionary<Thread, DebugLockTracker> _readers;

/// <summary> Constructs the lock tracking object </summary>
public DebugLocking(ILockStrategy lck)
: this(lck, false, 30000, 0, false, 0)
{ }
/// <summary> Constructs the lock tracking object </summary>
public DebugLocking(ILockStrategy lck, bool captureStack, int limitTimeout, int limitNestedReaders, bool concurrentReads, int limitNestedWriters)
{
_captureStack = captureStack;
_limitTimeout = limitTimeout < 0 ? int.MaxValue : limitTimeout;
_limitNestedReaders = limitNestedReaders;
_concurrentReads = concurrentReads;
_limitNestedWriters = limitNestedWriters;
_lock = Check.NotNull(lck);
_readers = new Dictionary<Thread, DebugLockTracker>();
}

/// <summary> Capture the stack on every lock aquisition and release </summary>
public bool CaptureStack { get { return _captureStack; } }

/// <summary> Returns the highest number of concurrent reads </summary>
public int MaxReaderCount;

/// <summary> Returns the highest number of concurrent writes (aka max recursive count) </summary>
public int MaxWriterCount;

/// <summary> Returns the total number of current readers for all threads </summary>
public int CurrentReaderCount;

/// <summary> Returns the total number of current writers for all threads </summary>
public int CurrentWriterCount;

/// <summary> Returns the total number of read locks acquired </summary>
public int TotalReaderCount;

/// <summary> Returns the total number of write locks acquired </summary>
public int TotalWriterCount;

/// <summary> Returns the total number of current readers for this thread </summary>
public int LocalReaderCount
{
get
{
DebugLockTracker reader;
lock (_readers)
return _readers.TryGetValue(Thread.CurrentThread, out reader) ? reader.Count : 0;
}
}

/// <summary> Returns the total number of current writers for this thread </summary>
public int LocalWriterCount
{
get
{
DebugLockTracker writer = _writer;
return writer != null && ReferenceEquals(writer.Owner, Thread.CurrentThread) ? writer.Count : 0;
}
}

/// <summary> Changes every time a write lock is aquired. If WriteVersion == 0, no write locks have been issued. </summary>
public int WriteVersion { get { return _lock.WriteVersion; } }

/// <summary> Disposes of this lock </summary>
public void Dispose()
{
if (!_disposed)
{
_disposed = true;
_lock.Dispose();

DebugAssertionFailedException.Assert(CurrentReaderCount == 0, "Lock disposed with active readers.");
DebugAssertionFailedException.Assert(CurrentWriterCount == 0, "Lock disposed with active writers.");
}
}

private void AssertValid()
{
if (_disposed)
throw new ObjectDisposedException(String.Format("{0}({1})", GetType(), _lock.GetType()));
}

private int MaxTimeout(int timeout) { return Math.Min(_limitTimeout, timeout < 0 ? int.MaxValue : timeout); }

private static void AddCount(ref int maxValue, ref int currentValue, ref int totalValue)
{
Interlocked.Increment(ref totalValue);
int newMax = Interlocked.Increment(ref currentValue);
int oldMax;
while (newMax > (oldMax = maxValue))
Interlocked.CompareExchange(ref maxValue, newMax, oldMax);
}

/// <summary>
/// Returns true if the lock was successfully obtained within the timeout specified
/// </summary>
public bool TryRead(int timeout)
{
AssertValid();
Thread thread = Thread.CurrentThread;
int myWriteCount = 0;
DebugLockTracker reader, writer = _writer;

lock (_readers)
if (!_readers.TryGetValue(thread, out reader))
_readers.Add(thread, reader = new DebugLockTracker(thread));
int myReadCount = reader.Count;

if (writer != null && ReferenceEquals(writer.Owner, thread))
myWriteCount = writer.Count;

DebugAssertionFailedException.Assert((myReadCount + myWriteCount) <= _limitNestedReaders, "Current thread already holds max read locks.");

if (!_lock.TryRead(MaxTimeout(timeout)))
{
DebugAssertionFailedException.Assert(timeout < _limitTimeout, "Possible dead-lock in read lock, timeout limit reached.");
return false;
}

writer = _writer;
DebugAssertionFailedException.Assert(_concurrentReads || (writer == null || myWriteCount > 0), "Read lock acquired while writer lock exists.");

reader.AddLock(CaptureStack);
AddCount(ref MaxReaderCount, ref CurrentReaderCount, ref TotalReaderCount);
return true;
}

/// <summary>
/// Releases a read lock
/// </summary>
public void ReleaseRead()
{
AssertValid();
Thread thread = Thread.CurrentThread;
DebugLockTracker reader;

lock (_readers)
DebugAssertionFailedException.Assert(_readers.TryGetValue(thread, out reader) && reader.Count > 0, "Unable to release an unacquired read lock.");

DebugLockTracker writer = _writer;
DebugAssertionFailedException.Assert(_concurrentReads || writer == null || ReferenceEquals(writer.Owner, thread), "Read lock release while writer lock exists.");

_lock.ReleaseRead();
reader.ReleaseLock(CaptureStack);
Interlocked.Decrement(ref CurrentReaderCount);
}

/// <summary>
/// Returns true if the lock was successfully obtained within the timeout specified
/// </summary>
public bool TryWrite(int timeout)
{
AssertValid();
Thread thread = Thread.CurrentThread;
int myWriteCount = 0, myReadCount = 0;
DebugLockTracker reader, writer = _writer;

lock (_readers)
if (_readers.TryGetValue(thread, out reader))
myReadCount = reader.Count;

if (writer != null && ReferenceEquals(writer.Owner, thread))
myWriteCount = writer.Count;

DebugAssertionFailedException.Assert(myWriteCount > 0 || myReadCount == 0, "Potential dead-lock in acquire writer while reading.");
DebugAssertionFailedException.Assert(myWriteCount <= _limitNestedWriters, "Current thread already holds max write locks.");

if (!_lock.TryWrite(MaxTimeout(timeout)))
{
DebugAssertionFailedException.Assert(timeout < _limitTimeout, "Possible dead-lock in write lock, timeout limit reached.");
return false;
}

DebugAssertionFailedException.Assert(myWriteCount > 0 || _writer == null, "Write lock acquired while write lock exists.");
DebugAssertionFailedException.Assert(_concurrentReads || myReadCount == CurrentReaderCount, "Write lock acquired while reader lock exists.");

_writer = writer = _writer ?? new DebugLockTracker(thread);

writer.AddLock(CaptureStack);
AddCount(ref MaxWriterCount, ref CurrentWriterCount, ref TotalWriterCount);
return true;
}

/// <summary>
/// Releases a writer lock
/// </summary>
public void ReleaseWrite()
{
AssertValid();
Thread thread = Thread.CurrentThread;

DebugLockTracker writer = _writer;
DebugAssertionFailedException.Assert(writer != null && ReferenceEquals(writer.Owner, thread) && writer.Count > 0, "Unable to release an unacquired write lock.");

if (writer != null)
{
writer.ReleaseLock(CaptureStack);
if(writer.Count == 0)
_writer = null;
}
_lock.ReleaseWrite();
Interlocked.Decrement(ref CurrentWriterCount);
}

class DebugLockTracker
{
const int MaxDepth = 64;

public int Count;
public readonly Thread Owner;
readonly StackTrace[] _acquiredFrom;
readonly StackTrace[] _releasedFrom;

public DebugLockTracker(Thread owner)
{
Owner = owner;
Count = 0;
_acquiredFrom = new StackTrace[MaxDepth];
_releasedFrom = new StackTrace[MaxDepth];
}

public void AddLock(bool captureStack)
{
_acquiredFrom[Count] = captureStack ? new StackTrace(2, false) : null;
_releasedFrom[Count] = null;
Count++;
}

public void ReleaseLock(bool captureStack)
{
Count--;
_releasedFrom[Count] = captureStack ? new StackTrace(2, false) : null;
}
}

#region ILockStrategy Members

/// <summary>
/// Returns a reader lock that can be elevated to a write lock
/// </summary>
/// <exception cref="System.TimeoutException"/>
public ReadLock Read(int timeout) { return ReadLock.Acquire(this, timeout); }

/// <summary>
/// Returns a reader lock that can be elevated to a write lock
/// </summary>
public ReadLock Read() { return ReadLock.Acquire(this, -1); }

/// <summary>
/// Returns a read and write lock
/// </summary>
/// <exception cref="System.TimeoutException"/>
public WriteLock Write(int timeout) { return WriteLock.Acquire(this, timeout); }

/// <summary>
/// Returns a read and write lock
/// </summary>
public WriteLock Write() { return WriteLock.Acquire(this, -1); }

#endregion
}
}

Change log

59104bd26e63 by rogerk on Apr 26, 2011   Diff
Release version 1.11.426.305
Go to: 
Project members, sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 12865 bytes, 316 lines
Powered by Google Project Hosting