FOSSredder 0.5.0
Loading...
Searching...
No Matches
Scheduler.h
Go to the documentation of this file.
1
5
6#pragma once
7
8#include <condition_variable>
9#include <cstddef>
10#include <cstdint>
11#include <deque>
12#include <functional>
13#include <mutex>
14#include <memory>
15#include <thread>
16#include <vector>
17
19class IErrorReporter;
20}
21
22namespace core::jobs {
23
28public:
33 explicit SlotLimiter(std::size_t slots);
34
38 void acquire();
39
43 void release();
44
45private:
46 std::size_t slots_;
47 std::size_t available_;
48 std::mutex m_;
49 std::condition_variable cv_;
50};
51
55class Scheduler {
56public:
57 using Task = std::function<void()>;
58
65 Scheduler(std::size_t workers,
66 std::size_t queueCapacity,
67 std::shared_ptr<core::ports::diagnostics::IErrorReporter> errorReporter);
68
72 ~Scheduler();
73
77 Scheduler(const Scheduler&) = delete;
78
83 Scheduler& operator=(const Scheduler&) = delete;
84
89 void enqueue(Task t);
90
94 void stop();
95
96private:
100 void workerLoop();
101
102 std::deque<Task> q_;
103 std::size_t cap_;
104 std::mutex m_;
105 std::condition_variable cvNotEmpty_;
106 std::condition_variable cvNotFull_;
107
108 bool stopping_ = false;
109 std::vector<std::thread> workers_;
110 std::shared_ptr<core::ports::diagnostics::IErrorReporter> errorReporter_;
111};
112
113}
void enqueue(Task t)
Enqueue a task, blocking when the queue is full.
Definition Scheduler.cpp:85
Scheduler(const Scheduler &)=delete
Copying a scheduler is disabled.
std::function< void()> Task
Definition Scheduler.h:57
Scheduler & operator=(const Scheduler &)=delete
Copy-assigning a scheduler is disabled.
Scheduler(std::size_t workers, std::size_t queueCapacity, std::shared_ptr< core::ports::diagnostics::IErrorReporter > errorReporter)
Create a scheduler with bounded workers and queue capacity.
Definition Scheduler.cpp:43
void stop()
Stop accepting new tasks and join all worker threads.
Definition Scheduler.cpp:61
~Scheduler()
Stop workers and destroy the scheduler.
Definition Scheduler.cpp:56
void release()
Release one acquired slot and wake a waiting caller.
Definition Scheduler.cpp:34
SlotLimiter(std::size_t slots)
Create a limiter with at least one available slot.
Definition Scheduler.cpp:21
void acquire()
Block until a slot is available and acquire it.
Definition Scheduler.cpp:27
Port for reporting structured diagnostic error events.