Skip to content

Commit 0f89a18

Browse files
committed
- replaced ThreadState with CancellationToken
1 parent aafd0fa commit 0f89a18

9 files changed

Lines changed: 38 additions & 62 deletions

File tree

Shuttle.Core.Threading.Tests/ThreadActivityFixture.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Threading;
23
using Moq;
34
using NUnit.Framework;
45

@@ -17,16 +18,13 @@ public void Should_be_able_to_have_the_thread_wait()
1718
});
1819

1920
var start = DateTime.Now;
21+
var token = new CancellationToken(false);
2022

21-
var mockState = new Mock<IThreadState>();
22-
23-
mockState.Setup(mock => mock.Active).Returns(true);
24-
25-
activity.Waiting(mockState.Object);
23+
activity.Waiting(token);
2624

2725
Assert.IsTrue((DateTime.Now - start).TotalMilliseconds >= 250);
2826

29-
activity.Waiting(mockState.Object);
27+
activity.Waiting(token);
3028

3129
Assert.IsTrue((DateTime.Now - start).TotalMilliseconds >= 750);
3230
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
using System.Threading;
2+
13
namespace Shuttle.Core.Threading
24
{
35
public interface IProcessor
46
{
5-
void Execute(IThreadState state);
7+
void Execute(CancellationToken cancellationToken);
68
}
79
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
using System.Threading;
2+
13
namespace Shuttle.Core.Threading
24
{
35
public interface IThreadActivity
46
{
5-
void Waiting(IThreadState state);
7+
void Waiting(CancellationToken cancellationToken);
68
void Working();
79
}
810
}

Shuttle.Core.Threading/IThreadState.cs

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

Shuttle.Core.Threading/ProcessorThread.cs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,17 @@
77

88
namespace Shuttle.Core.Threading
99
{
10-
public class ProcessorThread : IThreadState
10+
public class ProcessorThread
1111
{
1212
private static readonly int ThreadJoinTimeoutInterval =
1313
ConfigurationItem<int>.ReadSetting("ThreadJoinTimeoutInterval", 1000).GetValue();
1414

1515
private readonly ILog _log;
1616
private readonly string _name;
1717
private readonly IProcessor _processor;
18+
private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
1819

19-
private volatile bool _active;
20+
private bool _started;
2021

2122
private Thread _thread;
2223

@@ -27,14 +28,16 @@ public ProcessorThread(string name, IProcessor processor)
2728
_name = name;
2829
_processor = processor;
2930

31+
CancellationToken = _cancellationTokenSource.Token;
32+
3033
_log = Log.For(this);
3134
}
3235

33-
public bool Active => _active;
36+
public CancellationToken CancellationToken {get; }
3437

3538
public void Start()
3639
{
37-
if (_active)
40+
if (_started)
3841
{
3942
return;
4043
}
@@ -57,8 +60,6 @@ public void Start()
5760
_thread.IsBackground = true;
5861
_thread.Priority = ThreadPriority.Normal;
5962

60-
_active = true;
61-
6263
_thread.Start();
6364

6465
if (Log.IsTraceEnabled)
@@ -67,15 +68,17 @@ public void Start()
6768
_processor.GetType().FullName));
6869
}
6970

70-
while (!_thread.IsAlive && _active)
71+
while (!_thread.IsAlive && !CancellationToken.IsCancellationRequested)
7172
{
7273
}
7374

74-
if (_active && Log.IsTraceEnabled)
75+
if (!CancellationToken.IsCancellationRequested && Log.IsTraceEnabled)
7576
{
7677
_log.Trace(string.Format(Resources.ProcessorThreadActive, _thread.ManagedThreadId,
7778
_processor.GetType().FullName));
7879
}
80+
81+
_started = true;
7982
}
8083

8184
public void Stop()
@@ -86,7 +89,7 @@ public void Stop()
8689
_processor.GetType().FullName));
8790
}
8891

