Merge branch 'file-i11e' into 'master'

core: interrupt: use the more generic vlc_poll_file_i11e() to check file interruptions

See merge request videolan/vlc!2405
This commit is contained in:
Steve Lhomme 2024-04-28 07:10:58 +00:00
commit 7076ec3cee
7 changed files with 39 additions and 66 deletions

View File

@ -28,7 +28,6 @@
# endif
struct pollfd;
struct iovec;
struct sockaddr;
struct msghdr;
@ -108,10 +107,13 @@ static inline int vlc_msleep_i11e(vlc_tick_t delay)
*/
VLC_API int vlc_poll_i11e(struct pollfd *, unsigned, int);
VLC_API ssize_t vlc_readv_i11e(int fd, struct iovec *, int);
VLC_API ssize_t vlc_writev_i11e(int fd, const struct iovec *, int);
VLC_API ssize_t vlc_read_i11e(int fd, void *, size_t);
VLC_API ssize_t vlc_write_i11e(int fd, const void *, size_t);
/**
* Checks that a file/pipe file descriptor has data to read/write. This function
* sets EINTR errno upon VLC I/O interruption except on Windows.
*
* @warning This function ignores the non-blocking file flag.
*/
VLC_API int vlc_poll_file_i11e(int fd, unsigned int mask);
VLC_API ssize_t vlc_recvmsg_i11e(int fd, struct msghdr *, int flags);
VLC_API ssize_t vlc_sendmsg_i11e(int fd, const struct msghdr *, int flags);

View File

