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