89-
_active = false;
92+
_cancellationTokenSource.Cancel();
9093

9194
_processor.AttemptDispose();
9295

@@ -98,15 +101,15 @@ public void Stop()
98101

99102
private void Work()
100103
{
101-
while (_active)
104+
while (!CancellationToken.IsCancellationRequested)
102105
{
103106
if (Log.IsVerboseEnabled)
104107
{
105108
_log.Verbose(string.Format(Resources.ProcessorExecuting, _thread.ManagedThreadId,
106109
_processor.GetType().FullName));
107110
}
108111

109-
_processor.Execute(this);
112+
_processor.Execute(CancellationToken);
110113
}
111114

112115
if (Log.IsTraceEnabled)
@@ -118,7 +121,7 @@ private void Work()
118121

119122
internal void Deactivate()
120123
{
121-
_active = false;
124+
_cancellationTokenSource.Cancel();
122125
}
123126
}
124127
}

Shuttle.Core.Threading/Properties/AssemblyInfo.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@
3333
[assembly: AssemblyTitle(".NET Standard 2.0")]
3434
#endif
3535

36-
[assembly: AssemblyVersion("10.1.0.0")]
37-
[assembly: AssemblyCopyright("Copyright © Eben Roux 2018")]
36+
[assembly: AssemblyVersion("11.0.0.0")]
37+
[assembly: AssemblyCopyright("Copyright © Eben Roux 2019")]
3838
[assembly: AssemblyProduct("Shuttle.Core.Threading")]
3939
[assembly: AssemblyCompany("Shuttle")]
4040
[assembly: AssemblyConfiguration("Release")]
41-
[assembly: AssemblyInformationalVersion("10.1.0")]
41+
[assembly: AssemblyInformationalVersion("11.0.0")]
4242
[assembly: ComVisible(false)]

Shuttle.Core.Threading/ThreadActivity.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Threading;
23
using Shuttle.Core.Contract;
34

45
namespace Shuttle.Core.Threading
@@ -26,11 +27,11 @@ public ThreadActivity(IThreadActivityConfiguration threadActivityConfiguration)
2627
_durationIndex = 0;
2728
}
2829

29-
public void Waiting(IThreadState state)
30+
public void Waiting(CancellationToken cancellationToken)
3031
{
3132
var ms = (int) GetSleepTimeSpan().TotalMilliseconds;
3233

33-
ThreadSleep.While(ms, state);
34+
ThreadSleep.While(ms, cancellationToken);
3435
}
3536

3637
public void Working()

Shuttle.Core.Threading/ThreadSleep.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
using System;
22
using System.Threading;
3+
using Shuttle.Core.Contract;
34

45
namespace Shuttle.Core.Threading
56
{
67
public static class ThreadSleep
78
{
89
public const int MaxStepSize = 1000;
910

10-
public static void While(int ms, IThreadState state)
11+
public static void While(int ms, CancellationToken cancellationToken)
1112
{
1213
// step size should be as large as possible,
1314
// max step size by default
14-
While(ms, state, MaxStepSize);
15+
While(ms, cancellationToken, MaxStepSize);
1516
}
1617

17-
public static void While(int ms, IThreadState state, int step)
18+
public static void While(int ms, CancellationToken cancellationToken, int step)
1819
{
20+
Guard.AgainstNull(cancellationToken, nameof(cancellationToken));
21+
1922
if (ms < 0)
2023
{
2124
return;
@@ -34,7 +37,7 @@ public static void While(int ms, IThreadState state, int step)
3437

3538
var remaining = (int) (end - DateTime.UtcNow).TotalMilliseconds;
3639

37-
while (state.Active && remaining > 0)
40+
while (!cancellationToken.IsCancellationRequested && remaining > 0)
3841
{
3942
var sleep = remaining < step ? remaining : step;
4043
Thread.Sleep(sleep);

Shuttle.Core.Threading/ThreadState.cs

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

0 commit comments

Comments
 (0)