Skip to content

Commit 6709ec9

Browse files
Merge pull request #1 from Open-NET-Libraries/devnew
Merge 2.0 version from dev into master.
2 parents d9215da + 2ecfbcc commit 6709ec9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+5029
-1865
lines changed

GlobalSuppressions.cs

Lines changed: 0 additions & 9 deletions
This file was deleted.

Lock.cs

Lines changed: 0 additions & 45 deletions
This file was deleted.

LockBase.cs

Lines changed: 0 additions & 26 deletions
This file was deleted.

LockType.cs

Lines changed: 0 additions & 8 deletions
This file was deleted.

Open.Threading.ReadWrite.sln

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.1.32210.238
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Open.Threading.ReadWrite", "Source\Open.Threading.ReadWrite.csproj", "{48806258-D862-469E-BBA4-24AFDA225396}"
7+
EndProject
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Open.Threading.ReadWrite.Tests", "tests\Open.Threading.ReadWrite.Tests.csproj", "{D89362E8-D174-4A58-B724-F2D18C26323F}"
9+
EndProject
10+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Open.Threading.ReadWriteHelper", "ReadWriteHelper\Open.Threading.ReadWriteHelper.csproj", "{43E381F7-4AA9-497C-AB0B-13BFA7700876}"
11+
EndProject
12+
Global
13+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
14+
Debug|Any CPU = Debug|Any CPU
15+
Release|Any CPU = Release|Any CPU
16+
EndGlobalSection
17+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
18+
{48806258-D862-469E-BBA4-24AFDA225396}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
19+
{48806258-D862-469E-BBA4-24AFDA225396}.Debug|Any CPU.Build.0 = Debug|Any CPU
20+
{48806258-D862-469E-BBA4-24AFDA225396}.Release|Any CPU.ActiveCfg = Release|Any CPU
21+
{48806258-D862-469E-BBA4-24AFDA225396}.Release|Any CPU.Build.0 = Release|Any CPU
22+
{D89362E8-D174-4A58-B724-F2D18C26323F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
23+
{D89362E8-D174-4A58-B724-F2D18C26323F}.Debug|Any CPU.Build.0 = Debug|Any CPU
24+
{D89362E8-D174-4A58-B724-F2D18C26323F}.Release|Any CPU.ActiveCfg = Release|Any CPU
25+
{D89362E8-D174-4A58-B724-F2D18C26323F}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{43E381F7-4AA9-497C-AB0B-13BFA7700876}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{43E381F7-4AA9-497C-AB0B-13BFA7700876}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{43E381F7-4AA9-497C-AB0B-13BFA7700876}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{43E381F7-4AA9-497C-AB0B-13BFA7700876}.Release|Any CPU.Build.0 = Release|Any CPU
30+
EndGlobalSection
31+
GlobalSection(SolutionProperties) = preSolution
32+
HideSolutionNode = FALSE
33+
EndGlobalSection
34+
GlobalSection(ExtensibilityGlobals) = postSolution
35+
SolutionGuid = {7FC82DDF-CFB6-44E5-89D0-029040909BD3}
36+
EndGlobalSection
37+
EndGlobal

README.md

Lines changed: 158 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,162 @@
11
# Open.Threading.ReadWrite
22

3-
Useful set of extensions and classes for simplifying and optimizing read-write synchronization.
3+
Useful set of extensions and classes for simplifying and optimizing read-write synchronization with `ReaderWriterLockSlim`.
44

5+
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](https://www.nuget.org/packages/Open.Threading.ReadWrite/blob/master/LICENSE)
6+
![100% code coverage](https://img.shields.io/badge/coverage-100%25-green)
57
[![NuGet](https://img.shields.io/nuget/v/Open.Threading.ReadWrite.svg)](https://www.nuget.org/packages/Open.Threading.ReadWrite/)
8+
9+
## Purpose
10+
11+
There are very common but tedious patterns to follow when utilizing a `ReaderWriterLockSlim`. Some of the more complex but useful patterns involve reading (with or without a read lock) and then if a condition is met, acquiring a write lock before proceeding.
12+
13+
This extension library removes the tediousness of properly acquiring and releasing various locks and provides easy access to timeout values if needed.
14+
15+
## Basics
16+
17+
---
18+
19+
### Read
20+
21+
```cs
22+
rwLockSlim.Read(() =>
23+
{
24+
/* do work inside a read lock */
25+
});
26+
```
27+
28+
```cs
29+
using(rwLockSlim.ReadLock())
30+
{
31+
/* do work inside a read lock */
32+
}
33+
```
34+
35+
```cs
36+
int result = rwLockSlim.Read(() =>
37+
{
38+
int i;
39+
/* produce a result inside read lock */
40+
return i;
41+
});
42+
```
43+
44+
---
45+
46+
### Write
47+
48+
```cs
49+
rwLockSlim.Write(() =>
50+
{
51+
/* do work inside a read lock */
52+
});
53+
```
54+
55+
```cs
56+
using(rwLockSlim.WriteLock())
57+
{
58+
/* do work inside a write lock */
59+
}
60+
```
61+
62+
```cs
63+
int result = rwLockSlim.Write(() =>
64+
{
65+
int i;
66+
/* produce a result inside write lock */
67+
return i;
68+
});
69+
```
70+
71+
---
72+
73+
### Upgradable Read
74+
75+
```cs
76+
using(rwLockSlim.UpgradableReadLock())
77+
{
78+
/* do work inside an upgradable read lock */
79+
if(condition)
80+
{
81+
using(rwLockSlim.WriteLock())
82+
{
83+
/* upgraded to a write lock */
84+
}
85+
}
86+
}
87+
```
88+
89+
90+
## With Timeouts
91+
92+
These throw a `TimeoutException` if a lock cannot be acquired within the time specified.
93+
94+
---
95+
96+
### Read
97+
98+
```cs
99+
rwLockSlim.Read(1000 /* ms */, () =>
100+
{
101+
/* do work inside a read lock */
102+
});
103+
```
104+
105+
or
106+
107+
```cs
108+
using(rwLockSlim.ReadLock(1000)) // ms
109+
{
110+
/* do work inside a read lock */
111+
}
112+
```
113+
114+
---
115+
116+
### Write
117+
118+
```cs
119+
rwLockSlim.Write(1000 /* ms */, () =>
120+
{
121+
/* do work inside a write lock */
122+
});
123+
```
124+
125+
or
126+
127+
```cs
128+
using(rwLockSlim.WriteLock(1000)) // ms
129+
{
130+
/* do work inside a write lock */
131+
}
132+
```
133+
134+
## Advanced Examples
135+
136+
---
137+
138+
### WriteConditional
139+
140+
This example demonstrates how to properly query a value before writing with a 1 second timeout.
141+
142+
```cs
143+
var actionWasInvoked = rwLockSlim.WriteConditional(1000 /* ms */,
144+
() => /* condition that is queried inside an upgradable read lock */,
145+
() => /* do work inside a write lock */);
146+
```
147+
148+
### ReadWriteConditional
149+
150+
This more advanced example optimizes the process of reading and then writing by first testing the condition within a read lock before attempting with an upgradable read lock.
151+
152+
```cs
153+
int result = 0;
154+
bool actionWasInvoked = rwLockSlim.ReadWriteConditional(ref result,
155+
isUpgraded => /* condition that is first queried inside a read lock */,
156+
() =>
157+
{
158+
int i;
159+
/* do work inside a write lock */
160+
return i;
161+
});
162+
```

ReadLock.cs

Lines changed: 0 additions & 46 deletions
This file was deleted.

0 commit comments

Comments
 (0)