13
0

Fix poll() edge case on macOS, clean up logic

Return from poll_for_request() when CrossThreadChannel is
closed/destroyed. -- see also f4166fb61d

This also cleans up poll API usage, and check for nonnegative
return value is added.
This commit is contained in:
Robin Gareus 2022-04-23 22:18:55 +02:00
parent 2be95cf197
commit c1d6c0690e
Signed by: rgareus
GPG Key ID: A090BCE02CF57F04

View File

@ -1,6 +1,6 @@
/* /*
* Copyright (C) 2014-2016 Paul Davis <paul@linuxaudiosystems.com> * Copyright (C) 2014-2016 Paul Davis <paul@linuxaudiosystems.com>
* Copyright (C) 2015-2016 Robin Gareus <robin@gareus.org> * Copyright (C) 2015-2022 Robin Gareus <robin@gareus.org>
* *
* This program is free software; you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -95,21 +95,31 @@ CrossThreadChannel::deliver (char msg)
bool bool
CrossThreadChannel::poll_for_request() CrossThreadChannel::poll_for_request()
{ {
struct pollfd pfd[1]; struct pollfd pfd;
pfd[0].fd = fds[0]; pfd.fd = fds[0];
pfd[0].events = POLLIN|POLLERR|POLLHUP; pfd.events = POLLIN|POLLERR|POLLHUP|POLLNVAL;
while(true) { while(true) {
if (poll (pfd, 1, -1) < 0) { #ifdef __APPLE__
/* on macOS poll() will not return when the pipe
* is closed in an EOF state. ork around with a timeout.
*/
int rv = poll (&pfd, 1, 1000);
#else
int rv = poll (&pfd, 1, -1);
#endif
if (rv == -1) {
/* error */
if (errno == EINTR) { if (errno == EINTR) {
continue; continue;
} }
break; break;
} }
if (pfd[0].revents & ~POLLIN) {
if (pfd.revents & ~POLLIN) {
break; break;
} }
if (pfd[0].revents & POLLIN) { if (rv > 0 && pfd.revents & POLLIN) {
return true; return true;
} }
} }