Fix thinko causing possible memory corruption.
[ardour.git] / libs / ardour / lv2 / lv2plug.in / ns / ext / worker / worker.h
1 /*
2   Copyright 2012 David Robillard <http://drobilla.net>
3
4   Permission to use, copy, modify, and/or distribute this software for any
5   purpose with or without fee is hereby granted, provided that the above
6   copyright notice and this permission notice appear in all copies.
7
8   THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9   WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10   MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11   ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17 /**
18    @file worker.h C header for the LV2 Worker extension
19    <http://lv2plug.in/ns/ext/worker>.
20 */
21
22 #ifndef LV2_WORKER_H
23 #define LV2_WORKER_H
24
25 #include <stdint.h>
26
27 #include "lv2/lv2plug.in/ns/lv2core/lv2.h"
28
29 #define LV2_WORKER_URI    "http://lv2plug.in/ns/ext/worker"
30 #define LV2_WORKER_PREFIX LV2_WORKER_URI "#"
31
32 #define LV2_WORKER__interface LV2_WORKER_PREFIX "interface"
33 #define LV2_WORKER__schedule  LV2_WORKER_PREFIX "schedule"
34
35 /**
36    A status code for worker functions.
37 */
38 typedef enum {
39         LV2_WORKER_SUCCESS       = 0,  /**< Completed successfully. */
40         LV2_WORKER_ERR_UNKNOWN   = 1,  /**< Unknown error. */
41         LV2_WORKER_ERR_NO_SPACE  = 2   /**< Failed due to lack of space. */
42 } LV2_Worker_Status;
43
44 typedef void* LV2_Worker_Respond_Handle;
45
46 /**
47    A function to respond to run() from the worker method.
48
49    The @p data MUST be safe for the host to copy and later pass to
50    work_response(), and the host MUST guarantee that it will be eventually
51    passed to work_response() if this function returns LV2_WORKER_SUCCESS.
52 */
53 typedef LV2_Worker_Status (*LV2_Worker_Respond_Function)(
54         LV2_Worker_Respond_Handle handle,
55         uint32_t                  size,
56         const void*               data);
57
58 /**
59    LV2 Plugin Worker Interface.
60
61    This is the interface provided by the plugin to implement a worker method.
62    The plugin's extension_data() method should return an LV2_Worker_Interface
63    when called with LV2_WORKER__interface as its argument.
64 */
65 typedef struct _LV2_Worker_Interface {
66         /**
67            The worker method.  This is called by the host in a non-realtime context
68            as requested, possibly with an arbitrary message to handle.
69
70            A response can be sent to run() using @p respond.  The plugin MUST NOT
71            make any assumptions about which thread calls this method, other than
72            the fact that there are no real-time requirements.
73
74            @param instance The LV2 instance this is a method on.
75            @param respond  A function for sending a response to run().
76            @param handle   Must be passed to @p respond if it is called.
77            @param size     The size of @p data.
78            @param data     Data from run(), or NULL.
79         */
80         LV2_Worker_Status (*work)(LV2_Handle                  instance,
81                                   LV2_Worker_Respond_Function respond,
82                                   LV2_Worker_Respond_Handle   handle,
83                                   uint32_t                    size,
84                                   const void*                 data);
85
86         /**
87            Handle a response from the worker.  This is called by the host in the
88            run() context when a response from the worker is ready.
89
90            @param instance The LV2 instance this is a method on.
91            @param size     The size of @p body.
92            @param body     Message body, or NULL.
93         */
94         LV2_Worker_Status (*work_response)(LV2_Handle  instance,
95                                            uint32_t    size,
96                                            const void* body);
97
98         /**
99            Called when all responses for this cycle have been delivered.
100
101            Since work_response() may be called after run() finished, this provides
102            a hook for code that must run after the cycle is completed.
103
104            This field may be NULL if the plugin has no use for it.  Otherwise, the
105            host MUST call it after every run(), regardless of whether or not any
106            responses were sent that cycle.
107         */
108         LV2_Worker_Status (*end_run)(LV2_Handle instance);
109 } LV2_Worker_Interface;
110
111 typedef void* LV2_Worker_Schedule_Handle;
112
113 typedef struct _LV2_Worker_Schedule {
114         /**
115            Opaque host data.
116         */
117         LV2_Worker_Schedule_Handle handle;
118
119         /**
120            Request from run() that the host call the worker.
121
122            This function is in the audio threading class.  It should be called from
123            run() to request that the host call the work() method in a non-realtime
124            context with the given arguments.
125
126            This function is always safe to call from run(), but it is not
127            guaranteed that the worker is actually called from a different thread.
128            In particular, when free-wheeling (e.g. for offline rendering), the
129            worker may be executed immediately.  This allows single-threaded
130            processing with sample accuracy and avoids timing problems when run() is
131            executing much faster or slower than real-time.
132
133            Plugins SHOULD be written in such a way that if the worker runs
134            immediately, and responses from the worker are delivered immediately,
135            the effect of the work takes place immediately with sample accuracy.
136
137            The @p data MUST be safe for the host to copy and later pass to work(),
138            and the host MUST guarantee that it will be eventually passed to work()
139            if this function returns LV2_WORKER_SUCCESS.
140
141            @param handle The handle field of this struct.
142            @param size   The size of @p data.
143            @param data   Message to pass to work(), or NULL.
144         */
145         LV2_Worker_Status (*schedule_work)(LV2_Worker_Schedule_Handle handle,
146                                            uint32_t                   size,
147                                            const void*                data);
148 } LV2_Worker_Schedule;
149
150 #endif  /* LV2_WORKER_H */