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 result = WAKE_EVENT (set);
187 /* note how bad things can happen when the 2 threads both raise and release the
188 * wakeup. This is however not a problem because you must always pair a raise
190 static inline gboolean
191 release_wakeup (GstPoll * set)
193 gboolean result = TRUE;
195 if (g_atomic_int_dec_and_test (&set->control_pending)) {
196 result = RELEASE_EVENT (set);
202 release_all_wakeup (GstPoll * set)
207 if (!(old = g_atomic_int_get (&set->control_pending)))
208 /* nothing pending, just exit */
211 /* try to remove all pending control messages */
212 if (g_atomic_int_compare_and_exchange (&set->control_pending, old, 0)) {
213 /* we managed to remove all messages, read the control socket */
214 if (RELEASE_EVENT (set))
217 /* retry again until we read it successfully */
218 G_ATOMIC_INT_ADD (&set->control_pending, 1);
225 find_index (GArray * array, GstPollFD * fd)
234 /* start by assuming the index found in the fd is still valid */
235 if (fd->idx >= 0 && fd->idx < array->len) {
237 ifd = &g_array_index (array, struct pollfd, fd->idx);
239 ifd = &g_array_index (array, WinsockFd, fd->idx);
242 if (ifd->fd == fd->fd) {
247 /* the pollfd array has changed and we need to lookup the fd again */
248 for (i = 0; i < array->len; i++) {
250 ifd = &g_array_index (array, struct pollfd, i);
252 ifd = &g_array_index (array, WinsockFd, i);
255 if (ifd->fd == fd->fd) {
265 #if !defined(HAVE_PPOLL) && defined(HAVE_POLL)
266 /* check if all file descriptors will fit in an fd_set */
268 selectable_fds (const GstPoll * set)
272 g_mutex_lock (set->lock);
273 for (i = 0; i < set->fds->len; i++) {
274 struct pollfd *pfd = &g_array_index (set->fds, struct pollfd, i);
276 if (pfd->fd >= FD_SETSIZE)
279 g_mutex_unlock (set->lock);
285 g_mutex_unlock (set->lock);
290 /* check if the timeout will convert to a timeout value used for poll()
291 * without a loss of precision
294 pollable_timeout (GstClockTime timeout)
296 if (timeout == GST_CLOCK_TIME_NONE)
299 /* not a nice multiple of milliseconds */
300 if (timeout % 1000000)
308 choose_mode (const GstPoll * set, GstClockTime timeout)
312 if (set->mode == GST_POLL_MODE_AUTO) {
314 mode = GST_POLL_MODE_PPOLL;
315 #elif defined(HAVE_POLL)
316 if (!selectable_fds (set) || pollable_timeout (timeout)) {
317 mode = GST_POLL_MODE_POLL;
320 mode = GST_POLL_MODE_PSELECT;
322 mode = GST_POLL_MODE_SELECT;
325 #elif defined(HAVE_PSELECT)
326 mode = GST_POLL_MODE_PSELECT;
328 mode = GST_POLL_MODE_SELECT;
338 pollfd_to_fd_set (GstPoll * set, fd_set * readfds, fd_set * writefds,
348 g_mutex_lock (set->lock);
350 for (i = 0; i < set->active_fds->len; i++) {
351 struct pollfd *pfd = &g_array_index (set->fds, struct pollfd, i);
353 if (pfd->fd < FD_SETSIZE) {
354 if (pfd->events & POLLIN)
355 FD_SET (pfd->fd, readfds);
356 if (pfd->events & POLLOUT)
357 FD_SET (pfd->fd, writefds);
359 FD_SET (pfd->fd, errorfds);
360 if (pfd->fd > max_fd && (pfd->events & (POLLIN | POLLOUT)))
365 g_mutex_unlock (set->lock);
371 fd_set_to_pollfd (GstPoll * set, fd_set * readfds, fd_set * writefds,
376 g_mutex_lock (set->lock);
378 for (i = 0; i < set->active_fds->len; i++) {
379 struct pollfd *pfd = &g_array_index (set->active_fds, struct pollfd, i);
381 if (pfd->fd < FD_SETSIZE) {
383 if (FD_ISSET (pfd->fd, readfds))
384 pfd->revents |= POLLIN;
385 if (FD_ISSET (pfd->fd, writefds))
386 pfd->revents |= POLLOUT;
387 if (FD_ISSET (pfd->fd, errorfds))
388 pfd->revents |= POLLERR;
392 g_mutex_unlock (set->lock);
394 #else /* G_OS_WIN32 */
396 * Translate errors thrown by the Winsock API used by GstPoll:
397 * WSAEventSelect, WSAWaitForMultipleEvents and WSAEnumNetworkEvents
400 gst_poll_winsock_error_to_errno (DWORD last_error)
402 switch (last_error) {
403 case WSA_INVALID_HANDLE:
408 case WSA_NOT_ENOUGH_MEMORY:
412 * Anything else, including:
413 * WSA_INVALID_PARAMETER, WSAEFAULT, WSAEINPROGRESS, WSAENETDOWN,
422 gst_poll_free_winsock_event (GstPoll * set, gint idx)
424 WinsockFd *wfd = &g_array_index (set->fds, WinsockFd, idx);
425 HANDLE event = g_array_index (set->events, HANDLE, idx);
427 WSAEventSelect (wfd->fd, event, 0);
432 gst_poll_update_winsock_event_mask (GstPoll * set, gint idx, glong flags,
437 wfd = &g_array_index (set->fds, WinsockFd, idx);
440 wfd->event_mask |= flags;
442 wfd->event_mask &= ~flags;
444 /* reset ignored state if the new mask doesn't overlap at all */
445 if ((wfd->ignored_event_mask & wfd->event_mask) == 0)
446 wfd->ignored_event_mask = 0;
450 gst_poll_prepare_winsock_active_sets (GstPoll * set)
454 g_array_set_size (set->active_fds, 0);
455 g_array_set_size (set->active_fds_ignored, 0);
456 g_array_set_size (set->active_events, 0);
457 g_array_append_val (set->active_events, set->wakeup_event);
459 for (i = 0; i < set->fds->len; i++) {
460 WinsockFd *wfd = &g_array_index (set->fds, WinsockFd, i);
461 HANDLE event = g_array_index (set->events, HANDLE, i);
463 if (wfd->ignored_event_mask == 0) {
466 g_array_append_val (set->active_fds, *wfd);
467 g_array_append_val (set->active_events, event);
469 ret = WSAEventSelect (wfd->fd, event, wfd->event_mask);
470 if (G_UNLIKELY (ret != 0)) {
471 errno = gst_poll_winsock_error_to_errno (WSAGetLastError ());
475 g_array_append_val (set->active_fds_ignored, wfd);
483 gst_poll_collect_winsock_events (GstPoll * set)
488 * We need to check which events are signaled, and call
489 * WSAEnumNetworkEvents for those that are, which resets
490 * the event and clears the internal network event records.
493 for (i = 0; i < set->active_fds->len; i++) {
494 WinsockFd *wfd = &g_array_index (set->active_fds, WinsockFd, i);
495 HANDLE event = g_array_index (set->active_events, HANDLE, i + 1);
498 wait_ret = WaitForSingleObject (event, 0);
499 if (wait_ret == WAIT_OBJECT_0) {
500 gint enum_ret = WSAEnumNetworkEvents (wfd->fd, event, &wfd->events);
502 if (G_UNLIKELY (enum_ret != 0)) {
504 errno = gst_poll_winsock_error_to_errno (WSAGetLastError ());
510 /* clear any previously stored result */
511 memset (&wfd->events, 0, sizeof (wfd->events));
515 /* If all went well we also need to reset the ignored fds. */
517 res += set->active_fds_ignored->len;
519 for (i = 0; i < set->active_fds_ignored->len; i++) {
520 WinsockFd *wfd = g_array_index (set->active_fds_ignored, WinsockFd *, i);
522 wfd->ignored_event_mask = 0;
525 g_array_set_size (set->active_fds_ignored, 0);
534 * @controllable: whether it should be possible to control a wait.
536 * Create a new file descriptor set. If @controllable, it
537 * is possible to restart or flush a call to gst_poll_wait() with
538 * gst_poll_restart() and gst_poll_set_flushing() respectively.
540 * Free-function: gst_poll_free
542 * Returns: (transfer full): a new #GstPoll, or %NULL in case of an error.
543 * Free with gst_poll_free().
548 gst_poll_new (gboolean controllable)
552 GST_DEBUG ("controllable : %d", controllable);
554 nset = g_slice_new0 (GstPoll);
555 nset->lock = g_mutex_new ();
557 nset->mode = GST_POLL_MODE_AUTO;
558 nset->fds = g_array_new (FALSE, FALSE, sizeof (struct pollfd));
559 nset->active_fds = g_array_new (FALSE, FALSE, sizeof (struct pollfd));
560 nset->control_read_fd.fd = -1;
561 nset->control_write_fd.fd = -1;
563 gint control_sock[2];
565 if (socketpair (PF_UNIX, SOCK_STREAM, 0, control_sock) < 0)
568 fcntl (control_sock[0], F_SETFL, O_NONBLOCK);
569 fcntl (control_sock[1], F_SETFL, O_NONBLOCK);
571 nset->control_read_fd.fd = control_sock[0];
572 nset->control_write_fd.fd = control_sock[1];
574 gst_poll_add_fd_unlocked (nset, &nset->control_read_fd);
575 gst_poll_fd_ctl_read_unlocked (nset, &nset->control_read_fd, TRUE);
578 nset->mode = GST_POLL_MODE_WINDOWS;
579 nset->fds = g_array_new (FALSE, FALSE, sizeof (WinsockFd));
580 nset->active_fds = g_array_new (FALSE, FALSE, sizeof (WinsockFd));
581 nset->active_fds_ignored = g_array_new (FALSE, FALSE, sizeof (WinsockFd *));
582 nset->events = g_array_new (FALSE, FALSE, sizeof (HANDLE));
583 nset->active_events = g_array_new (FALSE, FALSE, sizeof (HANDLE));
585 nset->wakeup_event = CreateEvent (NULL, TRUE, FALSE, NULL);
588 /* ensure (re)build, though already sneakily set in non-windows case */
591 nset->controllable = controllable;
599 GST_WARNING ("%p: can't create socket pair !", nset);
600 gst_poll_free (nset);
607 * gst_poll_new_timer:
609 * Create a new poll object that can be used for scheduling cancellable
612 * A timeout is performed with gst_poll_wait(). Multiple timeouts can be
613 * performed from different threads.
615 * Free-function: gst_poll_free
617 * Returns: (transfer full): a new #GstPoll, or %NULL in case of an error.
618 * Free with gst_poll_free().
623 gst_poll_new_timer (void)
627 /* make a new controllable poll set */
628 if (!(poll = gst_poll_new (TRUE)))
640 * @set: (transfer full): a file descriptor set.
642 * Free a file descriptor set.
647 gst_poll_free (GstPoll * set)
649 g_return_if_fail (set != NULL);
651 GST_DEBUG ("%p: freeing", set);
654 if (set->control_write_fd.fd >= 0)
655 close (set->control_write_fd.fd);
656 if (set->control_read_fd.fd >= 0)
657 close (set->control_read_fd.fd);
659 CloseHandle (set->wakeup_event);
664 for (i = 0; i < set->events->len; i++)
665 gst_poll_free_winsock_event (set, i);
668 g_array_free (set->active_events, TRUE);
669 g_array_free (set->events, TRUE);
670 g_array_free (set->active_fds_ignored, TRUE);
673 g_array_free (set->active_fds, TRUE);
674 g_array_free (set->fds, TRUE);
675 g_mutex_free (set->lock);
676 g_slice_free (GstPoll, set);
680 * gst_poll_get_read_gpollfd:
684 * Get a GPollFD for the reading part of the control socket. This is useful when
685 * integrating with a GSource and GMainLoop.
690 gst_poll_get_read_gpollfd (GstPoll * set, GPollFD * fd)
692 g_return_if_fail (set != NULL);
693 g_return_if_fail (fd != NULL);
696 fd->fd = set->control_read_fd.fd;
698 #if GLIB_SIZEOF_VOID_P == 8
699 fd->fd = (gint64) set->wakeup_event;
701 fd->fd = (gint) set->wakeup_event;
704 fd->events = G_IO_IN | G_IO_HUP | G_IO_ERR;
712 * Initializes @fd. Alternatively you can initialize it with
718 gst_poll_fd_init (GstPollFD * fd)
720 g_return_if_fail (fd != NULL);
727 gst_poll_add_fd_unlocked (GstPoll * set, GstPollFD * fd)
731 GST_DEBUG ("%p: fd (fd:%d, idx:%d)", set, fd->fd, fd->idx);
733 idx = find_index (set->fds, fd);
739 nfd.events = POLLERR | POLLNVAL | POLLHUP;
742 g_array_append_val (set->fds, nfd);
744 fd->idx = set->fds->len - 1;
750 wfd.event_mask = FD_CLOSE;
751 memset (&wfd.events, 0, sizeof (wfd.events));
752 wfd.ignored_event_mask = 0;
753 event = WSACreateEvent ();
755 g_array_append_val (set->fds, wfd);
756 g_array_append_val (set->events, event);
758 fd->idx = set->fds->len - 1;
762 GST_WARNING ("%p: couldn't find fd !", set);
770 * @set: a file descriptor set.
771 * @fd: a file descriptor.
773 * Add a file descriptor to the file descriptor set.
775 * Returns: %TRUE if the file descriptor was successfully added to the set.
780 gst_poll_add_fd (GstPoll * set, GstPollFD * fd)
784 g_return_val_if_fail (set != NULL, FALSE);
785 g_return_val_if_fail (fd != NULL, FALSE);
786 g_return_val_if_fail (fd->fd >= 0, FALSE);
788 g_mutex_lock (set->lock);
790 ret = gst_poll_add_fd_unlocked (set, fd);
792 g_mutex_unlock (set->lock);
798 * gst_poll_remove_fd:
799 * @set: a file descriptor set.
800 * @fd: a file descriptor.
802 * Remove a file descriptor from the file descriptor set.
804 * Returns: %TRUE if the file descriptor was successfully removed from the set.
809 gst_poll_remove_fd (GstPoll * set, GstPollFD * fd)
813 g_return_val_if_fail (set != NULL, FALSE);
814 g_return_val_if_fail (fd != NULL, FALSE);
815 g_return_val_if_fail (fd->fd >= 0, FALSE);
818 GST_DEBUG ("%p: fd (fd:%d, idx:%d)", set, fd->fd, fd->idx);
820 g_mutex_lock (set->lock);
822 /* get the index, -1 is an fd that is not added */
823 idx = find_index (set->fds, fd);
826 gst_poll_free_winsock_event (set, idx);
827 g_array_remove_index_fast (set->events, idx);
830 /* remove the fd at index, we use _remove_index_fast, which copies the last
831 * element of the array to the freed index */
832 g_array_remove_index_fast (set->fds, idx);
834 /* mark fd as removed by setting the index to -1 */
838 GST_WARNING ("%p: couldn't find fd !", set);
841 g_mutex_unlock (set->lock);
847 * gst_poll_fd_ctl_write:
848 * @set: a file descriptor set.
849 * @fd: a file descriptor.
850 * @active: a new status.
852 * Control whether the descriptor @fd in @set will be monitored for
855 * Returns: %TRUE if the descriptor was successfully updated.
860 gst_poll_fd_ctl_write (GstPoll * set, GstPollFD * fd, gboolean active)
864 g_return_val_if_fail (set != NULL, FALSE);
865 g_return_val_if_fail (fd != NULL, FALSE);
866 g_return_val_if_fail (fd->fd >= 0, FALSE);
868 GST_DEBUG ("%p: fd (fd:%d, idx:%d), active : %d", set,
869 fd->fd, fd->idx, active);
871 g_mutex_lock (set->lock);
873 idx = find_index (set->fds, fd);
876 struct pollfd *pfd = &g_array_index (set->fds, struct pollfd, idx);
879 pfd->events |= POLLOUT;
881 pfd->events &= ~POLLOUT;
883 GST_LOG ("pfd->events now %d (POLLOUT:%d)", pfd->events, POLLOUT);
885 gst_poll_update_winsock_event_mask (set, idx, FD_WRITE | FD_CONNECT,
890 GST_WARNING ("%p: couldn't find fd !", set);
893 g_mutex_unlock (set->lock);
899 gst_poll_fd_ctl_read_unlocked (GstPoll * set, GstPollFD * fd, gboolean active)
903 GST_DEBUG ("%p: fd (fd:%d, idx:%d), active : %d", set,
904 fd->fd, fd->idx, active);
906 idx = find_index (set->fds, fd);
910 struct pollfd *pfd = &g_array_index (set->fds, struct pollfd, idx);
913 pfd->events |= (POLLIN | POLLPRI);
915 pfd->events &= ~(POLLIN | POLLPRI);
917 gst_poll_update_winsock_event_mask (set, idx, FD_READ | FD_ACCEPT, active);
921 GST_WARNING ("%p: couldn't find fd !", set);
928 * gst_poll_fd_ctl_read:
929 * @set: a file descriptor set.
930 * @fd: a file descriptor.
931 * @active: a new status.
933 * Control whether the descriptor @fd in @set will be monitored for
936 * Returns: %TRUE if the descriptor was successfully updated.
941 gst_poll_fd_ctl_read (GstPoll * set, GstPollFD * fd, gboolean active)
945 g_return_val_if_fail (set != NULL, FALSE);
946 g_return_val_if_fail (fd != NULL, FALSE);
947 g_return_val_if_fail (fd->fd >= 0, FALSE);
949 g_mutex_lock (set->lock);
951 ret = gst_poll_fd_ctl_read_unlocked (set, fd, active);
953 g_mutex_unlock (set->lock);
959 * gst_poll_fd_ignored:
960 * @set: a file descriptor set.
961 * @fd: a file descriptor.
963 * Mark @fd as ignored so that the next call to gst_poll_wait() will yield
964 * the same result for @fd as last time. This function must be called if no
965 * operation (read/write/recv/send/etc.) will be performed on @fd before
966 * the next call to gst_poll_wait().
968 * The reason why this is needed is because the underlying implementation
969 * might not allow querying the fd more than once between calls to one of
970 * the re-enabling operations.
975 gst_poll_fd_ignored (GstPoll * set, GstPollFD * fd)
980 g_return_if_fail (set != NULL);
981 g_return_if_fail (fd != NULL);
982 g_return_if_fail (fd->fd >= 0);
984 g_mutex_lock (set->lock);
986 idx = find_index (set->fds, fd);
988 WinsockFd *wfd = &g_array_index (set->fds, WinsockFd, idx);
990 wfd->ignored_event_mask = wfd->event_mask & (FD_READ | FD_WRITE);
994 g_mutex_unlock (set->lock);
999 * gst_poll_fd_has_closed:
1000 * @set: a file descriptor set.
1001 * @fd: a file descriptor.
1003 * Check if @fd in @set has closed the connection.
1005 * Returns: %TRUE if the connection was closed.
1010 gst_poll_fd_has_closed (const GstPoll * set, GstPollFD * fd)
1012 gboolean res = FALSE;
1015 g_return_val_if_fail (set != NULL, FALSE);
1016 g_return_val_if_fail (fd != NULL, FALSE);
1017 g_return_val_if_fail (fd->fd >= 0, FALSE);
1019 GST_DEBUG ("%p: fd (fd:%d, idx:%d)", set, fd->fd, fd->idx);
1021 g_mutex_lock (set->lock);
1023 idx = find_index (set->active_fds, fd);
1026 struct pollfd *pfd = &g_array_index (set->active_fds, struct pollfd, idx);
1028 res = (pfd->revents & POLLHUP) != 0;
1030 WinsockFd *wfd = &g_array_index (set->active_fds, WinsockFd, idx);
1032 res = (wfd->events.lNetworkEvents & FD_CLOSE) != 0;
1035 GST_WARNING ("%p: couldn't find fd !", set);
1038 g_mutex_unlock (set->lock);
1044 * gst_poll_fd_has_error:
1045 * @set: a file descriptor set.
1046 * @fd: a file descriptor.
1048 * Check if @fd in @set has an error.
1050 * Returns: %TRUE if the descriptor has an error.
1055 gst_poll_fd_has_error (const GstPoll * set, GstPollFD * fd)
1057 gboolean res = FALSE;
1060 g_return_val_if_fail (set != NULL, FALSE);
1061 g_return_val_if_fail (fd != NULL, FALSE);
1062 g_return_val_if_fail (fd->fd >= 0, FALSE);
1064 GST_DEBUG ("%p: fd (fd:%d, idx:%d)", set, fd->fd, fd->idx);
1066 g_mutex_lock (set->lock);
1068 idx = find_index (set->active_fds, fd);
1071 struct pollfd *pfd = &g_array_index (set->active_fds, struct pollfd, idx);
1073 res = (pfd->revents & (POLLERR | POLLNVAL)) != 0;
1075 WinsockFd *wfd = &g_array_index (set->active_fds, WinsockFd, idx);
1077 res = (wfd->events.iErrorCode[FD_CLOSE_BIT] != 0) ||
1078 (wfd->events.iErrorCode[FD_READ_BIT] != 0) ||
1079 (wfd->events.iErrorCode[FD_WRITE_BIT] != 0) ||
1080 (wfd->events.iErrorCode[FD_ACCEPT_BIT] != 0) ||
1081 (wfd->events.iErrorCode[FD_CONNECT_BIT] != 0);
1084 GST_WARNING ("%p: couldn't find fd !", set);
1087 g_mutex_unlock (set->lock);
1093 gst_poll_fd_can_read_unlocked (const GstPoll * set, GstPollFD * fd)
1095 gboolean res = FALSE;
1098 GST_DEBUG ("%p: fd (fd:%d, idx:%d)", set, fd->fd, fd->idx);
1100 idx = find_index (set->active_fds, fd);
1103 struct pollfd *pfd = &g_array_index (set->active_fds, struct pollfd, idx);
1105 res = (pfd->revents & (POLLIN | POLLPRI)) != 0;
1107 WinsockFd *wfd = &g_array_index (set->active_fds, WinsockFd, idx);
1109 res = (wfd->events.lNetworkEvents & (FD_READ | FD_ACCEPT)) != 0;
1112 GST_WARNING ("%p: couldn't find fd !", set);
1119 * gst_poll_fd_can_read:
1120 * @set: a file descriptor set.
1121 * @fd: a file descriptor.
1123 * Check if @fd in @set has data to be read.
1125 * Returns: %TRUE if the descriptor has data to be read.
1130 gst_poll_fd_can_read (const GstPoll * set, GstPollFD * fd)
1132 gboolean res = FALSE;
1134 g_return_val_if_fail (set != NULL, FALSE);
1135 g_return_val_if_fail (fd != NULL, FALSE);
1136 g_return_val_if_fail (fd->fd >= 0, FALSE);
1138 g_mutex_lock (set->lock);
1140 res = gst_poll_fd_can_read_unlocked (set, fd);
1142 g_mutex_unlock (set->lock);
1148 * gst_poll_fd_can_write:
1149 * @set: a file descriptor set.
1150 * @fd: a file descriptor.
1152 * Check if @fd in @set can be used for writing.
1154 * Returns: %TRUE if the descriptor can be used for writing.
1159 gst_poll_fd_can_write (const GstPoll * set, GstPollFD * fd)
1161 gboolean res = FALSE;
1164 g_return_val_if_fail (set != NULL, FALSE);
1165 g_return_val_if_fail (fd != NULL, FALSE);
1166 g_return_val_if_fail (fd->fd >= 0, FALSE);
1168 GST_DEBUG ("%p: fd (fd:%d, idx:%d)", set, fd->fd, fd->idx);
1170 g_mutex_lock (set->lock);
1172 idx = find_index (set->active_fds, fd);
1175 struct pollfd *pfd = &g_array_index (set->active_fds, struct pollfd, idx);
1177 res = (pfd->revents & POLLOUT) != 0;
1179 WinsockFd *wfd = &g_array_index (set->active_fds, WinsockFd, idx);
1181 res = (wfd->events.lNetworkEvents & FD_WRITE) != 0;
1184 GST_WARNING ("%p: couldn't find fd !", set);
1187 g_mutex_unlock (set->lock);
1195 * @timeout: a timeout in nanoseconds.
1197 * Wait for activity on the file descriptors in @set. This function waits up to
1198 * the specified @timeout. A timeout of #GST_CLOCK_TIME_NONE waits forever.
1200 * For #GstPoll objects created with gst_poll_new(), this function can only be
1201 * called from a single thread at a time. If called from multiple threads,
1202 * -1 will be returned with errno set to EPERM.
1204 * This is not true for timer #GstPoll objects created with
1205 * gst_poll_new_timer(), where it is allowed to have multiple threads waiting
1208 * Returns: The number of #GstPollFD in @set that have activity or 0 when no
1209 * activity was detected after @timeout. If an error occurs, -1 is returned
1215 gst_poll_wait (GstPoll * set, GstClockTime timeout)
1217 gboolean restarting;
1222 g_return_val_if_fail (set != NULL, -1);
1224 GST_DEBUG ("timeout :%" GST_TIME_FORMAT, GST_TIME_ARGS (timeout));
1226 is_timer = set->timer;
1228 /* add one more waiter */
1229 old_waiting = INC_WAITING (set);
1231 /* we cannot wait from multiple threads unless we are a timer */
1232 if (G_UNLIKELY (old_waiting > 0 && !is_timer))
1233 goto already_waiting;
1235 /* flushing, exit immediately */
1236 if (G_UNLIKELY (IS_FLUSHING (set)))
1245 mode = choose_mode (set, timeout);
1247 if (TEST_REBUILD (set)) {
1248 g_mutex_lock (set->lock);
1250 g_array_set_size (set->active_fds, set->fds->len);
1251 memcpy (set->active_fds->data, set->fds->data,
1252 set->fds->len * sizeof (struct pollfd));
1254 if (!gst_poll_prepare_winsock_active_sets (set))
1257 g_mutex_unlock (set->lock);
1261 case GST_POLL_MODE_AUTO:
1262 g_assert_not_reached ();
1264 case GST_POLL_MODE_PPOLL:
1268 struct timespec *tsptr;
1270 if (timeout != GST_CLOCK_TIME_NONE) {
1271 GST_TIME_TO_TIMESPEC (timeout, ts);
1278 ppoll ((struct pollfd *) set->active_fds->data,
1279 set->active_fds->len, tsptr, NULL);
1281 g_assert_not_reached ();
1286 case GST_POLL_MODE_POLL:
1291 if (timeout != GST_CLOCK_TIME_NONE) {
1292 t = GST_TIME_AS_MSECONDS (timeout);
1298 poll ((struct pollfd *) set->active_fds->data,
1299 set->active_fds->len, t);
1301 g_assert_not_reached ();
1306 case GST_POLL_MODE_PSELECT:
1307 #ifndef HAVE_PSELECT
1309 g_assert_not_reached ();
1314 case GST_POLL_MODE_SELECT:
1322 max_fd = pollfd_to_fd_set (set, &readfds, &writefds, &errorfds);
1324 if (mode == GST_POLL_MODE_SELECT) {
1326 struct timeval *tvptr;
1328 if (timeout != GST_CLOCK_TIME_NONE) {
1329 GST_TIME_TO_TIMEVAL (timeout, tv);
1335 GST_DEBUG ("Calling select");
1336 res = select (max_fd + 1, &readfds, &writefds, &errorfds, tvptr);
1337 GST_DEBUG ("After select, res:%d", res);
1341 struct timespec *tsptr;
1343 if (timeout != GST_CLOCK_TIME_NONE) {
1344 GST_TIME_TO_TIMESPEC (timeout, ts);
1350 GST_DEBUG ("Calling pselect");
1352 pselect (max_fd + 1, &readfds, &writefds, &errorfds, tsptr, NULL);
1353 GST_DEBUG ("After pselect, res:%d", res);
1358 fd_set_to_pollfd (set, &readfds, &writefds, &errorfds);
1360 #else /* G_OS_WIN32 */
1361 g_assert_not_reached ();
1366 case GST_POLL_MODE_WINDOWS:
1369 gint ignore_count = set->active_fds_ignored->len;
1372 if (G_LIKELY (ignore_count == 0)) {
1373 if (timeout != GST_CLOCK_TIME_NONE)
1374 t = GST_TIME_AS_MSECONDS (timeout);
1378 /* already one or more ignored fds, so we quickly sweep the others */
1382 if (set->active_events->len != 0) {
1383 wait_ret = WSAWaitForMultipleEvents (set->active_events->len,
1384 (HANDLE *) set->active_events->data, FALSE, t, FALSE);
1386 wait_ret = WSA_WAIT_FAILED;
1387 WSASetLastError (WSA_INVALID_PARAMETER);
1390 if (ignore_count == 0 && wait_ret == WSA_WAIT_TIMEOUT) {
1392 } else if (wait_ret == WSA_WAIT_FAILED) {
1394 errno = gst_poll_winsock_error_to_errno (WSAGetLastError ());
1396 /* the first entry is the wakeup event */
1397 if (wait_ret - WSA_WAIT_EVENT_0 >= 1) {
1398 res = gst_poll_collect_winsock_events (set);
1400 res = 1; /* wakeup event */
1404 g_assert_not_reached ();
1412 /* Applications needs to clear the control socket themselves for timer
1414 * For other polls, we need to clear the control socket. If there was only
1415 * one socket with activity and it was the control socket, we need to
1417 if (release_all_wakeup (set) > 0 && res == 1)
1421 if (G_UNLIKELY (IS_FLUSHING (set))) {
1422 /* we got woken up and we are flushing, we need to stop */
1427 } while (G_UNLIKELY (restarting));
1449 g_mutex_unlock (set->lock);
1457 * gst_poll_set_controllable:
1459 * @controllable: new controllable state.
1461 * When @controllable is %TRUE, this function ensures that future calls to
1462 * gst_poll_wait() will be affected by gst_poll_restart() and
1463 * gst_poll_set_flushing().
1465 * Returns: %TRUE if the controllability of @set could be updated.
1470 gst_poll_set_controllable (GstPoll * set, gboolean controllable)
1472 g_return_val_if_fail (set != NULL, FALSE);
1474 GST_LOG ("%p: controllable : %d", set, controllable);
1476 set->controllable = controllable;
1485 * Restart any gst_poll_wait() that is in progress. This function is typically
1486 * used after adding or removing descriptors to @set.
1488 * If @set is not controllable, then this call will have no effect.
1493 gst_poll_restart (GstPoll * set)
1495 g_return_if_fail (set != NULL);
1497 if (set->controllable && GET_WAITING (set) > 0) {
1498 /* we are controllable and waiting, wake up the waiter. The socket will be
1499 * cleared by the _wait() thread and the poll will be restarted */
1505 * gst_poll_set_flushing:
1507 * @flushing: new flushing state.
1509 * When @flushing is %TRUE, this function ensures that current and future calls
1510 * to gst_poll_wait() will return -1, with errno set to EBUSY.
1512 * Unsetting the flushing state will restore normal operation of @set.
1517 gst_poll_set_flushing (GstPoll * set, gboolean flushing)
1519 g_return_if_fail (set != NULL);
1521 /* update the new state first */
1522 SET_FLUSHING (set, flushing);
1524 if (flushing && set->controllable && GET_WAITING (set) > 0) {
1525 /* we are flushing, controllable and waiting, wake up the waiter. When we
1526 * stop the flushing operation we don't clear the wakeup fd here, this will
1527 * happen in the _wait() thread. */
1533 * gst_poll_write_control:
1536 * Write a byte to the control socket of the controllable @set.
1537 * This function is mostly useful for timer #GstPoll objects created with
1538 * gst_poll_new_timer().
1540 * It will make any current and future gst_poll_wait() function return with
1541 * 1, meaning the control socket is set. After an equal amount of calls to
1542 * gst_poll_read_control() have been performed, calls to gst_poll_wait() will
1543 * block again until their timeout expired.
1545 * Returns: %TRUE on success. %FALSE when @set is not controllable or when the
1546 * byte could not be written.
1551 gst_poll_write_control (GstPoll * set)
1555 g_return_val_if_fail (set != NULL, FALSE);
1556 g_return_val_if_fail (set->timer, FALSE);
1558 res = raise_wakeup (set);
1564 * gst_poll_read_control:
1567 * Read a byte from the control socket of the controllable @set.
1568 * This function is mostly useful for timer #GstPoll objects created with
1569 * gst_poll_new_timer().
1571 * Returns: %TRUE on success. %FALSE when @set is not controllable or when there
1572 * was no byte to read.
1577 gst_poll_read_control (GstPoll * set)
1581 g_return_val_if_fail (set != NULL, FALSE);
1582 g_return_val_if_fail (set->timer, FALSE);
1584 res = release_wakeup (set);