My favorites
▼
|
Sign in
csharptest-net
CSharpTest.Net's Code Library
Project Home
Downloads
Issues
Source
Repository:
default
wiki
Checkout
Browse
Changes
Clones
Source path:
hg
/
src
/
Library
/
Threading
/
WaitAndContinueWorker.cs
Branch:
default
Tag:
<none>
v1.10.1024.336
v1.10.1124.358
v1.10.420.164
v1.10.607.213
v1.10.913.269
v1.11.426.305
v1.11.924.348
v1.9.1004.144
4e0bd400d42a
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
#region Copyright 2010-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.IO;
using System.Threading;
namespace CSharpTest.Net.Threading
{
/// <summary>
/// Represents a single worker thread that processes IWaitAndContinue work items
/// </summary>
public class WaitAndContinueWorker : IDisposable
{
readonly Thread _worker;
readonly WorkerControl _control;
readonly WaitAndContinueList _work;
/// <summary> Raised when an uncaught exception is thrown while processing the work queue </summary>
public event ErrorEventHandler OnError;
/// <summary> Constructs a thread to process IWaitAndContinue work items </summary>
public WaitAndContinueWorker()
{
_control = new WorkerControl();
_work = new WaitAndContinueList();
_work.AddWork(_control);
_worker = new Thread(Run);
_worker.SetApartmentState(ApartmentState.MTA);
_worker.IsBackground = true;
_worker.Name = GetType().Name;
_worker.Start();
}
/// <summary> Returns true if the work queue is empty </summary>
public bool IsEmpty { get { return _control.HasQuit || _work.Count <= 1; } }
/// <summary> Adds a unit of work to the list </summary>
public void AddWork(IWaitAndContinue item)
{
if (_control.HasQuit) throw new ObjectDisposedException(GetType().FullName);
_work.AddWork(item);
_control.Modified();
}
/// <summary> Adds a unit of work to the list </summary>
public void AddWork(WaitAndContinueList list)
{
if (_control.HasQuit) throw new ObjectDisposedException(GetType().FullName);
_work.AddWork(list);
_control.Modified();
}
/// <summary>
/// Exits the worker thread and, if complete is true, waits for the remaining
/// tasks to complete
/// </summary>
public bool Complete(bool complete, int timeout)
{
if (!_control.HasQuit)
{
_control.Quit();
if (!_worker.Join(timeout <= 0 ? timeout : Math.Max(1000, timeout)))
{
_worker.Abort();
_worker.Join();
complete = false;
}
}
_control.Dispose();
try
{
while (complete && !_work.IsEmpty && _work.PerformWork(timeout))
{ }
}
finally
{
_work.Dispose();
}
return complete;
}
/// <summary>
/// Terminates all work by aborting the worker thread even if work is in progress
/// </summary>
public void Abort()
{
if (_worker.IsAlive)
{
_control.Quit();
if (!_worker.Join(100))
_worker.Abort();
_worker.Join();
}
_control.Dispose();
_work.Dispose();
}
/// <summary> Disposes of the worker thread and all pending work </summary>
public void Dispose()
{
Complete(false, 0);
}
void Run()
{
while (!_control.HasQuit)
{
try
{
IWaitAndContinue ignore;
_work.PerformWork(Timeout.Infinite, out ignore);
}
catch (ThreadAbortException) { return; }
catch (Exception ex)
{
ErrorEventHandler h = OnError;
if (h != null)
h(this, new ErrorEventArgs(ex));
}
}
}
class WorkerControl : IWaitAndContinue
{
readonly ManualResetEvent _quit = new ManualResetEvent(false);
readonly AutoResetEvent _modified = new AutoResetEvent(false);
byte _hasQuit;
bool _disposed;
public void Dispose()
{
_hasQuit = 1;
_disposed = true;
_quit.Close();
_modified.Close();
}
public bool HasQuit { get { return _hasQuit == 1; } }
public void Quit()
{
Thread.VolatileWrite(ref _hasQuit, 1);
try { if (!_disposed) _quit.Set(); }
catch (ObjectDisposedException)
{ return; }
}
public void Modified()
{
try { if(!_disposed) _modified.Set(); }
catch (ObjectDisposedException)
{ return; }
}
bool IWaitAndContinue.Completed { get { return _disposed; } }
int IWaitAndContinue.HandleCount { get { return 2; } }
void IWaitAndContinue.CopyHandles(WaitHandle[] array, int offset)
{
array[offset] = _quit;
array[offset + 1] = _modified;
}
void IWaitAndContinue.ContinueProcessing(WaitHandle handleSignaled)
{ }
}
}
}
Show details
Hide details
Change log
59104bd26e63
by rogerk on Apr 26, 2011
Diff
Release version 1.11.426.305
Go to:
/.hgignore
...sTree.Test/BPlusTree.Test.csproj
...ree/BPlusTree.Test/BasicTests.cs
...ree.Test/SampleCustomTypeTest.cs
...ee.Test/SampleTypes/DataValue.cs
...mpleTypes/DataValueSerializer.cs
...Tree.Test/SampleTypes/KeyInfo.cs
...t/SampleTypes/KeyInfoComparer.cs
...SampleTypes/KeyInfoSerializer.cs
.../BPlusTree.Test/SequenceTests.cs
...ree.Test/TestBPlusTreeOptions.cs
...BPlusTree.Test/TestCollection.cs
...usTree.Test/TestCustomStorage.cs
...BPlusTree.Test/TestDictionary.cs
...sTree.Test/TestFileSerialized.cs
...ree.Test/TestMemorySerialized.cs
...ree.Test/TestSimpleDictionary.cs
...usTree.Test/ThreadedBTreeTest.cs
/src/BPlusTree/BPlusTree.csproj
.../Collections/BPlusTransaction.cs
...tions/BPlusTransactionFactory.cs
...e/Collections/BPlusTree.Debug.cs
...Collections/BPlusTree.Options.cs
...ollections/BPlusTree.Recovery.cs
...lusTree/Collections/BPlusTree.cs
...BPlusTree/Collections/Element.cs
...usTree/Collections/Enumerator.cs
...usTree/Collections/Interfaces.cs
...sTree/Collections/Node.Delete.cs
...sTree/Collections/Node.Insert.cs
...sTree/Collections/Node.Search.cs
...ee/Collections/Node.Serialize.cs
/src/BPlusTree/Collections/Node.cs
...ee/Collections/NodeCache.Base.cs
...ee/Collections/NodeCache.Full.cs
...ee/Collections/NodeCache.None.cs
.../Collections/NodeCache.Normal.cs
...usTree/Collections/NodeHandle.cs
...BPlusTree/Collections/NodePin.cs
...e/Collections/NodeTransaction.cs
/src/BPlusTree/Exceptions.cs
...sTree/Properties/AssemblyInfo.cs
.../BPlusTree/Resources.Designer.cs
/src/BPlusTree/Resources.resx
...PlusTree/Storage/Storage.Disk.cs
...usTree/Storage/Storage.Memory.cs
/src/CSBuild.exe
/src/CSBuild.xsd
/src/CmdTool.config
...g/UserSettingsSection.Upgrade.cs
Project members,
sign in
to write a code review
Older revisions
All revisions of this file
File info
Size: 6079 bytes, 182 lines
View raw file
Powered by
Google Project Hosting