nsbaci 1.0
Loading...
Searching...
No Matches
scheduler.h
Go to the documentation of this file.
1
12
13#ifndef NSBACI_SERVICES_RUNTIME_SCHEDULER_H
14#define NSBACI_SERVICES_RUNTIME_SCHEDULER_H
15
16#include <optional>
17#include <unordered_map>
18#include <vector>
19
20#include "thread.h"
21
27
36class Scheduler {
37 public:
38 Scheduler() = default;
39 virtual ~Scheduler() = default;
40
45 virtual Thread* pickNext() = 0;
46
51 virtual void addThread(Thread thread) = 0;
52
56 virtual void blockCurrent() = 0;
57
62 virtual void blockOnSemaphore(uint32_t semaphoreAddr) = 0;
63
69 virtual size_t unblockSemaphore(uint32_t semaphoreAddr) = 0;
70
75 virtual void unblock(nsbaci::types::ThreadID threadId) = 0;
76
80 virtual void yield() = 0;
81
85 virtual void terminateCurrent() = 0;
86
91 virtual bool hasThreads() const = 0;
92
97 virtual Thread* current() = 0;
98
102 virtual void clear() = 0;
103
108 virtual void unblockIO() = 0;
109
114 virtual const std::vector<Thread>& getThreads() const = 0;
115
120 virtual void blockOnCoend(int32_t expectedThreads) = 0;
121
126 virtual void checkCoendUnblock() = 0;
127
128 protected:
129 std::vector<Thread> threads;
130 std::vector<size_t> readyQueue;
131 std::vector<size_t> blockedQueue;
132 std::vector<size_t> ioQueue;
133 std::optional<size_t> runningIndex;
134
136 std::unordered_map<uint32_t, std::vector<size_t>> semaphoreQueues;
137
139 std::vector<std::pair<size_t, int32_t>> coendQueue;
140};
141
142} // namespace nsbaci::services::runtime
143
144#endif // NSBACI_SERVICES_RUNTIME_SCHEDULER_H
virtual const std::vector< Thread > & getThreads() const =0
Get all threads managed by the scheduler.
virtual Thread * pickNext()=0
Pick the next thread to run.
virtual void blockCurrent()=0
Block the currently running thread.
virtual void blockOnCoend(int32_t expectedThreads)=0
Block current thread on coend (waiting for spawned threads).
std::optional< size_t > runningIndex
Index of currently running thread.
Definition scheduler.h:133
virtual void terminateCurrent()=0
Terminate the currently running thread.
std::vector< size_t > blockedQueue
Indices of blocked threads.
Definition scheduler.h:131
virtual bool hasThreads() const =0
Check if there are any threads left to run.
virtual void blockOnSemaphore(uint32_t semaphoreAddr)=0
Block current thread on a semaphore.
virtual void addThread(Thread thread)=0
Add a new thread to the scheduler.
std::vector< std::pair< size_t, int32_t > > coendQueue
Threads waiting at coend, with their expected thread counts.
Definition scheduler.h:139
virtual void unblockIO()=0
Move all I/O waiting threads back to ready queue.
std::vector< size_t > ioQueue
Indices of I/O waiting threads.
Definition scheduler.h:132
std::vector< Thread > threads
All threads owned by scheduler.
Definition scheduler.h:129
std::unordered_map< uint32_t, std::vector< size_t > > semaphoreQueues
Map from semaphore address to list of waiting thread indices.
Definition scheduler.h:136
virtual size_t unblockSemaphore(uint32_t semaphoreAddr)=0
Unblock threads waiting on a semaphore.
virtual void clear()=0
Clear all threads and reset scheduler state.
virtual void checkCoendUnblock()=0
Check if coend-blocked threads should be unblocked.
virtual void yield()=0
Yield the current thread (move to back of ready queue).
virtual void unblock(nsbaci::types::ThreadID threadId)=0
Move a thread from blocked to ready state.
virtual Thread * current()=0
Get the currently running thread.
std::vector< size_t > readyQueue
Indices of ready threads.
Definition scheduler.h:130
Represents a thread in the runtime service.
Definition thread.h:31
Runtime services namespace for nsbaci.
Thread class declaration for nsbaci runtime service.