VLC  3.0.15
vlc_interrupt.h
Go to the documentation of this file.
1 /*****************************************************************************
2  * vlc_interrupt.h:
3  *****************************************************************************
4  * Copyright (C) 2015 Remlab T:mi
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation; either version 2.1 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19  *****************************************************************************/
20 
21 /**
22  * @file
23  * This file declares interruptible sleep functions.
24  */
25 
26 #ifndef VLC_INTERRUPT_H
27 # define VLC_INTERRUPT_H 1
28 # include <vlc_threads.h>
29 # ifndef _WIN32
30 # include <sys/socket.h> /* socklen_t */
31 # else
32 # include <ws2tcpip.h>
33 # endif
34 
35 struct pollfd;
36 struct iovec;
37 struct sockaddr;
38 struct msghdr;
39 
40 /**
41  * @defgroup interrupt Interruptible sleep
42  * @{
43  * @defgroup interrupt_sleep Interruptible sleep functions
44  * @{
45  */
46 
47 /**
48  * Interruptible variant of vlc_sem_wait().
49  *
50  * Waits on a semaphore like vlc_sem_wait(). If the calling thread has an
51  * interruption context (as set by vlc_interrupt_set()), and another thread
52  * invokes vlc_interrupt_raise() on that context, the semaphore is incremented.
53  *
54  * @warning The calling thread should be the only thread ever to wait on the
55  * specified semaphore. Otherwise, interruptions may not be delivered
56  * accurately (the wrong thread may be woken up).
57  *
58  * @note This function is (always) a cancellation point.
59  *
60  * @return EINTR if the semaphore was incremented due to an interruption,
61  * otherwise zero.
62  */
64 
65 /**
66  * Interruptible variant of mwait().
67  *
68  * Waits for a specified timestamp or, if the calling thread has an
69  * interruption context, an interruption.
70  *
71  * @return EINTR if an interruption occurred, otherwise 0 once the timestamp is
72  * reached.
73  */
75 
76 /**
77  * Interruptible variant of msleep().
78  *
79  * Waits for a specified timeout duration or, if the calling thread has an
80  * interruption context, an interruption.
81  *
82  * @param delay timeout value (in microseconds)
83  *
84  * @return EINTR if an interruption occurred, otherwise 0 once the timeout
85  * expired.
86  */
87 static inline int vlc_msleep_i11e(mtime_t delay)
88 {
89  return vlc_mwait_i11e(mdate() + delay);
90 }
91 
92 /**
93  * Interruptible variant of poll().
94  *
95  * Waits for file descriptors I/O events, a timeout, a signal or a VLC I/O
96  * interruption. Except for VLC I/O interruptions, this function behaves
97  * just like the standard poll().
98  *
99  * @note This function is always a cancellation point (as poll()).
100  * @see poll() manual page
101  *
102  * @param fds table of events to wait for
103  * @param nfds number of entries in the table
104  * @param timeout time to wait in milliseconds or -1 for infinite
105  *
106  * @return A strictly positive result represent the number of pending events.
107  * 0 is returned if the time-out is reached without events.
108  * -1 is returned if a VLC I/O interrupt occurs (and errno is set to EINTR)
109  * or if an error occurs.
110  */
111 VLC_API int vlc_poll_i11e(struct pollfd *, unsigned, int);
112 
113 VLC_API ssize_t vlc_readv_i11e(int fd, struct iovec *, int);
114 VLC_API ssize_t vlc_writev_i11e(int fd, const struct iovec *, int);
115 VLC_API ssize_t vlc_read_i11e(int fd, void *, size_t);
116 VLC_API ssize_t vlc_write_i11e(int fd, const void *, size_t);
117 
118 VLC_API ssize_t vlc_recvmsg_i11e(int fd, struct msghdr *, int flags);
119 VLC_API ssize_t vlc_sendmsg_i11e(int fd, const struct msghdr *, int flags);
120 
121 VLC_API ssize_t vlc_recvfrom_i11e(int fd, void *, size_t, int flags,
122  struct sockaddr *, socklen_t *);
123 VLC_API ssize_t vlc_sendto_i11e(int fd, const void *, size_t, int flags,
124  const struct sockaddr *, socklen_t);
125 
126 static inline ssize_t vlc_recv_i11e(int fd, void *buf, size_t len, int flags)
127 {
128  return vlc_recvfrom_i11e(fd, buf, len, flags, NULL, NULL);
129 }
130 
131 static inline
132 ssize_t vlc_send_i11e(int fd, const void *buf, size_t len, int flags)
133 {
134  return vlc_sendto_i11e(fd, buf, len, flags, NULL, 0);
135 }
136 
137 VLC_API int vlc_accept_i11e(int fd, struct sockaddr *, socklen_t *, bool);
138 
139 /**
140  * Registers a custom interrupt handler.
141  *
142  * Registers a custom callback as interrupt handler for the calling thread.
143  * The callback must be unregistered with vlc_interrupt_unregister() before
144  * thread termination and before any further callback registration.
145  *
146  * If the calling thread has no interruption context, this function has no
147  * effects.
148  */
149 VLC_API void vlc_interrupt_register(void (*cb)(void *), void *opaque);
150 
152 
153 /**
154  * @}
155  * @defgroup interrupt_context Interrupt context signaling and manipulation
156  * @{
157  */
159 
160 /**
161  * Creates an interruption context.
162  */
164 
165 /**
166  * Destroys an interrupt context.
167  */
169 
170 /**
171  * Sets the interruption context for the calling thread.
172  * @param newctx the interruption context to attach or NULL for none
173  * @return the previous interruption context or NULL if none
174  *
175  * @note This function is not a cancellation point.
176  * @warning A context can be attached to no more than one thread at a time.
177  */
179 
180 /**
181  * Raises an interruption through a specified context.
182  *
183  * This is used to asynchronously wake a thread up while it is waiting on some
184  * other events (typically I/O events).
185  *
186  * @note This function is thread-safe.
187  * @note This function is not a cancellation point.
188  */
190 
191 /**
192  * Marks the interruption context as "killed".
193  *
194  * This is not reversible.
195  */
197 
198 /**
199  * Checks if the interruption context was "killed".
200  *
201  * Indicates whether the interruption context of the calling thread (if any)
202  * was killed with vlc_interrupt_kill().
203  */
204 VLC_API bool vlc_killed(void) VLC_USED;
205 
206 /**
207  * Enables forwarding of interruption.
208  *
209  * If an interruption is raised through the context of the calling thread,
210  * it will be forwarded to the specified other context. This is used to cross
211  * thread boundaries.
212  *
213  * If the calling thread has no interrupt context, this function does nothing.
214  *
215  * @param to context to forward to
216  */
218  void *data[2]);
219 
220 /**
221  * Undoes vlc_interrupt_forward_start().
222  *
223  * This function must be called after each successful call to
224  * vlc_interrupt_forward_start() before any other interruptible call is made
225  * in the same thread.
226  *
227  * If an interruption was raised against the context of the calling thread
228  * (after the previous call to vlc_interrupt_forward_start()), it is dequeued.
229  *
230  * If the calling thread has no interrupt context, this function does nothing
231  * and returns zero.
232  *
233  * @return 0 if no interrupt was raised, EINTR if an interrupt was raised
234  */
235 VLC_API int vlc_interrupt_forward_stop(void *const data[2]);
236 
237 /** @} @} */
238 #endif
vlc_recvfrom_i11e
ssize_t vlc_recvfrom_i11e(int fd, void *, size_t, int flags, struct sockaddr *, socklen_t *)
Definition: interrupt.c:485
vlc_send_i11e
static ssize_t vlc_send_i11e(int fd, const void *buf, size_t len, int flags)
Definition: vlc_interrupt.h:132
VLC_API
#define VLC_API
Definition: fourcc_gen.c:30
vlc_interrupt::data
void * data
Definition: interrupt.h:36
vlc_interrupt_raise
void vlc_interrupt_raise(vlc_interrupt_t *)
Raises an interruption through a specified context.
Definition: interrupt.c:84
vlc_common.h
vlc_recvmsg_i11e
ssize_t vlc_recvmsg_i11e(int fd, struct msghdr *, int flags)
Definition: interrupt.c:471
vlc_sendto_i11e
ssize_t vlc_sendto_i11e(int fd, const void *, size_t, int flags, const struct sockaddr *, socklen_t)
Definition: interrupt.c:515
vlc_interrupt_destroy
void vlc_interrupt_destroy(vlc_interrupt_t *)
Destroys an interrupt context.
Definition: interrupt.c:77
pollfd
Definition: vlc_fixups.h:414
vlc_threads.h
vlc_poll_i11e
int vlc_poll_i11e(struct pollfd *, unsigned, int)
Interruptible variant of poll().
Definition: interrupt.c:372
vlc_readv_i11e
ssize_t vlc_readv_i11e(int fd, struct iovec *, int)
Wrapper for readv() that returns the EINTR error upon VLC I/O interruption.
Definition: interrupt.c:417
vlc_killed
bool vlc_killed(void)
Checks if the interruption context was "killed".
Definition: interrupt.c:186
vlc_interrupt_register
void vlc_interrupt_register(void(*cb)(void *), void *opaque)
Registers a custom interrupt handler.
Definition: interrupt.c:160
vlc_interrupt_kill
void vlc_interrupt_kill(vlc_interrupt_t *)
Marks the interruption context as "killed".
Definition: interrupt.c:178
vlc_interrupt_unregister
int vlc_interrupt_unregister(void)
Definition: interrupt.c:167
vlc_interrupt_set
vlc_interrupt_t * vlc_interrupt_set(vlc_interrupt_t *)
Sets the interruption context for the calling thread.
Definition: interrupt.c:99
vlc_interrupt
Definition: interrupt.h:30
vlc_write_i11e
ssize_t vlc_write_i11e(int fd, const void *, size_t)
Wrapper for write() that returns the EINTR error upon VLC I/O interruption.
Definition: interrupt.c:465
vlc_interrupt_create
vlc_interrupt_t * vlc_interrupt_create(void)
Creates an interruption context.
Definition: interrupt.c:59
vlc_accept_i11e
int vlc_accept_i11e(int fd, struct sockaddr *, socklen_t *, bool)
Definition: interrupt.c:529
vlc_msleep_i11e
static int vlc_msleep_i11e(mtime_t delay)
Interruptible variant of msleep().
Definition: vlc_interrupt.h:87
VLC_USED
#define VLC_USED
Definition: fourcc_gen.c:31
vlc_writev_i11e
ssize_t vlc_writev_i11e(int fd, const struct iovec *, int)
Wrapper for writev() that returns the EINTR error upon VLC I/O interruption.
Definition: interrupt.c:436
vlc_interrupt_forward_stop
int vlc_interrupt_forward_stop(void *const data[2])
Undoes vlc_interrupt_forward_start().
Definition: interrupt.c:275
vlc_sem_t
sem_t vlc_sem_t
Semaphore.
Definition: vlc_threads.h:297
mtime_t
int64_t mtime_t
High precision date or time interval.
Definition: vlc_common.h:150
mdate
mtime_t mdate(void)
Precision monotonic clock.
Definition: thread.c:406
vlc_recv_i11e
static ssize_t vlc_recv_i11e(int fd, void *buf, size_t len, int flags)
Definition: vlc_interrupt.h:126
vlc_interrupt_forward_start
void vlc_interrupt_forward_start(vlc_interrupt_t *to, void *data[2])
Enables forwarding of interruption.
Definition: interrupt.c:261
vlc_mwait_i11e
int vlc_mwait_i11e(mtime_t)
Interruptible variant of mwait().
Definition: interrupt.c:228
vlc_sem_wait_i11e
int vlc_sem_wait_i11e(vlc_sem_t *)
Interruptible variant of vlc_sem_wait().
Definition: interrupt.c:198
vlc_read_i11e
ssize_t vlc_read_i11e(int fd, void *, size_t)
Wrapper for read() that returns the EINTR error upon VLC I/O interruption.
Definition: interrupt.c:452
vlc_sendmsg_i11e
ssize_t vlc_sendmsg_i11e(int fd, const struct msghdr *, int flags)
Definition: interrupt.c:502