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