DoubleBufferHandler.cpp 1.92 KB
Newer Older
d11's avatar
no    
d11 committed
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
#include "../../../../../NomadSpecialModules/src/drivers/gigecam/photonics/DoubleBufferHandler.h"

#include <iostream>
#include <sstream>

#include "../../../../../NomadSpecialModules/src/drivers/gigecam/photonics/BufferInformation.h"

CDoubleBufferHandler::CDoubleBufferHandler()
: buffer_read_(nullptr)
, buffer_write_(nullptr)
, new_data(false)
{
}

CDoubleBufferHandler::~CDoubleBufferHandler()
{
}

void CDoubleBufferHandler::PushBuffer(BGAPI2::Buffer *buffer) {
    std::lock_guard<std::mutex> lock(buffer_exchange_lock_);
    if( !buffer_write_ ) {
        buffer_write_ = buffer;
        new_data = true;
    }
    else {
        buffer->QueueBuffer();
    }
}

BGAPI2::Buffer* CDoubleBufferHandler::PullBuffer() {
    std::lock_guard<std::mutex> lock(buffer_exchange_lock_);
    if (buffer_write_ ) {
        if (buffer_read_) {
            buffer_read_->QueueBuffer();
        }
        buffer_read_ = buffer_write_;
        buffer_write_ = nullptr;
        new_data = false;
    }
    return buffer_read_;
}

void CDoubleBufferHandler::FreeBuffer(BGAPI2::Buffer *buffer) {
    std::lock_guard<std::mutex> lock(buffer_exchange_lock_);
    if (buffer_read_ && buffer_write_) {
        buffer_read_->QueueBuffer();
        buffer_read_ = nullptr;
    }
}

void CDoubleBufferHandler::Init() {
    if (buffer_write_) {
        buffer_write_->QueueBuffer();
        buffer_write_ = nullptr;
    }
    if (buffer_read_) {
        buffer_read_->QueueBuffer();
        buffer_read_ = nullptr;
    }
    new_data = false;
}

bool CDoubleBufferHandler::HasNewData() {
    return new_data;
}

CDoubleBufferHandler::CDoubleBufferHandler(const CDoubleBufferHandler &instance) {
    buffer_read_ = instance.buffer_read_;
    buffer_write_ = instance.buffer_write_;
}

CDoubleBufferHandler & CDoubleBufferHandler::operator =(const CDoubleBufferHandler &instance) {
    buffer_read_ = instance.buffer_read_;
    buffer_write_ = instance.buffer_write_;
    return *this;
}