2 Copyright (C) 2012 Paul Davis
3 Author: David Robillard
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "ardour/worker.h"
23 #include "pbd/error.h"
27 Worker::Worker(Workee* workee, uint32_t ring_size)
29 , _thread(Glib::Thread::create(sigc::mem_fun(*this, &Worker::run), true))
30 , _requests(new RingBuffer<uint8_t>(ring_size))
31 , _responses(new RingBuffer<uint8_t>(ring_size))
32 , _response((uint8_t*)malloc(ring_size))
45 Worker::schedule(uint32_t size, const void* data)
47 if (_requests->write((const uint8_t*)&size, sizeof(size)) != sizeof(size)) {
50 if (_requests->write((const uint8_t*)data, size) != size) {
51 return false; // FIXME: corruption
58 Worker::respond(uint32_t size, const void* data)
60 if (_responses->write((const uint8_t*)&size, sizeof(size)) != sizeof(size)) {
63 if (_responses->write((const uint8_t*)data, size) != size) {
64 return false; // FIXME: corruption
70 Worker::emit_responses()
72 uint32_t read_space = _responses->read_space();
74 while (read_space > sizeof(size)) {
75 _responses->read((uint8_t*)&size, sizeof(size));
76 _responses->read(_response, size);
77 _workee->work_response(size, _response);
78 read_space -= sizeof(size) + size;
94 if (_requests->read((uint8_t*)&size, sizeof(size)) < sizeof(size)) {
95 PBD::error << "Worker: Error reading size from request ring"
100 if (size > buf_size) {
101 buf = realloc(buf, size);
105 if (_requests->read((uint8_t*)buf, size) < size) {
106 PBD::error << "Worker: Error reading body from request ring"
108 continue; // TODO: This is probably fatal
111 _workee->work(size, buf);
115 } // namespace ARDOUR