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>
75 #define EINPROGRESS WSAEINPROGRESS
78 #ifdef HAVE_SYS_POLL_H
85 #include <sys/socket.h>
88 /* OS/X needs this because of bad headers */
91 /* The poll() emulation on OS/X doesn't handle fds=NULL, nfds=0,
92 * so we prefer our own poll emulation.
94 #if defined(BROKEN_POLL)
100 #define GST_CAT_DEFAULT GST_CAT_POLL
103 typedef struct _WinsockFd WinsockFd;
109 WSANETWORKEVENTS events;
110 glong ignored_event_mask;
117 GST_POLL_MODE_SELECT,
118 GST_POLL_MODE_PSELECT,
121 GST_POLL_MODE_WINDOWS
129 /* array of fds, always written to and read from with lock */
131 /* array of active fds, only written to from the waiting thread with the
132 * lock and read from with the lock or without the lock from the waiting
138 GstPollFD control_read_fd;
139 GstPollFD control_write_fd;
141 GArray *active_fds_ignored;
143 GArray *active_events;
148 gboolean controllable;
149 volatile gint waiting;
150 volatile gint control_pending;
151 volatile gint flushing;
153 volatile gint rebuild;
156 static gboolean gst_poll_fd_ctl_read_unlocked (GstPoll * set, GstPollFD * fd,
158 static gboolean gst_poll_add_fd_unlocked (GstPoll * set, GstPollFD * fd);
160 #define IS_FLUSHING(s) (g_atomic_int_get(&(s)->flushing))
161 #define SET_FLUSHING(s,val) (g_atomic_int_set(&(s)->flushing, (val)))
163 #define INC_WAITING(s) (g_atomic_int_add(&(s)->waiting, 1))
164 #define DEC_WAITING(s) (g_atomic_int_add(&(s)->waiting, -1))
165 #define GET_WAITING(s) (g_atomic_int_get(&(s)->waiting))
167 #define TEST_REBUILD(s) (g_atomic_int_compare_and_exchange(&(s)->rebuild, 1, 0))
168 #define MARK_REBUILD(s) (g_atomic_int_set(&(s)->rebuild, 1))
171 #define WAKE_EVENT(s) (write ((s)->control_write_fd.fd, "W", 1) == 1)
172 #define RELEASE_EVENT(s) (read ((s)->control_read_fd.fd, (s)->buf, 1) == 1)
174 #define WAKE_EVENT(s) (SetEvent ((s)->wakeup_event), errno = GetLastError () == NO_ERROR ? 0 : EACCES, errno == 0 ? 1 : 0)
175 #define RELEASE_EVENT(s) (ResetEvent ((s)->wakeup_event))
178 /* the poll/select call is also performed on a control socket, that way
179 * we can send special commands to control it */
180 static inline gboolean
181 raise_wakeup (GstPoll * set)
183 gboolean result = TRUE;
185 if (g_atomic_int_add (&set->control_pending, 1) == 0) {
186 /* raise when nothing pending */
187 GST_LOG ("%p: raise", set);
188 result = WAKE_EVENT (set);
193 /* note how bad things can happen when the 2 threads both raise and release the
194 * wakeup. This is however not a problem because you must always pair a raise
196 static inline gboolean
197 release_wakeup (GstPoll * set)
199 gboolean result = TRUE;
201 if (g_atomic_int_dec_and_test (&set->control_pending)) {
202 GST_LOG ("%p: release", set);
203 result = RELEASE_EVENT (set);
209 release_all_wakeup (GstPoll * set)
214 if (!(old = g_atomic_int_get (&set->control_pending)))
215 /* nothing pending, just exit */
218 /* try to remove all pending control messages */
219 if (g_atomic_int_compare_and_exchange (&set->control_pending, old, 0)) {
220 /* we managed to remove all messages, read the control socket */
221 if (RELEASE_EVENT (set))
224 /* retry again until we read it successfully */
225 g_atomic_int_add (&set->control_pending, 1);
232 find_index (GArray * array, GstPollFD * fd)
241 /* start by assuming the index found in the fd is still valid */
242 if (fd->idx >= 0 && fd->idx < array->len) {
244 ifd = &g_array_index (array, struct pollfd, fd->idx);
246 ifd = &g_array_index (array, WinsockFd, fd->idx);
249 if (ifd->fd == fd->fd) {
254 /* the pollfd array has changed and we need to lookup the fd again */
255 for (i = 0; i < array->len; i++) {
257 ifd = &g_array_index (array, struct pollfd, i);
259 ifd = &g_array_index (array, WinsockFd, i);
262 if (ifd->fd == fd->fd) {
272 #if !defined(HAVE_PPOLL) && defined(HAVE_POLL)
273 /* check if all file descriptors will fit in an fd_set */
275 selectable_fds (const GstPoll * set)
279 g_mutex_lock (&set->lock);
280 for (i = 0; i < set->fds->len; i++) {
281 struct pollfd *pfd = &g_array_index (set->fds, struct pollfd, i);
283 if (pfd->fd >= FD_SETSIZE)
286 g_mutex_unlock (&set->lock);
292 g_mutex_unlock (&set->lock);
297 /* check if the timeout will convert to a timeout value used for poll()
298 * without a loss of precision
301 pollable_timeout (GstClockTime timeout)
303 if (timeout == GST_CLOCK_TIME_NONE)
306 /* not a nice multiple of milliseconds */
307 if (timeout % 1000000)
315 choose_mode (const GstPoll * set, GstClockTime timeout)
319 if (set->mode == GST_POLL_MODE_AUTO) {
321 mode = GST_POLL_MODE_PPOLL;
322 #elif defined(HAVE_POLL)
323 if (!selectable_fds (set) || pollable_timeout (timeout)) {
324 mode = GST_POLL_MODE_POLL;
327 mode = GST_POLL_MODE_PSELECT;
329 mode = GST_POLL_MODE_SELECT;
332 #elif defined(HAVE_PSELECT)
333 mode = GST_POLL_MODE_PSELECT;
335 mode = GST_POLL_MODE_SELECT;
345 pollfd_to_fd_set (GstPoll * set, fd_set * readfds, fd_set * writefds,
355 g_mutex_lock (&set->lock);
357 for (i = 0; i < set->active_fds->len; i++) {
358 struct pollfd *pfd = &g_array_index (set->fds, struct pollfd, i);
360 if (pfd->fd < FD_SETSIZE) {
361 if (pfd->events & POLLIN)
362 FD_SET (pfd->fd, readfds);
363 if (pfd->events & POLLOUT)
364 FD_SET (pfd->fd, writefds);
366 FD_SET (pfd->fd, errorfds);
367 if (pfd->fd > max_fd && (pfd->events & (POLLIN | POLLOUT)))
372 g_mutex_unlock (&set->lock);
378 fd_set_to_pollfd (GstPoll * set, fd_set * readfds, fd_set * writefds,
383 g_mutex_lock (&set->lock);
385 for (i = 0; i < set->active_fds->len; i++) {
386 struct pollfd *pfd = &g_array_index (set->active_fds, struct pollfd, i);
388 if (pfd->fd < FD_SETSIZE) {
390 if (FD_ISSET (pfd->fd, readfds))
391 pfd->revents |= POLLIN;
392 if (FD_ISSET (pfd->fd, writefds))
393 pfd->revents |= POLLOUT;
394 if (FD_ISSET (pfd->fd, errorfds))
395 pfd->revents |= POLLERR;
399 g_mutex_unlock (&set->lock);
401 #else /* G_OS_WIN32 */
403 * Translate errors thrown by the Winsock API used by GstPoll:
404 * WSAEventSelect, WSAWaitForMultipleEvents and WSAEnumNetworkEvents
407 gst_poll_winsock_error_to_errno (DWORD last_error)
409 switch (last_error) {
410 case WSA_INVALID_HANDLE:
415 case WSA_NOT_ENOUGH_MEMORY:
419 * Anything else, including:
420 * WSA_INVALID_PARAMETER, WSAEFAULT, WSAEINPROGRESS, WSAENETDOWN,
429 gst_poll_free_winsock_event (GstPoll * set, gint idx)
431 WinsockFd *wfd = &g_array_index (set->fds, WinsockFd, idx);
432 HANDLE event = g_array_index (set->events, HANDLE, idx);
434 WSAEventSelect (wfd->fd, event, 0);
439 gst_poll_update_winsock_event_mask (GstPoll * set, gint idx, glong flags,
444 wfd = &g_array_index (set->fds, WinsockFd, idx);
447 wfd->event_mask |= flags;
449 wfd->event_mask &= ~flags;
451 /* reset ignored state if the new mask doesn't overlap at all */
452 if ((wfd->ignored_event_mask & wfd->event_mask) == 0)
453 wfd->ignored_event_mask = 0;
457 gst_poll_prepare_winsock_active_sets (GstPoll * set)
461 g_array_set_size (set->active_fds, 0);
462 g_array_set_size (set->active_fds_ignored, 0);
463 g_array_set_size (set->active_events, 0);
464 g_array_append_val (set->active_events, set->wakeup_event);
466 for (i = 0; i < set->fds->len; i++) {
467 WinsockFd *wfd = &g_array_index (set->fds, WinsockFd, i);
468 HANDLE event = g_array_index (set->events, HANDLE, i);
470 if (wfd->ignored_event_mask == 0) {
473 g_array_append_val (set->active_fds, *wfd);
474 g_array_append_val (set->active_events, event);
476 ret = WSAEventSelect (wfd->fd, event, wfd->event_mask);
477 if (G_UNLIKELY (ret != 0)) {
478 errno = gst_poll_winsock_error_to_errno (WSAGetLastError ());
482 g_array_append_val (set->active_fds_ignored, wfd);
490 gst_poll_collect_winsock_events (GstPoll * set)
495 * We need to check which events are signaled, and call
496 * WSAEnumNetworkEvents for those that are, which resets
497 * the event and clears the internal network event records.
500 for (i = 0; i < set->active_fds->len; i++) {
501 WinsockFd *wfd = &g_array_index (set->active_fds, WinsockFd, i);
502 HANDLE event = g_array_index (set->active_events, HANDLE, i + 1);
505 wait_ret = WaitForSingleObject (event, 0);
506 if (wait_ret == WAIT_OBJECT_0) {
507 gint enum_ret = WSAEnumNetworkEvents (wfd->fd, event, &wfd->events);
509 if (G_UNLIKELY (enum_ret != 0)) {
511 errno = gst_poll_winsock_error_to_errno (WSAGetLastError ());
517 /* clear any previously stored result */
518 memset (&wfd->events, 0, sizeof (wfd->events));
522 /* If all went well we also need to reset the ignored fds. */
524 res += set->active_fds_ignored->len;
526 for (i = 0; i < set->active_fds_ignored->len; i++) {
527 WinsockFd *wfd = g_array_index (set->active_fds_ignored, WinsockFd *, i);
529 wfd->ignored_event_mask = 0;
532 g_array_set_size (set->active_fds_ignored, 0);
540 * gst_poll_new: (skip)
541 * @controllable: whether it should be possible to control a wait.
543 * Create a new file descriptor set. If @controllable, it
544 * is possible to restart or flush a call to gst_poll_wait() with
545 * gst_poll_restart() and gst_poll_set_flushing() respectively.
547 * Free-function: gst_poll_free
549 * Returns: (transfer full): a new #GstPoll, or %NULL in case of an error.
550 * Free with gst_poll_free().
555 gst_poll_new (gboolean controllable)
559 GST_DEBUG ("controllable : %d", controllable);
561 nset = g_slice_new0 (GstPoll);
562 g_mutex_init (&nset->lock);
564 nset->mode = GST_POLL_MODE_AUTO;
565 nset->fds = g_array_new (FALSE, FALSE, sizeof (struct pollfd));
566 nset->active_fds = g_array_new (FALSE, FALSE, sizeof (struct pollfd));
567 nset->control_read_fd.fd = -1;
568 nset->control_write_fd.fd = -1;
570 gint control_sock[2];
572 if (socketpair (PF_UNIX, SOCK_STREAM, 0, control_sock) < 0)
575 fcntl (control_sock[0], F_SETFL, O_NONBLOCK);
576 fcntl (control_sock[1], F_SETFL, O_NONBLOCK);
578 nset->control_read_fd.fd = control_sock[0];
579 nset->control_write_fd.fd = control_sock[1];
581 gst_poll_add_fd_unlocked (nset, &nset->control_read_fd);
582 gst_poll_fd_ctl_read_unlocked (nset, &nset->control_read_fd, TRUE);
585 nset->mode = GST_POLL_MODE_WINDOWS;
586 nset->fds = g_array_new (FALSE, FALSE, sizeof (WinsockFd));
587 nset->active_fds = g_array_new (FALSE, FALSE, sizeof (WinsockFd));
588 nset->active_fds_ignored = g_array_new (FALSE, FALSE, sizeof (WinsockFd *));
589 nset->events = g_array_new (FALSE, FALSE, sizeof (HANDLE));
590 nset->active_events = g_array_new (FALSE, FALSE, sizeof (HANDLE));
592 nset->wakeup_event = CreateEvent (NULL, TRUE, FALSE, NULL);
595 /* ensure (re)build, though already sneakily set in non-windows case */
598 nset->controllable = controllable;
606 GST_WARNING ("%p: can't create socket pair !", nset);
607 gst_poll_free (nset);
614 * gst_poll_new_timer: (skip)
616 * Create a new poll object that can be used for scheduling cancellable
619 * A timeout is performed with gst_poll_wait(). Multiple timeouts can be
620 * performed from different threads.
622 * Free-function: gst_poll_free
624 * Returns: (transfer full): a new #GstPoll, or %NULL in case of an error.
625 * Free with gst_poll_free().
630 gst_poll_new_timer (void)
634 /* make a new controllable poll set */
635 if (!(poll = gst_poll_new (TRUE)))
647 * @set: (transfer full): a file descriptor set.
649 * Free a file descriptor set.
654 gst_poll_free (GstPoll * set)
656 g_return_if_fail (set != NULL);
658 GST_DEBUG ("%p: freeing", set);
661 if (set->control_write_fd.fd >= 0)
662 close (set->control_write_fd.fd);
663 if (set->control_read_fd.fd >= 0)
664 close (set->control_read_fd.fd);
666 CloseHandle (set->wakeup_event);
671 for (i = 0; i < set->events->len; i++)
672 gst_poll_free_winsock_event (set, i);
675 g_array_free (set->active_events, TRUE);
676 g_array_free (set->events, TRUE);
677 g_array_free (set->active_fds_ignored, TRUE);
680 g_array_free (set->active_fds, TRUE);
681 g_array_free (set->fds, TRUE);
682 g_mutex_clear (&set->lock);
683 g_slice_free (GstPoll, set);
687 * gst_poll_get_read_gpollfd:
691 * Get a GPollFD for the reading part of the control socket. This is useful when
692 * integrating with a GSource and GMainLoop.
697 gst_poll_get_read_gpollfd (GstPoll * set, GPollFD * fd)
699 g_return_if_fail (set != NULL);
700 g_return_if_fail (fd != NULL);
703 fd->fd = set->control_read_fd.fd;
705 #if GLIB_SIZEOF_VOID_P == 8
706 fd->fd = (gint64) set->wakeup_event;
708 fd->fd = (gint) set->wakeup_event;
711 fd->events = G_IO_IN | G_IO_HUP | G_IO_ERR;
719 * Initializes @fd. Alternatively you can initialize it with
725 gst_poll_fd_init (GstPollFD * fd)
727 g_return_if_fail (fd != NULL);
734 gst_poll_add_fd_unlocked (GstPoll * set, GstPollFD * fd)
738 GST_DEBUG ("%p: fd (fd:%d, idx:%d)", set, fd->fd, fd->idx);
740 idx = find_index (set->fds, fd);
746 nfd.events = POLLERR | POLLNVAL | POLLHUP;
749 g_array_append_val (set->fds, nfd);
751 fd->idx = set->fds->len - 1;
757 wfd.event_mask = FD_CLOSE;
758 memset (&wfd.events, 0, sizeof (wfd.events));
759 wfd.ignored_event_mask = 0;
760 event = WSACreateEvent ();
762 g_array_append_val (set->fds, wfd);
763 g_array_append_val (set->events, event);
765 fd->idx = set->fds->len - 1;
769 GST_WARNING ("%p: couldn't find fd !", set);
777 * @set: a file descriptor set.
778 * @fd: a file descriptor.
780 * Add a file descriptor to the file descriptor set.
782 * Returns: %TRUE if the file descriptor was successfully added to the set.
787 gst_poll_add_fd (GstPoll * set, GstPollFD * fd)
791 g_return_val_if_fail (set != NULL, FALSE);
792 g_return_val_if_fail (fd != NULL, FALSE);
793 g_return_val_if_fail (fd->fd >= 0, FALSE);
795 g_mutex_lock (&set->lock);
797 ret = gst_poll_add_fd_unlocked (set, fd);
799 g_mutex_unlock (&set->lock);
805 * gst_poll_remove_fd:
806 * @set: a file descriptor set.
807 * @fd: a file descriptor.
809 * Remove a file descriptor from the file descriptor set.
811 * Returns: %TRUE if the file descriptor was successfully removed from the set.
816 gst_poll_remove_fd (GstPoll * set, GstPollFD * fd)
820 g_return_val_if_fail (set != NULL, FALSE);
821 g_return_val_if_fail (fd != NULL, FALSE);
822 g_return_val_if_fail (fd->fd >= 0, FALSE);
825 GST_DEBUG ("%p: fd (fd:%d, idx:%d)", set, fd->fd, fd->idx);
827 g_mutex_lock (&set->lock);
829 /* get the index, -1 is an fd that is not added */
830 idx = find_index (set->fds, fd);
833 gst_poll_free_winsock_event (set, idx);
834 g_array_remove_index_fast (set->events, idx);
837 /* remove the fd at index, we use _remove_index_fast, which copies the last
838 * element of the array to the freed index */
839 g_array_remove_index_fast (set->fds, idx);
841 /* mark fd as removed by setting the index to -1 */
845 GST_WARNING ("%p: couldn't find fd !", set);
848 g_mutex_unlock (&set->lock);
854 * gst_poll_fd_ctl_write:
855 * @set: a file descriptor set.
856 * @fd: a file descriptor.
857 * @active: a new status.
859 * Control whether the descriptor @fd in @set will be monitored for
862 * Returns: %TRUE if the descriptor was successfully updated.
867 gst_poll_fd_ctl_write (GstPoll * set, GstPollFD * fd, gboolean active)
871 g_return_val_if_fail (set != NULL, FALSE);
872 g_return_val_if_fail (fd != NULL, FALSE);
873 g_return_val_if_fail (fd->fd >= 0, FALSE);
875 GST_DEBUG ("%p: fd (fd:%d, idx:%d), active : %d", set,
876 fd->fd, fd->idx, active);
878 g_mutex_lock (&set->lock);
880 idx = find_index (set->fds, fd);
883 struct pollfd *pfd = &g_array_index (set->fds, struct pollfd, idx);
886 pfd->events |= POLLOUT;
888 pfd->events &= ~POLLOUT;
890 GST_LOG ("pfd->events now %d (POLLOUT:%d)", pfd->events, POLLOUT);
892 gst_poll_update_winsock_event_mask (set, idx, FD_WRITE | FD_CONNECT,
897 GST_WARNING ("%p: couldn't find fd !", set);
900 g_mutex_unlock (&set->lock);
906 gst_poll_fd_ctl_read_unlocked (GstPoll * set, GstPollFD * fd, gboolean active)
910 GST_DEBUG ("%p: fd (fd:%d, idx:%d), active : %d", set,
911 fd->fd, fd->idx, active);
913 idx = find_index (set->fds, fd);
917 struct pollfd *pfd = &g_array_index (set->fds, struct pollfd, idx);
920 pfd->events |= (POLLIN | POLLPRI);
922 pfd->events &= ~(POLLIN | POLLPRI);
924 gst_poll_update_winsock_event_mask (set, idx, FD_READ | FD_ACCEPT, active);
928 GST_WARNING ("%p: couldn't find fd !", set);
935 * gst_poll_fd_ctl_read:
936 * @set: a file descriptor set.
937 * @fd: a file descriptor.
938 * @active: a new status.
940 * Control whether the descriptor @fd in @set will be monitored for
943 * Returns: %TRUE if the descriptor was successfully updated.
948 gst_poll_fd_ctl_read (GstPoll * set, GstPollFD * fd, gboolean active)
952 g_return_val_if_fail (set != NULL, FALSE);
953 g_return_val_if_fail (fd != NULL, FALSE);
954 g_return_val_if_fail (fd->fd >= 0, FALSE);
956 g_mutex_lock (&set->lock);
958 ret = gst_poll_fd_ctl_read_unlocked (set, fd, active);
960 g_mutex_unlock (&set->lock);
966 * gst_poll_fd_ignored:
967 * @set: a file descriptor set.
968 * @fd: a file descriptor.
970 * Mark @fd as ignored so that the next call to gst_poll_wait() will yield
971 * the same result for @fd as last time. This function must be called if no
972 * operation (read/write/recv/send/etc.) will be performed on @fd before
973 * the next call to gst_poll_wait().
975 * The reason why this is needed is because the underlying implementation
976 * might not allow querying the fd more than once between calls to one of
977 * the re-enabling operations.
982 gst_poll_fd_ignored (GstPoll * set, GstPollFD * fd)
987 g_return_if_fail (set != NULL);
988 g_return_if_fail (fd != NULL);
989 g_return_if_fail (fd->fd >= 0);
991 g_mutex_lock (&set->lock);
993 idx = find_index (set->fds, fd);
995 WinsockFd *wfd = &g_array_index (set->fds, WinsockFd, idx);
997 wfd->ignored_event_mask = wfd->event_mask & (FD_READ | FD_WRITE);
1001 g_mutex_unlock (&set->lock);
1006 * gst_poll_fd_has_closed:
1007 * @set: a file descriptor set.
1008 * @fd: a file descriptor.
1010 * Check if @fd in @set has closed the connection.
1012 * Returns: %TRUE if the connection was closed.
1017 gst_poll_fd_has_closed (const GstPoll * set, GstPollFD * fd)
1019 gboolean res = FALSE;
1022 g_return_val_if_fail (set != NULL, FALSE);
1023 g_return_val_if_fail (fd != NULL, FALSE);
1024 g_return_val_if_fail (fd->fd >= 0, FALSE);
1026 GST_DEBUG ("%p: fd (fd:%d, idx:%d)", set, fd->fd, fd->idx);
1028 g_mutex_lock (&((GstPoll *) set)->lock);
1030 idx = find_index (set->active_fds, fd);
1033 struct pollfd *pfd = &g_array_index (set->active_fds, struct pollfd, idx);
1035 res = (pfd->revents & POLLHUP) != 0;
1037 WinsockFd *wfd = &g_array_index (set->active_fds, WinsockFd, idx);
1039 res = (wfd->events.lNetworkEvents & FD_CLOSE) != 0;
1042 GST_WARNING ("%p: couldn't find fd !", set);
1045 g_mutex_unlock (&((GstPoll *) set)->lock);
1051 * gst_poll_fd_has_error:
1052 * @set: a file descriptor set.
1053 * @fd: a file descriptor.
1055 * Check if @fd in @set has an error.
1057 * Returns: %TRUE if the descriptor has an error.
1062 gst_poll_fd_has_error (const GstPoll * set, GstPollFD * fd)
1064 gboolean res = FALSE;
1067 g_return_val_if_fail (set != NULL, FALSE);
1068 g_return_val_if_fail (fd != NULL, FALSE);
1069 g_return_val_if_fail (fd->fd >= 0, FALSE);
1071 GST_DEBUG ("%p: fd (fd:%d, idx:%d)", set, fd->fd, fd->idx);
1073 g_mutex_lock (&((GstPoll *) set)->lock);
1075 idx = find_index (set->active_fds, fd);
1078 struct pollfd *pfd = &g_array_index (set->active_fds, struct pollfd, idx);
1080 res = (pfd->revents & (POLLERR | POLLNVAL)) != 0;
1082 WinsockFd *wfd = &g_array_index (set->active_fds, WinsockFd, idx);
1084 res = (wfd->events.iErrorCode[FD_CLOSE_BIT] != 0) ||
1085 (wfd->events.iErrorCode[FD_READ_BIT] != 0) ||
1086 (wfd->events.iErrorCode[FD_WRITE_BIT] != 0) ||
1087 (wfd->events.iErrorCode[FD_ACCEPT_BIT] != 0) ||
1088 (wfd->events.iErrorCode[FD_CONNECT_BIT] != 0);
1091 GST_WARNING ("%p: couldn't find fd !", set);
1094 g_mutex_unlock (&((GstPoll *) set)->lock);
1100 gst_poll_fd_can_read_unlocked (const GstPoll * set, GstPollFD * fd)
1102 gboolean res = FALSE;
1105 GST_DEBUG ("%p: fd (fd:%d, idx:%d)", set, fd->fd, fd->idx);
1107 idx = find_index (set->active_fds, fd);
1110 struct pollfd *pfd = &g_array_index (set->active_fds, struct pollfd, idx);
1112 res = (pfd->revents & (POLLIN | POLLPRI)) != 0;
1114 WinsockFd *wfd = &g_array_index (set->active_fds, WinsockFd, idx);
1116 res = (wfd->events.lNetworkEvents & (FD_READ | FD_ACCEPT)) != 0;
1119 GST_WARNING ("%p: couldn't find fd !", set);
1126 * gst_poll_fd_can_read:
1127 * @set: a file descriptor set.
1128 * @fd: a file descriptor.
1130 * Check if @fd in @set has data to be read.
1132 * Returns: %TRUE if the descriptor has data to be read.
1137 gst_poll_fd_can_read (const GstPoll * set, GstPollFD * fd)
1139 gboolean res = FALSE;
1141 g_return_val_if_fail (set != NULL, FALSE);
1142 g_return_val_if_fail (fd != NULL, FALSE);
1143 g_return_val_if_fail (fd->fd >= 0, FALSE);
1145 g_mutex_lock (&((GstPoll *) set)->lock);
1147 res = gst_poll_fd_can_read_unlocked (set, fd);
1149 g_mutex_unlock (&((GstPoll *) set)->lock);
1155 * gst_poll_fd_can_write:
1156 * @set: a file descriptor set.
1157 * @fd: a file descriptor.
1159 * Check if @fd in @set can be used for writing.
1161 * Returns: %TRUE if the descriptor can be used for writing.
1166 gst_poll_fd_can_write (const GstPoll * set, GstPollFD * fd)
1168 gboolean res = FALSE;
1171 g_return_val_if_fail (set != NULL, FALSE);
1172 g_return_val_if_fail (fd != NULL, FALSE);
1173 g_return_val_if_fail (fd->fd >= 0, FALSE);
1175 GST_DEBUG ("%p: fd (fd:%d, idx:%d)", set, fd->fd, fd->idx);
1177 g_mutex_lock (&((GstPoll *) set)->lock);
1179 idx = find_index (set->active_fds, fd);
1182 struct pollfd *pfd = &g_array_index (set->active_fds, struct pollfd, idx);
1184 res = (pfd->revents & POLLOUT) != 0;
1186 WinsockFd *wfd = &g_array_index (set->active_fds, WinsockFd, idx);
1188 res = (wfd->events.lNetworkEvents & FD_WRITE) != 0;
1191 GST_WARNING ("%p: couldn't find fd !", set);
1194 g_mutex_unlock (&((GstPoll *) set)->lock);
1202 * @timeout: a timeout in nanoseconds.
1204 * Wait for activity on the file descriptors in @set. This function waits up to
1205 * the specified @timeout. A timeout of #GST_CLOCK_TIME_NONE waits forever.
1207 * For #GstPoll objects created with gst_poll_new(), this function can only be
1208 * called from a single thread at a time. If called from multiple threads,
1209 * -1 will be returned with errno set to EPERM.
1211 * This is not true for timer #GstPoll objects created with
1212 * gst_poll_new_timer(), where it is allowed to have multiple threads waiting
1215 * Returns: The number of #GstPollFD in @set that have activity or 0 when no
1216 * activity was detected after @timeout. If an error occurs, -1 is returned
1222 gst_poll_wait (GstPoll * set, GstClockTime timeout)
1224 gboolean restarting;
1229 g_return_val_if_fail (set != NULL, -1);
1231 GST_DEBUG ("timeout :%" GST_TIME_FORMAT, GST_TIME_ARGS (timeout));
1233 is_timer = set->timer;
1235 /* add one more waiter */
1236 old_waiting = INC_WAITING (set);
1238 /* we cannot wait from multiple threads unless we are a timer */
1239 if (G_UNLIKELY (old_waiting > 0 && !is_timer))
1240 goto already_waiting;
1242 /* flushing, exit immediately */
1243 if (G_UNLIKELY (IS_FLUSHING (set)))
1252 mode = choose_mode (set, timeout);
1254 if (TEST_REBUILD (set)) {
1255 g_mutex_lock (&set->lock);
1257 g_array_set_size (set->active_fds, set->fds->len);
1258 memcpy (set->active_fds->data, set->fds->data,
1259 set->fds->len * sizeof (struct pollfd));
1261 if (!gst_poll_prepare_winsock_active_sets (set))
1264 g_mutex_unlock (&set->lock);
1268 case GST_POLL_MODE_AUTO:
1269 g_assert_not_reached ();
1271 case GST_POLL_MODE_PPOLL:
1275 struct timespec *tsptr;
1277 if (timeout != GST_CLOCK_TIME_NONE) {
1278 GST_TIME_TO_TIMESPEC (timeout, ts);
1285 ppoll ((struct pollfd *) set->active_fds->data,
1286 set->active_fds->len, tsptr, NULL);
1288 g_assert_not_reached ();
1293 case GST_POLL_MODE_POLL:
1298 if (timeout != GST_CLOCK_TIME_NONE) {
1299 t = GST_TIME_AS_MSECONDS (timeout);
1305 poll ((struct pollfd *) set->active_fds->data,
1306 set->active_fds->len, t);
1308 g_assert_not_reached ();
1313 case GST_POLL_MODE_PSELECT:
1314 #ifndef HAVE_PSELECT
1316 g_assert_not_reached ();
1321 case GST_POLL_MODE_SELECT:
1329 max_fd = pollfd_to_fd_set (set, &readfds, &writefds, &errorfds);
1331 if (mode == GST_POLL_MODE_SELECT) {
1333 struct timeval *tvptr;
1335 if (timeout != GST_CLOCK_TIME_NONE) {
1336 GST_TIME_TO_TIMEVAL (timeout, tv);
1342 GST_DEBUG ("Calling select");
1343 res = select (max_fd + 1, &readfds, &writefds, &errorfds, tvptr);
1344 GST_DEBUG ("After select, res:%d", res);
1348 struct timespec *tsptr;
1350 if (timeout != GST_CLOCK_TIME_NONE) {
1351 GST_TIME_TO_TIMESPEC (timeout, ts);
1357 GST_DEBUG ("Calling pselect");
1359 pselect (max_fd + 1, &readfds, &writefds, &errorfds, tsptr, NULL);
1360 GST_DEBUG ("After pselect, res:%d", res);
1365 fd_set_to_pollfd (set, &readfds, &writefds, &errorfds);
1367 #else /* G_OS_WIN32 */
1368 g_assert_not_reached ();
1373 case GST_POLL_MODE_WINDOWS:
1376 gint ignore_count = set->active_fds_ignored->len;
1379 if (G_LIKELY (ignore_count == 0)) {
1380 if (timeout != GST_CLOCK_TIME_NONE)
1381 t = GST_TIME_AS_MSECONDS (timeout);
1385 /* already one or more ignored fds, so we quickly sweep the others */
1389 if (set->active_events->len != 0) {
1390 wait_ret = WSAWaitForMultipleEvents (set->active_events->len,
1391 (HANDLE *) set->active_events->data, FALSE, t, FALSE);
1393 wait_ret = WSA_WAIT_FAILED;
1394 WSASetLastError (WSA_INVALID_PARAMETER);
1397 if (ignore_count == 0 && wait_ret == WSA_WAIT_TIMEOUT) {
1399 } else if (wait_ret == WSA_WAIT_FAILED) {
1401 errno = gst_poll_winsock_error_to_errno (WSAGetLastError ());
1403 /* the first entry is the wakeup event */
1404 if (wait_ret - WSA_WAIT_EVENT_0 >= 1) {
1405 res = gst_poll_collect_winsock_events (set);
1407 res = 1; /* wakeup event */
1411 g_assert_not_reached ();
1419 /* Applications needs to clear the control socket themselves for timer
1421 * For other polls, we need to clear the control socket. If there was only
1422 * one socket with activity and it was the control socket, we need to
1424 if (release_all_wakeup (set) > 0 && res == 1)
1428 /* we got woken up and we are flushing, we need to stop */
1429 if (G_UNLIKELY (IS_FLUSHING (set)))
1432 } while (G_UNLIKELY (restarting));
1441 GST_LOG ("%p: we are already waiting", set);
1448 GST_LOG ("%p: we are flushing", set);
1456 GST_LOG ("%p: winsock error", set);
1457 g_mutex_unlock (&set->lock);
1465 * gst_poll_set_controllable:
1467 * @controllable: new controllable state.
1469 * When @controllable is %TRUE, this function ensures that future calls to
1470 * gst_poll_wait() will be affected by gst_poll_restart() and
1471 * gst_poll_set_flushing().
1473 * Returns: %TRUE if the controllability of @set could be updated.
1478 gst_poll_set_controllable (GstPoll * set, gboolean controllable)
1480 g_return_val_if_fail (set != NULL, FALSE);
1482 GST_LOG ("%p: controllable : %d", set, controllable);
1484 set->controllable = controllable;
1493 * Restart any gst_poll_wait() that is in progress. This function is typically
1494 * used after adding or removing descriptors to @set.
1496 * If @set is not controllable, then this call will have no effect.
1501 gst_poll_restart (GstPoll * set)
1503 g_return_if_fail (set != NULL);
1505 if (set->controllable && GET_WAITING (set) > 0) {
1506 /* we are controllable and waiting, wake up the waiter. The socket will be
1507 * cleared by the _wait() thread and the poll will be restarted */
1513 * gst_poll_set_flushing:
1515 * @flushing: new flushing state.
1517 * When @flushing is %TRUE, this function ensures that current and future calls
1518 * to gst_poll_wait() will return -1, with errno set to EBUSY.
1520 * Unsetting the flushing state will restore normal operation of @set.
1525 gst_poll_set_flushing (GstPoll * set, gboolean flushing)
1527 g_return_if_fail (set != NULL);
1529 GST_LOG ("%p: flushing: %d", set, flushing);
1531 /* update the new state first */
1532 SET_FLUSHING (set, flushing);
1534 if (flushing && set->controllable && GET_WAITING (set) > 0) {
1535 /* we are flushing, controllable and waiting, wake up the waiter. When we
1536 * stop the flushing operation we don't clear the wakeup fd here, this will
1537 * happen in the _wait() thread. */
1543 * gst_poll_write_control:
1546 * Write a byte to the control socket of the controllable @set.
1547 * This function is mostly useful for timer #GstPoll objects created with
1548 * gst_poll_new_timer().
1550 * It will make any current and future gst_poll_wait() function return with
1551 * 1, meaning the control socket is set. After an equal amount of calls to
1552 * gst_poll_read_control() have been performed, calls to gst_poll_wait() will
1553 * block again until their timeout expired.
1555 * Returns: %TRUE on success. %FALSE when @set is not controllable or when the
1556 * byte could not be written.
1561 gst_poll_write_control (GstPoll * set)
1565 g_return_val_if_fail (set != NULL, FALSE);
1566 g_return_val_if_fail (set->timer, FALSE);
1568 res = raise_wakeup (set);
1574 * gst_poll_read_control:
1577 * Read a byte from the control socket of the controllable @set.
1578 * This function is mostly useful for timer #GstPoll objects created with
1579 * gst_poll_new_timer().
1581 * Returns: %TRUE on success. %FALSE when @set is not controllable or when there
1582 * was no byte to read.
1587 gst_poll_read_control (GstPoll * set)
1591 g_return_val_if_fail (set != NULL, FALSE);
1592 g_return_val_if_fail (set->timer, FALSE);
1594 res = release_wakeup (set);