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