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 writeable. 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 writeable 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 writeable, 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>
75 #define EINPROGRESS WSAEINPROGRESS
80 #include <sys/socket.h>
83 /* OS/X needs this because of bad headers */
86 /* The poll() emulation on OS/X doesn't handle fds=NULL, nfds=0,
87 * so we prefer our own poll emulation.
89 #if defined(BROKEN_POLL)
95 #define GST_CAT_DEFAULT GST_CAT_POLL
98 typedef struct _WinsockFd WinsockFd;
104 WSANETWORKEVENTS events;
105 glong ignored_event_mask;
112 GST_POLL_MODE_SELECT,
113 GST_POLL_MODE_PSELECT,
116 GST_POLL_MODE_WINDOWS
124 /* array of fds, always written to and read from with lock */
126 /* array of active fds, only written to from the waiting thread with the
127 * lock and read from with the lock or without the lock from the waiting
133 GstPollFD control_read_fd;
134 GstPollFD control_write_fd;
136 GArray *active_fds_ignored;
138 GArray *active_events;
143 gboolean controllable;
144 volatile gint waiting;
145 volatile gint control_pending;
146 volatile gint flushing;
148 volatile gint rebuild;
151 static gboolean gst_poll_fd_ctl_read_unlocked (GstPoll * set, GstPollFD * fd,
153 static gboolean gst_poll_add_fd_unlocked (GstPoll * set, GstPollFD * fd);
155 #define IS_FLUSHING(s) (g_atomic_int_get(&(s)->flushing))
156 #define SET_FLUSHING(s,val) (g_atomic_int_set(&(s)->flushing, (val)))
158 #define INC_WAITING(s) (G_ATOMIC_INT_ADD(&(s)->waiting, 1))
159 #define DEC_WAITING(s) (G_ATOMIC_INT_ADD(&(s)->waiting, -1))
160 #define GET_WAITING(s) (g_atomic_int_get(&(s)->waiting))
162 #define TEST_REBUILD(s) (g_atomic_int_compare_and_exchange(&(s)->rebuild, 1, 0))
163 #define MARK_REBUILD(s) (g_atomic_int_set(&(s)->rebuild, 1))
166 #define WAKE_EVENT(s) (write ((s)->control_write_fd.fd, "W", 1) == 1)
167 #define RELEASE_EVENT(s) (read ((s)->control_read_fd.fd, (s)->buf, 1) == 1)
169 #define WAKE_EVENT(s) (SetEvent ((s)->wakeup_event), errno = GetLastError () == NO_ERROR ? 0 : EACCES, errno == 0 ? 1 : 0)
170 #define RELEASE_EVENT(s) (ResetEvent ((s)->wakeup_event))
173 /* the poll/select call is also performed on a control socket, that way
174 * we can send special commands to control it */
175 static inline gboolean
176 raise_wakeup (GstPoll * set)
178 gboolean result = TRUE;
180 if (G_ATOMIC_INT_ADD (&set->control_pending, 1) == 0) {
181 /* raise when nothing pending */
182 GST_LOG ("%p: raise", set);
183 result = WAKE_EVENT (set);
188 /* note how bad things can happen when the 2 threads both raise and release the
189 * wakeup. This is however not a problem because you must always pair a raise
191 static inline gboolean
192 release_wakeup (GstPoll * set)
194 gboolean result = TRUE;
196 if (g_atomic_int_dec_and_test (&set->control_pending)) {
197 GST_LOG ("%p: release", set);
198 result = RELEASE_EVENT (set);
204 release_all_wakeup (GstPoll * set)
209 if (!(old = g_atomic_int_get (&set->control_pending)))
210 /* nothing pending, just exit */
213 /* try to remove all pending control messages */
214 if (g_atomic_int_compare_and_exchange (&set->control_pending, old, 0)) {
215 /* we managed to remove all messages, read the control socket */
216 if (RELEASE_EVENT (set))
219 /* retry again until we read it successfully */
220 G_ATOMIC_INT_ADD (&set->control_pending, 1);
227 find_index (GArray * array, GstPollFD * fd)
236 /* start by assuming the index found in the fd is still valid */
237 if (fd->idx >= 0 && fd->idx < array->len) {
239 ifd = &g_array_index (array, struct pollfd, fd->idx);
241 ifd = &g_array_index (array, WinsockFd, fd->idx);
244 if (ifd->fd == fd->fd) {
249 /* the pollfd array has changed and we need to lookup the fd again */
250 for (i = 0; i < array->len; i++) {
252 ifd = &g_array_index (array, struct pollfd, i);
254 ifd = &g_array_index (array, WinsockFd, i);
257 if (ifd->fd == fd->fd) {
267 #if !defined(HAVE_PPOLL) && defined(HAVE_POLL)
268 /* check if all file descriptors will fit in an fd_set */
270 selectable_fds (const GstPoll * set)
274 g_mutex_lock (set->lock);
275 for (i = 0; i < set->fds->len; i++) {
276 struct pollfd *pfd = &g_array_index (set->fds, struct pollfd, i);
278 if (pfd->fd >= FD_SETSIZE)
281 g_mutex_unlock (set->lock);
287 g_mutex_unlock (set->lock);
292 /* check if the timeout will convert to a timeout value used for poll()
293 * without a loss of precision
296 pollable_timeout (GstClockTime timeout)
298 if (timeout == GST_CLOCK_TIME_NONE)
301 /* not a nice multiple of milliseconds */
302 if (timeout % 1000000)
310 choose_mode (const GstPoll * set, GstClockTime timeout)
314 if (set->mode == GST_POLL_MODE_AUTO) {
316 mode = GST_POLL_MODE_PPOLL;
317 #elif defined(HAVE_POLL)
318 if (!selectable_fds (set) || pollable_timeout (timeout)) {
319 mode = GST_POLL_MODE_POLL;
322 mode = GST_POLL_MODE_PSELECT;
324 mode = GST_POLL_MODE_SELECT;
327 #elif defined(HAVE_PSELECT)
328 mode = GST_POLL_MODE_PSELECT;
330 mode = GST_POLL_MODE_SELECT;
340 pollfd_to_fd_set (GstPoll * set, fd_set * readfds, fd_set * writefds,
350 g_mutex_lock (set->lock);
352 for (i = 0; i < set->active_fds->len; i++) {
353 struct pollfd *pfd = &g_array_index (set->fds, struct pollfd, i);
355 if (pfd->fd < FD_SETSIZE) {
356 if (pfd->events & POLLIN)
357 FD_SET (pfd->fd, readfds);
358 if (pfd->events & POLLOUT)
359 FD_SET (pfd->fd, writefds);
361 FD_SET (pfd->fd, errorfds);
362 if (pfd->fd > max_fd && (pfd->events & (POLLIN | POLLOUT)))
367 g_mutex_unlock (set->lock);
373 fd_set_to_pollfd (GstPoll * set, fd_set * readfds, fd_set * writefds,
378 g_mutex_lock (set->lock);
380 for (i = 0; i < set->active_fds->len; i++) {
381 struct pollfd *pfd = &g_array_index (set->active_fds, struct pollfd, i);
383 if (pfd->fd < FD_SETSIZE) {
385 if (FD_ISSET (pfd->fd, readfds))
386 pfd->revents |= POLLIN;
387 if (FD_ISSET (pfd->fd, writefds))
388 pfd->revents |= POLLOUT;
389 if (FD_ISSET (pfd->fd, errorfds))
390 pfd->revents |= POLLERR;
394 g_mutex_unlock (set->lock);
396 #else /* G_OS_WIN32 */
398 * Translate errors thrown by the Winsock API used by GstPoll:
399 * WSAEventSelect, WSAWaitForMultipleEvents and WSAEnumNetworkEvents
402 gst_poll_winsock_error_to_errno (DWORD last_error)
404 switch (last_error) {
405 case WSA_INVALID_HANDLE:
410 case WSA_NOT_ENOUGH_MEMORY:
414 * Anything else, including:
415 * WSA_INVALID_PARAMETER, WSAEFAULT, WSAEINPROGRESS, WSAENETDOWN,
424 gst_poll_free_winsock_event (GstPoll * set, gint idx)
426 WinsockFd *wfd = &g_array_index (set->fds, WinsockFd, idx);
427 HANDLE event = g_array_index (set->events, HANDLE, idx);
429 WSAEventSelect (wfd->fd, event, 0);
434 gst_poll_update_winsock_event_mask (GstPoll * set, gint idx, glong flags,
439 wfd = &g_array_index (set->fds, WinsockFd, idx);
442 wfd->event_mask |= flags;
444 wfd->event_mask &= ~flags;
446 /* reset ignored state if the new mask doesn't overlap at all */
447 if ((wfd->ignored_event_mask & wfd->event_mask) == 0)
448 wfd->ignored_event_mask = 0;
452 gst_poll_prepare_winsock_active_sets (GstPoll * set)
456 g_array_set_size (set->active_fds, 0);
457 g_array_set_size (set->active_fds_ignored, 0);
458 g_array_set_size (set->active_events, 0);
459 g_array_append_val (set->active_events, set->wakeup_event);
461 for (i = 0; i < set->fds->len; i++) {
462 WinsockFd *wfd = &g_array_index (set->fds, WinsockFd, i);
463 HANDLE event = g_array_index (set->events, HANDLE, i);
465 if (wfd->ignored_event_mask == 0) {
468 g_array_append_val (set->active_fds, *wfd);
469 g_array_append_val (set->active_events, event);
471 ret = WSAEventSelect (wfd->fd, event, wfd->event_mask);
472 if (G_UNLIKELY (ret != 0)) {
473 errno = gst_poll_winsock_error_to_errno (WSAGetLastError ());
477 g_array_append_val (set->active_fds_ignored, wfd);
485 gst_poll_collect_winsock_events (GstPoll * set)
490 * We need to check which events are signaled, and call
491 * WSAEnumNetworkEvents for those that are, which resets
492 * the event and clears the internal network event records.
495 for (i = 0; i < set->active_fds->len; i++) {
496 WinsockFd *wfd = &g_array_index (set->active_fds, WinsockFd, i);
497 HANDLE event = g_array_index (set->active_events, HANDLE, i + 1);
500 wait_ret = WaitForSingleObject (event, 0);
501 if (wait_ret == WAIT_OBJECT_0) {
502 gint enum_ret = WSAEnumNetworkEvents (wfd->fd, event, &wfd->events);
504 if (G_UNLIKELY (enum_ret != 0)) {
506 errno = gst_poll_winsock_error_to_errno (WSAGetLastError ());
512 /* clear any previously stored result */
513 memset (&wfd->events, 0, sizeof (wfd->events));
517 /* If all went well we also need to reset the ignored fds. */
519 res += set->active_fds_ignored->len;
521 for (i = 0; i < set->active_fds_ignored->len; i++) {
522 WinsockFd *wfd = g_array_index (set->active_fds_ignored, WinsockFd *, i);
524 wfd->ignored_event_mask = 0;
527 g_array_set_size (set->active_fds_ignored, 0);
535 * gst_poll_new: (skip)
536 * @controllable: whether it should be possible to control a wait.
538 * Create a new file descriptor set. If @controllable, it
539 * is possible to restart or flush a call to gst_poll_wait() with
540 * gst_poll_restart() and gst_poll_set_flushing() respectively.
542 * Free-function: gst_poll_free
544 * Returns: (transfer full): a new #GstPoll, or %NULL in case of an error.
545 * Free with gst_poll_free().
550 gst_poll_new (gboolean controllable)
554 GST_DEBUG ("controllable : %d", controllable);
556 nset = g_slice_new0 (GstPoll);
557 nset->lock = g_mutex_new ();
559 nset->mode = GST_POLL_MODE_AUTO;
560 nset->fds = g_array_new (FALSE, FALSE, sizeof (struct pollfd));
561 nset->active_fds = g_array_new (FALSE, FALSE, sizeof (struct pollfd));
562 nset->control_read_fd.fd = -1;
563 nset->control_write_fd.fd = -1;
565 gint control_sock[2];
567 if (socketpair (PF_UNIX, SOCK_STREAM, 0, control_sock) < 0)
570 fcntl (control_sock[0], F_SETFL, O_NONBLOCK);
571 fcntl (control_sock[1], F_SETFL, O_NONBLOCK);
573 nset->control_read_fd.fd = control_sock[0];
574 nset->control_write_fd.fd = control_sock[1];
576 gst_poll_add_fd_unlocked (nset, &nset->control_read_fd);
577 gst_poll_fd_ctl_read_unlocked (nset, &nset->control_read_fd, TRUE);
580 nset->mode = GST_POLL_MODE_WINDOWS;
581 nset->fds = g_array_new (FALSE, FALSE, sizeof (WinsockFd));
582 nset->active_fds = g_array_new (FALSE, FALSE, sizeof (WinsockFd));
583 nset->active_fds_ignored = g_array_new (FALSE, FALSE, sizeof (WinsockFd *));
584 nset->events = g_array_new (FALSE, FALSE, sizeof (HANDLE));
585 nset->active_events = g_array_new (FALSE, FALSE, sizeof (HANDLE));
587 nset->wakeup_event = CreateEvent (NULL, TRUE, FALSE, NULL);
590 /* ensure (re)build, though already sneakily set in non-windows case */
593 nset->controllable = controllable;
601 GST_WARNING ("%p: can't create socket pair !", nset);
602 gst_poll_free (nset);
609 * gst_poll_new_timer: (skip)
611 * Create a new poll object that can be used for scheduling cancellable
614 * A timeout is performed with gst_poll_wait(). Multiple timeouts can be
615 * performed from different threads.
617 * Free-function: gst_poll_free
619 * Returns: (transfer full): a new #GstPoll, or %NULL in case of an error.
620 * Free with gst_poll_free().
625 gst_poll_new_timer (void)
629 /* make a new controllable poll set */
630 if (!(poll = gst_poll_new (TRUE)))
642 * @set: (transfer full): a file descriptor set.
644 * Free a file descriptor set.
649 gst_poll_free (GstPoll * set)
651 g_return_if_fail (set != NULL);
653 GST_DEBUG ("%p: freeing", set);
656 if (set->control_write_fd.fd >= 0)
657 close (set->control_write_fd.fd);
658 if (set->control_read_fd.fd >= 0)
659 close (set->control_read_fd.fd);
661 CloseHandle (set->wakeup_event);
666 for (i = 0; i < set->events->len; i++)
667 gst_poll_free_winsock_event (set, i);
670 g_array_free (set->active_events, TRUE);
671 g_array_free (set->events, TRUE);
672 g_array_free (set->active_fds_ignored, TRUE);
675 g_array_free (set->active_fds, TRUE);
676 g_array_free (set->fds, TRUE);
677 g_mutex_free (set->lock);
678 g_slice_free (GstPoll, set);
682 * gst_poll_get_read_gpollfd:
686 * Get a GPollFD for the reading part of the control socket. This is useful when
687 * integrating with a GSource and GMainLoop.
692 gst_poll_get_read_gpollfd (GstPoll * set, GPollFD * fd)
694 g_return_if_fail (set != NULL);
695 g_return_if_fail (fd != NULL);
698 fd->fd = set->control_read_fd.fd;
700 #if GLIB_SIZEOF_VOID_P == 8
701 fd->fd = (gint64) set->wakeup_event;
703 fd->fd = (gint) set->wakeup_event;
706 fd->events = G_IO_IN | G_IO_HUP | G_IO_ERR;
714 * Initializes @fd. Alternatively you can initialize it with
720 gst_poll_fd_init (GstPollFD * fd)
722 g_return_if_fail (fd != NULL);
729 gst_poll_add_fd_unlocked (GstPoll * set, GstPollFD * fd)
733 GST_DEBUG ("%p: fd (fd:%d, idx:%d)", set, fd->fd, fd->idx);
735 idx = find_index (set->fds, fd);
741 nfd.events = POLLERR | POLLNVAL | POLLHUP;
744 g_array_append_val (set->fds, nfd);
746 fd->idx = set->fds->len - 1;
752 wfd.event_mask = FD_CLOSE;
753 memset (&wfd.events, 0, sizeof (wfd.events));
754 wfd.ignored_event_mask = 0;
755 event = WSACreateEvent ();
757 g_array_append_val (set->fds, wfd);
758 g_array_append_val (set->events, event);
760 fd->idx = set->fds->len - 1;
764 GST_WARNING ("%p: couldn't find fd !", set);
772 * @set: a file descriptor set.
773 * @fd: a file descriptor.
775 * Add a file descriptor to the file descriptor set.
777 * Returns: %TRUE if the file descriptor was successfully added to the set.
782 gst_poll_add_fd (GstPoll * set, GstPollFD * fd)
786 g_return_val_if_fail (set != NULL, FALSE);
787 g_return_val_if_fail (fd != NULL, FALSE);
788 g_return_val_if_fail (fd->fd >= 0, FALSE);
790 g_mutex_lock (set->lock);
792 ret = gst_poll_add_fd_unlocked (set, fd);
794 g_mutex_unlock (set->lock);
800 * gst_poll_remove_fd:
801 * @set: a file descriptor set.
802 * @fd: a file descriptor.
804 * Remove a file descriptor from the file descriptor set.
806 * Returns: %TRUE if the file descriptor was successfully removed from the set.
811 gst_poll_remove_fd (GstPoll * set, GstPollFD * fd)
815 g_return_val_if_fail (set != NULL, FALSE);
816 g_return_val_if_fail (fd != NULL, FALSE);
817 g_return_val_if_fail (fd->fd >= 0, FALSE);
820 GST_DEBUG ("%p: fd (fd:%d, idx:%d)", set, fd->fd, fd->idx);
822 g_mutex_lock (set->lock);
824 /* get the index, -1 is an fd that is not added */
825 idx = find_index (set->fds, fd);
828 gst_poll_free_winsock_event (set, idx);
829 g_array_remove_index_fast (set->events, idx);
832 /* remove the fd at index, we use _remove_index_fast, which copies the last
833 * element of the array to the freed index */
834 g_array_remove_index_fast (set->fds, idx);
836 /* mark fd as removed by setting the index to -1 */
840 GST_WARNING ("%p: couldn't find fd !", set);
843 g_mutex_unlock (set->lock);
849 * gst_poll_fd_ctl_write:
850 * @set: a file descriptor set.
851 * @fd: a file descriptor.
852 * @active: a new status.
854 * Control whether the descriptor @fd in @set will be monitored for
857 * Returns: %TRUE if the descriptor was successfully updated.
862 gst_poll_fd_ctl_write (GstPoll * set, GstPollFD * fd, gboolean active)
866 g_return_val_if_fail (set != NULL, FALSE);
867 g_return_val_if_fail (fd != NULL, FALSE);
868 g_return_val_if_fail (fd->fd >= 0, FALSE);
870 GST_DEBUG ("%p: fd (fd:%d, idx:%d), active : %d", set,
871 fd->fd, fd->idx, active);
873 g_mutex_lock (set->lock);
875 idx = find_index (set->fds, fd);
878 struct pollfd *pfd = &g_array_index (set->fds, struct pollfd, idx);
881 pfd->events |= POLLOUT;
883 pfd->events &= ~POLLOUT;
885 GST_LOG ("pfd->events now %d (POLLOUT:%d)", pfd->events, POLLOUT);
887 gst_poll_update_winsock_event_mask (set, idx, FD_WRITE | FD_CONNECT,
892 GST_WARNING ("%p: couldn't find fd !", set);
895 g_mutex_unlock (set->lock);
901 gst_poll_fd_ctl_read_unlocked (GstPoll * set, GstPollFD * fd, gboolean active)
905 GST_DEBUG ("%p: fd (fd:%d, idx:%d), active : %d", set,
906 fd->fd, fd->idx, active);
908 idx = find_index (set->fds, fd);
912 struct pollfd *pfd = &g_array_index (set->fds, struct pollfd, idx);
915 pfd->events |= (POLLIN | POLLPRI);
917 pfd->events &= ~(POLLIN | POLLPRI);
919 gst_poll_update_winsock_event_mask (set, idx, FD_READ | FD_ACCEPT, active);
923 GST_WARNING ("%p: couldn't find fd !", set);
930 * gst_poll_fd_ctl_read:
931 * @set: a file descriptor set.
932 * @fd: a file descriptor.
933 * @active: a new status.
935 * Control whether the descriptor @fd in @set will be monitored for
938 * Returns: %TRUE if the descriptor was successfully updated.
943 gst_poll_fd_ctl_read (GstPoll * set, GstPollFD * fd, gboolean active)
947 g_return_val_if_fail (set != NULL, FALSE);
948 g_return_val_if_fail (fd != NULL, FALSE);
949 g_return_val_if_fail (fd->fd >= 0, FALSE);
951 g_mutex_lock (set->lock);
953 ret = gst_poll_fd_ctl_read_unlocked (set, fd, active);
955 g_mutex_unlock (set->lock);
961 * gst_poll_fd_ignored:
962 * @set: a file descriptor set.
963 * @fd: a file descriptor.
965 * Mark @fd as ignored so that the next call to gst_poll_wait() will yield
966 * the same result for @fd as last time. This function must be called if no
967 * operation (read/write/recv/send/etc.) will be performed on @fd before
968 * the next call to gst_poll_wait().
970 * The reason why this is needed is because the underlying implementation
971 * might not allow querying the fd more than once between calls to one of
972 * the re-enabling operations.
977 gst_poll_fd_ignored (GstPoll * set, GstPollFD * fd)
982 g_return_if_fail (set != NULL);
983 g_return_if_fail (fd != NULL);
984 g_return_if_fail (fd->fd >= 0);
986 g_mutex_lock (set->lock);
988 idx = find_index (set->fds, fd);
990 WinsockFd *wfd = &g_array_index (set->fds, WinsockFd, idx);
992 wfd->ignored_event_mask = wfd->event_mask & (FD_READ | FD_WRITE);
996 g_mutex_unlock (set->lock);
1001 * gst_poll_fd_has_closed:
1002 * @set: a file descriptor set.
1003 * @fd: a file descriptor.
1005 * Check if @fd in @set has closed the connection.
1007 * Returns: %TRUE if the connection was closed.
1012 gst_poll_fd_has_closed (const GstPoll * set, GstPollFD * fd)
1014 gboolean res = FALSE;
1017 g_return_val_if_fail (set != NULL, FALSE);
1018 g_return_val_if_fail (fd != NULL, FALSE);
1019 g_return_val_if_fail (fd->fd >= 0, FALSE);
1021 GST_DEBUG ("%p: fd (fd:%d, idx:%d)", set, fd->fd, fd->idx);
1023 g_mutex_lock (set->lock);
1025 idx = find_index (set->active_fds, fd);
1028 struct pollfd *pfd = &g_array_index (set->active_fds, struct pollfd, idx);
1030 res = (pfd->revents & POLLHUP) != 0;
1032 WinsockFd *wfd = &g_array_index (set->active_fds, WinsockFd, idx);
1034 res = (wfd->events.lNetworkEvents & FD_CLOSE) != 0;
1037 GST_WARNING ("%p: couldn't find fd !", set);
1040 g_mutex_unlock (set->lock);
1046 * gst_poll_fd_has_error:
1047 * @set: a file descriptor set.
1048 * @fd: a file descriptor.
1050 * Check if @fd in @set has an error.
1052 * Returns: %TRUE if the descriptor has an error.
1057 gst_poll_fd_has_error (const GstPoll * set, GstPollFD * fd)
1059 gboolean res = FALSE;
1062 g_return_val_if_fail (set != NULL, FALSE);
1063 g_return_val_if_fail (fd != NULL, FALSE);
1064 g_return_val_if_fail (fd->fd >= 0, FALSE);
1066 GST_DEBUG ("%p: fd (fd:%d, idx:%d)", set, fd->fd, fd->idx);
1068 g_mutex_lock (set->lock);
1070 idx = find_index (set->active_fds, fd);
1073 struct pollfd *pfd = &g_array_index (set->active_fds, struct pollfd, idx);
1075 res = (pfd->revents & (POLLERR | POLLNVAL)) != 0;
1077 WinsockFd *wfd = &g_array_index (set->active_fds, WinsockFd, idx);
1079 res = (wfd->events.iErrorCode[FD_CLOSE_BIT] != 0) ||
1080 (wfd->events.iErrorCode[FD_READ_BIT] != 0) ||
1081 (wfd->events.iErrorCode[FD_WRITE_BIT] != 0) ||
1082 (wfd->events.iErrorCode[FD_ACCEPT_BIT] != 0) ||
1083 (wfd->events.iErrorCode[FD_CONNECT_BIT] != 0);
1086 GST_WARNING ("%p: couldn't find fd !", set);
1089 g_mutex_unlock (set->lock);
1095 gst_poll_fd_can_read_unlocked (const GstPoll * set, GstPollFD * fd)
1097 gboolean res = FALSE;
1100 GST_DEBUG ("%p: fd (fd:%d, idx:%d)", set, fd->fd, fd->idx);
1102 idx = find_index (set->active_fds, fd);
1105 struct pollfd *pfd = &g_array_index (set->active_fds, struct pollfd, idx);
1107 res = (pfd->revents & (POLLIN | POLLPRI)) != 0;
1109 WinsockFd *wfd = &g_array_index (set->active_fds, WinsockFd, idx);
1111 res = (wfd->events.lNetworkEvents & (FD_READ | FD_ACCEPT)) != 0;
1114 GST_WARNING ("%p: couldn't find fd !", set);
1121 * gst_poll_fd_can_read:
1122 * @set: a file descriptor set.
1123 * @fd: a file descriptor.
1125 * Check if @fd in @set has data to be read.
1127 * Returns: %TRUE if the descriptor has data to be read.
1132 gst_poll_fd_can_read (const GstPoll * set, GstPollFD * fd)
1134 gboolean res = FALSE;
1136 g_return_val_if_fail (set != NULL, FALSE);
1137 g_return_val_if_fail (fd != NULL, FALSE);
1138 g_return_val_if_fail (fd->fd >= 0, FALSE);
1140 g_mutex_lock (set->lock);
1142 res = gst_poll_fd_can_read_unlocked (set, fd);
1144 g_mutex_unlock (set->lock);
1150 * gst_poll_fd_can_write:
1151 * @set: a file descriptor set.
1152 * @fd: a file descriptor.
1154 * Check if @fd in @set can be used for writing.
1156 * Returns: %TRUE if the descriptor can be used for writing.
1161 gst_poll_fd_can_write (const GstPoll * set, GstPollFD * fd)
1163 gboolean res = FALSE;
1166 g_return_val_if_fail (set != NULL, FALSE);
1167 g_return_val_if_fail (fd != NULL, FALSE);
1168 g_return_val_if_fail (fd->fd >= 0, FALSE);
1170 GST_DEBUG ("%p: fd (fd:%d, idx:%d)", set, fd->fd, fd->idx);
1172 g_mutex_lock (set->lock);
1174 idx = find_index (set->active_fds, fd);
1177 struct pollfd *pfd = &g_array_index (set->active_fds, struct pollfd, idx);
1179 res = (pfd->revents & POLLOUT) != 0;
1181 WinsockFd *wfd = &g_array_index (set->active_fds, WinsockFd, idx);
1183 res = (wfd->events.lNetworkEvents & FD_WRITE) != 0;
1186 GST_WARNING ("%p: couldn't find fd !", set);
1189 g_mutex_unlock (set->lock);
1197 * @timeout: a timeout in nanoseconds.
1199 * Wait for activity on the file descriptors in @set. This function waits up to
1200 * the specified @timeout. A timeout of #GST_CLOCK_TIME_NONE waits forever.
1202 * For #GstPoll objects created with gst_poll_new(), this function can only be
1203 * called from a single thread at a time. If called from multiple threads,
1204 * -1 will be returned with errno set to EPERM.
1206 * This is not true for timer #GstPoll objects created with
1207 * gst_poll_new_timer(), where it is allowed to have multiple threads waiting
1210 * Returns: The number of #GstPollFD in @set that have activity or 0 when no
1211 * activity was detected after @timeout. If an error occurs, -1 is returned
1217 gst_poll_wait (GstPoll * set, GstClockTime timeout)
1219 gboolean restarting;
1224 g_return_val_if_fail (set != NULL, -1);
1226 GST_DEBUG ("timeout :%" GST_TIME_FORMAT, GST_TIME_ARGS (timeout));
1228 is_timer = set->timer;
1230 /* add one more waiter */
1231 old_waiting = INC_WAITING (set);
1233 /* we cannot wait from multiple threads unless we are a timer */
1234 if (G_UNLIKELY (old_waiting > 0 && !is_timer))
1235 goto already_waiting;
1237 /* flushing, exit immediately */
1238 if (G_UNLIKELY (IS_FLUSHING (set)))
1247 mode = choose_mode (set, timeout);
1249 if (TEST_REBUILD (set)) {
1250 g_mutex_lock (set->lock);
1252 g_array_set_size (set->active_fds, set->fds->len);
1253 memcpy (set->active_fds->data, set->fds->data,
1254 set->fds->len * sizeof (struct pollfd));
1256 if (!gst_poll_prepare_winsock_active_sets (set))
1259 g_mutex_unlock (set->lock);
1263 case GST_POLL_MODE_AUTO:
1264 g_assert_not_reached ();
1266 case GST_POLL_MODE_PPOLL:
1270 struct timespec *tsptr;
1272 if (timeout != GST_CLOCK_TIME_NONE) {
1273 GST_TIME_TO_TIMESPEC (timeout, ts);
1280 ppoll ((struct pollfd *) set->active_fds->data,
1281 set->active_fds->len, tsptr, NULL);
1283 g_assert_not_reached ();
1288 case GST_POLL_MODE_POLL:
1293 if (timeout != GST_CLOCK_TIME_NONE) {
1294 t = GST_TIME_AS_MSECONDS (timeout);
1300 poll ((struct pollfd *) set->active_fds->data,
1301 set->active_fds->len, t);
1303 g_assert_not_reached ();
1308 case GST_POLL_MODE_PSELECT:
1309 #ifndef HAVE_PSELECT
1311 g_assert_not_reached ();
1316 case GST_POLL_MODE_SELECT:
1324 max_fd = pollfd_to_fd_set (set, &readfds, &writefds, &errorfds);
1326 if (mode == GST_POLL_MODE_SELECT) {
1328 struct timeval *tvptr;
1330 if (timeout != GST_CLOCK_TIME_NONE) {
1331 GST_TIME_TO_TIMEVAL (timeout, tv);
1337 GST_DEBUG ("Calling select");
1338 res = select (max_fd + 1, &readfds, &writefds, &errorfds, tvptr);
1339 GST_DEBUG ("After select, res:%d", res);
1343 struct timespec *tsptr;
1345 if (timeout != GST_CLOCK_TIME_NONE) {
1346 GST_TIME_TO_TIMESPEC (timeout, ts);
1352 GST_DEBUG ("Calling pselect");
1354 pselect (max_fd + 1, &readfds, &writefds, &errorfds, tsptr, NULL);
1355 GST_DEBUG ("After pselect, res:%d", res);
1360 fd_set_to_pollfd (set, &readfds, &writefds, &errorfds);
1362 #else /* G_OS_WIN32 */
1363 g_assert_not_reached ();
1368 case GST_POLL_MODE_WINDOWS:
1371 gint ignore_count = set->active_fds_ignored->len;
1374 if (G_LIKELY (ignore_count == 0)) {
1375 if (timeout != GST_CLOCK_TIME_NONE)
1376 t = GST_TIME_AS_MSECONDS (timeout);
1380 /* already one or more ignored fds, so we quickly sweep the others */
1384 if (set->active_events->len != 0) {
1385 wait_ret = WSAWaitForMultipleEvents (set->active_events->len,
1386 (HANDLE *) set->active_events->data, FALSE, t, FALSE);
1388 wait_ret = WSA_WAIT_FAILED;
1389 WSASetLastError (WSA_INVALID_PARAMETER);
1392 if (ignore_count == 0 && wait_ret == WSA_WAIT_TIMEOUT) {
1394 } else if (wait_ret == WSA_WAIT_FAILED) {
1396 errno = gst_poll_winsock_error_to_errno (WSAGetLastError ());
1398 /* the first entry is the wakeup event */
1399 if (wait_ret - WSA_WAIT_EVENT_0 >= 1) {
1400 res = gst_poll_collect_winsock_events (set);
1402 res = 1; /* wakeup event */
1406 g_assert_not_reached ();
1414 /* Applications needs to clear the control socket themselves for timer
1416 * For other polls, we need to clear the control socket. If there was only
1417 * one socket with activity and it was the control socket, we need to
1419 if (release_all_wakeup (set) > 0 && res == 1)
1423 /* we got woken up and we are flushing, we need to stop */
1424 if (G_UNLIKELY (IS_FLUSHING (set)))
1427 } while (G_UNLIKELY (restarting));
1436 GST_LOG ("%p: we are already waiting", set);
1443 GST_LOG ("%p: we are flushing", set);
1451 GST_LOG ("%p: winsock error", set);
1452 g_mutex_unlock (set->lock);
1460 * gst_poll_set_controllable:
1462 * @controllable: new controllable state.
1464 * When @controllable is %TRUE, this function ensures that future calls to
1465 * gst_poll_wait() will be affected by gst_poll_restart() and
1466 * gst_poll_set_flushing().
1468 * Returns: %TRUE if the controllability of @set could be updated.
1473 gst_poll_set_controllable (GstPoll * set, gboolean controllable)
1475 g_return_val_if_fail (set != NULL, FALSE);
1477 GST_LOG ("%p: controllable : %d", set, controllable);
1479 set->controllable = controllable;
1488 * Restart any gst_poll_wait() that is in progress. This function is typically
1489 * used after adding or removing descriptors to @set.
1491 * If @set is not controllable, then this call will have no effect.
1496 gst_poll_restart (GstPoll * set)
1498 g_return_if_fail (set != NULL);
1500 if (set->controllable && GET_WAITING (set) > 0) {
1501 /* we are controllable and waiting, wake up the waiter. The socket will be
1502 * cleared by the _wait() thread and the poll will be restarted */
1508 * gst_poll_set_flushing:
1510 * @flushing: new flushing state.
1512 * When @flushing is %TRUE, this function ensures that current and future calls
1513 * to gst_poll_wait() will return -1, with errno set to EBUSY.
1515 * Unsetting the flushing state will restore normal operation of @set.
1520 gst_poll_set_flushing (GstPoll * set, gboolean flushing)
1522 g_return_if_fail (set != NULL);
1524 GST_LOG ("%p: flushing: %d", set, flushing);
1526 /* update the new state first */
1527 SET_FLUSHING (set, flushing);
1529 if (flushing && set->controllable && GET_WAITING (set) > 0) {
1530 /* we are flushing, controllable and waiting, wake up the waiter. When we
1531 * stop the flushing operation we don't clear the wakeup fd here, this will
1532 * happen in the _wait() thread. */
1538 * gst_poll_write_control:
1541 * Write a byte to the control socket of the controllable @set.
1542 * This function is mostly useful for timer #GstPoll objects created with
1543 * gst_poll_new_timer().
1545 * It will make any current and future gst_poll_wait() function return with
1546 * 1, meaning the control socket is set. After an equal amount of calls to
1547 * gst_poll_read_control() have been performed, calls to gst_poll_wait() will
1548 * block again until their timeout expired.
1550 * Returns: %TRUE on success. %FALSE when @set is not controllable or when the
1551 * byte could not be written.
1556 gst_poll_write_control (GstPoll * set)
1560 g_return_val_if_fail (set != NULL, FALSE);
1561 g_return_val_if_fail (set->timer, FALSE);
1563 res = raise_wakeup (set);
1569 * gst_poll_read_control:
1572 * Read a byte from the control socket of the controllable @set.
1573 * This function is mostly useful for timer #GstPoll objects created with
1574 * gst_poll_new_timer().
1576 * Returns: %TRUE on success. %FALSE when @set is not controllable or when there
1577 * was no byte to read.
1582 gst_poll_read_control (GstPoll * set)
1586 g_return_val_if_fail (set != NULL, FALSE);
1587 g_return_val_if_fail (set->timer, FALSE);
1589 res = release_wakeup (set);