1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
/*
Copyright (C) 2012 Paul Davis
Author: David Robillard
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdlib.h>
#include "ardour/worker.h"
#include "pbd/error.h"
namespace ARDOUR {
Worker::Worker(Workee* workee, uint32_t ring_size)
: _workee(workee)
, _thread(Glib::Thread::create(sigc::mem_fun(*this, &Worker::run), true))
, _requests(new RingBuffer<uint8_t>(ring_size))
, _responses(new RingBuffer<uint8_t>(ring_size))
, _response((uint8_t*)malloc(ring_size))
, _sem(0)
, _exit(false)
{}
Worker::~Worker()
{
_exit = true;
_sem.post();
_thread->join();
}
bool
Worker::schedule(uint32_t size, const void* data)
{
if (_requests->write((const uint8_t*)&size, sizeof(size)) != sizeof(size)) {
return false;
}
if (_requests->write((const uint8_t*)data, size) != size) {
return false; // FIXME: corruption
}
_sem.post();
return true;
}
bool
Worker::respond(uint32_t size, const void* data)
{
if (_responses->write((const uint8_t*)&size, sizeof(size)) != sizeof(size)) {
return false;
}
if (_responses->write((const uint8_t*)data, size) != size) {
return false; // FIXME: corruption
}
return true;
}
void
Worker::emit_responses()
{
uint32_t read_space = _responses->read_space();
uint32_t size = 0;
while (read_space > sizeof(size)) {
_responses->read((uint8_t*)&size, sizeof(size));
_responses->read(_response, size);
_workee->work_response(size, _response);
read_space -= sizeof(size) + size;
}
}
void
Worker::run()
{
void* buf = NULL;
size_t buf_size = 0;
while (true) {
_sem.wait();
if (_exit) {
return;
}
uint32_t size = 0;
if (_requests->read((uint8_t*)&size, sizeof(size)) < sizeof(size)) {
PBD::error << "Worker: Error reading size from request ring"
<< endmsg;
continue;
}
if (size > buf_size) {
buf = realloc(buf, size);
buf_size = size;
}
if (_requests->read((uint8_t*)buf, size) < size) {
PBD::error << "Worker: Error reading body from request ring"
<< endmsg;
continue; // TODO: This is probably fatal
}
_workee->work(size, buf);
}
}
} // namespace ARDOUR
|