Simplify GCancellable support on win32

There is no need to have a GIOChannel in the GPollFD in
g_cancellable_create_pollfd. All we need is an Event object that
we signal when cancelling and reset when resetting.

Also, supporting g_cancellable_get_fd on Windows using _pipe is useless
as it doesn't work with any corresponding poll() function, so just don't
support that on win32.

I tested this with the cancellation support in GSocket from gnio.
This commit is contained in:
Alexander Larsson 2009-05-06 13:10:58 +02:00
parent 2fff3026ef
commit e10edefff1

View File

@ -27,10 +27,7 @@
#include <fcntl.h> #include <fcntl.h>
#include <gioerror.h> #include <gioerror.h>
#ifdef G_OS_WIN32 #ifdef G_OS_WIN32
#include <io.h> #include <windows.h>
#ifndef pipe
#define pipe(fds) _pipe(fds, 4096, _O_BINARY)
#endif
#endif #endif
#include "gcancellable.h" #include "gcancellable.h"
#include "glibintl.h" #include "glibintl.h"
@ -63,7 +60,7 @@ struct _GCancellable
int cancel_pipe[2]; int cancel_pipe[2];
#ifdef G_OS_WIN32 #ifdef G_OS_WIN32
GIOChannel *read_channel; HANDLE event;
#endif #endif
}; };
@ -87,8 +84,8 @@ g_cancellable_finalize (GObject *object)
close (cancellable->cancel_pipe[1]); close (cancellable->cancel_pipe[1]);
#ifdef G_OS_WIN32 #ifdef G_OS_WIN32
if (cancellable->read_channel) if (cancellable->event)
g_io_channel_unref (cancellable->read_channel); CloseHandle (cancellable->event);
#endif #endif
G_OBJECT_CLASS (g_cancellable_parent_class)->finalize (object); G_OBJECT_CLASS (g_cancellable_parent_class)->finalize (object);
@ -173,6 +170,7 @@ g_cancellable_class_init (GCancellableClass *klass)
} }
#ifndef G_OS_WIN32
static void static void
set_fd_nonblocking (int fd) set_fd_nonblocking (int fd)
{ {
@ -204,6 +202,7 @@ g_cancellable_open_pipe (GCancellable *cancellable)
else else
g_warning ("Failed to create pipe for GCancellable. Out of file descriptors?"); g_warning ("Failed to create pipe for GCancellable. Out of file descriptors?");
} }
#endif
static void static void
g_cancellable_init (GCancellable *cancellable) g_cancellable_init (GCancellable *cancellable)
@ -318,19 +317,15 @@ g_cancellable_reset (GCancellable *cancellable)
return; return;
} }
if (!cancellable->cancelled) if (cancellable->cancelled)
{ {
char ch; char ch;
/* Make sure we're not leaving old cancel state around */ /* Make sure we're not leaving old cancel state around */
#ifdef G_OS_WIN32 #ifdef G_OS_WIN32
if (cancellable->read_channel) if (cancellable->event)
{ ResetEvent (cancellable->event);
gsize bytes_read;
g_io_channel_read_chars (cancellable->read_channel, &ch, 1,
&bytes_read, NULL);
}
else else
#endif #endif
if (cancellable->cancel_pipe[0] != -1) if (cancellable->cancel_pipe[0] != -1)
@ -404,17 +399,21 @@ g_cancellable_get_fd (GCancellable *cancellable)
int fd; int fd;
if (cancellable == NULL) if (cancellable == NULL)
return -1; return -1;
#ifdef G_OS_WIN32
return -1;
#else
G_LOCK(cancellable); G_LOCK(cancellable);
if (!cancellable->allocated_pipe) if (!cancellable->allocated_pipe)
{ {
cancellable->allocated_pipe = TRUE; cancellable->allocated_pipe = TRUE;
g_cancellable_open_pipe (cancellable); g_cancellable_open_pipe (cancellable);
} }
fd = cancellable->cancel_pipe[0]; fd = cancellable->cancel_pipe[0];
G_UNLOCK(cancellable); G_UNLOCK(cancellable);
#endif
return fd; return fd;
} }
@ -424,7 +423,9 @@ g_cancellable_get_fd (GCancellable *cancellable)
* @pollfd: a pointer to a #GPollFD * @pollfd: a pointer to a #GPollFD
* *
* Creates a #GPollFD corresponding to @cancellable; this can be passed * Creates a #GPollFD corresponding to @cancellable; this can be passed
* to g_poll() and used to poll for cancellation. * to g_poll() and used to poll for cancellation. This is useful both
* for unix systems without a native poll and for portability to
* windows.
* *
* You are not supposed to read from the fd yourself, just check for * You are not supposed to read from the fd yourself, just check for
* readable status. Reading to unset the readable status is done * readable status. Reading to unset the readable status is done
@ -438,23 +439,16 @@ g_cancellable_make_pollfd (GCancellable *cancellable, GPollFD *pollfd)
g_return_if_fail (pollfd != NULL); g_return_if_fail (pollfd != NULL);
#ifdef G_OS_WIN32 #ifdef G_OS_WIN32
if (!cancellable->read_channel) if (!cancellable->event)
{ {
int fd = g_cancellable_get_fd (cancellable); /* A manual reset anonymous event, starting unset */
cancellable->read_channel = g_io_channel_win32_new_fd (fd); cancellable->event = CreateEvent (NULL, TRUE, FALSE, NULL);
g_io_channel_set_buffered (cancellable->read_channel, FALSE);
g_io_channel_set_flags (cancellable->read_channel,
G_IO_FLAG_NONBLOCK, NULL);
g_io_channel_set_encoding (cancellable->read_channel, NULL, NULL);
} }
g_io_channel_win32_make_pollfd (cancellable->read_channel, G_IO_IN, pollfd); pollfd->fd = (gintptr)cancellable->event;
/* (We need to keep cancellable->read_channel around, because it's
* keeping track of state related to the pollfd.)
*/
#else /* !G_OS_WIN32 */ #else /* !G_OS_WIN32 */
pollfd->fd = g_cancellable_get_fd (cancellable); pollfd->fd = g_cancellable_get_fd (cancellable);
pollfd->events = G_IO_IN;
#endif /* G_OS_WIN32 */ #endif /* G_OS_WIN32 */
pollfd->events = G_IO_IN;
pollfd->revents = 0; pollfd->revents = 0;
} }
@ -492,6 +486,8 @@ g_cancellable_cancel (GCancellable *cancellable)
cancel = TRUE; cancel = TRUE;
cancellable->cancelled = TRUE; cancellable->cancelled = TRUE;
cancellable->cancelled_running = TRUE; cancellable->cancelled_running = TRUE;
if (cancellable->event)
SetEvent(cancellable->event);
if (cancellable->cancel_pipe[1] != -1) if (cancellable->cancel_pipe[1] != -1)
write (cancellable->cancel_pipe[1], &ch, 1); write (cancellable->cancel_pipe[1], &ch, 1);
} }