LV2 worker: prevent corruption of ringbuffer
[ardour.git] / libs / ardour / worker.cc
1 /*
2   Copyright (C) 2012 Paul Davis
3   Author: David Robillard
4
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.
9
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.
14
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.
18 */
19
20 #include <stdlib.h>
21
22 #include "ardour/worker.h"
23 #include "pbd/error.h"
24
25 namespace ARDOUR {
26
27 Worker::Worker(Workee* workee, uint32_t ring_size)
28         : _workee(workee)
29         , _requests(new RingBuffer<uint8_t>(ring_size))
30         , _responses(new RingBuffer<uint8_t>(ring_size))
31         , _response((uint8_t*)malloc(ring_size))
32         , _sem(0)
33         , _exit(false)
34         , _thread (Glib::Threads::Thread::create(sigc::mem_fun(*this, &Worker::run)))
35 {}
36
37 Worker::~Worker()
38 {
39         _exit = true;
40         _sem.post();
41         _thread->join();
42 }
43
44 bool
45 Worker::schedule(uint32_t size, const void* data)
46 {
47         if (_requests->write_space() < size + sizeof(size)) {
48                 return false;
49         }
50         if (_requests->write((const uint8_t*)&size, sizeof(size)) != sizeof(size)) {
51                 return false;
52         }
53         if (_requests->write((const uint8_t*)data, size) != size) {
54                 return false;
55         }
56         _sem.post();
57         return true;
58 }
59
60 bool
61 Worker::respond(uint32_t size, const void* data)
62 {
63         if (_requests->write_space() < size + sizeof(size)) {
64                 return false;
65         }
66         if (_responses->write((const uint8_t*)&size, sizeof(size)) != sizeof(size)) {
67                 return false;
68         }
69         if (_responses->write((const uint8_t*)data, size) != size) {
70                 return false;
71         }
72         return true;
73 }
74
75 bool
76 Worker::verify_message_completeness(RingBuffer<uint8_t>* rb)
77 {
78         uint32_t read_space = rb->read_space();
79         uint32_t size;
80         RingBuffer<uint8_t>::rw_vector vec;
81         rb->get_read_vector (&vec);
82         if (vec.len[0] >= sizeof(size)) {
83                 memcpy (&size, vec.buf[0], sizeof (size));
84         } else {
85                 memcpy (&size, vec.buf[0], vec.len[0]);
86                 memcpy (&size + vec.len[0], vec.buf[1], sizeof(size) - vec.len[0]);
87         }
88         if (read_space < size+sizeof(size)) {
89                 /* message from writer is yet incomplete. respond next cycle */
90                 return false;
91         }
92         return true;
93 }
94
95 void
96 Worker::emit_responses()
97 {
98         uint32_t read_space = _responses->read_space();
99         uint32_t size       = 0;
100         while (read_space >= sizeof(size)) {
101                 if (!verify_message_completeness(_responses)) {
102                         /* message from writer is yet incomplete. respond next cycle */
103                         return;
104                 }
105                 /* read and send response */
106                 _responses->read((uint8_t*)&size, sizeof(size));
107                 _responses->read(_response, size);
108                 _workee->work_response(size, _response);
109                 read_space -= sizeof(size) + size;
110         }
111 }
112
113 void
114 Worker::run()
115 {
116         void*  buf      = NULL;
117         size_t buf_size = 0;
118         while (true) {
119                 _sem.wait();
120                 if (_exit) {
121                         return;
122                 }
123
124                 uint32_t size = _requests->read_space();
125                 if (size < sizeof(size)) {
126                         PBD::error << "Worker: no work-data on ring buffer" << endmsg;
127                         continue;
128                 }
129                 while (!verify_message_completeness(_requests)) {
130                         ::usleep(2000);
131                         if (_exit) {
132                                 return;
133                         }
134                 }
135                 if (_requests->read((uint8_t*)&size, sizeof(size)) < sizeof(size)) {
136                         PBD::error << "Worker: Error reading size from request ring"
137                                    << endmsg;
138                         continue;
139                 }
140
141                 if (size > buf_size) {
142                         buf = realloc(buf, size);
143                         buf_size = size;
144                 }
145
146                 if (_requests->read((uint8_t*)buf, size) < size) {
147                         PBD::error << "Worker: Error reading body from request ring"
148                                    << endmsg;
149                         continue;  // TODO: This is probably fatal
150                 }
151
152                 _workee->work(size, buf);
153         }
154 }
155
156 } // namespace ARDOUR