glib/gpoll W32: pass along GPollFD ptr for msg

Instead of just indicating that messages should be polled for, save the pointer to GPollFD that contains G_WIN32_MSG_HANDLE, and pass it along. This way it won't be necessary to loop through all fds later on, searching for G_WIN32_MSG_HANDLE.

https://bugzilla.gnome.org/show_bug.cgi?id=785468
This commit is contained in:
Руслан Ижбулатов 2017-07-29 08:06:18 +00:00
parent 1f3da929f5
commit 201977983e

View File

@ -129,7 +129,7 @@ g_poll (GPollFD *fds,
#ifdef G_OS_WIN32 #ifdef G_OS_WIN32
static int static int
poll_rest (gboolean poll_msgs, poll_rest (GPollFD *msg_fd,
HANDLE *handles, HANDLE *handles,
gint nhandles, gint nhandles,
GPollFD *fds, GPollFD *fds,
@ -140,7 +140,7 @@ poll_rest (gboolean poll_msgs,
GPollFD *f; GPollFD *f;
int recursed_result; int recursed_result;
if (poll_msgs) if (msg_fd != NULL)
{ {
/* Wait for either messages or handles /* Wait for either messages or handles
* -> Use MsgWaitForMultipleObjectsEx * -> Use MsgWaitForMultipleObjectsEx
@ -192,18 +192,16 @@ poll_rest (gboolean poll_msgs,
ready, ready,
(ready == WAIT_FAILED ? " (WAIT_FAILED)" : (ready == WAIT_FAILED ? " (WAIT_FAILED)" :
(ready == WAIT_TIMEOUT ? " (WAIT_TIMEOUT)" : (ready == WAIT_TIMEOUT ? " (WAIT_TIMEOUT)" :
(poll_msgs && ready == WAIT_OBJECT_0 + nhandles ? " (msg)" : "")))); (msg_fd != NULL && ready == WAIT_OBJECT_0 + nhandles ? " (msg)" : ""))));
if (ready == WAIT_FAILED) if (ready == WAIT_FAILED)
return -1; return -1;
else if (ready == WAIT_TIMEOUT || else if (ready == WAIT_TIMEOUT ||
ready == WAIT_IO_COMPLETION) ready == WAIT_IO_COMPLETION)
return 0; return 0;
else if (poll_msgs && ready == WAIT_OBJECT_0 + nhandles) else if (msg_fd != NULL && ready == WAIT_OBJECT_0 + nhandles)
{ {
for (f = fds; f < &fds[nfds]; ++f) msg_fd->revents |= G_IO_IN;
if (f->fd == G_WIN32_MSG_HANDLE && f->events & G_IO_IN)
f->revents |= G_IO_IN;
/* If we have a timeout, or no handles to poll, be satisfied /* If we have a timeout, or no handles to poll, be satisfied
* with just noticing we have messages waiting. * with just noticing we have messages waiting.
@ -214,7 +212,7 @@ poll_rest (gboolean poll_msgs,
/* If no timeout and handles to poll, recurse to poll them, /* If no timeout and handles to poll, recurse to poll them,
* too. * too.
*/ */
recursed_result = poll_rest (FALSE, handles, nhandles, fds, nfds, 0); recursed_result = poll_rest (NULL, handles, nhandles, fds, nfds, 0);
return (recursed_result == -1) ? -1 : 1 + recursed_result; return (recursed_result == -1) ? -1 : 1 + recursed_result;
} }
else if (ready >= WAIT_OBJECT_0 && ready < WAIT_OBJECT_0 + nhandles) else if (ready >= WAIT_OBJECT_0 && ready < WAIT_OBJECT_0 + nhandles)
@ -239,7 +237,7 @@ poll_rest (gboolean poll_msgs,
for (i = ready - WAIT_OBJECT_0 + 1; i < nhandles; i++) for (i = ready - WAIT_OBJECT_0 + 1; i < nhandles; i++)
handles[i-1] = handles[i]; handles[i-1] = handles[i];
nhandles--; nhandles--;
recursed_result = poll_rest (FALSE, handles, nhandles, fds, nfds, 0); recursed_result = poll_rest (NULL, handles, nhandles, fds, nfds, 0);
return (recursed_result == -1) ? -1 : 1 + recursed_result; return (recursed_result == -1) ? -1 : 1 + recursed_result;
} }
return 1; return 1;
@ -254,7 +252,7 @@ g_poll (GPollFD *fds,
gint timeout) gint timeout)
{ {
HANDLE handles[MAXIMUM_WAIT_OBJECTS]; HANDLE handles[MAXIMUM_WAIT_OBJECTS];
gboolean poll_msgs = FALSE; GPollFD *msg_fd = NULL;
GPollFD *f; GPollFD *f;
gint nhandles = 0; gint nhandles = 0;
int retval; int retval;
@ -266,9 +264,9 @@ g_poll (GPollFD *fds,
{ {
if (f->fd == G_WIN32_MSG_HANDLE && (f->events & G_IO_IN)) if (f->fd == G_WIN32_MSG_HANDLE && (f->events & G_IO_IN))
{ {
if (_g_main_poll_debug && !poll_msgs) if (_g_main_poll_debug && msg_fd == NULL)
g_print (" MSG"); g_print (" MSG");
poll_msgs = TRUE; msg_fd = f;
} }
else if (f->fd > 0) else if (f->fd > 0)
{ {
@ -294,26 +292,26 @@ g_poll (GPollFD *fds,
timeout = INFINITE; timeout = INFINITE;
/* Polling for several things? */ /* Polling for several things? */
if (nhandles > 1 || (nhandles > 0 && poll_msgs)) if (nhandles > 1 || (nhandles > 0 && msg_fd != NULL))
{ {
/* First check if one or several of them are immediately /* First check if one or several of them are immediately
* available * available
*/ */
retval = poll_rest (poll_msgs, handles, nhandles, fds, nfds, 0); retval = poll_rest (msg_fd, handles, nhandles, fds, nfds, 0);
/* If not, and we have a significant timeout, poll again with /* If not, and we have a significant timeout, poll again with
* timeout then. Note that this will return indication for only * timeout then. Note that this will return indication for only
* one event, or only for messages. * one event, or only for messages.
*/ */
if (retval == 0 && (timeout == INFINITE || timeout > 0)) if (retval == 0 && (timeout == INFINITE || timeout > 0))
retval = poll_rest (poll_msgs, handles, nhandles, fds, nfds, timeout); retval = poll_rest (msg_fd, handles, nhandles, fds, nfds, timeout);
} }
else else
{ {
/* Just polling for one thing, so no need to check first if /* Just polling for one thing, so no need to check first if
* available immediately * available immediately
*/ */
retval = poll_rest (poll_msgs, handles, nhandles, fds, nfds, timeout); retval = poll_rest (msg_fd, handles, nhandles, fds, nfds, timeout);
} }
if (retval == -1) if (retval == -1)