Skip to content

Commit 04cc7ae

Browse files
authored
Merge pull request #3 from MirekVales/vFiesta
vFiesta
2 parents a93ad12 + 9e6405a commit 04cc7ae

21 files changed

+181
-153
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace WhatNow.Contracts.Statistics
2+
{
3+
using System.Collections.Generic;
4+
5+
public class FixedSizedQueue<T> : Queue<T>
6+
{
7+
public int MaxCapacity { get; }
8+
9+
public FixedSizedQueue(int capacity) : base(capacity)
10+
=> MaxCapacity = capacity;
11+
12+
public new void Enqueue(T item)
13+
{
14+
if (MaxCapacity != 0 && MaxCapacity < Count + 1)
15+
Dequeue();
16+
17+
base.Enqueue(item);
18+
}
19+
}
20+
}

WhatNow/WhatNow.Essentials/ActionDispatcher.cs

+14-15
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
using Helios.Concurrency;
2-
using System;
3-
using System.Collections.Generic;
4-
using System.Linq;
5-
using System.Threading;
6-
using System.Threading.Tasks;
7-
using WhatNow.Contracts;
8-
using WhatNow.Contracts.Actions;
9-
using WhatNow.Contracts.Execution;
10-
using WhatNow.Contracts.Resources;
11-
using WhatNow.Essentials.Resources;
12-
13-
namespace WhatNow.Essentials
1+
namespace WhatNow.Essentials
142
{
3+
using Helios.Concurrency;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Threading;
8+
using System.Threading.Tasks;
9+
using WhatNow.Contracts;
10+
using WhatNow.Contracts.Actions;
11+
using WhatNow.Contracts.Execution;
12+
using WhatNow.Contracts.Resources;
13+
using WhatNow.Essentials.Resources;
14+
1515
public class ActionDispatcher : IActionDispatcher
1616
{
1717
readonly IActionPipe[] pipes;
@@ -147,7 +147,6 @@ await TaskFactory.StartNew(() =>
147147
{
148148
while (!IsFinished)
149149
DoEvents();
150-
151150
});
152151
}
153152

@@ -193,4 +192,4 @@ public void Dispose()
193192
pool.Dispose();
194193
}
195194
}
196-
}
195+
}

WhatNow/WhatNow.Essentials/ActionPipe.cs

