-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathParallelOperationsQueue.cs
More file actions
91 lines (79 loc) · 3.01 KB
/
ParallelOperationsQueue.cs
File metadata and controls
91 lines (79 loc) · 3.01 KB
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
// Copyright (c) 2023, Phoenix Contact GmbH & Co. KG
// Licensed under the Apache License, Version 2.0
using System;
using System.Collections.Concurrent;
using System.Threading;
using Microsoft.Extensions.Logging;
namespace Moryx.Threading
{
/// <summary>
/// Task queue that utilizes parallel operations and to execute tasks
/// on a different thread WITHOUT blocking a thread during inactivity.
/// The queue preserves the order of execution and avoids parallel execution
/// of two tasks
/// </summary>
public class ParallelOperationsQueue<TElement>
{
/// <summary>
/// Thread counter of active
/// </summary>
private int _pendingElements;
/// <summary>
/// Queue of unprocessed events
/// </summary>
private readonly ConcurrentQueue<TElement> _eventQueue = new ConcurrentQueue<TElement>();
/// <summary>
/// Target callback for the event
/// </summary>
private Action<TElement> ElementExecution { get; }
/// <summary>
/// Reference to the managing <see cref="IParallelOperations"/> instance
/// </summary>
private IParallelOperations ParallelOperations { get; }
/// <summary>
/// Logger instance to log errors
/// </summary>
public ILogger ErrorLogger { get; }
/// <summary>
/// Provide the number of unprocessed elements in the queue
/// </summary>
public int PendingElements => _pendingElements;
/// <summary>
/// Create a new <see cref="Threading.ParallelOperations.EventDecoupler{TEventArgs}"/> to decouple a single listener from an event
/// </summary>
public ParallelOperationsQueue(Action<TElement> elementExecution, IParallelOperations parallelOperations, ILogger errorLogger)
{
ElementExecution = elementExecution;
ParallelOperations = parallelOperations;
ErrorLogger = errorLogger;
}
/// <summary>
/// Target for the worker thread to process the event queue
/// </summary>
private void ProcessEventQueue()
{
do
{
_eventQueue.TryDequeue(out var nextElement);
try
{
ElementExecution.Invoke(nextElement);
}
catch (Exception ex)
{
ErrorLogger.Log(LogLevel.Error, ex, "Exception during queue element execution!");
}
} while (Interlocked.Decrement(ref _pendingElements) > 0);
}
/// <summary>
/// Enqueue a new element that shall be processed in a different thread from the queue
/// </summary>
/// <param name="element"></param>
public void Enqueue(TElement element)
{
_eventQueue.Enqueue(element);
if (Interlocked.Increment(ref _pendingElements) == 1)
ParallelOperations.ExecuteParallel(ProcessEventQueue);
}
}
}