-
Notifications
You must be signed in to change notification settings - Fork 0
/
intrusive_strand.h
56 lines (38 loc) · 1.12 KB
/
intrusive_strand.h
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
//
// Created by konstantin on 17.06.24.
//
#pragma once
#include <mutex>
#include <components/intrusive/list.h>
#include <components/sync/spinLock.h>
#include <executor/executor.h>
namespace NExecutors {
class IntrusiveStrand : public IExecutor {
using TaskList = NComponents::IntrusiveList<TaskBase>;
enum class State : uint32_t {
READY,
PLAN,
RUNNING
};
IExecutor& underlying;
mutable NSync::SpinLock spinlock;
TaskList tasks;
std::atomic<State> state{State::READY};
public:
explicit IntrusiveStrand(IExecutor& underlying);
IntrusiveStrand(const IntrusiveStrand&) = delete;
IntrusiveStrand& operator=(const IntrusiveStrand&) = delete;
IntrusiveStrand(IntrusiveStrand&&) = delete;
IntrusiveStrand& operator=(IntrusiveStrand&&) = delete;
void Submit(TaskBase* task) override;
private:
void SubmitInternal();
void RunBatch();
void PlanTask(State from);
bool ChangeState(State from, State to);
bool IsTasksEmpty() const {
std::lock_guard lock(spinlock);
return tasks.IsEmpty();
}
};
} // namespace NExecutors