+19-13
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
using System;
2-
using System.Linq;
3-
using WhatNow.Contracts.Data;
4-
using System.Threading.Tasks;
5-
using WhatNow.Contracts.Actions;
6-
using System.Collections.Generic;
7-
using WhatNow.Contracts.Dependency;
8-
using WhatNow.Contracts.Resources;
9-
using WhatNow.Contracts.Statistics;
10-
11-
namespace WhatNow.Essentials
1+
namespace WhatNow.Essentials
122
{
3+
using System;
4+
using System.Linq;
5+
using WhatNow.Contracts.Data;
6+
using System.Threading.Tasks;
7+
using WhatNow.Contracts.Actions;
8+
using System.Collections.Generic;
9+
using WhatNow.Contracts.Dependency;
10+
using WhatNow.Contracts.Resources;
11+
using WhatNow.Contracts.Statistics;
12+
1313
public class ActionPipe : IActionPipe
1414
{
1515
public ActionToken ActionToken { get; }
@@ -31,13 +31,15 @@ public class ActionPipe : IActionPipe
3131
public bool BreakRequested => Current.Any(a => a.BreakRequested);
3232

3333
readonly object executionsLock = new object();
34-
readonly Dictionary<Type, Queue<TimeSpan>> executions;
34+
readonly Dictionary<Type, FixedSizedQueue<TimeSpan>> executions;
3535

3636
public IEnumerable<BreakRequestReason> BreakReasons => actions
3737
.Select(a => a.Value.BreakRequestReason)
3838
.Where(br => br != null)
3939
.ToArray();
4040

41+
public const int MAX_QUEUE_SIZE = 100;
42+
4143
public ActionPipe(IActionPipeMap map, ActionToken actionToken, IDependencyResolver dependencyResolver)
4244
{
4345
ActionToken = actionToken;
@@ -50,7 +52,7 @@ public ActionPipe(IActionPipeMap map, ActionToken actionToken, IDependencyResolv
5052

5153
Current = new IAction[0];
5254

53-
executions = actions.Keys.ToDictionary(k => k, _ => new Queue<TimeSpan>());
55+
executions = actions.Keys.ToDictionary(k => k, _ => new FixedSizedQueue<TimeSpan>(MAX_QUEUE_SIZE));
5456
}
5557

5658
public void Restart()
@@ -63,6 +65,10 @@ public void Restart()
6365
.ToDictionary(k => k, t => (IAction)dependencyResolver.Resolve(t));
6466

6567
Current = new IAction[0];
68+
69+
lock (executionsLock)
70+
foreach (var type in executions.Values)
71+
type.Clear();
6672
}
6773

6874
public bool TryGetNextTask(IResourceManager resourceManager, TaskFactory taskFactory, out Task task)

WhatNow/WhatNow.Essentials/BlockStopwatch.cs

+6-10
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
using System;
2-
using System.Diagnostics;
3-
4-
namespace WhatNow.Essentials
1+
namespace WhatNow.Essentials
52
{
3+
using System;
4+
using System.Diagnostics;
5+
66
public class BlockStopwatch : IDisposable
77
{
88
readonly Stopwatch stopwatch;
@@ -11,14 +11,10 @@ public class BlockStopwatch : IDisposable
1111
public TimeSpan ElapsedTime => stopwatch.Elapsed;
1212

1313
public BlockStopwatch()
14-
{
15-
stopwatch = Stopwatch.StartNew();
16-
}
14+
=> stopwatch = Stopwatch.StartNew();
1715

1816
public BlockStopwatch(Action<TimeSpan> closeAction) : this()
19-
{
20-
this.closeAction = closeAction;
21-
}
17+
=> this.closeAction = closeAction;
2218

2319
public void Invoke()
2420
=> closeAction?.Invoke(ElapsedTime);

WhatNow/WhatNow.Essentials/Dependency/CustomDependencyResolver.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
using System;
2-
using WhatNow.Contracts.Dependency;
3-
4-
namespace WhatNow.Essentials.Dependency
1+
namespace WhatNow.Essentials.Dependency
52
{
3+
using System;
4+
using WhatNow.Contracts.Dependency;
5+
66
public class CustomDependencyResolver : IDependencyResolver
77
{
88
readonly Func<Type, object> resolve;

WhatNow/WhatNow.Essentials/Dependency/TransientDependencyResolver.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
using System;
2-
using WhatNow.Contracts.Dependency;
3-
4-
namespace WhatNow.Essentials.Dependency
1+
namespace WhatNow.Essentials.Dependency
52
{
3+
using System;
4+
using WhatNow.Contracts.Dependency;
5+
66
public class TransientDependencyResolver : IDependencyResolver
77
{
88
public object Resolve(Type type) => Activator.CreateInstance(type);

WhatNow/WhatNow.Essentials/ResourceTypes/ThreadPool.cs

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
using Helios.Concurrency;
2-
using System;
3-
using System.Collections.Generic;
4-
using System.Threading;
5-
using System.Threading.Tasks;
6-
using WhatNow.Contracts.Resources;
7-
using WhatNow.Contracts.Resources.Accessable;
8-
using WhatNow.Contracts.ThreadPool;
9-
10-
namespace WhatNow.Essentials.ResourceTypes
1+
namespace WhatNow.Essentials.ResourceTypes
112
{
3+
using System;
4+
using System.Threading;
5+
using Helios.Concurrency;
6+
using System.Threading.Tasks;
7+
using System.Collections.Generic;
8+
using WhatNow.Contracts.Resources;
9+
using WhatNow.Contracts.ThreadPool;
10+
using WhatNow.Contracts.Resources.Accessable;
11+
1212
public class ThreadPool : IAccessableResource, IThreadPool
1313
{
1414
readonly DedicatedThreadPool threadPool;

WhatNow/WhatNow.Essentials/ResourceTypes/ThreadPoolResourceDefinition.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
using Helios.Concurrency;
2-
using System;
3-
using System.Threading.Tasks;
4-
using WhatNow.Contracts.Resources;
5-
6-
namespace WhatNow.Essentials.ResourceTypes
1+
namespace WhatNow.Essentials.ResourceTypes
72
{
3+
using System;
4+
using Helios.Concurrency;
5+
using System.Threading.Tasks;
6+
using WhatNow.Contracts.Resources;
7+
88
public class ThreadPoolResourceDefinition : ResourceDefinition
99
{
1010
readonly int numberOfThreads;

WhatNow/WhatNow.Essentials/Resources/Monitoring/AccessMonitorMemory.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using WhatNow.Contracts.Resources;
4-
5-
namespace WhatNow.Essentials.Resources.Monitoring
1+
namespace WhatNow.Essentials.Resources.Monitoring
62
{
3+
using System;
4+
using System.Collections.Generic;
5+
using WhatNow.Contracts.Resources;
6+
77
public class AccessMonitor
88
{
99
readonly object eventsLock = new object();

WhatNow/WhatNow.Essentials/Resources/Monitoring/AccessMonitorSink.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
using System;
2-
using WhatNow.Contracts.Resources;
3-
using WhatNow.Contracts.Resources.Monitoring;
4-
5-
namespace WhatNow.Essentials.Resources.Monitoring
1+
namespace WhatNow.Essentials.Resources.Monitoring
62
{
3+
using System;
4+
using WhatNow.Contracts.Resources;
5+
using WhatNow.Contracts.Resources.Monitoring;
6+
77
public class AccessMonitorSink : IAccessMonitor
88
{
99
public void AccessEvent(ResourceDefinition resource)

WhatNow/WhatNow.Essentials/Resources/Monitoring/AccessStopwatch.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
using System;
2-
using WhatNow.Contracts.Resources;
3-
using WhatNow.Contracts.Resources.Monitoring;
4-
5-
namespace WhatNow.Essentials.Resources.Monitoring
1+
namespace WhatNow.Essentials.Resources.Monitoring
62
{
3+
using System;
4+
using WhatNow.Contracts.Resources;
5+
using WhatNow.Contracts.Resources.Monitoring;
6+
77
public class AccessStopwatch
88
{
99
public DateTime Created { get; }

WhatNow/WhatNow.Essentials/Resources/Monitoring/Event.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
using System;
2-
using WhatNow.Contracts.Resources;
3-
4-
namespace WhatNow.Essentials.Resources.Monitoring
1+
namespace WhatNow.Essentials.Resources.Monitoring
52
{
3+
using System;
4+
using WhatNow.Contracts.Resources;
5+
66
public struct Event
77
{
88
public DateTime Created { get; }

WhatNow/WhatNow.Essentials/Resources/ResourceManager.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using WhatNow.Contracts.Resources;
4-
using WhatNow.Contracts.Resources.Monitoring;
5-
using WhatNow.Essentials.Resources.Monitoring;
6-
7-
namespace WhatNow.Essentials.Resources
1+
namespace WhatNow.Essentials.Resources
82
{
3+
using System;
4+
using System.Collections.Generic;
5+
using WhatNow.Contracts.Resources;
6+
using WhatNow.Contracts.Resources.Monitoring;
7+
using WhatNow.Essentials.Resources.Monitoring;
8+
99
public class ResourceManager : IResourceManager
1010
{
1111
public IAccessMonitor AccessMonitor;

WhatNow/WhatNow.Essentials/Resources/ResourcePlan.cs

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Threading.Tasks;
5-
using WhatNow.Contracts.Resources;
6-
using WhatNow.Essentials.ResourceTypes;
7-
8-
namespace WhatNow.Essentials.Resources
1+
namespace WhatNow.Essentials.Resources
92
{
3+
using System;
4+
using System.Linq;
5+
using System.Threading.Tasks;
6+
using System.Collections.Generic;
7+
using WhatNow.Contracts.Resources;
8+
using WhatNow.Essentials.ResourceTypes;
9+
1010
public class ResourcePlan : IResourcePlan
1111
{
1212
public static IResourcePlan Empty => new ResourcePlan();

WhatNow/WhatNow.Essentials/TaskList.cs

+21-12
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
using System;
2-
using System.Threading;
3-
using System.Threading.Tasks;
4-
using System.Collections.Generic;
5-
using Helios.Concurrency;
6-
using WhatNow.Contracts.ThreadPool;
7-
8-
namespace WhatNow.Essentials
1+
namespace WhatNow.Essentials
92
{
3+
using System;
4+
using System.Threading;
5+
using Helios.Concurrency;
6+
using System.Threading.Tasks;
7+
using System.Collections.Generic;
8+
using WhatNow.Contracts.ThreadPool;
9+
1010
public class TaskList : ITaskList
1111
{
1212
readonly CancellationTokenSource cancellationTokenSource;
@@ -59,7 +59,7 @@ public ITaskList With(IEnumerable<Action> actions)
5959

6060
return this;
6161
}
62-
62+
6363
public ITaskList With(Action<CancellationToken> action)
6464
{
6565
ThrowIfDisposed();
@@ -82,7 +82,16 @@ public void WaitAllFinished()
8282

8383
Task.WaitAll(tasks.ToArray());
8484

85-
ClearTasks();
85+
ClearCompletedTasks();
86+
}
87+
88+
public async Task WaitAllFinishedAsync()
89+
{
90+
ThrowIfDisposed();
91+
92+
await Task.WhenAll(tasks.ToArray());
93+
94+
ClearCompletedTasks();
8695
}
8796

8897
void ThrowIfDisposed()
@@ -102,13 +111,13 @@ public void Dispose()
102111
cancellationTokenSource?.Dispose();
103112
pool?.Dispose();
104113

105-
ClearTasks();
114+
ClearCompletedTasks();
106115

107116
disposed = true;
108117
}
109118
}
110119

111-
void ClearTasks()
120+
void ClearCompletedTasks()
112121
{
113122
foreach (var task in tasks)
114123
task.Dispose();

0 commit comments

Comments
 (0)