My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
using System;
using System.Threading;
using Retlang.Core;

namespace Retlang.Fibers
{
/// <summary>
/// Fiber implementation backed by a dedicated thread.
/// <see cref="IFiber"/>
/// </summary>
public class ThreadFiber : IFiber
{
private static int THREAD_COUNT;
private readonly Subscriptions _subscriptions = new Subscriptions();

private readonly Thread _thread;
private readonly IQueue _queue;
private readonly Scheduler _scheduler;

/// <summary>
/// Create a thread fiber with the default queue.
/// </summary>
public ThreadFiber()
: this(new DefaultQueue())
{}

/// <summary>
/// Creates a thread fiber with a specified queue.
/// </summary>
/// <param name="queue"></param>
public ThreadFiber(IQueue queue)
: this(queue, "ThreadFiber-" + GetNextThreadId())
{}

/// <summary>
/// Creates a thread fiber with a specified name.
/// </summary>
/// /// <param name="threadName"></param>
public ThreadFiber(string threadName)
: this(new DefaultQueue(), threadName)
{}


/// <summary>
/// Creates a thread fiber.
/// </summary>
/// <param name="queue"></param>
/// <param name="threadName"></param>
/// <param name="isBackground"></param>
/// <param name="priority"></param>
public ThreadFiber(IQueue queue, string threadName, bool isBackground = true, ThreadPriority priority = ThreadPriority.Normal)
{
_queue = queue;
_thread = new Thread(RunThread);
_thread.Name = threadName;
_thread.IsBackground = isBackground;
_thread.Priority = priority;
_scheduler = new Scheduler(this);
}

/// <summary>
/// <see cref="IFiber"/>
/// </summary>
public Thread Thread
{
get { return _thread; }
}

private static int GetNextThreadId()
{
return Interlocked.Increment(ref THREAD_COUNT);
}

private void RunThread()
{
_queue.Run();
}

/// <summary>
/// Enqueue a single action.
/// </summary>
/// <param name="action"></param>
public void Enqueue(Action action)
{
_queue.Enqueue(action);
}

///<summary>
/// Register subscription to be unsubcribed from when the fiber is disposed.
///</summary>
///<param name="toAdd"></param>
public void RegisterSubscription(IDisposable toAdd)
{
_subscriptions.Add(toAdd);
}

///<summary>
/// Deregister a subscription.
///</summary>
///<param name="toRemove"></param>
///<returns></returns>
public bool DeregisterSubscription(IDisposable toRemove)
{
return _subscriptions.Remove(toRemove);
}

///<summary>
/// Number of subscriptions.
///</summary>
public int NumSubscriptions
{
get { return _subscriptions.Count; }
}

/// <summary>
/// <see cref="IScheduler.Schedule(Action,long)"/>
/// </summary>
/// <param name="action"></param>
/// <param name="firstInMs"></param>
/// <returns></returns>
public IDisposable Schedule(Action action, long firstInMs)
{
return _scheduler.Schedule(action, firstInMs);
}

/// <summary>
/// <see cref="IScheduler.ScheduleOnInterval(Action,long,long)"/>
/// </summary>
/// <param name="action"></param>
/// <param name="firstInMs"></param>
/// <param name="regularInMs"></param>
public IDisposable ScheduleOnInterval(Action action, long firstInMs, long regularInMs)
{
return _scheduler.ScheduleOnInterval(action, firstInMs, regularInMs);
}

/// <summary>
/// <see cref="IFiber.Start"/>
/// </summary>
public void Start()
{
_thread.Start();
}

///<summary>
/// Calls join on the thread.
///</summary>
public void Join()
{
_thread.Join();
}

/// <summary>
/// Stops the thread.
/// </summary>
public void Dispose()
{
_scheduler.Dispose();
_subscriptions.Dispose();
_queue.Stop();
}
}
}

Change log

r422 by graham.m.nash on Oct 7, 2010   Diff
Fixed variable names in some public
methods.
Upped version to 1.0.5 and built.
Go to: 
Project members, sign in to write a code review

Older revisions

r408 by graham.m.nash on Sep 7, 2010   Diff
Documentation fixes
r401 by graham.m.nash on Sep 3, 2010   Diff
Added BusyWaitQueue
Extracted DefaultQueue from
BoundedQueue and made DefaultQueue
default in ThreadFiber
Various renames/removals of redundant
...
r400 by graham.m.nash on Sep 3, 2010   Diff
Removed unnecessary EnqueueAll
All revisions of this file

File info

Size: 4770 bytes, 163 lines

File properties

svn:mergeinfo
Powered by Google Project Hosting