2 Copyright (C) 2012-2016 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.
23 #include "ardour/worker.h"
24 #include "pbd/error.h"
26 #include <glibmm/timer.h>
30 Worker::Worker(Workee* workee, uint32_t ring_size, bool threaded)
32 , _requests(threaded ? new RingBuffer<uint8_t>(ring_size) : NULL)
33 , _responses(new RingBuffer<uint8_t>(ring_size))
34 , _response((uint8_t*)malloc(ring_size))
35 , _sem("worker_semaphore", 0)
38 , _synchronous(!threaded)
41 _thread = Glib::Threads::Thread::create(
42 sigc::mem_fun(*this, &Worker::run));
56 Worker::schedule(uint32_t size, const void* data)
58 if (_synchronous || !_requests) {
59 _workee->work(*this, size, data);
62 if (_requests->write_space() < size + sizeof(size)) {
65 if (_requests->write((const uint8_t*)&size, sizeof(size)) != sizeof(size)) {
68 if (_requests->write((const uint8_t*)data, size) != size) {
76 Worker::respond(uint32_t size, const void* data)
78 if (_responses->write_space() < size + sizeof(size)) {
81 if (_responses->write((const uint8_t*)&size, sizeof(size)) != sizeof(size)) {
84 if (_responses->write((const uint8_t*)data, size) != size) {
91 Worker::verify_message_completeness(RingBuffer<uint8_t>* rb)
93 uint32_t read_space = rb->read_space();
95 RingBuffer<uint8_t>::rw_vector vec;
96 rb->get_read_vector (&vec);
97 if (vec.len[0] + vec.len[1] < sizeof(size)) {
100 if (vec.len[0] >= sizeof(size)) {
101 memcpy (&size, vec.buf[0], sizeof (size));
103 memcpy (&size, vec.buf[0], vec.len[0]);
104 memcpy (&size + vec.len[0], vec.buf[1], sizeof(size) - vec.len[0]);
106 if (read_space < size+sizeof(size)) {
107 /* message from writer is yet incomplete. respond next cycle */
114 Worker::emit_responses()
116 uint32_t read_space = _responses->read_space();
118 while (read_space >= sizeof(size)) {
119 if (!verify_message_completeness(_responses)) {
120 /* message from writer is yet incomplete. respond next cycle */
123 /* read and send response */
124 _responses->read((uint8_t*)&size, sizeof(size));
125 _responses->read(_response, size);
126 _workee->work_response(size, _response);
127 read_space -= sizeof(size) + size;
143 uint32_t size = _requests->read_space();
144 if (size < sizeof(size)) {
145 PBD::error << "Worker: no work-data on ring buffer" << endmsg;
148 while (!verify_message_completeness(_requests)) {
155 if (_requests->read((uint8_t*)&size, sizeof(size)) < sizeof(size)) {
156 PBD::error << "Worker: Error reading size from request ring"
161 if (size > buf_size) {
162 buf = realloc(buf, size);
166 PBD::error << "Worker: Error allocating memory"
168 buf_size = 0; // TODO: This is probably fatal
172 if (_requests->read((uint8_t*)buf, size) < size) {
173 PBD::error << "Worker: Error reading body from request ring"
175 continue; // TODO: This is probably fatal
178 _workee->work(*this, size, buf);
182 } // namespace ARDOUR