giomodule test: force shared library build
[platform/upstream/glib.git] / gio / gasynchelper.c
1 /* GIO - GLib Input, Output and Streaming Library
2  * 
3  * Copyright (C) 2006-2007 Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Author: Alexander Larsson <alexl@redhat.com>
21  */
22
23 #include "config.h"
24
25 #include "gasynchelper.h"
26
27
28 /*< private >
29  * SECTION:gasynchelper
30  * @short_description: Asynchronous Helper Functions
31  * @include: gio/gio.h
32  * @see_also: #GAsyncResult
33  * 
34  * Provides helper functions for asynchronous operations.
35  *
36  **/
37
38 #ifdef G_OS_WIN32
39 gboolean
40 _g_win32_overlap_wait_result (HANDLE           hfile,
41                               OVERLAPPED      *overlap,
42                               DWORD           *transferred,
43                               GCancellable    *cancellable)
44 {
45   GPollFD pollfd[2];
46   gboolean result = FALSE;
47   gint num, npoll;
48
49   pollfd[0].fd = (gint)overlap->hEvent;
50   pollfd[0].events = G_IO_IN;
51   num = 1;
52
53   if (g_cancellable_make_pollfd (cancellable, &pollfd[1]))
54     num++;
55
56 loop:
57   npoll = g_poll (pollfd, num, -1);
58   if (npoll <= 0)
59     /* error out, should never happen */
60     goto end;
61
62   if (g_cancellable_is_cancelled (cancellable))
63     {
64       /* CancelIO only cancels pending operations issued by the
65        * current thread and since we're doing only sync operations,
66        * this is safe.... */
67       /* CancelIoEx is only Vista+. Since we have only one overlap
68        * operaton on this thread, we can just use: */
69       result = CancelIo (hfile);
70       g_warn_if_fail (result);
71     }
72
73   result = GetOverlappedResult (overlap->hEvent, overlap, transferred, FALSE);
74   if (result == FALSE &&
75       GetLastError () == ERROR_IO_INCOMPLETE &&
76       !g_cancellable_is_cancelled (cancellable))
77     goto loop;
78
79 end:
80   if (num > 1)
81     g_cancellable_release_fd (cancellable);
82
83   return result;
84 }
85 #endif