gpoll.h 4.1 KB

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