LV2: Immediately respond after processing scheduled work in sync exec mode
[ardour.git] / libs / ardour / worker.cc
1 /*
2   Copyright (C) 2012-2016 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 #include <unistd.h>
22
23 #include "ardour/worker.h"
24 #include "pbd/error.h"
25 #include "pbd/compose.h"
26
27 #include <glibmm/timer.h>
28
29 namespace ARDOUR {
30
31 Worker::Worker(Workee* workee, uint32_t ring_size, bool threaded)
32         : _workee(workee)
33         , _requests(threaded ? new PBD::RingBuffer<uint8_t>(ring_size) : NULL)
34         , _responses(new PBD::RingBuffer<uint8_t>(ring_size))
35         , _response((uint8_t*)malloc(ring_size))
36         , _sem(string_compose ("worker_semaphore%1", this).c_str(), 0)
37         , _thread(NULL)
38         , _exit(false)
39         , _synchronous(!threaded)
40 {
41         if (threaded) {
42                 _thread = Glib::Threads::Thread::create(
43                         sigc::mem_fun(*this, &Worker::run));
44         }
45 }
46
47 Worker::~Worker()
48 {
49         _exit = true;
50         _sem.signal();
51         if (_thread) {
52                 _thread->join();
53         }
54         delete _responses;
55         delete _requests;
56         free (_response);
57 }
58
59 bool
60 Worker::schedule(uint32_t size, const void* data)
61 {
62         if (_synchronous || !_requests) {
63                 _workee->work(*this, size, data);
64                 emit_responses ();
65                 return true;
66         }
67         if (_requests->write_space() < size + sizeof(size)) {
68                 return false;
69         }
70         if (_requests->write((const uint8_t*)&size, sizeof(size)) != sizeof(size)) {
71                 return false;
72         }
73         if (_requests->write((const uint8_t*)data, size) != size) {
74                 return false;
75         }
76         _sem.signal();
77         return true;
78 }
79
80 bool
81 Worker::respond(uint32_t size, const void* data)
82 {
83         if (_responses->write_space() < size + sizeof(size)) {
84                 return false;
85         }
86         if (_responses->write((const uint8_t*)&size, sizeof(size)) != sizeof(size)) {
87                 return false;
88         }
89         if (_responses->write((const uint8_t*)data, size) != size) {
90                 return false;
91         }
92         return true;
93 }
94
95 bool
96 Worker::verify_message_completeness(PBD::RingBuffer<uint8_t>* rb)
97 {
98         uint32_t read_space = rb->read_space();
99         uint32_t size;
100         PBD::RingBuffer<uint8_t>::rw_vector vec;
101         rb->get_read_vector (&vec);
102         if (vec.len[0] + vec.len[1] < sizeof(size)) {
103                 return false;
104         }
105         if (vec.len[0] >= sizeof(size)) {
106                 memcpy (&size, vec.buf[0], sizeof (size));
107         } else {
108                 memcpy (&size, vec.buf[0], vec.len[0]);
109                 memcpy (&size + vec.len[0], vec.buf[1], sizeof(size) - vec.len[0]);
110         }
111         if (read_space < size+sizeof(size)) {
112                 /* message from writer is yet incomplete. respond next cycle */
113                 return false;
114         }
115         return true;
116 }
117
118 void
119 Worker::emit_responses()
120 {
121         uint32_t read_space = _responses->read_space();
122         uint32_t size       = 0;
123         while (read_space >= sizeof(size)) {
124                 if (!verify_message_completeness(_responses)) {
125                         /* message from writer is yet incomplete. respond next cycle */
126                         return;
127                 }
128                 /* read and send response */
129                 _responses->read((uint8_t*)&size, sizeof(size));
130                 _responses->read(_response, size);
131                 _workee->work_response(size, _response);
132                 read_space -= sizeof(size) + size;
133         }
134 }
135
136 void
137 Worker::run()
138 {
139         void*  buf      = NULL;
140         size_t buf_size = 0;
141         while (true) {
142                 _sem.wait();
143                 if (_exit) {
144                         free(buf);
145                         return;
146                 }
147
148                 uint32_t size = _requests->read_space();
149                 if (size < sizeof(size)) {
150                         PBD::error << "Worker: no work-data on ring buffer" << endmsg;
151                         continue;
152                 }
153                 while (!verify_message_completeness(_requests)) {
154                         Glib::usleep(2000);
155                         if (_exit) {
156                                 if (buf) free(buf);
157                                 return;
158                         }
159                 }
160                 if (_requests->read((uint8_t*)&size, sizeof(size)) < sizeof(size)) {
161                         PBD::error << "Worker: Error reading size from request ring"
162                                    << endmsg;
163                         continue;
164                 }
165
166                 if (size > buf_size) {
167                         buf = realloc(buf, size);
168                         if (buf) {
169                                 buf_size = size;
170                         } else {
171                                 PBD::error << "Worker: Error allocating memory"
172                                            << endmsg;
173                                 buf_size = 0; // TODO: This is probably fatal
174                         }
175                 }
176
177                 if (_requests->read((uint8_t*)buf, size) < size) {
178                         PBD::error << "Worker: Error reading body from request ring"
179                                    << endmsg;
180                         continue;  // TODO: This is probably fatal
181                 }
182
183                 _workee->work(*this, size, buf);
184         }
185 }
186
187 } // namespace ARDOUR