summaryrefslogtreecommitdiffstats
path: root/third_party/pipewire/spa/support/loop.h
blob: 3c93573c8fe629ac19c3c059b4bb10a5e87c17c8 (plain)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/* Simple Plugin API
 *
 * Copyright © 2018 Wim Taymans
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#ifndef SPA_LOOP_H
#define SPA_LOOP_H

#ifdef __cplusplus
extern "C" {
#endif

#include <spa/utils/defs.h>
#include <spa/utils/hook.h>
#include <spa/support/system.h>

/** \defgroup spa_loop Loop
 * Event loop interface
 */

/**
 * \addtogroup spa_loop
 * \{
 */

#define SPA_TYPE_INTERFACE_Loop		SPA_TYPE_INFO_INTERFACE_BASE "Loop"
#define SPA_TYPE_INTERFACE_DataLoop	SPA_TYPE_INFO_INTERFACE_BASE "DataLoop"
#define SPA_VERSION_LOOP		0
struct spa_loop { struct spa_interface iface; };

#define SPA_TYPE_INTERFACE_LoopControl	SPA_TYPE_INFO_INTERFACE_BASE "LoopControl"
#define SPA_VERSION_LOOP_CONTROL	0
struct spa_loop_control { struct spa_interface iface; };

#define SPA_TYPE_INTERFACE_LoopUtils	SPA_TYPE_INFO_INTERFACE_BASE "LoopUtils"
#define SPA_VERSION_LOOP_UTILS		0
struct spa_loop_utils { struct spa_interface iface; };

struct spa_source;

typedef void (*spa_source_func_t) (struct spa_source *source);

struct spa_source {
	struct spa_loop *loop;
	spa_source_func_t func;
	void *data;
	int fd;
	uint32_t mask;
	uint32_t rmask;
	/* private data for the loop implementer */
	void *priv;
};

typedef int (*spa_invoke_func_t) (struct spa_loop *loop,
				  bool async,
				  uint32_t seq,
				  const void *data,
				  size_t size,
				  void *user_data);

/**
 * Register sources and work items to an event loop
 */
struct spa_loop_methods {
	/* the version of this structure. This can be used to expand this
	 * structure in the future */
#define SPA_VERSION_LOOP_METHODS	0
	uint32_t version;

	/** add a source to the loop */
	int (*add_source) (void *object,
			   struct spa_source *source);

	/** update the source io mask */
	int (*update_source) (void *object,
			struct spa_source *source);

	/** remove a source from the loop */
	int (*remove_source) (void *object,
			struct spa_source *source);

	/** invoke a function in the context of this loop */
	int (*invoke) (void *object,
		       spa_invoke_func_t func,
		       uint32_t seq,
		       const void *data,
		       size_t size,
		       bool block,
		       void *user_data);
};

