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