gpoll.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* gpoll.h - poll(2) support
  2. * Copyright (C) 2008 Red Hat, Inc.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Library General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Library General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Library General Public
  15. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #ifndef __G_POLL_H__
  18. #define __G_POLL_H__
  19. #if !defined (__GLIB_H_INSIDE__) && !defined (__G_MAIN_H__) && !defined (GLIB_COMPILATION)
  20. #error "Only <glib.h> can be included directly."
  21. #endif
  22. #include <glib/gtypes.h>
  23. G_BEGIN_DECLS
  24. /* Any definitions using GPollFD or GPollFunc are primarily
  25. * for Unix and not guaranteed to be the compatible on all
  26. * operating systems on which GLib runs. Right now, the
  27. * GLib does use these functions on Win32 as well, but interprets
  28. * them in a fairly different way than on Unix. If you use
  29. * these definitions, you are should be prepared to recode
  30. * for different operating systems.
  31. *
  32. * Note that on systems with a working poll(2), that function is used
  33. * in place of g_poll(). Thus g_poll() must have the same signature as
  34. * poll(), meaning GPollFD must have the same layout as struct pollfd.
  35. *
  36. * On Win32, the fd in a GPollFD should be Win32 HANDLE (*not* a file
  37. * descriptor as provided by the C runtime) that can be used by
  38. * MsgWaitForMultipleObjects. This does *not* include file handles
  39. * from CreateFile, SOCKETs, nor pipe handles. (But you can use
  40. * WSAEventSelect to signal events when a SOCKET is readable).
  41. *
  42. * On Win32, fd can also be the special value G_WIN32_MSG_HANDLE to
  43. * indicate polling for messages.
  44. *
  45. * But note that G_WIN32_MSG_HANDLE GPollFDs should not be used by GDK
  46. * (GTK) programs, as GDK itself wants to read messages and convert them
  47. * to GDK events.
  48. *
  49. * So, unless you really know what you are doing, it's best not to try
  50. * to use the main loop polling stuff for your own needs on
  51. * Windows.
  52. */
  53. typedef struct _GPollFD GPollFD;
  54. /**
  55. * GPollFunc:
  56. * @ufds: an array of #GPollFD elements
  57. * @nfsd: the number of elements in @ufds
  58. * @timeout_: the maximum time to wait for an event of the file descriptors.
  59. * A negative value indicates an infinite timeout.
  60. *
  61. * Specifies the type of function passed to g_main_context_set_poll_func().
  62. * The semantics of the function should match those of the poll() system call.
  63. *
  64. * Returns: the number of #GPollFD elements which have events or errors
  65. * reported, or -1 if an error occurred.
  66. */
  67. typedef gint (*GPollFunc) (GPollFD *ufds,
  68. guint nfsd,
  69. gint timeout_);
  70. /**
  71. * GPollFD:
  72. * @fd: the file descriptor to poll (or a HANDLE on Win32)
  73. * @events: a bitwise combination from #GIOCondition, specifying which
  74. * events should be polled for. Typically for reading from a file
  75. * descriptor you would use %G_IO_IN | %G_IO_HUP | %G_IO_ERR, and
  76. * for writing you would use %G_IO_OUT | %G_IO_ERR.
  77. * @revents: a bitwise combination of flags from #GIOCondition, returned
  78. * from the poll() function to indicate which events occurred.
  79. *
  80. * Represents a file descriptor, which events to poll for, and which events
  81. * occurred.
  82. */
  83. struct _GPollFD
  84. {
  85. #if defined (G_OS_WIN32) && GLIB_SIZEOF_VOID_P == 8
  86. gint64 fd;
  87. #else
  88. gint fd;
  89. #endif
  90. gushort events;
  91. gushort revents;
  92. };
  93. #ifdef G_OS_WIN32
  94. #if GLIB_SIZEOF_VOID_P == 8
  95. #define G_POLLFD_FORMAT "%#I64x"
  96. #else
  97. #define G_POLLFD_FORMAT "%#x"
  98. #endif
  99. #else
  100. #define G_POLLFD_FORMAT "%d"
  101. #endif
  102. GLIB_AVAILABLE_IN_ALL
  103. gint
  104. g_poll (GPollFD *fds,
  105. guint nfds,
  106. gint timeout);
  107. G_END_DECLS
  108. #endif /* __G_POLL_H__ */