13
0

LV2 worker: prevent corruption of ringbuffer

git-svn-id: svn://localhost/ardour2/branches/3.0@13146 d708f5d6-7413-0410-9779-e7cbd77b26cf
This commit is contained in:
Robin Gareus 2012-08-28 15:42:35 +00:00
parent a8f0c3255f
commit 9ab45f37c6
2 changed files with 57 additions and 5 deletions

View File

@ -75,6 +75,16 @@ public:
private:
void run();
/**
Peek in RB, get size and check if a block of 'size' is available.
Handle the unlikley edge-case, if we're called in between the
responder writing 'size' and 'data'.
@param rb the ringbuffer to check
@return true if the message is complete, false otherwise
*/
bool verify_message_completeness(RingBuffer<uint8_t>* rb);
Workee* _workee;
RingBuffer<uint8_t>* _requests;
@ -82,7 +92,8 @@ private:
uint8_t* _response;
PBD::Semaphore _sem;
bool _exit;
Glib::Threads::Thread* _thread;
Glib::Threads::Thread* _thread;
};
} // namespace ARDOUR

View File

@ -44,11 +44,14 @@ Worker::~Worker()
bool
Worker::schedule(uint32_t size, const void* data)
{
if (_requests->write_space() < size + sizeof(size)) {
return false;
}
if (_requests->write((const uint8_t*)&size, sizeof(size)) != sizeof(size)) {
return false;
}
if (_requests->write((const uint8_t*)data, size) != size) {
return false; // FIXME: corruption
return false;
}
_sem.post();
return true;
@ -57,11 +60,34 @@ Worker::schedule(uint32_t size, const void* data)
bool
Worker::respond(uint32_t size, const void* data)
{
if (_requests->write_space() < size + sizeof(size)) {
return false;
}
if (_responses->write((const uint8_t*)&size, sizeof(size)) != sizeof(size)) {
return false;
}
if (_responses->write((const uint8_t*)data, size) != size) {
return false; // FIXME: corruption
return false;
}
return true;
}
bool
Worker::verify_message_completeness(RingBuffer<uint8_t>* rb)
{
uint32_t read_space = rb->read_space();
uint32_t size;
RingBuffer<uint8_t>::rw_vector vec;
rb->get_read_vector (&vec);
if (vec.len[0] >= sizeof(size)) {
memcpy (&size, vec.buf[0], sizeof (size));
} else {
memcpy (&size, vec.buf[0], vec.len[0]);
memcpy (&size + vec.len[0], vec.buf[1], sizeof(size) - vec.len[0]);
}
if (read_space < size+sizeof(size)) {
/* message from writer is yet incomplete. respond next cycle */
return false;
}
return true;
}
@ -71,7 +97,12 @@ Worker::emit_responses()
{
uint32_t read_space = _responses->read_space();
uint32_t size = 0;
while (read_space > sizeof(size)) {
while (read_space >= sizeof(size)) {
if (!verify_message_completeness(_responses)) {
/* message from writer is yet incomplete. respond next cycle */
return;
}
/* read and send response */
_responses->read((uint8_t*)&size, sizeof(size));
_responses->read(_response, size);
_workee->work_response(size, _response);
@ -90,7 +121,17 @@ Worker::run()
return;
}
uint32_t size = 0;
uint32_t size = _requests->read_space();
if (size < sizeof(size)) {
PBD::error << "Worker: no work-data on ring buffer" << endmsg;
continue;
}
while (!verify_message_completeness(_requests)) {
::usleep(2000);
if (_exit) {
return;
}
}
if (_requests->read((uint8_t*)&size, sizeof(size)) < sizeof(size)) {
PBD::error << "Worker: Error reading size from request ring"
<< endmsg;