fix LV2 worker
[ardour.git] / libs / ardour / ardour / worker.h
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 #ifndef __ardour_worker_h__
21 #define __ardour_worker_h__
22
23 #include <stdint.h>
24
25 #include <glibmm/threads.h>
26
27 #include "pbd/ringbuffer.h"
28 #include "pbd/semaphore.h"
29
30 namespace ARDOUR {
31
32 /**
33    An object that needs to schedule non-RT work in the audio thread.
34 */
35 class Workee {
36 public:
37         virtual ~Workee() {}
38
39         /**
40            Do some work in the worker thread.
41         */
42         virtual int work(uint32_t size, const void* data) = 0;
43
44         /**
45            Handle a response from the worker thread in the audio thread.
46         */
47         virtual int work_response(uint32_t size, const void* data) = 0;
48 };
49
50 /**
51    A worker thread for non-realtime tasks scheduled in the audio thread.
52 */
53 class Worker
54 {
55 public:
56         Worker(Workee* workee, uint32_t ring_size);
57         ~Worker();
58
59         /**
60            Schedule work (audio thread).
61            @return false on error.
62         */
63         bool schedule(uint32_t size, const void* data);
64
65         /**
66            Respond from work (worker thread).
67            @return false on error.
68         */
69         bool respond(uint32_t size, const void* data);
70
71         /**
72            Emit any pending responses (audio thread).
73         */
74         void emit_responses();
75
76 private:
77         void run();
78
79         Workee*                _workee;
80         RingBuffer<uint8_t>*   _requests;
81         RingBuffer<uint8_t>*   _responses;
82         uint8_t*               _response;
83         PBD::Semaphore         _sem;
84         bool                   _exit;
85         Glib::Threads::Thread* _thread;
86 };
87
88 } // namespace ARDOUR
89
90 #endif  /* __ardour_worker_h__ */