summaryrefslogtreecommitdiff
path: root/include/runloop.h
blob: ea840e730c77c236484d23cc40c86e2883d435a7 (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
/*
 * liburtc
 * Copyright (C) 2019 Chris Hiszpanski
 * 
 * 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 3 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, see <https://www.gnu.org/licenses/>.
 */

/**
 * @file runloop.h
 * Event loop
 * A more elaborate file description.
 */

#ifndef URTC_RUNLOOP_H
#define URTC_RUNLOOP_H

#ifdef __cplusplus
extern "C" {
#endif

#include <poll.h>
#include <pthread.h>
#include <stdbool.h>

typedef void *(*callback_t)(int fd, void *arg);

typedef struct {
	// File descriptor sets for poll().
	struct pollfd *fds;
	nfds_t nfds;

	// Callback array. Index into array is file descriptor.
	callback_t *callbacks;
	void **args;

	// Pipe for aborting blocked poll(). Allows dynamic adds/removes.
	int abortpipe[2];
	bool done;

	// Run loop thread ID
	pthread_t tid;
} runloop_t;

/**
 * Create a new run loop
 *
 * Each run loop creates a separate thread which then repeatedly poll()s
 * for events. Upon receiving an event, the run loop calls the respective
 * file descriptor's callback function.
 *
 * @param[out] rl  Pointer to run loop struct
 */
int urtc__runloop_create(runloop_t *rl);

/**
 * Add file descriptor to run loop
 *
 * @param rl      Run loop
 * @param fd      File descriptor
 * @param events  Events to monitor (see struct pollfd)
 * @param cb      Callback to run on activity
 * @param arg     User-specified argument to pass to callback
 *
 * @return Zero on success. Negative on error.
 */
int urtc__runloop_add(
	runloop_t *rl,
	int fd,
	short events,
	void *(*cb)(int fd, void *arg),
	void *arg
);

/**
 * Remove a file descriptor from the run loop. No more events will be
 * processed for this file descriptor.
 *
 * @param rl Run loop.
 * @param fd File descriptor.
 *
 * @return Zero on success. Negative on error.
 */
int urtc__runloop_remove(runloop_t *rl, int fd);

int urtc__runloop_join(runloop_t *rl);

/**
 * Destroy a run loop
 *
 * @param rl Pointer to run loop object
 *
 * @return Zero on success; negative on error. On error, errno is set.
 */
int urtc__runloop_destroy(runloop_t *rl);

#ifdef __cplusplus
}
#endif

#endif /* URTC_RUNLOOP_H */