-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbgpoint.h
119 lines (97 loc) · 3.35 KB
/
bgpoint.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/** BufferGraph
* Copyright (C) Madura A.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this program; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02111-1307, USA.
*/
#pragma once
#include <atomic>
#include <thread>
#include "buffer.h"
#include "bufferconfig.h"
#include "common.h"
#include "queue.h"
#include "runnable.h"
#define BUFFERS 3
namespace BufferGraph {
class Point : public Runnable {
private:
std::atomic<int> _ref_counter;
std::vector<Buffer *> _buffers;
Queue<Buffer *> *_free_buffers;
std::mutex _lock;
const int _max_retries = 50;
protected:
std::vector<Point *> _sinks, _sources;
size_t _buffer_refs[BUFFERS];
Queue<Buffer *> *_used_buffers;
Buffer **_buffers_on_peak;
BufferConfig _config;
std::atomic<uint64_t> _cur_stream_id;
public:
Point() {
_cur_stream_id = 0UL;
auto _config = BufferConfig();
_free_buffers = new Queue<Buffer *>(BUFFERS + 1);
_used_buffers = new Queue<Buffer *>(BUFFERS + 1);
for (int i = 0; i < BUFFERS; i++) {
_buffer_refs[i] = 0;
auto b = new Buffer(_config, i);
_free_buffers->PushBlocking(b);
_buffers.push_back(b);
}
}
void SetBufferConfig(const BufferConfig *config) { _config = *config; }
BufferConfig *GetBufferConfig() { return &_config; }
// reentrant
// releases a buffer owned by self when called by
// another Point object (do not call manually, use
// ReleaseAllSources()
void ReleaseBuffer(Buffer *buffer);
// reentrant (assumed) -- not with ReleaseBuffer()
// returns what's on the top of the queue (oldest
// unprocessed buffer). It can return the same
// buffer for until all sinks have released buffer
// this is resolved in PeakAllSources() use that
// instead of this for easier interfacing
Buffer *PeakBuffer();
// use these functions after
void RegisterSink(Point *p);
void RegisterSource(Point *p);
// preallocates all resources before the BufferGraph
// starts rolling, must call after registering sinks
// and sources
void Preallocate();
// do work on the node, this should be called from
// a thread dedicated to the node
// Runnable::Run()
void Flush();
protected:
// non reentrant
// retrieves a buffer from the released buffer
Buffer *AcquireBuffer();
// non reentrant
// push the filled buffer you acquired by calling
// AcquireBuffer()
void PushBuffer(Buffer *buffer);
// non reentrant
// one thread should call the owned Point object's
// version of this function
Buffer **PeakAllSources();
// non reentrant
// never call other objects' version
void ReleaseAllSources(Buffer **buffers_on_peak);
};
}