Skip to content

Commit cb07c25

Browse files
author
Oren (electricessence)
committed
Reformat and contract improvments.
1 parent 80d63d8 commit cb07c25

8 files changed

Lines changed: 423 additions & 388 deletions

Lock.cs

Lines changed: 38 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,46 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Text;
42
using System.Threading;
53

64
namespace Open.Threading
75
{
8-
/// <summary>
9-
/// A simple lock class for allowing for a timeout. If no timeout is desired, then simply use the lock(){} statement.
10-
///
11-
/// Example:
12-
/// <code>
13-
/// using(new Lock(sync,1000)) // If the timeout expires an exception is thrown.
14-
/// {
15-
/// // do some synchronized work.
16-
/// }
17-
/// </code>
18-
/// ...or...
19-
/// <code>
20-
/// using(var syncLock = new Lock(sync,1000,false)) // If the timeout expires an exception is thrown.
21-
/// {
22-
/// if(!syncLock.LockHeld)
23-
/// {
24-
/// // Do some unsynchronized work.
25-
/// }
26-
/// {
27-
/// // do some synchronized work.
28-
/// }
29-
/// }
30-
/// </code>
31-
/// </summary>
32-
public class Lock : LockBase<object>
33-
{
34-
public Lock(object target, int millisecondsTimeout, bool throwIfTimeout = true)
35-
: base(target, Monitor.TryEnter(target, millisecondsTimeout))
36-
{
37-
if (!LockHeld && throwIfTimeout)
38-
throw new TimeoutException(String.Format(
39-
"Could not acquire a lock within the timeout specified. (millisecondsTimeout={0})", millisecondsTimeout));
6+
/// <summary>
7+
/// A simple lock class for allowing for a timeout. If no timeout is desired, then simply use the lock(){} statement.
8+
///
9+
/// Example:
10+
/// <code>
11+
/// using(new Lock(sync,1000)) // If the timeout expires an exception is thrown.
12+
/// {
13+
/// // do some synchronized work.
14+
/// }
15+
/// </code>
16+
/// ...or...
17+
/// <code>
18+
/// using(var syncLock = new Lock(sync,1000,false)) // If the timeout expires an exception is thrown.
19+
/// {
20+
/// if(!syncLock.LockHeld)
21+
/// {
22+
/// // Do some unsynchronized work.
23+
/// }
24+
/// {
25+
/// // do some synchronized work.
26+
/// }
27+
/// }
28+
/// </code>
29+
/// </summary>
30+
public class Lock : LockBase<object>
31+
{
32+
public Lock(object target, int millisecondsTimeout, bool throwIfTimeout = true)
33+
: base(target, Monitor.TryEnter(target, millisecondsTimeout))
34+
{
35+
if (!LockHeld && throwIfTimeout)
36+
throw new TimeoutException(String.Format(
37+
"Could not acquire a lock within the timeout specified. (millisecondsTimeout={0})", millisecondsTimeout));
4038

41-
}
39+
}
4240

43-
protected override void OnDispose(object target)
44-
{
45-
if (target != null) Monitor.Exit(target);
46-
}
47-
}
41+
protected override void OnDispose(object target)
42+
{
43+
if (target != null) Monitor.Exit(target);
44+
}
45+
}
4846
}

LockBase.cs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,24 @@
99

1010
namespace Open.Threading
1111
{
12-
public abstract class LockBase<TSync> : IDisposable
13-
where TSync : class
14-
{
15-
protected TSync _target;
16-
public readonly bool LockHeld;
12+
public abstract class LockBase<TSync> : IDisposable
13+
where TSync : class
14+
{
15+
protected TSync _target;
16+
public readonly bool LockHeld;
1717

18-
protected LockBase(TSync target, bool lockHeld)
19-
{
20-
LockHeld = lockHeld;
21-
if (lockHeld)
22-
_target = target;
23-
}
24-
protected abstract void OnDispose(TSync target);
18+
protected LockBase(TSync target, bool lockHeld)
19+
{
20+
LockHeld = lockHeld;
21+
if (lockHeld)
22+
_target = target;
23+
}
24+
protected abstract void OnDispose(TSync target);
2525

26-
public void Dispose()
27-
{
28-
OnDispose(Interlocked.Exchange(ref _target, null));
29-
}
30-
}
26+
public void Dispose()
27+
{
28+
OnDispose(Interlocked.Exchange(ref _target, null));
29+
}
30+
}
3131

3232
}