#define spa_loop_method(o,method,version,...)				\
({									\
	int _res = -ENOTSUP;						\
	struct spa_loop *_o = o;					\
	spa_interface_call_res(&_o->iface,				\
			struct spa_loop_methods, _res,			\
			method, version, ##__VA_ARGS__);		\
	_res;								\
})

#define spa_loop_add_source(l,...)	spa_loop_method(l,add_source,0,##__VA_ARGS__)
#define spa_loop_update_source(l,...)	spa_loop_method(l,update_source,0,##__VA_ARGS__)
#define spa_loop_remove_source(l,...)	spa_loop_method(l,remove_source,0,##__VA_ARGS__)
#define spa_loop_invoke(l,...)		spa_loop_method(l,invoke,0,##__VA_ARGS__)


/** Control hooks. These hooks can't be removed from their
 *  callbacks and must be removed from a safe place (when the loop
 *  is not running or when it is locked). */
struct spa_loop_control_hooks {
#define SPA_VERSION_LOOP_CONTROL_HOOKS	0
	uint32_t version;
	/** Executed right before waiting for events. It is typically used to
	 * release locks. */
	void (*before) (void *data);
	/** Executed right after waiting for events. It is typically used to
	 * reacquire locks. */
	void (*after) (void *data);
};

#define spa_loop_control_hook_before(l)							\
({											\
	struct spa_hook_list *_l = l;							\
	struct spa_hook *_h;								\
	spa_list_for_each_reverse(_h, &_l->list, link)					\
		spa_callbacks_call(&_h->cb, struct spa_loop_control_hooks, before, 0);	\
})

#define spa_loop_control_hook_after(l)							\
({											\
	struct spa_hook_list *_l = l;							\
	struct spa_hook *_h;								\
	spa_list_for_each(_h, &_l->list, link)						\
		spa_callbacks_call(&_h->cb, struct spa_loop_control_hooks, after, 0);	\
})

/**
 * Control an event loop
 */
struct spa_loop_control_methods {
	/* the version of this structure. This can be used to expand this
	 * structure in the future */
#define SPA_VERSION_LOOP_CONTROL_METHODS	0
	uint32_t version;

	int (*get_fd) (void *object);

	/** Add a hook
	 * \param ctrl the control to change
	 * \param hooks the hooks to add
	 *
	 * Adds hooks to the loop controlled by \a ctrl.
	 */
	void (*add_hook) (void *object,
			  struct spa_hook *hook,
			  const struct spa_loop_control_hooks *hooks,
			  void *data);

	/** Enter a loop
	 * \param ctrl the control
	 *
	 * Start an iteration of the loop. This function should be called
	 * before calling iterate and is typically used to capture the thread
	 * that this loop will run in.
	 */
	void (*enter) (void *object);
	/** Leave a loop
	 * \param ctrl the control
	 *
	 * Ends the iteration of a loop. This should be called after calling
	 * iterate.
	 */
	void (*leave) (void *object);

	/** Perform one iteration of the loop.
	 * \param ctrl the control
	 * \param timeout an optional timeout in milliseconds.
	 *	0 for no timeout, -1 for infinite timeout.
	 *
	 * This function will block
	 * up to \a timeout milliseconds and then dispatch the fds with activity.
	 * The number of dispatched fds is returned.
	 */
	int (*iterate) (void *object, int timeout);
};

#define spa_loop_control_method_v(o,method,version,...)			\
({									\
	struct spa_loop_control *_o = o;				\
	spa_interface_call(&_o->iface,					\
			struct spa_loop_control_methods,		\
			method, version, ##__VA_ARGS__);		\
})

#define spa_loop_control_method_r(o,method,version,...)			\
({									\
	int _res = -ENOTSUP;						\
	struct spa_loop_control *_o = o;				\
	spa_interface_call_res(&_o->iface,				\
			struct spa_loop_control_methods, _res,		\
			method, version, ##__VA_ARGS__);		\
	_res;								\
})

#define spa_loop_control_get_fd(l)		spa_loop_control_method_r(l,get_fd,0)
#define spa_loop_control_add_hook(l,...)	spa_loop_control_method_v(l,add_hook,0,__VA_ARGS__)
#define spa_loop_control_enter(l)		spa_loop_control_method_v(l,enter,0)
#define spa_loop_control_leave(l)		spa_loop_control_method_v(l,leave,0)
#define spa_loop_control_iterate(l,...)		spa_loop_control_method_r(l,iterate,0,__VA_ARGS__)

typedef void (*spa_source_io_func_t) (void *data, int fd, uint32_t mask);
typedef void (*spa_source_idle_func_t) (void *data);
typedef void (*spa_source_event_func_t) (void *data, uint64_t count);
typedef void (*spa_source_timer_func_t) (void *data, uint64_t expirations);
typedef void (*spa_source_signal_func_t) (void *data, int signal_number);

/**
 * Create sources for an event loop
 */
struct spa_loop_utils_methods {
	/* the version of this structure. This can be used to expand this
	 * structure in the future */
#define SPA_VERSION_LOOP_UTILS_METHODS	0
	uint32_t version;

	struct spa_source *(*add_io) (void *object,
				      int fd,
				      uint32_t mask,
				      bool close,
				      spa_source_io_func_t func, void *data);

	int (*update_io) (void *object, struct spa_source *source, uint32_t mask);

	struct spa_source *(*add_idle) (void *object,
					bool enabled,
					spa_source_idle_func_t func, void *data);
	int (*enable_idle) (void *object, struct spa_source *source, bool enabled);

	struct spa_source *(*add_event) (void *object,
					 spa_source_event_func_t func, void *data);
	int (*signal_event) (void *object, struct spa_source *source);

	struct spa_source *(*add_timer) (void *object,
					 spa_source_timer_func_t func, void *data);
	int (*update_timer) (void *object,
			     struct spa_source *source,
			     struct timespec *value,
			     struct timespec *interval,
			     bool absolute);
	struct spa_source *(*add_signal) (void *object,
					  int signal_number,
					  spa_source_signal_func_t func, void *data);

	/** destroy a source allocated with this interface. This function
	 * should only be called when the loop is not running or from the
	 * context of the running loop */
	void (*destroy_source) (void *object, struct spa_source *source);
};

#define spa_loop_utils_method_v(o,method,version,...)			\
({									\
	struct spa_loop_utils *_o = o;					\
	spa_interface_call(&_o->iface,					\
			struct spa_loop_utils_methods,			\
			method, version, ##__VA_ARGS__);		\
})

#define spa_loop_utils_method_r(o,method,version,...)			\
({									\
	int _res = -ENOTSUP;						\
	struct spa_loop_utils *_o = o;					\
	spa_interface_call_res(&_o->iface,				\
			struct spa_loop_utils_methods, _res,		\
			method, version, ##__VA_ARGS__);		\
	_res;								\
})
#define spa_loop_utils_method_s(o,method,version,...)			\
({									\
	struct spa_source *_res = NULL;					\
	struct spa_loop_utils *_o = o;					\
	spa_interface_call_res(&_o->iface,				\
			struct spa_loop_utils_methods, _res,		\
			method, version, ##__VA_ARGS__);		\
	_res;								\
})


#define spa_loop_utils_add_io(l,...)		spa_loop_utils_method_s(l,add_io,0,__VA_ARGS__)
#define spa_loop_utils_update_io(l,...)		spa_loop_utils_method_r(l,update_io,0,__VA_ARGS__)
#define spa_loop_utils_add_idle(l,...)		spa_loop_utils_method_s(l,add_idle,0,__VA_ARGS__)
#define spa_loop_utils_enable_idle(l,...)	spa_loop_utils_method_r(l,enable_idle,0,__VA_ARGS__)
#define spa_loop_utils_add_event(l,...)		spa_loop_utils_method_s(l,add_event,0,__VA_ARGS__)
#define spa_loop_utils_signal_event(l,...)	spa_loop_utils_method_r(l,signal_event,0,__VA_ARGS__)
#define spa_loop_utils_add_timer(l,...)		spa_loop_utils_method_s(l,add_timer,0,__VA_ARGS__)
#define spa_loop_utils_update_timer(l,...)	spa_loop_utils_method_r(l,update_timer,0,__VA_ARGS__)
#define spa_loop_utils_add_signal(l,...)	spa_loop_utils_method_s(l,add_signal,0,__VA_ARGS__)
#define spa_loop_utils_destroy_source(l,...)	spa_loop_utils_method_v(l,destroy_source,0,__VA_ARGS__)

/**
 * \}
 */

#ifdef __cplusplus
}  /* extern "C" */
#endif

#endif /* SPA_LOOP_H */