2 * Copyright (C) 1999 Erik Walthinsen <omega@cse.ogi.edu>
3 * Copyright (C) 2004 Wim Taymans <wim.taymans@gmail.com>
4 * Copyright (C) 2007 Peter Kjellerstedt <pkj@axis.com>
5 * Copyright (C) 2008 Ole André Vadla Ravnås <ole.andre.ravnas@tandberg.com>
7 * gstpoll.c: File descriptor set
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
26 * @short_description: Keep track of file descriptors and make it possible
27 * to wait on them in a cancellable way
29 * A #GstPoll keeps track of file descriptors much like fd_set (used with
30 * select()) or a struct pollfd array (used with poll()). Once created with
31 * gst_poll_new(), the set can be used to wait for file descriptors to be
32 * readable and/or writable. It is possible to make this wait be controlled
33 * by specifying %TRUE for the @controllable flag when creating the set (or
34 * later calling gst_poll_set_controllable()).
36 * New file descriptors are added to the set using gst_poll_add_fd(), and
37 * removed using gst_poll_remove_fd(). Controlling which file descriptors
38 * should be waited for to become readable and/or writable are done using
39 * gst_poll_fd_ctl_read() and gst_poll_fd_ctl_write().
41 * Use gst_poll_wait() to wait for the file descriptors to actually become
42 * readable and/or writable, or to timeout if no file descriptor is available
43 * in time. The wait can be controlled by calling gst_poll_restart() and
44 * gst_poll_set_flushing().
46 * Once the file descriptor set has been waited for, one can use
47 * gst_poll_fd_has_closed() to see if the file descriptor has been closed,
48 * gst_poll_fd_has_error() to see if it has generated an error,
49 * gst_poll_fd_can_read() to see if it is possible to read from the file
50 * descriptor, and gst_poll_fd_can_write() to see if it is possible to
59 #include "gst_private.h"
60 #include "glib-compat-private.h"
62 #include <sys/types.h>
77 #ifdef HAVE_SYS_POLL_H
84 #include <sys/socket.h>
89 # define EWOULDBLOCK EAGAIN /* This is just to placate gcc */
91 #endif /* G_OS_WIN32 */
93 /* OS/X needs this because of bad headers */
96 /* The poll() emulation on OS/X doesn't handle fds=NULL, nfds=0,
97 * so we prefer our own poll emulation.
99 #if defined(BROKEN_POLL)
105 #define GST_CAT_DEFAULT GST_CAT_POLL
108 typedef struct _WinsockFd WinsockFd;
114 WSANETWORKEVENTS events;
115 glong ignored_event_mask;
122 GST_POLL_MODE_SELECT,
123 GST_POLL_MODE_PSELECT,
126 GST_POLL_MODE_WINDOWS
134 /* array of fds, always written to and read from with lock */
136 /* array of active fds, only written to from the waiting thread with the
137 * lock and read from with the lock or without the lock from the waiting
142 GstPollFD control_read_fd;
143 GstPollFD control_write_fd;
145 GArray *active_fds_ignored;
147 GArray *active_events;
152 gboolean controllable;
153 volatile gint waiting;
154 volatile gint control_pending;
155 volatile gint flushing;
157 volatile gint rebuild;
160 static gboolean gst_poll_fd_ctl_read_unlocked (GstPoll * set, GstPollFD * fd,
162 static gboolean gst_poll_add_fd_unlocked (GstPoll * set, GstPollFD * fd);
164 #define IS_FLUSHING(s) (g_atomic_int_get(&(s)->flushing))
165 #define SET_FLUSHING(s,val) (g_atomic_int_set(&(s)->flushing, (val)))
167 #define INC_WAITING(s) (g_atomic_int_add(&(s)->waiting, 1))
168 #define DEC_WAITING(s) (g_atomic_int_add(&(s)->waiting, -1))
169 #define GET_WAITING(s) (g_atomic_int_get(&(s)->waiting))
171 #define TEST_REBUILD(s) (g_atomic_int_compare_and_exchange(&(s)->rebuild, 1, 0))
172 #define MARK_REBUILD(s) (g_atomic_int_set(&(s)->rebuild, 1))
177 wake_event (GstPoll * set)
180 while ((num_written = write (set->control_write_fd.fd, "W", 1)) != 1) {
181 if (num_written == -1 && errno != EAGAIN && errno != EINTR) {
182 g_critical ("%p: failed to wake event: %s", set, strerror (errno));
190 release_event (GstPoll * set)
192 gchar buf[1] = { '\0' };
194 while ((num_read = read (set->control_read_fd.fd, buf, 1)) != 1) {
195 if (num_read == -1 && errno != EAGAIN && errno != EINTR) {
196 g_critical ("%p: failed to release event: %s", set, strerror (errno));
206 format_last_error (gchar * buf, size_t buf_len)
208 DWORD flags = FORMAT_MESSAGE_FROM_SYSTEM;
212 id = GetLastError ();
213 FormatMessage (flags, src, id, lang, buf, (DWORD) buf_len, NULL);
218 wake_event (GstPoll * set)
222 if (!SetEvent (set->wakeup_event)) {
223 gchar msg[1024] = "<unknown>";
224 format_last_error (msg, sizeof (msg));
225 g_critical ("%p: failed to set wakup_event: %s", set, msg);
234 release_event (GstPoll * set)
240 status = WaitForSingleObject (set->wakeup_event, INFINITE);
242 const gchar *reason = "unknown";
243 gchar msg[1024] = "<unknown>";
246 reason = "WAIT_ABANDONED";
249 reason = "WAIT_TIMEOUT";
252 format_last_error (msg, sizeof (msg));
259 g_critical ("%p: failed to block on wakup_event: %s", set, reason);
264 if (!ResetEvent (set->wakeup_event)) {
265 gchar msg[1024] = "<unknown>";
266 format_last_error (msg, sizeof (msg));
267 g_critical ("%p: failed to reset wakup_event: %s", set, msg);
277 /* the poll/select call is also performed on a control socket, that way
278 * we can send special commands to control it */
279 static inline gboolean
280 raise_wakeup (GstPoll * set)
282 gboolean result = TRUE;
284 /* makes testing control_pending and WAKE_EVENT() atomic. */
285 g_mutex_lock (&set->lock);
287 if (set->control_pending == 0) {
288 /* raise when nothing pending */
289 GST_LOG ("%p: raise", set);
290 result = wake_event (set);
294 set->control_pending++;
297 g_mutex_unlock (&set->lock);
302 static inline gboolean
303 release_wakeup (GstPoll * set)
305 gboolean result = FALSE;
307 /* makes testing/modifying control_pending and RELEASE_EVENT() atomic. */
308 g_mutex_lock (&set->lock);
310 if (set->control_pending > 0) {
311 /* release, only if this was the last pending. */
312 if (set->control_pending == 1) {
313 GST_LOG ("%p: release", set);
314 result = release_event (set);
320 set->control_pending--;
326 g_mutex_unlock (&set->lock);
332 release_all_wakeup (GstPoll * set)
336 /* makes testing control_pending and RELEASE_EVENT() atomic. */
337 g_mutex_lock (&set->lock);
339 if ((old = set->control_pending) > 0) {
340 GST_LOG ("%p: releasing %d", set, old);
341 if (release_event (set)) {
342 set->control_pending = 0;
348 g_mutex_unlock (&set->lock);
354 find_index (GArray * array, GstPollFD * fd)
363 /* start by assuming the index found in the fd is still valid */
364 if (fd->idx >= 0 && fd->idx < array->len) {
366 ifd = &g_array_index (array, struct pollfd, fd->idx);
368 ifd = &g_array_index (array, WinsockFd, fd->idx);
371 if (ifd->fd == fd->fd) {
376 /* the pollfd array has changed and we need to lookup the fd again */
377 for (i = 0; i < array->len; i++) {
379 ifd = &g_array_index (array, struct pollfd, i);
381 ifd = &g_array_index (array, WinsockFd, i);
384 if (ifd->fd == fd->fd) {
394 #if !defined(HAVE_PPOLL) && defined(HAVE_POLL)
395 /* check if all file descriptors will fit in an fd_set */
397 selectable_fds (GstPoll * set)
401 g_mutex_lock (&set->lock);
402 for (i = 0; i < set->fds->len; i++) {
403 struct pollfd *pfd = &g_array_index (set->fds, struct pollfd, i);
405 if (pfd->fd >= FD_SETSIZE)
408 g_mutex_unlock (&set->lock);
414 g_mutex_unlock (&set->lock);
419 /* check if the timeout will convert to a timeout value used for poll()
420 * without a loss of precision
423 pollable_timeout (GstClockTime timeout)
425 if (timeout == GST_CLOCK_TIME_NONE)
428 /* not a nice multiple of milliseconds */
429 if (timeout % 1000000)
437 choose_mode (GstPoll * set, GstClockTime timeout)
441 if (set->mode == GST_POLL_MODE_AUTO) {
443 mode = GST_POLL_MODE_PPOLL;
444 #elif defined(HAVE_POLL)
445 if (!selectable_fds (set) || pollable_timeout (timeout)) {
446 mode = GST_POLL_MODE_POLL;
449 mode = GST_POLL_MODE_PSELECT;
451 mode = GST_POLL_MODE_SELECT;
454 #elif defined(HAVE_PSELECT)
455 mode = GST_POLL_MODE_PSELECT;
457 mode = GST_POLL_MODE_SELECT;
467 pollfd_to_fd_set (GstPoll * set, fd_set * readfds, fd_set * writefds,
477 g_mutex_lock (&set->lock);
479 for (i = 0; i < set->active_fds->len; i++) {
480 struct pollfd *pfd = &g_array_index (set->fds, struct pollfd, i);
482 if (pfd->fd < FD_SETSIZE) {
483 if (pfd->events & POLLIN)
484 FD_SET (pfd->fd, readfds);
485 if (pfd->events & POLLOUT)
486 FD_SET (pfd->fd, writefds);
488 FD_SET (pfd->fd, errorfds);
489 if (pfd->fd > max_fd && (pfd->events & (POLLIN | POLLOUT)))
494 g_mutex_unlock (&set->lock);
500 fd_set_to_pollfd (GstPoll * set, fd_set * readfds, fd_set * writefds,
505 g_mutex_lock (&set->lock);
507 for (i = 0; i < set->active_fds->len; i++) {
508 struct pollfd *pfd = &g_array_index (set->active_fds, struct pollfd, i);
510 if (pfd->fd < FD_SETSIZE) {
512 if (FD_ISSET (pfd->fd, readfds))
513 pfd->revents |= POLLIN;
514 if (FD_ISSET (pfd->fd, writefds))
515 pfd->revents |= POLLOUT;
516 if (FD_ISSET (pfd->fd, errorfds))
517 pfd->revents |= POLLERR;
521 g_mutex_unlock (&set->lock);
523 #else /* G_OS_WIN32 */
525 * Translate errors thrown by the Winsock API used by GstPoll:
526 * WSAEventSelect, WSAWaitForMultipleEvents and WSAEnumNetworkEvents
529 gst_poll_winsock_error_to_errno (DWORD last_error)
531 switch (last_error) {
532 case WSA_INVALID_HANDLE:
537 case WSA_NOT_ENOUGH_MEMORY:
541 * Anything else, including:
542 * WSA_INVALID_PARAMETER, WSAEFAULT, WSAEINPROGRESS, WSAENETDOWN,
551 gst_poll_free_winsock_event (GstPoll * set, gint idx)
553 WinsockFd *wfd = &g_array_index (set->fds, WinsockFd, idx);
554 HANDLE event = g_array_index (set->events, HANDLE, idx);
556 WSAEventSelect (wfd->fd, event, 0);
561 gst_poll_update_winsock_event_mask (GstPoll * set, gint idx, glong flags,
566 wfd = &g_array_index (set->fds, WinsockFd, idx);
569 wfd->event_mask |= flags;
571 wfd->event_mask &= ~flags;
573 /* reset ignored state if the new mask doesn't overlap at all */
574 if ((wfd->ignored_event_mask & wfd->event_mask) == 0)
575 wfd->ignored_event_mask = 0;
579 gst_poll_prepare_winsock_active_sets (GstPoll * set)
583 g_array_set_size (set->active_fds, 0);
584 g_array_set_size (set->active_fds_ignored, 0);
585 g_array_set_size (set->active_events, 0);
586 g_array_append_val (set->active_events, set->wakeup_event);
588 for (i = 0; i < set->fds->len; i++) {
589 WinsockFd *wfd = &g_array_index (set->fds, WinsockFd, i);
590 HANDLE event = g_array_index (set->events, HANDLE, i);
592 if (wfd->ignored_event_mask == 0) {
595 g_array_append_val (set->active_fds, *wfd);
596 g_array_append_val (set->active_events, event);
598 ret = WSAEventSelect (wfd->fd, event, wfd->event_mask);
599 if (G_UNLIKELY (ret != 0)) {
600 errno = gst_poll_winsock_error_to_errno (WSAGetLastError ());
604 g_array_append_val (set->active_fds_ignored, wfd);
612 gst_poll_collect_winsock_events (GstPoll * set)
617 * We need to check which events are signaled, and call
618 * WSAEnumNetworkEvents for those that are, which resets
619 * the event and clears the internal network event records.
622 for (i = 0; i < set->active_fds->len; i++) {
623 WinsockFd *wfd = &g_array_index (set->active_fds, WinsockFd, i);
624 HANDLE event = g_array_index (set->active_events, HANDLE, i + 1);
627 wait_ret = WaitForSingleObject (event, 0);
628 if (wait_ret == WAIT_OBJECT_0) {
629 gint enum_ret = WSAEnumNetworkEvents (wfd->fd, event, &wfd->events);
631 if (G_UNLIKELY (enum_ret != 0)) {
633 errno = gst_poll_winsock_error_to_errno (WSAGetLastError ());
639 /* clear any previously stored result */
640 memset (&wfd->events, 0, sizeof (wfd->events));
644 /* If all went well we also need to reset the ignored fds. */
646 res += set->active_fds_ignored->len;
648 for (i = 0; i < set->active_fds_ignored->len; i++) {
649 WinsockFd *wfd = g_array_index (set->active_fds_ignored, WinsockFd *, i);
651 wfd->ignored_event_mask = 0;
654 g_array_set_size (set->active_fds_ignored, 0);
662 * gst_poll_new: (skip)
663 * @controllable: whether it should be possible to control a wait.
665 * Create a new file descriptor set. If @controllable, it
666 * is possible to restart or flush a call to gst_poll_wait() with
667 * gst_poll_restart() and gst_poll_set_flushing() respectively.
669 * Free-function: gst_poll_free
671 * Returns: (transfer full) (nullable): a new #GstPoll, or %NULL in
672 * case of an error. Free with gst_poll_free().
675 gst_poll_new (gboolean controllable)
679 nset = g_slice_new0 (GstPoll);
680 GST_DEBUG ("%p: new controllable : %d", nset, controllable);
681 g_mutex_init (&nset->lock);
683 nset->mode = GST_POLL_MODE_AUTO;
684 nset->fds = g_array_new (FALSE, FALSE, sizeof (struct pollfd));
685 nset->active_fds = g_array_new (FALSE, FALSE, sizeof (struct pollfd));
686 nset->control_read_fd.fd = -1;
687 nset->control_write_fd.fd = -1;
689 gint control_sock[2];
691 if (socketpair (PF_UNIX, SOCK_STREAM, 0, control_sock) < 0)
694 nset->control_read_fd.fd = control_sock[0];
695 nset->control_write_fd.fd = control_sock[1];
697 gst_poll_add_fd_unlocked (nset, &nset->control_read_fd);
698 gst_poll_fd_ctl_read_unlocked (nset, &nset->control_read_fd, TRUE);
701 nset->mode = GST_POLL_MODE_WINDOWS;
702 nset->fds = g_array_new (FALSE, FALSE, sizeof (WinsockFd));
703 nset->active_fds = g_array_new (FALSE, FALSE, sizeof (WinsockFd));
704 nset->active_fds_ignored = g_array_new (FALSE, FALSE, sizeof (WinsockFd *));
705 nset->events = g_array_new (FALSE, FALSE, sizeof (HANDLE));
706 nset->active_events = g_array_new (FALSE, FALSE, sizeof (HANDLE));
708 nset->wakeup_event = CreateEvent (NULL, TRUE, FALSE, NULL);
711 /* ensure (re)build, though already sneakily set in non-windows case */
714 nset->controllable = controllable;
715 nset->control_pending = 0;
723 GST_WARNING ("%p: can't create socket pair !", nset);
724 gst_poll_free (nset);
731 * gst_poll_new_timer: (skip)
733 * Create a new poll object that can be used for scheduling cancellable
736 * A timeout is performed with gst_poll_wait(). Multiple timeouts can be
737 * performed from different threads.
739 * Free-function: gst_poll_free
741 * Returns: (transfer full) (nullable): a new #GstPoll, or %NULL in
742 * case of an error. Free with gst_poll_free().
745 gst_poll_new_timer (void)
749 /* make a new controllable poll set */
750 if (!(poll = gst_poll_new (TRUE)))
762 * @set: (transfer full): a file descriptor set.
764 * Free a file descriptor set.
767 gst_poll_free (GstPoll * set)
769 g_return_if_fail (set != NULL);
771 GST_DEBUG ("%p: freeing", set);
774 if (set->control_write_fd.fd >= 0)
775 close (set->control_write_fd.fd);
776 if (set->control_read_fd.fd >= 0)
777 close (set->control_read_fd.fd);
779 CloseHandle (set->wakeup_event);
784 for (i = 0; i < set->events->len; i++)
785 gst_poll_free_winsock_event (set, i);
788 g_array_free (set->active_events, TRUE);
789 g_array_free (set->events, TRUE);
790 g_array_free (set->active_fds_ignored, TRUE);
793 g_array_free (set->active_fds, TRUE);
794 g_array_free (set->fds, TRUE);
795 g_mutex_clear (&set->lock);
796 g_slice_free (GstPoll, set);
800 * gst_poll_get_read_gpollfd:
804 * Get a GPollFD for the reading part of the control socket. This is useful when
805 * integrating with a GSource and GMainLoop.
808 gst_poll_get_read_gpollfd (GstPoll * set, GPollFD * fd)
810 g_return_if_fail (set != NULL);
811 g_return_if_fail (fd != NULL);
814 fd->fd = set->control_read_fd.fd;
816 #if GLIB_SIZEOF_VOID_P == 8
817 fd->fd = (gint64) set->wakeup_event;
819 fd->fd = (gint) set->wakeup_event;
822 fd->events = G_IO_IN | G_IO_HUP | G_IO_ERR;
830 * Initializes @fd. Alternatively you can initialize it with
834 gst_poll_fd_init (GstPollFD * fd)
836 g_return_if_fail (fd != NULL);
843 gst_poll_add_fd_unlocked (GstPoll * set, GstPollFD * fd)
847 GST_DEBUG ("%p: fd (fd:%d, idx:%d)", set, fd->fd, fd->idx);
849 idx = find_index (set->fds, fd);
855 nfd.events = POLLERR | POLLNVAL | POLLHUP;
858 g_array_append_val (set->fds, nfd);
860 fd->idx = set->fds->len - 1;
866 wfd.event_mask = FD_CLOSE;
867 memset (&wfd.events, 0, sizeof (wfd.events));
868 wfd.ignored_event_mask = 0;
869 event = WSACreateEvent ();
871 g_array_append_val (set->fds, wfd);
872 g_array_append_val (set->events, event);
874 fd->idx = set->fds->len - 1;
878 GST_WARNING ("%p: fd already added !", set);
886 * @set: a file descriptor set.
887 * @fd: a file descriptor.
889 * Add a file descriptor to the file descriptor set.
891 * Returns: %TRUE if the file descriptor was successfully added to the set.
894 gst_poll_add_fd (GstPoll * set, GstPollFD * fd)
898 g_return_val_if_fail (set != NULL, FALSE);
899 g_return_val_if_fail (fd != NULL, FALSE);
900 g_return_val_if_fail (fd->fd >= 0, FALSE);
902 g_mutex_lock (&set->lock);
904 ret = gst_poll_add_fd_unlocked (set, fd);
906 g_mutex_unlock (&set->lock);
912 * gst_poll_remove_fd:
913 * @set: a file descriptor set.
914 * @fd: a file descriptor.
916 * Remove a file descriptor from the file descriptor set.
918 * Returns: %TRUE if the file descriptor was successfully removed from the set.
921 gst_poll_remove_fd (GstPoll * set, GstPollFD * fd)
925 g_return_val_if_fail (set != NULL, FALSE);
926 g_return_val_if_fail (fd != NULL, FALSE);
927 g_return_val_if_fail (fd->fd >= 0, FALSE);
930 GST_DEBUG ("%p: fd (fd:%d, idx:%d)", set, fd->fd, fd->idx);
932 g_mutex_lock (&set->lock);
934 /* get the index, -1 is an fd that is not added */
935 idx = find_index (set->fds, fd);
938 gst_poll_free_winsock_event (set, idx);
939 g_array_remove_index_fast (set->events, idx);
942 /* remove the fd at index, we use _remove_index_fast, which copies the last
943 * element of the array to the freed index */
944 g_array_remove_index_fast (set->fds, idx);
946 /* mark fd as removed by setting the index to -1 */
950 GST_WARNING ("%p: couldn't find fd !", set);
953 g_mutex_unlock (&set->lock);
959 * gst_poll_fd_ctl_write:
960 * @set: a file descriptor set.
961 * @fd: a file descriptor.
962 * @active: a new status.
964 * Control whether the descriptor @fd in @set will be monitored for
967 * Returns: %TRUE if the descriptor was successfully updated.
970 gst_poll_fd_ctl_write (GstPoll * set, GstPollFD * fd, gboolean active)
974 g_return_val_if_fail (set != NULL, FALSE);
975 g_return_val_if_fail (fd != NULL, FALSE);
976 g_return_val_if_fail (fd->fd >= 0, FALSE);
978 GST_DEBUG ("%p: fd (fd:%d, idx:%d), active : %d", set,
979 fd->fd, fd->idx, active);
981 g_mutex_lock (&set->lock);
983 idx = find_index (set->fds, fd);
986 struct pollfd *pfd = &g_array_index (set->fds, struct pollfd, idx);
989 pfd->events |= POLLOUT;
991 pfd->events &= ~POLLOUT;
993 GST_LOG ("%p: pfd->events now %d (POLLOUT:%d)", set, pfd->events, POLLOUT);
995 gst_poll_update_winsock_event_mask (set, idx, FD_WRITE | FD_CONNECT,
1000 GST_WARNING ("%p: couldn't find fd !", set);
1003 g_mutex_unlock (&set->lock);
1009 gst_poll_fd_ctl_read_unlocked (GstPoll * set, GstPollFD * fd, gboolean active)
1013 GST_DEBUG ("%p: fd (fd:%d, idx:%d), active : %d", set,
1014 fd->fd, fd->idx, active);
1016 idx = find_index (set->fds, fd);
1020 struct pollfd *pfd = &g_array_index (set->fds, struct pollfd, idx);
1023 pfd->events |= (POLLIN | POLLPRI);
1025 pfd->events &= ~(POLLIN | POLLPRI);
1027 gst_poll_update_winsock_event_mask (set, idx, FD_READ | FD_ACCEPT, active);
1031 GST_WARNING ("%p: couldn't find fd !", set);
1038 * gst_poll_fd_ctl_read:
1039 * @set: a file descriptor set.
1040 * @fd: a file descriptor.
1041 * @active: a new status.
1043 * Control whether the descriptor @fd in @set will be monitored for
1046 * Returns: %TRUE if the descriptor was successfully updated.
1049 gst_poll_fd_ctl_read (GstPoll * set, GstPollFD * fd, gboolean active)
1053 g_return_val_if_fail (set != NULL, FALSE);
1054 g_return_val_if_fail (fd != NULL, FALSE);
1055 g_return_val_if_fail (fd->fd >= 0, FALSE);
1057 g_mutex_lock (&set->lock);
1059 ret = gst_poll_fd_ctl_read_unlocked (set, fd, active);
1061 g_mutex_unlock (&set->lock);
1067 * gst_poll_fd_ignored:
1068 * @set: a file descriptor set.
1069 * @fd: a file descriptor.
1071 * Mark @fd as ignored so that the next call to gst_poll_wait() will yield
1072 * the same result for @fd as last time. This function must be called if no
1073 * operation (read/write/recv/send/etc.) will be performed on @fd before
1074 * the next call to gst_poll_wait().
1076 * The reason why this is needed is because the underlying implementation
1077 * might not allow querying the fd more than once between calls to one of
1078 * the re-enabling operations.
1081 gst_poll_fd_ignored (GstPoll * set, GstPollFD * fd)
1086 g_return_if_fail (set != NULL);
1087 g_return_if_fail (fd != NULL);
1088 g_return_if_fail (fd->fd >= 0);
1090 g_mutex_lock (&set->lock);
1092 idx = find_index (set->fds, fd);
1094 WinsockFd *wfd = &g_array_index (set->fds, WinsockFd, idx);
1096 wfd->ignored_event_mask = wfd->event_mask & (FD_READ | FD_WRITE);
1100 g_mutex_unlock (&set->lock);
1105 * gst_poll_fd_has_closed:
1106 * @set: a file descriptor set.
1107 * @fd: a file descriptor.
1109 * Check if @fd in @set has closed the connection.
1111 * Returns: %TRUE if the connection was closed.
1114 gst_poll_fd_has_closed (const GstPoll * set, GstPollFD * fd)
1116 gboolean res = FALSE;
1119 g_return_val_if_fail (set != NULL, FALSE);
1120 g_return_val_if_fail (fd != NULL, FALSE);
1121 g_return_val_if_fail (fd->fd >= 0, FALSE);
1123 g_mutex_lock (&((GstPoll *) set)->lock);
1125 idx = find_index (set->active_fds, fd);
1128 struct pollfd *pfd = &g_array_index (set->active_fds, struct pollfd, idx);
1130 res = (pfd->revents & POLLHUP) != 0;
1132 WinsockFd *wfd = &g_array_index (set->active_fds, WinsockFd, idx);
1134 res = (wfd->events.lNetworkEvents & FD_CLOSE) != 0;
1137 GST_WARNING ("%p: couldn't find fd !", set);
1139 g_mutex_unlock (&((GstPoll *) set)->lock);
1141 GST_DEBUG ("%p: fd (fd:%d, idx:%d) %d", set, fd->fd, fd->idx, res);
1147 * gst_poll_fd_has_error:
1148 * @set: a file descriptor set.
1149 * @fd: a file descriptor.
1151 * Check if @fd in @set has an error.
1153 * Returns: %TRUE if the descriptor has an error.
1156 gst_poll_fd_has_error (const GstPoll * set, GstPollFD * fd)
1158 gboolean res = FALSE;
1161 g_return_val_if_fail (set != NULL, FALSE);
1162 g_return_val_if_fail (fd != NULL, FALSE);
1163 g_return_val_if_fail (fd->fd >= 0, FALSE);
1165 g_mutex_lock (&((GstPoll *) set)->lock);
1167 idx = find_index (set->active_fds, fd);
1170 struct pollfd *pfd = &g_array_index (set->active_fds, struct pollfd, idx);
1172 res = (pfd->revents & (POLLERR | POLLNVAL)) != 0;
1174 WinsockFd *wfd = &g_array_index (set->active_fds, WinsockFd, idx);
1176 res = (wfd->events.iErrorCode[FD_CLOSE_BIT] != 0) ||
1177 (wfd->events.iErrorCode[FD_READ_BIT] != 0) ||
1178 (wfd->events.iErrorCode[FD_WRITE_BIT] != 0) ||
1179 (wfd->events.iErrorCode[FD_ACCEPT_BIT] != 0) ||
1180 (wfd->events.iErrorCode[FD_CONNECT_BIT] != 0);
1183 GST_WARNING ("%p: couldn't find fd !", set);
1185 g_mutex_unlock (&((GstPoll *) set)->lock);
1187 GST_DEBUG ("%p: fd (fd:%d, idx:%d) %d", set, fd->fd, fd->idx, res);
1193 gst_poll_fd_can_read_unlocked (const GstPoll * set, GstPollFD * fd)
1195 gboolean res = FALSE;
1198 idx = find_index (set->active_fds, fd);
1201 struct pollfd *pfd = &g_array_index (set->active_fds, struct pollfd, idx);
1203 res = (pfd->revents & (POLLIN | POLLPRI)) != 0;
1205 WinsockFd *wfd = &g_array_index (set->active_fds, WinsockFd, idx);
1207 res = (wfd->events.lNetworkEvents & (FD_READ | FD_ACCEPT)) != 0;
1210 GST_WARNING ("%p: couldn't find fd !", set);
1212 GST_DEBUG ("%p: fd (fd:%d, idx:%d) %d", set, fd->fd, fd->idx, res);
1218 * gst_poll_fd_can_read:
1219 * @set: a file descriptor set.
1220 * @fd: a file descriptor.
1222 * Check if @fd in @set has data to be read.
1224 * Returns: %TRUE if the descriptor has data to be read.
1227 gst_poll_fd_can_read (const GstPoll * set, GstPollFD * fd)
1229 gboolean res = FALSE;
1231 g_return_val_if_fail (set != NULL, FALSE);
1232 g_return_val_if_fail (fd != NULL, FALSE);
1233 g_return_val_if_fail (fd->fd >= 0, FALSE);
1235 g_mutex_lock (&((GstPoll *) set)->lock);
1237 res = gst_poll_fd_can_read_unlocked (set, fd);
1239 g_mutex_unlock (&((GstPoll *) set)->lock);
1245 * gst_poll_fd_can_write:
1246 * @set: a file descriptor set.
1247 * @fd: a file descriptor.
1249 * Check if @fd in @set can be used for writing.
1251 * Returns: %TRUE if the descriptor can be used for writing.
1254 gst_poll_fd_can_write (const GstPoll * set, GstPollFD * fd)
1256 gboolean res = FALSE;
1259 g_return_val_if_fail (set != NULL, FALSE);
1260 g_return_val_if_fail (fd != NULL, FALSE);
1261 g_return_val_if_fail (fd->fd >= 0, FALSE);
1263 g_mutex_lock (&((GstPoll *) set)->lock);
1265 idx = find_index (set->active_fds, fd);
1268 struct pollfd *pfd = &g_array_index (set->active_fds, struct pollfd, idx);
1270 res = (pfd->revents & POLLOUT) != 0;
1272 WinsockFd *wfd = &g_array_index (set->active_fds, WinsockFd, idx);
1274 res = (wfd->events.lNetworkEvents & FD_WRITE) != 0;
1277 GST_WARNING ("%p: couldn't find fd !", set);
1279 g_mutex_unlock (&((GstPoll *) set)->lock);
1281 GST_DEBUG ("%p: fd (fd:%d, idx:%d) %d", set, fd->fd, fd->idx, res);
1289 * @timeout: a timeout in nanoseconds.
1291 * Wait for activity on the file descriptors in @set. This function waits up to
1292 * the specified @timeout. A timeout of #GST_CLOCK_TIME_NONE waits forever.
1294 * For #GstPoll objects created with gst_poll_new(), this function can only be
1295 * called from a single thread at a time. If called from multiple threads,
1296 * -1 will be returned with errno set to EPERM.
1298 * This is not true for timer #GstPoll objects created with
1299 * gst_poll_new_timer(), where it is allowed to have multiple threads waiting
1302 * Returns: The number of #GstPollFD in @set that have activity or 0 when no
1303 * activity was detected after @timeout. If an error occurs, -1 is returned
1307 gst_poll_wait (GstPoll * set, GstClockTime timeout)
1309 gboolean restarting;
1314 g_return_val_if_fail (set != NULL, -1);
1316 GST_DEBUG ("%p: timeout :%" GST_TIME_FORMAT, set, GST_TIME_ARGS (timeout));
1318 is_timer = set->timer;
1320 /* add one more waiter */
1321 old_waiting = INC_WAITING (set);
1323 /* we cannot wait from multiple threads unless we are a timer */
1324 if (G_UNLIKELY (old_waiting > 0 && !is_timer))
1325 goto already_waiting;
1327 /* flushing, exit immediately */
1328 if (G_UNLIKELY (IS_FLUSHING (set)))
1337 mode = choose_mode (set, timeout);
1339 if (TEST_REBUILD (set)) {
1340 g_mutex_lock (&set->lock);
1342 g_array_set_size (set->active_fds, set->fds->len);
1343 memcpy (set->active_fds->data, set->fds->data,
1344 set->fds->len * sizeof (struct pollfd));
1346 if (!gst_poll_prepare_winsock_active_sets (set))
1349 g_mutex_unlock (&set->lock);
1353 case GST_POLL_MODE_AUTO:
1354 g_assert_not_reached ();
1356 case GST_POLL_MODE_PPOLL:
1360 struct timespec *tsptr;
1362 if (timeout != GST_CLOCK_TIME_NONE) {
1363 GST_TIME_TO_TIMESPEC (timeout, ts);
1370 ppoll ((struct pollfd *) set->active_fds->data,
1371 set->active_fds->len, tsptr, NULL);
1373 g_assert_not_reached ();
1378 case GST_POLL_MODE_POLL:
1383 if (timeout != GST_CLOCK_TIME_NONE) {
1384 t = GST_TIME_AS_MSECONDS (timeout);
1390 poll ((struct pollfd *) set->active_fds->data,
1391 set->active_fds->len, t);
1393 g_assert_not_reached ();
1398 case GST_POLL_MODE_PSELECT:
1399 #ifndef HAVE_PSELECT
1401 g_assert_not_reached ();
1406 case GST_POLL_MODE_SELECT:
1414 max_fd = pollfd_to_fd_set (set, &readfds, &writefds, &errorfds);
1416 if (mode == GST_POLL_MODE_SELECT) {
1418 struct timeval *tvptr;
1420 if (timeout != GST_CLOCK_TIME_NONE) {
1421 GST_TIME_TO_TIMEVAL (timeout, tv);
1427 GST_DEBUG ("%p: Calling select", set);
1428 res = select (max_fd + 1, &readfds, &writefds, &errorfds, tvptr);
1429 GST_DEBUG ("%p: After select, res:%d", set, res);
1433 struct timespec *tsptr;
1435 if (timeout != GST_CLOCK_TIME_NONE) {
1436 GST_TIME_TO_TIMESPEC (timeout, ts);
1442 GST_DEBUG ("%p: Calling pselect", set);
1444 pselect (max_fd + 1, &readfds, &writefds, &errorfds, tsptr, NULL);
1445 GST_DEBUG ("%p: After pselect, res:%d", set, res);
1450 fd_set_to_pollfd (set, &readfds, &writefds, &errorfds);
1452 #else /* G_OS_WIN32 */
1453 g_assert_not_reached ();
1458 case GST_POLL_MODE_WINDOWS:
1461 gint ignore_count = set->active_fds_ignored->len;
1464 if (G_LIKELY (ignore_count == 0)) {
1465 if (timeout != GST_CLOCK_TIME_NONE)
1466 t = GST_TIME_AS_MSECONDS (timeout);
1470 /* already one or more ignored fds, so we quickly sweep the others */
1474 if (set->active_events->len != 0) {
1475 wait_ret = WSAWaitForMultipleEvents (set->active_events->len,
1476 (HANDLE *) set->active_events->data, FALSE, t, FALSE);
1478 wait_ret = WSA_WAIT_FAILED;
1479 WSASetLastError (WSA_INVALID_PARAMETER);
1482 if (ignore_count == 0 && wait_ret == WSA_WAIT_TIMEOUT) {
1484 } else if (wait_ret == WSA_WAIT_FAILED) {
1486 errno = gst_poll_winsock_error_to_errno (WSAGetLastError ());
1488 /* the first entry is the wakeup event */
1489 if (wait_ret - WSA_WAIT_EVENT_0 >= 1) {
1490 res = gst_poll_collect_winsock_events (set);
1492 res = 1; /* wakeup event */
1496 g_assert_not_reached ();
1504 /* Applications needs to clear the control socket themselves for timer
1506 * For other polls, we need to clear the control socket. If there was only
1507 * one socket with activity and it was the control socket, we need to
1509 if (release_all_wakeup (set) > 0 && res == 1)
1513 /* we got woken up and we are flushing, we need to stop */
1514 if (G_UNLIKELY (IS_FLUSHING (set)))
1517 } while (G_UNLIKELY (restarting));
1526 GST_LOG ("%p: we are already waiting", set);
1533 GST_LOG ("%p: we are flushing", set);
1541 GST_LOG ("%p: winsock error", set);
1542 g_mutex_unlock (&set->lock);
1550 * gst_poll_set_controllable:
1552 * @controllable: new controllable state.
1554 * When @controllable is %TRUE, this function ensures that future calls to
1555 * gst_poll_wait() will be affected by gst_poll_restart() and
1556 * gst_poll_set_flushing().
1558 * This function only works for non-timer #GstPoll objects created with
1561 * Returns: %TRUE if the controllability of @set could be updated.
1564 gst_poll_set_controllable (GstPoll * set, gboolean controllable)
1566 g_return_val_if_fail (set != NULL, FALSE);
1567 g_return_val_if_fail (!set->timer, FALSE);
1569 GST_LOG ("%p: controllable : %d", set, controllable);
1571 set->controllable = controllable;
1580 * Restart any gst_poll_wait() that is in progress. This function is typically
1581 * used after adding or removing descriptors to @set.
1583 * If @set is not controllable, then this call will have no effect.
1585 * This function only works for non-timer #GstPoll objects created with
1589 gst_poll_restart (GstPoll * set)
1591 g_return_if_fail (set != NULL);
1592 g_return_if_fail (!set->timer);
1594 if (set->controllable && GET_WAITING (set) > 0) {
1595 /* we are controllable and waiting, wake up the waiter. The socket will be
1596 * cleared by the _wait() thread and the poll will be restarted */
1602 * gst_poll_set_flushing:
1604 * @flushing: new flushing state.
1606 * When @flushing is %TRUE, this function ensures that current and future calls
1607 * to gst_poll_wait() will return -1, with errno set to EBUSY.
1609 * Unsetting the flushing state will restore normal operation of @set.
1611 * This function only works for non-timer #GstPoll objects created with
1615 gst_poll_set_flushing (GstPoll * set, gboolean flushing)
1617 g_return_if_fail (set != NULL);
1618 g_return_if_fail (!set->timer);
1620 GST_LOG ("%p: flushing: %d", set, flushing);
1622 /* update the new state first */
1623 SET_FLUSHING (set, flushing);
1625 if (flushing && set->controllable && GET_WAITING (set) > 0) {
1626 /* we are flushing, controllable and waiting, wake up the waiter. When we
1627 * stop the flushing operation we don't clear the wakeup fd here, this will
1628 * happen in the _wait() thread. */
1634 * gst_poll_write_control:
1637 * Write a byte to the control socket of the controllable @set.
1638 * This function is mostly useful for timer #GstPoll objects created with
1639 * gst_poll_new_timer().
1641 * It will make any current and future gst_poll_wait() function return with
1642 * 1, meaning the control socket is set. After an equal amount of calls to
1643 * gst_poll_read_control() have been performed, calls to gst_poll_wait() will
1644 * block again until their timeout expired.
1646 * This function only works for timer #GstPoll objects created with
1647 * gst_poll_new_timer().
1649 * Returns: %TRUE on success. %FALSE when when the byte could not be written.
1650 * errno contains the detailed error code but will never be EAGAIN, EINTR or
1651 * EWOULDBLOCK. %FALSE always signals a critical error.
1654 gst_poll_write_control (GstPoll * set)
1658 g_return_val_if_fail (set != NULL, FALSE);
1659 g_return_val_if_fail (set->timer, FALSE);
1661 res = raise_wakeup (set);
1667 * gst_poll_read_control:
1670 * Read a byte from the control socket of the controllable @set.
1672 * This function only works for timer #GstPoll objects created with
1673 * gst_poll_new_timer().
1675 * Returns: %TRUE on success. %FALSE when when there was no byte to read or
1676 * reading the byte failed. If there was no byte to read, and only then, errno
1677 * will contain EWOULDBLOCK or EAGAIN. For all other values of errno this always signals a
1681 gst_poll_read_control (GstPoll * set)
1685 g_return_val_if_fail (set != NULL, FALSE);
1686 g_return_val_if_fail (set->timer, FALSE);
1688 res = release_wakeup (set);