-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathITaskDispatcher.hpp
131 lines (113 loc) · 4 KB
/
ITaskDispatcher.hpp
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
//
// Copyright (c) Microsoft Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
//
#ifndef ITASKDISPATCHER_HPP
#define ITASKDISPATCHER_HPP
#include "IModule.hpp"
#include "ctmacros.hpp"
#include <atomic>
#include <string>
#include <vector>
///@cond INTERNAL_DOCS
namespace MAT_NS_BEGIN
{
/// <summary>
/// The Task class represents a single executable task that can be dispatched to an asynchronous worker
/// thread.
/// Individual Task implementations override operator() to execute
/// </summary>
class Task
{
/// <summary>
/// Atomic counter that returns sequentially incrementing unique Task ID
/// </summary>
static uint64_t GetNewTid()
{
static std::atomic<uint64_t> lastTid;
return lastTid.fetch_add(1);
}
public:
/// <summary>
/// Type of work item
/// </summary>
volatile enum
{
/// <summary>
/// A Shutdown item indicates that the worker thread should terminate and stop processing further items
/// </summary>
Shutdown,
/// <summary>
/// A Call item is a generic functor that should execute as soon as possible
/// </summary>
Call,
/// <summary>
/// A TimedCall item is a generic functor that should execute at the time specified by TargetTime
/// </summary>
TimedCall,
/// <summary>
/// A Done item is an item that has been marked by the worker thread as already completed.
/// </summary>
Done,
/// <summary>
/// A Cancelled item is an item that has been marked by the worker thread as Cancelled.
/// </summary>
Cancelled
} Type;
Task() :
tid(GetNewTid())
{};
/// <summary>
/// The time (in milliseconds since epoch) when this work item should be executed
/// </summary>
uint64_t TargetTime;
/// <summary>
/// Unique Task Id.
/// TODO: [maxgolov] - use this Task Id for task cancellation instead of raw ptr
/// </summary>
uint64_t tid;
/// <summary>
/// The Task class destructor.
/// </summary>
virtual ~Task() noexcept = default;
/// <summary>
/// The functor implementation that executes task logic. This method is overridden by Task
/// implementations.
/// </summary>
virtual void operator()() {}
/// <summary>
/// The typename of the underlying functor executed by this work item
/// </summary>
std::string TypeName;
};
/// <summary>
/// The ITaskDispatcher class manages dispatching of asynchronous tasks to background worker thread(s).
/// Individual TaskDispatcher implementations can manage creation/destruction of thread resources.
/// </summary>
class ITaskDispatcher : public IModule
{
public:
/// <summary>
/// The ITaskDispatcher class destructor.
/// </summary>
virtual ~ITaskDispatcher() noexcept = default;
/// <summary>
/// Terminate worker thread(s) and clean up thread resources
/// </summary>
virtual void Join() = 0;
/// <summary>
/// Queue an asynchronous task for dispatching to a worker thread
/// </summary>
/// <param name="task">Task to be executed on a worker thread</param>
virtual void Queue(Task* task) = 0;
/// <summary>
/// Cancel a previously queued tasks
/// </summary>
/// <param name="task">Task to be cancelled</param>
/// <param name="waitTime">Amount of time to wait for if the task is currently executing</param>
/// <returns>True if successfully cancelled, else false</returns>
virtual bool Cancel(Task* task, uint64_t waitTime = 0) = 0;
};
/// @endcond
} MAT_NS_END
#endif // ITASKDISPATCHER_HPP