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., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
26 * @short_description: Keep track of file descriptors and make it possible
27 * to wait on them in a cancelable 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>
87 /* OS/X needs this because of bad headers */
90 /* The poll() emulation on OS/X doesn't handle fds=NULL, nfds=0,
91 * so we prefer our own poll emulation.
93 #if defined(BROKEN_POLL)
99 #define GST_CAT_DEFAULT GST_CAT_POLL
102 typedef struct _WinsockFd WinsockFd;
108 WSANETWORKEVENTS events;
109 glong ignored_event_mask;
116 GST_POLL_MODE_SELECT,
117 GST_POLL_MODE_PSELECT,
120 GST_POLL_MODE_WINDOWS
128 /* array of fds, always written to and read from with lock */
130 /* array of active fds, only written to from the waiting thread with the
131 * lock and read from with the lock or without the lock from the waiting
137 GstPollFD control_read_fd;
138 GstPollFD control_write_fd;
140 GArray *active_fds_ignored;
142 GArray *active_events;
147 gboolean controllable;
148 volatile gint waiting;
149 volatile gint control_pending;
150 volatile gint flushing;
152 volatile gint rebuild;
155 static gboolean gst_poll_fd_ctl_read_unlocked (GstPoll * set, GstPollFD * fd,
157 static gboolean gst_poll_add_fd_unlocked (GstPoll * set, GstPollFD * fd);
159 #define IS_FLUSHING(s) (g_atomic_int_get(&(s)->flushing))
160 #define SET_FLUSHING(s,val) (g_atomic_int_set(&(s)->flushing, (val)))
162 #define INC_WAITING(s) (g_atomic_int_add(&(s)->waiting, 1))
163 #define DEC_WAITING(s) (g_atomic_int_add(&(s)->waiting, -1))
164 #define GET_WAITING(s) (g_atomic_int_get(&(s)->waiting))
166 #define TEST_REBUILD(s) (g_atomic_int_compare_and_exchange(&(s)->rebuild, 1, 0))
167 #define MARK_REBUILD(s) (g_atomic_int_set(&(s)->rebuild, 1))
170 #define WAKE_EVENT(s) (write ((s)->control_write_fd.fd, "W", 1) == 1)
171 #define RELEASE_EVENT(s) (read ((s)->control_read_fd.fd, (s)->buf, 1) == 1)
173 #define WAKE_EVENT(s) (SetEvent ((s)->wakeup_event), errno = GetLastError () == NO_ERROR ? 0 : EACCES, errno == 0 ? 1 : 0)
174 #define RELEASE_EVENT(s) (ResetEvent ((s)->wakeup_event))
177 /* the poll/select call is also performed on a control socket, that way
178 * we can send special commands to control it */
179 static inline gboolean
180 raise_wakeup (GstPoll * set)
182 gboolean result = TRUE;
184 if (g_atomic_int_add (&set->control_pending, 1) == 0) {
185 /* raise when nothing pending */
186 GST_LOG ("%p: raise", set);
187 result = WAKE_EVENT (set);
192 /* note how bad things can happen when the 2 threads both raise and release the
193 * wakeup. This is however not a problem because you must always pair a raise
195 static inline gboolean
196 release_wakeup (GstPoll * set)
198 gboolean result = TRUE;
200 if (g_atomic_int_dec_and_test (&set->control_pending)) {
201 GST_LOG ("%p: release", set);
202 result = RELEASE_EVENT (set);
208 release_all_wakeup (GstPoll * set)
213 if (!(old = g_atomic_int_get (&set->control_pending)))
214 /* nothing pending, just exit */
217 /* try to remove all pending control messages */
218 if (g_atomic_int_compare_and_exchange (&set->control_pending, old, 0)) {
219 /* we managed to remove all messages, read the control socket */
220 if (RELEASE_EVENT (set))
223 /* retry again until we read it successfully */
224 g_atomic_int_add (&set->control_pending, 1);
231 find_index (GArray * array, GstPollFD * fd)
240 /* start by assuming the index found in the fd is still valid */
241 if (fd->idx >= 0 && fd->idx < array->len) {
243 ifd = &g_array_index (array, struct pollfd, fd->idx);
245 ifd = &g_array_index (array, WinsockFd, fd->idx);
248 if (ifd->fd == fd->fd) {
253 /* the pollfd array has changed and we need to lookup the fd again */
254 for (i = 0; i < array->len; i++) {
256 ifd = &g_array_index (array, struct pollfd, i);
258 ifd = &g_array_index (array, WinsockFd, i);
261 if (ifd->fd == fd->fd) {
271 #if !defined(HAVE_PPOLL) && defined(HAVE_POLL)
272 /* check if all file descriptors will fit in an fd_set */
274 selectable_fds (const GstPoll * set)
278 g_mutex_lock (&set->lock);
279 for (i = 0; i < set->fds->len; i++) {
280 struct pollfd *pfd = &g_array_index (set->fds, struct pollfd, i);
282 if (pfd->fd >= FD_SETSIZE)
285 g_mutex_unlock (&set->lock);
291 g_mutex_unlock (&set->lock);
296 /* check if the timeout will convert to a timeout value used for poll()
297 * without a loss of precision
300 pollable_timeout (GstClockTime timeout)
302 if (timeout == GST_CLOCK_TIME_NONE)
305 /* not a nice multiple of milliseconds */
306 if (timeout % 1000000)
314 choose_mode (const GstPoll * set, GstClockTime timeout)
318 if (set->mode == GST_POLL_MODE_AUTO) {
320 mode = GST_POLL_MODE_PPOLL;
321 #elif defined(HAVE_POLL)
322 if (!selectable_fds (set) || pollable_timeout (timeout)) {
323 mode = GST_POLL_MODE_POLL;
326 mode = GST_POLL_MODE_PSELECT;
328 mode = GST_POLL_MODE_SELECT;
331 #elif defined(HAVE_PSELECT)
332 mode = GST_POLL_MODE_PSELECT;
334 mode = GST_POLL_MODE_SELECT;
344 pollfd_to_fd_set (GstPoll * set, fd_set * readfds, fd_set * writefds,
354 g_mutex_lock (&set->lock);
356 for (i = 0; i < set->active_fds->len; i++) {
357 struct pollfd *pfd = &g_array_index (set->fds, struct pollfd, i);
359 if (pfd->fd < FD_SETSIZE) {
360 if (pfd->events & POLLIN)
361 FD_SET (pfd->fd, readfds);
362 if (pfd->events & POLLOUT)
363 FD_SET (pfd->fd, writefds);
365 FD_SET (pfd->fd, errorfds);
366 if (pfd->fd > max_fd && (pfd->events & (POLLIN | POLLOUT)))
371 g_mutex_unlock (&set->lock);
377 fd_set_to_pollfd (GstPoll * set, fd_set * readfds, fd_set * writefds,
382 g_mutex_lock (&set->lock);
384 for (i = 0; i < set->active_fds->len; i++) {
385 struct pollfd *pfd = &g_array_index (set->active_fds, struct pollfd, i);
387 if (pfd->fd < FD_SETSIZE) {
389 if (FD_ISSET (pfd->fd, readfds))
390 pfd->revents |= POLLIN;
391 if (FD_ISSET (pfd->fd, writefds))
392 pfd->revents |= POLLOUT;
393 if (FD_ISSET (pfd->fd, errorfds))
394 pfd->revents |= POLLERR;
398 g_mutex_unlock (&set->lock);
400 #else /* G_OS_WIN32 */
402 * Translate errors thrown by the Winsock API used by GstPoll:
403 * WSAEventSelect, WSAWaitForMultipleEvents and WSAEnumNetworkEvents
406 gst_poll_winsock_error_to_errno (DWORD last_error)
408 switch (last_error) {
409 case WSA_INVALID_HANDLE:
414 case WSA_NOT_ENOUGH_MEMORY:
418 * Anything else, including:
419 * WSA_INVALID_PARAMETER, WSAEFAULT, WSAEINPROGRESS, WSAENETDOWN,
428 gst_poll_free_winsock_event (GstPoll * set, gint idx)
430 WinsockFd *wfd = &g_array_index (set->fds, WinsockFd, idx);
431 HANDLE event = g_array_index (set->events, HANDLE, idx);
433 WSAEventSelect (wfd->fd, event, 0);
438 gst_poll_update_winsock_event_mask (GstPoll * set, gint idx, glong flags,
443 wfd = &g_array_index (set->fds, WinsockFd, idx);
446 wfd->event_mask |= flags;
448 wfd->event_mask &= ~flags;
450 /* reset ignored state if the new mask doesn't overlap at all */
451 if ((wfd->ignored_event_mask & wfd->event_mask) == 0)
452 wfd->ignored_event_mask = 0;
456 gst_poll_prepare_winsock_active_sets (GstPoll * set)
460 g_array_set_size (set->active_fds, 0);
461 g_array_set_size (set->active_fds_ignored, 0);
462 g_array_set_size (set->active_events, 0);
463 g_array_append_val (set->active_events, set->wakeup_event);
465 for (i = 0; i < set->fds->len; i++) {
466 WinsockFd *wfd = &g_array_index (set->fds, WinsockFd, i);
467 HANDLE event = g_array_index (set->events, HANDLE, i);
469 if (wfd->ignored_event_mask == 0) {
472 g_array_append_val (set->active_fds, *wfd);
473 g_array_append_val (set->active_events, event);
475 ret = WSAEventSelect (wfd->fd, event, wfd->event_mask);
476 if (G_UNLIKELY (ret != 0)) {
477 errno = gst_poll_winsock_error_to_errno (WSAGetLastError ());
481 g_array_append_val (set->active_fds_ignored, wfd);
489 gst_poll_collect_winsock_events (GstPoll * set)
494 * We need to check which events are signaled, and call
495 * WSAEnumNetworkEvents for those that are, which resets
496 * the event and clears the internal network event records.
499 for (i = 0; i < set->active_fds->len; i++) {
500 WinsockFd *wfd = &g_array_index (set->active_fds, WinsockFd, i);
501 HANDLE event = g_array_index (set->active_events, HANDLE, i + 1);
504 wait_ret = WaitForSingleObject (event, 0);
505 if (wait_ret == WAIT_OBJECT_0) {
506 gint enum_ret = WSAEnumNetworkEvents (wfd->fd, event, &wfd->events);
508 if (G_UNLIKELY (enum_ret != 0)) {
510 errno = gst_poll_winsock_error_to_errno (WSAGetLastError ());
516 /* clear any previously stored result */
517 memset (&wfd->events, 0, sizeof (wfd->events));
521 /* If all went well we also need to reset the ignored fds. */
523 res += set->active_fds_ignored->len;
525 for (i = 0; i < set->active_fds_ignored->len; i++) {
526 WinsockFd *wfd = g_array_index (set->active_fds_ignored, WinsockFd *, i);
528 wfd->ignored_event_mask = 0;
531 g_array_set_size (set->active_fds_ignored, 0);
539 * gst_poll_new: (skip)
540 * @controllable: whether it should be possible to control a wait.
542 * Create a new file descriptor set. If @controllable, it
543 * is possible to restart or flush a call to gst_poll_wait() with
544 * gst_poll_restart() and gst_poll_set_flushing() respectively.
546 * Free-function: gst_poll_free
548 * Returns: (transfer full): a new #GstPoll, or %NULL in case of an error.
549 * Free with gst_poll_free().
554 gst_poll_new (gboolean controllable)
558 GST_DEBUG ("controllable : %d", controllable);
560 nset = g_slice_new0 (GstPoll);
561 g_mutex_init (&nset->lock);
563 nset->mode = GST_POLL_MODE_AUTO;
564 nset->fds = g_array_new (FALSE, FALSE, sizeof (struct pollfd));
565 nset->active_fds = g_array_new (FALSE, FALSE, sizeof (struct pollfd));
566 nset->control_read_fd.fd = -1;
567 nset->control_write_fd.fd = -1;
569 gint control_sock[2];
571 if (socketpair (PF_UNIX, SOCK_STREAM, 0, control_sock) < 0)
574 fcntl (control_sock[0], F_SETFL, O_NONBLOCK);
575 fcntl (control_sock[1], F_SETFL, O_NONBLOCK);
577 nset->control_read_fd.fd = control_sock[0];
578 nset->control_write_fd.fd = control_sock[1];
580 gst_poll_add_fd_unlocked (nset, &nset->control_read_fd);
581 gst_poll_fd_ctl_read_unlocked (nset, &nset->control_read_fd, TRUE);
584 nset->mode = GST_POLL_MODE_WINDOWS;
585 nset->fds = g_array_new (FALSE, FALSE, sizeof (WinsockFd));
586 nset->active_fds = g_array_new (FALSE, FALSE, sizeof (WinsockFd));
587 nset->active_fds_ignored = g_array_new (FALSE, FALSE, sizeof (WinsockFd *));
588 nset->events = g_array_new (FALSE, FALSE, sizeof (HANDLE));
589 nset->active_events = g_array_new (FALSE, FALSE, sizeof (HANDLE));
591 nset->wakeup_event = CreateEvent (NULL, TRUE, FALSE, NULL);
594 /* ensure (re)build, though already sneakily set in non-windows case */
597 nset->controllable = controllable;
605 GST_WARNING ("%p: can't create socket pair !", nset);
606 gst_poll_free (nset);
613 * gst_poll_new_timer: (skip)
615 * Create a new poll object that can be used for scheduling cancellable
618 * A timeout is performed with gst_poll_wait(). Multiple timeouts can be
619 * performed from different threads.
621 * Free-function: gst_poll_free
623 * Returns: (transfer full): a new #GstPoll, or %NULL in case of an error.
624 * Free with gst_poll_free().
629 gst_poll_new_timer (void)
633 /* make a new controllable poll set */
634 if (!(poll = gst_poll_new (TRUE)))
646 * @set: (transfer full): a file descriptor set.
648 * Free a file descriptor set.
653 gst_poll_free (GstPoll * set)
655 g_return_if_fail (set != NULL);
657 GST_DEBUG ("%p: freeing", set);
660 if (set->control_write_fd.fd >= 0)
661 close (set->control_write_fd.fd);
662 if (set->control_read_fd.fd >= 0)
663 close (set->control_read_fd.fd);
665 CloseHandle (set->wakeup_event);
670 for (i = 0; i < set->events->len; i++)
671 gst_poll_free_winsock_event (set, i);
674 g_array_free (set->active_events, TRUE);
675 g_array_free (set->events, TRUE);
676 g_array_free (set->active_fds_ignored, TRUE);
679 g_array_free (set->active_fds, TRUE);
680 g_array_free (set->fds, TRUE);
681 g_mutex_clear (&set->lock);
682 g_slice_free (GstPoll, set);
686 * gst_poll_get_read_gpollfd:
690 * Get a GPollFD for the reading part of the control socket. This is useful when
691 * integrating with a GSource and GMainLoop.
696 gst_poll_get_read_gpollfd (GstPoll * set, GPollFD * fd)
698 g_return_if_fail (set != NULL);
699 g_return_if_fail (fd != NULL);
702 fd->fd = set->control_read_fd.fd;
704 #if GLIB_SIZEOF_VOID_P == 8
705 fd->fd = (gint64) set->wakeup_event;
707 fd->fd = (gint) set->wakeup_event;
710 fd->events = G_IO_IN | G_IO_HUP | G_IO_ERR;
718 * Initializes @fd. Alternatively you can initialize it with
724 gst_poll_fd_init (GstPollFD * fd)
726 g_return_if_fail (fd != NULL);
733 gst_poll_add_fd_unlocked (GstPoll * set, GstPollFD * fd)
737 GST_DEBUG ("%p: fd (fd:%d, idx:%d)", set, fd->fd, fd->idx);
739 idx = find_index (set->fds, fd);
745 nfd.events = POLLERR | POLLNVAL | POLLHUP;
748 g_array_append_val (set->fds, nfd);
750 fd->idx = set->fds->len - 1;
756 wfd.event_mask = FD_CLOSE;
757 memset (&wfd.events, 0, sizeof (wfd.events));
758 wfd.ignored_event_mask = 0;
759 event = WSACreateEvent ();
761 g_array_append_val (set->fds, wfd);
762 g_array_append_val (set->events, event);
764 fd->idx = set->fds->len - 1;
768 GST_WARNING ("%p: couldn't find fd !", set);
776 * @set: a file descriptor set.
777 * @fd: a file descriptor.
779 * Add a file descriptor to the file descriptor set.
781 * Returns: %TRUE if the file descriptor was successfully added to the set.
786 gst_poll_add_fd (GstPoll * set, GstPollFD * fd)
790 g_return_val_if_fail (set != NULL, FALSE);
791 g_return_val_if_fail (fd != NULL, FALSE);
792 g_return_val_if_fail (fd->fd >= 0, FALSE);
794 g_mutex_lock (&set->lock);
796 ret = gst_poll_add_fd_unlocked (set, fd);
798 g_mutex_unlock (&set->lock);
804 * gst_poll_remove_fd:
805 * @set: a file descriptor set.
806 * @fd: a file descriptor.
808 * Remove a file descriptor from the file descriptor set.
810 * Returns: %TRUE if the file descriptor was successfully removed from the set.
815 gst_poll_remove_fd (GstPoll * set, GstPollFD * fd)
819 g_return_val_if_fail (set != NULL, FALSE);
820 g_return_val_if_fail (fd != NULL, FALSE);
821 g_return_val_if_fail (fd->fd >= 0, FALSE);
824 GST_DEBUG ("%p: fd (fd:%d, idx:%d)", set, fd->fd, fd->idx);
826 g_mutex_lock (&set->lock);
828 /* get the index, -1 is an fd that is not added */
829 idx = find_index (set->fds, fd);
832 gst_poll_free_winsock_event (set, idx);
833 g_array_remove_index_fast (set->events, idx);
836 /* remove the fd at index, we use _remove_index_fast, which copies the last
837 * element of the array to the freed index */
838 g_array_remove_index_fast (set->fds, idx);
840 /* mark fd as removed by setting the index to -1 */
844 GST_WARNING ("%p: couldn't find fd !", set);
847 g_mutex_unlock (&set->lock);
853 * gst_poll_fd_ctl_write:
854 * @set: a file descriptor set.
855 * @fd: a file descriptor.
856 * @active: a new status.
858 * Control whether the descriptor @fd in @set will be monitored for
861 * Returns: %TRUE if the descriptor was successfully updated.
866 gst_poll_fd_ctl_write (GstPoll * set, GstPollFD * fd, gboolean active)
870 g_return_val_if_fail (set != NULL, FALSE);
871 g_return_val_if_fail (fd != NULL, FALSE);
872 g_return_val_if_fail (fd->fd >= 0, FALSE);
874 GST_DEBUG ("%p: fd (fd:%d, idx:%d), active : %d", set,
875 fd->fd, fd->idx, active);
877 g_mutex_lock (&set->lock);
879 idx = find_index (set->fds, fd);
882 struct pollfd *pfd = &g_array_index (set->fds, struct pollfd, idx);
885 pfd->events |= POLLOUT;
887 pfd->events &= ~POLLOUT;
889 GST_LOG ("pfd->events now %d (POLLOUT:%d)", pfd->events, POLLOUT);
891 gst_poll_update_winsock_event_mask (set, idx, FD_WRITE | FD_CONNECT,
896 GST_WARNING ("%p: couldn't find fd !", set);
899 g_mutex_unlock (&set->lock);
905 gst_poll_fd_ctl_read_unlocked (GstPoll * set, GstPollFD * fd, gboolean active)
909 GST_DEBUG ("%p: fd (fd:%d, idx:%d), active : %d", set,
910 fd->fd, fd->idx, active);
912 idx = find_index (set->fds, fd);
916 struct pollfd *pfd = &g_array_index (set->fds, struct pollfd, idx);
919 pfd->events |= (POLLIN | POLLPRI);
921 pfd->events &= ~(POLLIN | POLLPRI);
923 gst_poll_update_winsock_event_mask (set, idx, FD_READ | FD_ACCEPT, active);
927 GST_WARNING ("%p: couldn't find fd !", set);
934 * gst_poll_fd_ctl_read:
935 * @set: a file descriptor set.
936 * @fd: a file descriptor.
937 * @active: a new status.
939 * Control whether the descriptor @fd in @set will be monitored for
942 * Returns: %TRUE if the descriptor was successfully updated.
947 gst_poll_fd_ctl_read (GstPoll * set, GstPollFD * fd, gboolean active)
951 g_return_val_if_fail (set != NULL, FALSE);
952 g_return_val_if_fail (fd != NULL, FALSE);
953 g_return_val_if_fail (fd->fd >= 0, FALSE);
955 g_mutex_lock (&set->lock);
957 ret = gst_poll_fd_ctl_read_unlocked (set, fd, active);
959 g_mutex_unlock (&set->lock);
965 * gst_poll_fd_ignored:
966 * @set: a file descriptor set.
967 * @fd: a file descriptor.
969 * Mark @fd as ignored so that the next call to gst_poll_wait() will yield
970 * the same result for @fd as last time. This function must be called if no
971 * operation (read/write/recv/send/etc.) will be performed on @fd before
972 * the next call to gst_poll_wait().
974 * The reason why this is needed is because the underlying implementation
975 * might not allow querying the fd more than once between calls to one of
976 * the re-enabling operations.
981 gst_poll_fd_ignored (GstPoll * set, GstPollFD * fd)
986 g_return_if_fail (set != NULL);
987 g_return_if_fail (fd != NULL);
988 g_return_if_fail (fd->fd >= 0);
990 g_mutex_lock (&set->lock);
992 idx = find_index (set->fds, fd);
994 WinsockFd *wfd = &g_array_index (set->fds, WinsockFd, idx);
996 wfd->ignored_event_mask = wfd->event_mask & (FD_READ | FD_WRITE);
1000 g_mutex_unlock (&set->lock);
1005 * gst_poll_fd_has_closed:
1006 * @set: a file descriptor set.
1007 * @fd: a file descriptor.
1009 * Check if @fd in @set has closed the connection.
1011 * Returns: %TRUE if the connection was closed.
1016 gst_poll_fd_has_closed (const GstPoll * set, GstPollFD * fd)
1018 gboolean res = FALSE;
1021 g_return_val_if_fail (set != NULL, FALSE);
1022 g_return_val_if_fail (fd != NULL, FALSE);
1023 g_return_val_if_fail (fd->fd >= 0, FALSE);
1025 GST_DEBUG ("%p: fd (fd:%d, idx:%d)", set, fd->fd, fd->idx);
1027 g_mutex_lock (&((GstPoll *) set)->lock);
1029 idx = find_index (set->active_fds, fd);
1032 struct pollfd *pfd = &g_array_index (set->active_fds, struct pollfd, idx);
1034 res = (pfd->revents & POLLHUP) != 0;
1036 WinsockFd *wfd = &g_array_index (set->active_fds, WinsockFd, idx);
1038 res = (wfd->events.lNetworkEvents & FD_CLOSE) != 0;
1041 GST_WARNING ("%p: couldn't find fd !", set);
1044 g_mutex_unlock (&((GstPoll *) set)->lock);
1050 * gst_poll_fd_has_error:
1051 * @set: a file descriptor set.
1052 * @fd: a file descriptor.
1054 * Check if @fd in @set has an error.
1056 * Returns: %TRUE if the descriptor has an error.
1061 gst_poll_fd_has_error (const GstPoll * set, GstPollFD * fd)
1063 gboolean res = FALSE;
1066 g_return_val_if_fail (set != NULL, FALSE);
1067 g_return_val_if_fail (fd != NULL, FALSE);
1068 g_return_val_if_fail (fd->fd >= 0, FALSE);
1070 GST_DEBUG ("%p: fd (fd:%d, idx:%d)", set, fd->fd, fd->idx);
1072 g_mutex_lock (&((GstPoll *) set)->lock);
1074 idx = find_index (set->active_fds, fd);
1077 struct pollfd *pfd = &g_array_index (set->active_fds, struct pollfd, idx);
1079 res = (pfd->revents & (POLLERR | POLLNVAL)) != 0;
1081 WinsockFd *wfd = &g_array_index (set->active_fds, WinsockFd, idx);
1083 res = (wfd->events.iErrorCode[FD_CLOSE_BIT] != 0) ||
1084 (wfd->events.iErrorCode[FD_READ_BIT] != 0) ||
1085 (wfd->events.iErrorCode[FD_WRITE_BIT] != 0) ||
1086 (wfd->events.iErrorCode[FD_ACCEPT_BIT] != 0) ||
1087 (wfd->events.iErrorCode[FD_CONNECT_BIT] != 0);
1090 GST_WARNING ("%p: couldn't find fd !", set);
1093 g_mutex_unlock (&((GstPoll *) set)->lock);
1099 gst_poll_fd_can_read_unlocked (const GstPoll * set, GstPollFD * fd)
1101 gboolean res = FALSE;
1104 GST_DEBUG ("%p: fd (fd:%d, idx:%d)", set, fd->fd, fd->idx);
1106 idx = find_index (set->active_fds, fd);
1109 struct pollfd *pfd = &g_array_index (set->active_fds, struct pollfd, idx);
1111 res = (pfd->revents & (POLLIN | POLLPRI)) != 0;
1113 WinsockFd *wfd = &g_array_index (set->active_fds, WinsockFd, idx);
1115 res = (wfd->events.lNetworkEvents & (FD_READ | FD_ACCEPT)) != 0;
1118 GST_WARNING ("%p: couldn't find fd !", set);
1125 * gst_poll_fd_can_read:
1126 * @set: a file descriptor set.
1127 * @fd: a file descriptor.
1129 * Check if @fd in @set has data to be read.
1131 * Returns: %TRUE if the descriptor has data to be read.
1136 gst_poll_fd_can_read (const GstPoll * set, GstPollFD * fd)
1138 gboolean res = FALSE;
1140 g_return_val_if_fail (set != NULL, FALSE);
1141 g_return_val_if_fail (fd != NULL, FALSE);
1142 g_return_val_if_fail (fd->fd >= 0, FALSE);
1144 g_mutex_lock (&((GstPoll *) set)->lock);
1146 res = gst_poll_fd_can_read_unlocked (set, fd);
1148 g_mutex_unlock (&((GstPoll *) set)->lock);
1154 * gst_poll_fd_can_write:
1155 * @set: a file descriptor set.
1156 * @fd: a file descriptor.
1158 * Check if @fd in @set can be used for writing.
1160 * Returns: %TRUE if the descriptor can be used for writing.
1165 gst_poll_fd_can_write (const GstPoll * set, GstPollFD * fd)
1167 gboolean res = FALSE;
1170 g_return_val_if_fail (set != NULL, FALSE);
1171 g_return_val_if_fail (fd != NULL, FALSE);
1172 g_return_val_if_fail (fd->fd >= 0, FALSE);
1174 GST_DEBUG ("%p: fd (fd:%d, idx:%d)", set, fd->fd, fd->idx);
1176 g_mutex_lock (&((GstPoll *) set)->lock);
1178 idx = find_index (set->active_fds, fd);
1181 struct pollfd *pfd = &g_array_index (set->active_fds, struct pollfd, idx);
1183 res = (pfd->revents & POLLOUT) != 0;
1185 WinsockFd *wfd = &g_array_index (set->active_fds, WinsockFd, idx);
1187 res = (wfd->events.lNetworkEvents & FD_WRITE) != 0;
1190 GST_WARNING ("%p: couldn't find fd !", set);
1193 g_mutex_unlock (&((GstPoll *) set)->lock);
1201 * @timeout: a timeout in nanoseconds.
1203 * Wait for activity on the file descriptors in @set. This function waits up to
1204 * the specified @timeout. A timeout of #GST_CLOCK_TIME_NONE waits forever.
1206 * For #GstPoll objects created with gst_poll_new(), this function can only be
1207 * called from a single thread at a time. If called from multiple threads,
1208 * -1 will be returned with errno set to EPERM.
1210 * This is not true for timer #GstPoll objects created with
1211 * gst_poll_new_timer(), where it is allowed to have multiple threads waiting
1214 * Returns: The number of #GstPollFD in @set that have activity or 0 when no
1215 * activity was detected after @timeout. If an error occurs, -1 is returned
1221 gst_poll_wait (GstPoll * set, GstClockTime timeout)
1223 gboolean restarting;
1228 g_return_val_if_fail (set != NULL, -1);
1230 GST_DEBUG ("timeout :%" GST_TIME_FORMAT, GST_TIME_ARGS (timeout));
1232 is_timer = set->timer;
1234 /* add one more waiter */
1235 old_waiting = INC_WAITING (set);
1237 /* we cannot wait from multiple threads unless we are a timer */
1238 if (G_UNLIKELY (old_waiting > 0 && !is_timer))
1239 goto already_waiting;
1241 /* flushing, exit immediately */
1242 if (G_UNLIKELY (IS_FLUSHING (set)))
1251 mode = choose_mode (set, timeout);
1253 if (TEST_REBUILD (set)) {
1254 g_mutex_lock (&set->lock);
1256 g_array_set_size (set->active_fds, set->fds->len);
1257 memcpy (set->active_fds->data, set->fds->data,
1258 set->fds->len * sizeof (struct pollfd));
1260 if (!gst_poll_prepare_winsock_active_sets (set))
1263 g_mutex_unlock (&set->lock);
1267 case GST_POLL_MODE_AUTO:
1268 g_assert_not_reached ();
1270 case GST_POLL_MODE_PPOLL:
1274 struct timespec *tsptr;
1276 if (timeout != GST_CLOCK_TIME_NONE) {
1277 GST_TIME_TO_TIMESPEC (timeout, ts);
1284 ppoll ((struct pollfd *) set->active_fds->data,
1285 set->active_fds->len, tsptr, NULL);
1287 g_assert_not_reached ();
1292 case GST_POLL_MODE_POLL:
1297 if (timeout != GST_CLOCK_TIME_NONE) {
1298 t = GST_TIME_AS_MSECONDS (timeout);
1304 poll ((struct pollfd *) set->active_fds->data,
1305 set->active_fds->len, t);
1307 g_assert_not_reached ();
1312 case GST_POLL_MODE_PSELECT:
1313 #ifndef HAVE_PSELECT
1315 g_assert_not_reached ();
1320 case GST_POLL_MODE_SELECT:
1328 max_fd = pollfd_to_fd_set (set, &readfds, &writefds, &errorfds);
1330 if (mode == GST_POLL_MODE_SELECT) {
1332 struct timeval *tvptr;
1334 if (timeout != GST_CLOCK_TIME_NONE) {
1335 GST_TIME_TO_TIMEVAL (timeout, tv);
1341 GST_DEBUG ("Calling select");
1342 res = select (max_fd + 1, &readfds, &writefds, &errorfds, tvptr);
1343 GST_DEBUG ("After select, res:%d", res);
1347 struct timespec *tsptr;
1349 if (timeout != GST_CLOCK_TIME_NONE) {
1350 GST_TIME_TO_TIMESPEC (timeout, ts);
1356 GST_DEBUG ("Calling pselect");
1358 pselect (max_fd + 1, &readfds, &writefds, &errorfds, tsptr, NULL);
1359 GST_DEBUG ("After pselect, res:%d", res);
1364 fd_set_to_pollfd (set, &readfds, &writefds, &errorfds);
1366 #else /* G_OS_WIN32 */
1367 g_assert_not_reached ();
1372 case GST_POLL_MODE_WINDOWS:
1375 gint ignore_count = set->active_fds_ignored->len;
1378 if (G_LIKELY (ignore_count == 0)) {
1379 if (timeout != GST_CLOCK_TIME_NONE)
1380 t = GST_TIME_AS_MSECONDS (timeout);
1384 /* already one or more ignored fds, so we quickly sweep the others */
1388 if (set->active_events->len != 0) {
1389 wait_ret = WSAWaitForMultipleEvents (set->active_events->len,
1390 (HANDLE *) set->active_events->data, FALSE, t, FALSE);
1392 wait_ret = WSA_WAIT_FAILED;
1393 WSASetLastError (WSA_INVALID_PARAMETER);
1396 if (ignore_count == 0 && wait_ret == WSA_WAIT_TIMEOUT) {
1398 } else if (wait_ret == WSA_WAIT_FAILED) {
1400 errno = gst_poll_winsock_error_to_errno (WSAGetLastError ());
1402 /* the first entry is the wakeup event */
1403 if (wait_ret - WSA_WAIT_EVENT_0 >= 1) {
1404 res = gst_poll_collect_winsock_events (set);
1406 res = 1; /* wakeup event */
1410 g_assert_not_reached ();
1418 /* Applications needs to clear the control socket themselves for timer
1420 * For other polls, we need to clear the control socket. If there was only
1421 * one socket with activity and it was the control socket, we need to
1423 if (release_all_wakeup (set) > 0 && res == 1)
1427 /* we got woken up and we are flushing, we need to stop */
1428 if (G_UNLIKELY (IS_FLUSHING (set)))
1431 } while (G_UNLIKELY (restarting));
1440 GST_LOG ("%p: we are already waiting", set);
1447 GST_LOG ("%p: we are flushing", set);
1455 GST_LOG ("%p: winsock error", set);
1456 g_mutex_unlock (&set->lock);
1464 * gst_poll_set_controllable:
1466 * @controllable: new controllable state.
1468 * When @controllable is %TRUE, this function ensures that future calls to
1469 * gst_poll_wait() will be affected by gst_poll_restart() and
1470 * gst_poll_set_flushing().
1472 * Returns: %TRUE if the controllability of @set could be updated.
1477 gst_poll_set_controllable (GstPoll * set, gboolean controllable)
1479 g_return_val_if_fail (set != NULL, FALSE);
1481 GST_LOG ("%p: controllable : %d", set, controllable);
1483 set->controllable = controllable;
1492 * Restart any gst_poll_wait() that is in progress. This function is typically
1493 * used after adding or removing descriptors to @set.
1495 * If @set is not controllable, then this call will have no effect.
1500 gst_poll_restart (GstPoll * set)
1502 g_return_if_fail (set != NULL);
1504 if (set->controllable && GET_WAITING (set) > 0) {
1505 /* we are controllable and waiting, wake up the waiter. The socket will be
1506 * cleared by the _wait() thread and the poll will be restarted */
1512 * gst_poll_set_flushing:
1514 * @flushing: new flushing state.
1516 * When @flushing is %TRUE, this function ensures that current and future calls
1517 * to gst_poll_wait() will return -1, with errno set to EBUSY.
1519 * Unsetting the flushing state will restore normal operation of @set.
1524 gst_poll_set_flushing (GstPoll * set, gboolean flushing)
1526 g_return_if_fail (set != NULL);
1528 GST_LOG ("%p: flushing: %d", set, flushing);
1530 /* update the new state first */
1531 SET_FLUSHING (set, flushing);
1533 if (flushing && set->controllable && GET_WAITING (set) > 0) {
1534 /* we are flushing, controllable and waiting, wake up the waiter. When we
1535 * stop the flushing operation we don't clear the wakeup fd here, this will
1536 * happen in the _wait() thread. */
1542 * gst_poll_write_control:
1545 * Write a byte to the control socket of the controllable @set.
1546 * This function is mostly useful for timer #GstPoll objects created with
1547 * gst_poll_new_timer().
1549 * It will make any current and future gst_poll_wait() function return with
1550 * 1, meaning the control socket is set. After an equal amount of calls to
1551 * gst_poll_read_control() have been performed, calls to gst_poll_wait() will
1552 * block again until their timeout expired.
1554 * Returns: %TRUE on success. %FALSE when @set is not controllable or when the
1555 * byte could not be written.
1560 gst_poll_write_control (GstPoll * set)
1564 g_return_val_if_fail (set != NULL, FALSE);
1565 g_return_val_if_fail (set->timer, FALSE);
1567 res = raise_wakeup (set);
1573 * gst_poll_read_control:
1576 * Read a byte from the control socket of the controllable @set.
1577 * This function is mostly useful for timer #GstPoll objects created with
1578 * gst_poll_new_timer().
1580 * Returns: %TRUE on success. %FALSE when @set is not controllable or when there
1581 * was no byte to read.
1586 gst_poll_read_control (GstPoll * set)
1590 g_return_val_if_fail (set != NULL, FALSE);
1591 g_return_val_if_fail (set->timer, FALSE);
1593 res = release_wakeup (set);