-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThreadExecutor.h
105 lines (84 loc) · 1.99 KB
/
ThreadExecutor.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
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
#pragma once
#include <deque>
#include <queue>
#include <mutex>
#include <atomic>
#include <vector>
#include <thread>
#include <barrier>
#include <condition_variable>
class ThreadedExecutor {
public:
explicit ThreadedExecutor(int inputSize, int outputSize, const std::size_t numThreads = 1);
~ThreadedExecutor();
void* getInputBuffer(void);
void queueInputBuffer(void* buffer);
void returnOutputBuffer(void* buffer);
void* dequeueOutputBuffer(void);
void stop(void) noexcept;
virtual void kernel(const int id) = 0;
protected:
void start(void);
void loop(void);
void* input;
void* output;
const int numThreads;
std::mutex mMutex;
std::atomic_bool mFinished;
std::condition_variable mCv;
std::vector<std::thread> mThreads;
//class DoubleBuffer {
//public:
// DoubleBuffer(void);
//
// void setBuffers(void* curr, void* next);
//
// void* Produce(void);
//
// void* Consume(void);
//
// void Stop(void);
//
//private:
// std::atomic_bool running;
// std::atomic_bool is_full;
// std::atomic_bool is_empty;
// void* active_buffer;
// void* inactive_buffer;
// std::mutex mutex;
// std::condition_variable full_cv;
// std::condition_variable empty_cv;
//};
//
//DoubleBuffer readBuffer;
//DoubleBuffer writeBuffer;
class Queue {
public:
Queue(void);
void Stop(void);
void enqueue(void* buffer);
void* dequeue();
private:
bool mStopping;
std::mutex mMutex;
std::queue<void*> mQueue;
std::condition_variable mCvDeq;
std::condition_variable mCvEnq;
};
Queue inputQueue;
Queue outputQueue;
class BufferPool {
public:
BufferPool(int bufferSize);
~BufferPool();
void* GetBuffer();
void ReturnBuffer(void* buffer);
private:
int mBufferSize; // Size of each buffer in the pool
std::mutex mMutex; // Mutex to protect access to the pool
std::vector<void*> mBuffers; // All of the buffers in the pool
std::deque<void*> mAvailableBuffers; // Buffers that are currently available for use
};
BufferPool inputBufferPool;
BufferPool outputBufferPool;
};