@ -43,6 +43,9 @@
# include <sys/vfs.h>
# include <linux/magic.h>
#endif
#ifdef HAVE_POLL_H
#include <poll.h>
#endif
#if defined( _WIN32 )
# include <io.h>
@ -309,7 +312,12 @@ static ssize_t Read (stream_t *p_access, void *p_buffer, size_t i_len)
access_sys_t *p_sys = p_access->p_sys;
int fd = p_sys->fd;
ssize_t val = vlc_read_i11e (fd, p_buffer, i_len);
ssize_t val;
if (vlc_poll_file_i11e(fd, POLLIN) < 0)
val = -1;
else
val = read(fd, p_buffer, i_len);
if (val < 0)
{
switch (errno)

View File

@ -39,6 +39,9 @@
#include <vlc_plugin.h>
#include <vlc_spawn.h>
#include <vlc_interrupt.h>
#ifdef HAVE_POLL_H
#include <poll.h>
#endif
struct ytdl_json {
struct vlc_logger *logger;
@ -57,7 +60,11 @@ size_t json_read(void *data, void *buf, size_t size)
struct ytdl_json *sys = data;
while (!vlc_killed()) {
ssize_t val = vlc_read_i11e(sys->fd, buf, size);
ssize_t val;
if (vlc_poll_file_i11e(sys->fd, POLLIN) < 0)
val = -1;
else
val = read(sys->fd, buf, size);
if (val >= 0)
return val;

View File

@ -40,6 +40,9 @@
# undef HAVE_VMSPLICE
#endif
#include <vlc_interrupt.h>
#ifdef HAVE_POLL_H
#include <poll.h>
#endif
#include <signal.h>
@ -196,7 +199,11 @@ static void *Thread (void *data)
static ssize_t Read (stream_t *stream, void *buf, size_t buflen)
{
stream_sys_t *sys = stream->p_sys;
ssize_t val = vlc_read_i11e (sys->read_fd, buf, buflen);
ssize_t val;
if (vlc_poll_file_i11e(sys->read_fd, POLLIN) < 0)
val = -1;
else
val = read(sys->read_fd, buf, buflen);
return (val >= 0) ? val : 0;
}

View File

@ -645,10 +645,7 @@ vlc_ml_folder_list_release
vlc_ml_bookmark_release
vlc_ml_bookmark_list_release
vlc_poll_i11e
vlc_read_i11e
vlc_readv_i11e
vlc_write_i11e
vlc_writev_i11e
vlc_poll_file_i11e
vlc_recvmsg_i11e
vlc_recvfrom_i11e
vlc_sendmsg_i11e

View File

@ -417,7 +417,7 @@ int vlc_poll_i11e(struct pollfd *fds, unsigned nfds, int timeout)
return ret;
}
static int vlc_poll_file(int fd, unsigned int mask)
int vlc_poll_file_i11e(int fd, unsigned int mask)
{
struct pollfd ufd;
@ -428,7 +428,7 @@ static int vlc_poll_file(int fd, unsigned int mask)
static int vlc_poll_sock(int sock, unsigned int mask)
{
return vlc_poll_file(sock, mask);
return vlc_poll_file_i11e(sock, mask);
}
#else /* !_WIN32 */
@ -485,7 +485,7 @@ int vlc_poll_i11e(struct pollfd *fds, unsigned nfds, int timeout)
return ret;
}
static int vlc_poll_file(int fd, unsigned int mask)
int vlc_poll_file_i11e(int fd, unsigned int mask)
{
(void) fd; (void) mask;
return 1;
@ -509,54 +509,6 @@ static int vlc_poll_sock(int sock, unsigned int mask)
* block in spite of an interruption. This should never happen in practice.
*/
/**
* Wrapper for readv() that returns the EINTR error upon VLC I/O interruption.
* @warning This function ignores the non-blocking file flag.
*/
ssize_t vlc_readv_i11e(int fd, struct iovec *iov, int count)
{
if (vlc_poll_file(fd, POLLIN) < 0)
return -1;
return readv(fd, iov, count);
}
/**
* Wrapper for writev() that returns the EINTR error upon VLC I/O interruption.
*
* @note Like writev(), once some but not all bytes are written, the function
* might wait for write completion, regardless of signals and interruptions.
* @warning This function ignores the non-blocking file flag.
*/
ssize_t vlc_writev_i11e(int fd, const struct iovec *iov, int count)
{
if (vlc_poll_file(fd, POLLOUT) < 0)
return -1;
return vlc_writev(fd, iov, count);
}
/**
* Wrapper for read() that returns the EINTR error upon VLC I/O interruption.
* @warning This function ignores the non-blocking file flag.
*/
ssize_t vlc_read_i11e(int fd, void *buf, size_t count)
{
struct iovec iov = { .iov_base = buf, .iov_len = count };
return vlc_readv_i11e(fd, &iov, 1);
}
/**
* Wrapper for write() that returns the EINTR error upon VLC I/O interruption.
*
* @note Like write(), once some but not all bytes are written, the function
* might wait for write completion, regardless of signals and interruptions.
* @warning This function ignores the non-blocking file flag.
*/
ssize_t vlc_write_i11e(int fd, const void *buf, size_t count)
{
struct iovec iov = { .iov_base = (void*)buf, .iov_len = count };
return vlc_writev_i11e(fd, &iov, 1);
}
ssize_t vlc_recvmsg_i11e(int fd, struct msghdr *msg, int flags)
{
if (vlc_poll_sock(fd, POLLIN) < 0)

View File

@ -89,11 +89,11 @@ static void test_context_simple(vlc_interrupt_t *ctx)
assert(errno == EINTR);
c = 12;
assert(vlc_write_i11e(fds[0], &c, 1) == 1);
assert(vlc_send_i11e(fds[0], &c, 1, 0) == 1);
c = 0;
assert(vlc_read_i11e(fds[1], &c, 1) == 1 && c == 12);
assert(vlc_recv_i11e(fds[1], &c, 1, 0) == 1 && c == 12);
vlc_interrupt_raise(ctx);
assert(vlc_read_i11e(fds[1], &c, 1) == -1);
assert(vlc_recv_i11e(fds[1], &c, 1, 0) == -1);
assert(errno == EINTR);
c = 42;