LockType.cs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Text;
4-
5-
namespace Open.Threading
1+
namespace Open.Threading
62
{
7-
public enum LockType : byte
8-
{
9-
Read,
10-
ReadUpgradeable,
11-
Write
12-
}
3+
public enum LockType : byte
4+
{
5+
Read,
6+
ReadUpgradeable,
7+
Write
8+
}
139
}

ReadLock.cs

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,48 @@
22

33
namespace Open.Threading
44
{
5-
/// <summary>
6-
/// A simple read-only locking class which also allows for a timeout.
7-
/// Extensions are available for use with a ReaderWriterLockSlim.
8-
///
9-
/// Example:
10-
/// <code>
11-
/// using(readWriteLockSlimInstance.ReadLock())
12-
/// {
13-
/// // do some synchronized work.
14-
/// }
15-
/// </code>
16-
/// ...or...
17-
/// <code>
18-
/// using(readWriteLockSlimInstance.ReadLock(1000)) // If the timeout expires an exception is thrown.
19-
/// {
20-
/// // do some synchronized work.
21-
/// }
22-
/// </code>
23-
/// ...or...
24-
/// <code>
25-
/// using(var syncLock = readWriteLockSlimInstance.ReadLock(1000,false)) // If the timeout expires an exception is thrown.
26-
/// {
27-
/// if(!syncLock.LockHeld)
28-
/// {
29-
/// // Do some unsynchronized work.
30-
/// }
31-
/// {
32-
/// // do some synchronized work.
33-
/// }
34-
/// }
35-
/// </code>
36-
/// </summary>
37-
public sealed class ReadLock : LockBase<ReaderWriterLockSlim>
38-
{
39-
public ReadLock(ReaderWriterLockSlim target, int? millisecondsTimeout = null, bool throwIfTimeout = true)
40-
: base(target, target.EnterReadLock(millisecondsTimeout, throwIfTimeout))
41-
{
42-
}
5+
/// <summary>
6+
/// A simple read-only locking class which also allows for a timeout.
7+
/// Extensions are available for use with a ReaderWriterLockSlim.
8+
///
9+
/// Example:
10+
/// <code>
11+
/// using(readWriteLockSlimInstance.ReadLock())
12+
/// {
13+
/// // do some synchronized work.
14+
/// }
15+
/// </code>
16+
/// ...or...
17+
/// <code>
18+
/// using(readWriteLockSlimInstance.ReadLock(1000)) // If the timeout expires an exception is thrown.
19+
/// {
20+
/// // do some synchronized work.
21+
/// }
22+
/// </code>
23+
/// ...or...
24+
/// <code>
25+
/// using(var syncLock = readWriteLockSlimInstance.ReadLock(1000,false)) // If the timeout expires an exception is thrown.
26+
/// {
27+
/// if(!syncLock.LockHeld)
28+
/// {
29+
/// // Do some unsynchronized work.
30+
/// }
31+
/// {
32+
/// // do some synchronized work.
33+
/// }
34+
/// }
35+
/// </code>
36+
/// </summary>
37+
public sealed class ReadLock : LockBase<ReaderWriterLockSlim>
38+
{
39+
public ReadLock(ReaderWriterLockSlim target, int? millisecondsTimeout = null, bool throwIfTimeout = true)
40+
: base(target, target.EnterReadLock(millisecondsTimeout, throwIfTimeout))
41+
{
42+
}
4343

44-
protected override void OnDispose(ReaderWriterLockSlim target)
45-
{
46-
target?.ExitReadLock();
47-
}
48-
}
44+
protected override void OnDispose(ReaderWriterLockSlim target)
45+
{
46+
target?.ExitReadLock();
47+
}
48+
}
4949
}

0 commit comments

Comments
 (0)