Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
StackExchange.Redis
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
tsai
StackExchange.Redis
Commits
30d8a7c5
Commit
30d8a7c5
authored
Mar 25, 2014
by
Marc Gravell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
More thread-race avoidance
parent
df6523ef
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
84 additions
and
1 deletion
+84
-1
PreserveOrder.cs
StackExchange.Redis.Tests/PreserveOrder.cs
+73
-0
StackExchange.Redis.Tests.csproj
StackExchange.Redis.Tests/StackExchange.Redis.Tests.csproj
+1
-0
CompletionManager.cs
StackExchange.Redis/StackExchange/Redis/CompletionManager.cs
+10
-1
No files found.
StackExchange.Redis.Tests/PreserveOrder.cs
0 → 100644
View file @
30d8a7c5
using
System
;
using
System.Collections.Generic
;
using
System.Diagnostics
;
using
System.Threading
;
using
NUnit.Framework
;
namespace
StackExchange.Redis.Tests
{
[
TestFixture
]
public
class
PreserveOrder
:
TestBase
{
[
Test
]
[
TestCase
(
true
)]
[
TestCase
(
false
)]
public
void
Execute
(
bool
preserveAsyncOrder
)
{
using
(
var
conn
=
Create
())
{
var
sub
=
conn
.
GetSubscriber
();
var
received
=
new
List
<
int
>();
Console
.
WriteLine
(
"Subscribing..."
);
const
int
COUNT
=
1000
;
sub
.
Subscribe
(
"foo"
,
(
channel
,
message
)
=>
{
lock
(
received
)
{
received
.
Add
((
int
)
message
);
if
(
received
.
Count
==
COUNT
)
Monitor
.
PulseAll
(
received
);
// wake the test rig
}
Thread
.
Sleep
(
1
);
// you kinda need to be slow, otherwise
// the pool will end up doing everything on one thread
});
conn
.
PreserveAsyncOrder
=
preserveAsyncOrder
;
Console
.
WriteLine
();
Console
.
WriteLine
(
"Sending ({0})..."
,
(
preserveAsyncOrder
?
"preserved order"
:
"any order"
));
lock
(
received
)
{
received
.
Clear
();
// we'll also use received as a wait-detection mechanism; sneaky
// note: this does not do any cheating;
// it all goes to the server and back
for
(
int
i
=
0
;
i
<
COUNT
;
i
++)
{
sub
.
Publish
(
"foo"
,
i
);
}
Console
.
WriteLine
(
"Allowing time for delivery etc..."
);
var
watch
=
Stopwatch
.
StartNew
();
if
(!
Monitor
.
Wait
(
received
,
10000
))
{
Console
.
WriteLine
(
"Timed out; expect less data"
);
}
watch
.
Stop
();
Console
.
WriteLine
(
"Checking..."
);
lock
(
received
)
{
Console
.
WriteLine
(
"Received: {0} in {1}ms"
,
received
.
Count
,
watch
.
ElapsedMilliseconds
);
int
wrongOrder
=
0
;
for
(
int
i
=
0
;
i
<
Math
.
Min
(
COUNT
,
received
.
Count
);
i
++)
{
if
(
received
[
i
]
!=
i
)
wrongOrder
++;
}
Console
.
WriteLine
(
"Out of order: "
+
wrongOrder
);
if
(
preserveAsyncOrder
)
Assert
.
AreEqual
(
0
,
wrongOrder
);
else
Assert
.
AreNotEqual
(
0
,
wrongOrder
);
}
}
}
}
}
}
StackExchange.Redis.Tests/StackExchange.Redis.Tests.csproj
View file @
30d8a7c5
...
...
@@ -77,6 +77,7 @@
<Compile
Include=
"Locking.cs"
/>
<Compile
Include=
"MultiMaster.cs"
/>
<Compile
Include=
"Naming.cs"
/>
<Compile
Include=
"PreserveOrder.cs"
/>
<Compile
Include=
"Properties\AssemblyInfo.cs"
/>
<Compile
Include=
"PubSub.cs"
/>
<Compile
Include=
"RealWorld.cs"
/>
...
...
StackExchange.Redis/StackExchange/Redis/CompletionManager.cs
View file @
30d8a7c5
...
...
@@ -117,7 +117,16 @@ private void ProcessAsyncCompletionQueueImpl()
int
currentThread
=
Environment
.
CurrentManagedThreadId
;
try
{
if
(
Interlocked
.
CompareExchange
(
ref
activeAsyncWorkerThread
,
currentThread
,
0
)
!=
0
)
return
;
while
(
Interlocked
.
CompareExchange
(
ref
activeAsyncWorkerThread
,
currentThread
,
0
)
!=
0
)
{
// if we don't win the lock, check whether there is still work; if there is we
// need to retry to prevent a nasty race condition
lock
(
asyncCompletionQueue
)
{
if
(
asyncCompletionQueue
.
Count
==
0
)
return
;
// another thread drained it; can exit
}
Thread
.
Sleep
(
1
);
}
int
total
=
0
;
do
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment