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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
#pragma once
#include "std/function.hpp"
#include "std/shared_ptr.hpp"
#include "std/vector.hpp"
#include "base/cancellable.hpp"
#include "base/thread.hpp"
#include "base/threaded_list.hpp"
namespace core
{
/// class, that executes task, specified as a functors on the specified number of threads
/// - all tasks are stored in the single ThreadedList
class CommandsQueue
{
private:
class Routine;
public:
struct Command;
/// execution environment for single command
/// - passed into the task functor
/// - task functor should check the IsCancelled()
/// on the reasonable small interval and cancel
/// it's work upon receiving "true".
class Environment : public my::Cancellable
{
private:
size_t m_threadNum;
protected:
explicit Environment(size_t threadNum);
friend class Routine;
public:
size_t threadNum() const; //< number of the thread, which is executing the commands
};
/// single commmand
typedef function<void(Environment const &)> function_t;
/// basic command
/// - could wait for the completion of its execution
struct BaseCommand
{
shared_ptr<threads::Condition> m_cond;
mutable int m_waitCount;
mutable bool m_isCompleted;
/// should we create the threads::Condition ?
/// this flag is used to save resources
BaseCommand(bool isWaitable);
/// call this function when the execution
/// of the command is finished to release the waiters.
void finish() const;
/// @warning only single thread could "join" command
void join();
};
/// chain of commands
struct Chain
{
private:
list<function_t> m_fns;
public:
Chain();
Chain(function_t const & fn)
{
m_fns.push_back(fn);
}
Chain & addCommand(function_t const & fn)
{
m_fns.push_back(fn);
return *this;
}
void operator()(Environment const & env);
};
/// single command.
/// - commands could be chained together, using Chain class
struct Command : BaseCommand
{
private:
function_t m_fn;
public:
Command(bool isWaitable = false);
template <typename tt>
Command(tt t, bool isWaitable = false)
: BaseCommand(isWaitable), m_fn(t)
{}
void perform(Environment const & env) const;
};
private:
/// single execution routine
class Routine : public threads::IRoutine
{
private:
CommandsQueue * m_parent;
Environment m_env;
public:
Routine(CommandsQueue * parent, size_t idx);
/// threads::IRoutine overrides:
void Do() override;
void Cancel() override;
void CancelCommand();
};
/// class, which excapsulates thread and routine into single class.
struct Executor
{
threads::Thread m_thread;
void Cancel();
void CancelCommand();
};
vector<Executor> m_executors;
ThreadedList<shared_ptr<Command> > m_commands;
list<shared_ptr<Command> > m_initCommands;
list<shared_ptr<Command> > m_finCommands;
list<shared_ptr<Command> > m_cancelCommands;
friend class Routine;
threads::Condition m_cond;
size_t m_activeCommands;
void FinishCommand();
CommandsQueue(CommandsQueue const &);
CommandsQueue const & operator=(CommandsQueue const &);
void ClearImpl(list<shared_ptr<Command> > & l);
public:
CommandsQueue(size_t executorsCount);
~CommandsQueue();
/// Number of executors in this queue
size_t ExecutorsCount() const;
/// Adding different types of commands
/// @{
void AddCommand(shared_ptr<Command> const & cmd);
void AddInitCommand(shared_ptr<Command> const & cmd);
void AddFinCommand(shared_ptr<Command> const & cmd);
void AddCancelCommand(shared_ptr<Command> const & cmd);
/// @}
void Start();
void Cancel();
void CancelCommands();
void Join();
void Clear();
template<typename command_tt>
shared_ptr<Command> AddCommand(command_tt cmd, bool isWaitable = false)
{
shared_ptr<Command> pcmd(new Command(cmd, isWaitable));
AddCommand(pcmd);
return pcmd;
}
template <typename command_tt>
shared_ptr<Command> AddInitCommand(command_tt cmd, bool isWaitable = false)
{
shared_ptr<Command> const pcmd(new Command(cmd, isWaitable));
AddInitCommand(pcmd);
return pcmd;
}
template <typename command_tt>
shared_ptr<Command> const AddFinCommand(command_tt cmd, bool isWaitable = false)
{
shared_ptr<Command> pcmd(new Command(cmd, isWaitable));
AddFinCommand(pcmd);
return pcmd;
}
template <typename command_tt>
shared_ptr<Command> const AddCancelCommand(command_tt cmd, bool isWaitable = false)
{
shared_ptr<Command> pcmd(new Command(cmd, isWaitable));
AddCancelCommand(pcmd);
return pcmd;
}
};
}
|