gpoll.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 <glibconfig.h>
  23. #include <glib/gtypes.h>
  24. G_BEGIN_DECLS
  25. /* Any definitions using GPollFD or GPollFunc are primarily
  26. * for Unix and not guaranteed to be the compatible on all
  27. * operating systems on which GLib runs. Right now, the
  28. * GLib does use these functions on Win32 as well, but interprets
  29. * them in a fairly different way than on Unix. If you use
  30. * these definitions, you are should be prepared to recode
  31. * for different operating systems.
  32. *
  33. * Note that on systems with a working poll(2), that function is used
  34. * in place of g_poll(). Thus g_poll() must have the same signature as
  35. * poll(), meaning GPollFD must have the same layout as struct pollfd.
  36. *
  37. * On Win32, the fd in a GPollFD should be Win32 HANDLE (*not* a file
  38. * descriptor as provided by the C runtime) that can be used by
  39. * MsgWaitForMultipleObjects. This does *not* include file handles
  40. * from CreateFile, SOCKETs, nor pipe handles. (But you can use
  41. * WSAEventSelect to signal events when a SOCKET is readable).
  42. *
  43. * On Win32, fd can also be the special value G_WIN32_MSG_HANDLE to
  44. * indicate polling for messages.
  45. *
  46. * But note that G_WIN32_MSG_HANDLE GPollFDs should not be used by GDK
  47. * (GTK) programs, as GDK itself wants to read messages and convert them
  48. * to GDK events.
  49. *
  50. * So, unless you really know what you are doing, it's best not to try
  51. * to use the main loop polling stuff for your own needs on
  52. * Windows.
  53. */
  54. typedef struct _GPollFD GPollFD;
  55. /**
  56. * GPollFunc:
  57. * @ufds: an array of #GPollFD elements
  58. * @nfsd: the number of elements in @ufds
  59. * @timeout_: the maximum time to wait for an event of the file descriptors.
  60. * A negative value indicates an infinite timeout.
  61. *
  62. * Specifies the type of function passed to g_main_context_set_poll_func().
  63. * The semantics of the function should match those of the poll() system call.
  64. *
  65. * Returns: the number of #GPollFD elements which have events or errors
  66. * reported, or -1 if an error occurred.
  67. */
  68. typedef gint (*GPollFunc) (GPollFD *ufds,
  69. guint nfsd,
  70. gint timeout_);
  71. /**
  72. * GPollFD:
  73. * @fd: the file descriptor to poll (or a HANDLE on Win32)
  74. * @events: a bitwise combination from #GIOCondition, specifying which
  75. * events should be polled for. Typically for reading from a file
  76. * descriptor you would use %G_IO_IN | %G_IO_HUP | %G_IO_ERR, and
  77. * for writing you would use %G_IO_OUT | %G_IO_ERR.
  78. * @revents: a bitwise combination of flags from #GIOCondition, returned
  79. * from the poll() function to indicate which events occurred.
  80. *
  81. * Represents a file descriptor, which events to poll for, and which events
  82. * occurred.
  83. */
  84. struct _GPollFD
  85. {
  86. #if defined (G_OS_WIN32) && GLIB_SIZEOF_VOID_P == 8
  87. #ifndef __GTK_DOC_IGNORE__
  88. gint64 fd;
  89. #endif
  90. #else
  91. gint fd;
  92. #endif
  93. gushort events;
  94. gushort revents;
  95. };
  96. /**
  97. * G_POLLFD_FORMAT:
  98. *
  99. * A format specifier that can be used in printf()-style format strings
  100. * when printing the @fd member of a #GPollFD.
  101. */
  102. /* defined in glibconfig.h */
  103. GLIB_AVAILABLE_IN_ALL
  104. gint
  105. g_poll (GPollFD *fds,
  106. guint nfds,
  107. gint timeout);
  108. G_END_DECLS
  109. #endif /* __G_POLL_H__ */