gasynchelper.c 2.42 KB
Newer Older
1 2 3 4 5 6 7
/* GIO - GLib Input, Output and Streaming Library
 * 
 * Copyright (C) 2006-2007 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
8
 * version 2.1 of the License, or (at your option) any later version.
9 10 11 12 13 14 15
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
16
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 18 19 20
 *
 * Author: Alexander Larsson <alexl@redhat.com>
 */

21
#include "config.h"
22 23 24

#include "gasynchelper.h"

25

26
/*< private >
27 28
 * SECTION:gasynchelper
 * @short_description: Asynchronous Helper Functions
Matthias Clasen's avatar
Matthias Clasen committed
29
 * @include: gio/gio.h
30
 * @see_also: #GAsyncResult
31 32 33 34 35
 * 
 * Provides helper functions for asynchronous operations.
 *
 **/

36 37 38 39 40
#ifdef G_OS_WIN32
gboolean
_g_win32_overlap_wait_result (HANDLE           hfile,
                              OVERLAPPED      *overlap,
                              DWORD           *transferred,
41
                              GCancellable    *cancellable)
42
{
43
  GPollFD pollfd[2];
44 45 46
  gboolean result = FALSE;
  gint num, npoll;

47 48 49
#if GLIB_SIZEOF_VOID_P == 8
  pollfd[0].fd = (gint64)overlap->hEvent;
#else
50
  pollfd[0].fd = (gint)overlap->hEvent;
51
#endif
52 53 54
  pollfd[0].events = G_IO_IN;
  num = 1;

55 56 57 58
  if (g_cancellable_make_pollfd (cancellable, &pollfd[1]))
    num++;

loop:
59 60 61 62 63
  npoll = g_poll (pollfd, num, -1);
  if (npoll <= 0)
    /* error out, should never happen */
    goto end;

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
  if (g_cancellable_is_cancelled (cancellable))
    {
      /* CancelIO only cancels pending operations issued by the
       * current thread and since we're doing only sync operations,
       * this is safe.... */
      /* CancelIoEx is only Vista+. Since we have only one overlap
       * operaton on this thread, we can just use: */
      result = CancelIo (hfile);
      g_warn_if_fail (result);
    }

  result = GetOverlappedResult (overlap->hEvent, overlap, transferred, FALSE);
  if (result == FALSE &&
      GetLastError () == ERROR_IO_INCOMPLETE &&
      !g_cancellable_is_cancelled (cancellable))
    goto loop;
80 81

end:
82 83 84
  if (num > 1)
    g_cancellable_release_fd (cancellable);

85 86 87
  return result;
}
#endif