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
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 result = WAKE_EVENT (set);
192 /* note how bad things can happen when the 2 threads both raise and release the
193 * wakeup. This is however not a problem because you must always pair a raise
195 static inline gboolean
196 release_wakeup (GstPoll * set)
198 gboolean result = TRUE;
200 if (g_atomic_int_dec_and_test (&set->control_pending)) {
201 result = RELEASE_EVENT (set);
207 release_all_wakeup (GstPoll * set)
212 if (!(old = g_atomic_int_get (&set->control_pending)))
213 /* nothing pending, just exit */
216 /* try to remove all pending control messages */
217 if (g_atomic_int_compare_and_exchange (&set->control_pending, old, 0)) {
218 /* we managed to remove all messages, read the control socket */
219 if (RELEASE_EVENT (set))
222 /* retry again until we read it successfully */
223 G_ATOMIC_INT_ADD (&set->control_pending, 1);
230 find_index (GArray * array, GstPollFD * fd)
239 /* start by assuming the index found in the fd is still valid */
240 if (fd->idx >= 0 && fd->idx < array->len) {
242 ifd = &g_array_index (array, struct pollfd, fd->idx);
244 ifd = &g_array_index (array, WinsockFd, fd->idx);
247 if (ifd->fd == fd->fd) {
252 /* the pollfd array has changed and we need to lookup the fd again */
253 for (i = 0; i < array->len; i++) {
255 ifd = &g_array_index (array, struct pollfd, i);
257 ifd = &g_array_index (array, WinsockFd, i);
260 if (ifd->fd == fd->fd) {
270 #if !defined(HAVE_PPOLL) && defined(HAVE_POLL)
271 /* check if all file descriptors will fit in an fd_set */
273 selectable_fds (const GstPoll * set)
277 g_mutex_lock (set->lock);
278 for (i = 0; i < set->fds->len; i++) {
279 struct pollfd *pfd = &g_array_index (set->fds, struct pollfd, i);
281 if (pfd->fd >= FD_SETSIZE)
284 g_mutex_unlock (set->lock);
290 g_mutex_unlock (set->lock);
295 /* check if the timeout will convert to a timeout value used for poll()
296 * without a loss of precision
299 pollable_timeout (GstClockTime timeout)
301 if (timeout == GST_CLOCK_TIME_NONE)
304 /* not a nice multiple of milliseconds */
305 if (timeout % 1000000)
313 choose_mode (const GstPoll * set, GstClockTime timeout)
317 if (set->mode == GST_POLL_MODE_AUTO) {
319 mode = GST_POLL_MODE_PPOLL;
320 #elif defined(HAVE_POLL)
321 if (!selectable_fds (set) || pollable_timeout (timeout)) {
322 mode = GST_POLL_MODE_POLL;
325 mode = GST_POLL_MODE_PSELECT;
327 mode = GST_POLL_MODE_SELECT;
330 #elif defined(HAVE_PSELECT)
331 mode = GST_POLL_MODE_PSELECT;
333 mode = GST_POLL_MODE_SELECT;
343 pollfd_to_fd_set (GstPoll * set, fd_set * readfds, fd_set * writefds,
353 g_mutex_lock (set->lock);
355 for (i = 0; i < set->active_fds->len; i++) {
356 struct pollfd *pfd = &g_array_index (set->fds, struct pollfd, i);
358 if (pfd->fd < FD_SETSIZE) {
359 if (pfd->events & POLLIN)
360 FD_SET (pfd->fd, readfds);
361 if (pfd->events & POLLOUT)
362 FD_SET (pfd->fd, writefds);
364 FD_SET (pfd->fd, errorfds);
365 if (pfd->fd > max_fd && (pfd->events & (POLLIN | POLLOUT)))
370 g_mutex_unlock (set->lock);
376 fd_set_to_pollfd (GstPoll * set, fd_set * readfds, fd_set * writefds,
381 g_mutex_lock (set->lock);
383 for (i = 0; i < set->active_fds->len; i++) {
384 struct pollfd *pfd = &g_array_index (set->active_fds, struct pollfd, i);
386 if (pfd->fd < FD_SETSIZE) {
388 if (FD_ISSET (pfd->fd, readfds))
389 pfd->revents |= POLLIN;
390 if (FD_ISSET (pfd->fd, writefds))
391 pfd->revents |= POLLOUT;
392 if (FD_ISSET (pfd->fd, errorfds))
393 pfd->revents |= POLLERR;
397 g_mutex_unlock (set->lock);
399 #else /* G_OS_WIN32 */
401 * Translate errors thrown by the Winsock API used by GstPoll:
402 * WSAEventSelect, WSAWaitForMultipleEvents and WSAEnumNetworkEvents
405 gst_poll_winsock_error_to_errno (DWORD last_error)
407 switch (last_error) {
408 case WSA_INVALID_HANDLE:
413 case WSA_NOT_ENOUGH_MEMORY:
417 * Anything else, including:
418 * WSA_INVALID_PARAMETER, WSAEFAULT, WSAEINPROGRESS, WSAENETDOWN,
427 gst_poll_free_winsock_event (GstPoll * set, gint idx)
429 WinsockFd *wfd = &g_array_index (set->fds, WinsockFd, idx);
430 HANDLE event = g_array_index (set->events, HANDLE, idx);
432 WSAEventSelect (wfd->fd, event, 0);
437 gst_poll_update_winsock_event_mask (GstPoll * set, gint idx, glong flags,
442 wfd = &g_array_index (set->fds, WinsockFd, idx);
445 wfd->event_mask |= flags;
447 wfd->event_mask &= ~flags;
449 /* reset ignored state if the new mask doesn't overlap at all */
450 if ((wfd->ignored_event_mask & wfd->event_mask) == 0)
451 wfd->ignored_event_mask = 0;
455 gst_poll_prepare_winsock_active_sets (GstPoll * set)
459 g_array_set_size (set->active_fds, 0);
460 g_array_set_size (set->active_fds_ignored, 0);
461 g_array_set_size (set->active_events, 0);
462 g_array_append_val (set->active_events, set->wakeup_event);
464 for (i = 0; i < set->fds->len; i++) {
465 WinsockFd *wfd = &g_array_index (set->fds, WinsockFd, i);
466 HANDLE event = g_array_index (set->events, HANDLE, i);
468 if (wfd->ignored_event_mask == 0) {
471 g_array_append_val (set->active_fds, *wfd);
472 g_array_append_val (set->active_events, event);
474 ret = WSAEventSelect (wfd->fd, event, wfd->event_mask);
475 if (G_UNLIKELY (ret != 0)) {
476 errno = gst_poll_winsock_error_to_errno (WSAGetLastError ());
480 g_array_append_val (set->active_fds_ignored, wfd);
488 gst_poll_collect_winsock_events (GstPoll * set)
493 * We need to check which events are signaled, and call
494 * WSAEnumNetworkEvents for those that are, which resets
495 * the event and clears the internal network event records.
498 for (i = 0; i < set->active_fds->len; i++) {
499 WinsockFd *wfd = &g_array_index (set->active_fds, WinsockFd, i);
500 HANDLE event = g_array_index (set->active_events, HANDLE, i + 1);
503 wait_ret = WaitForSingleObject (event, 0);
504 if (wait_ret == WAIT_OBJECT_0) {
505 gint enum_ret = WSAEnumNetworkEvents (wfd->fd, event, &wfd->events);
507 if (G_UNLIKELY (enum_ret != 0)) {
509 errno = gst_poll_winsock_error_to_errno (WSAGetLastError ());
515 /* clear any previously stored result */
516 memset (&wfd->events, 0, sizeof (wfd->events));
520 /* If all went well we also need to reset the ignored fds. */
522 res += set->active_fds_ignored->len;
524 for (i = 0; i < set->active_fds_ignored->len; i++) {
525 WinsockFd *wfd = g_array_index (set->active_fds_ignored, WinsockFd *, i);
527 wfd->ignored_event_mask = 0;
530 g_array_set_size (set->active_fds_ignored, 0);
539 * @controllable: whether it should be possible to control a wait.
541 * Create a new file descriptor set. If @controllable, it
542 * is possible to restart or flush a call to gst_poll_wait() with
543 * gst_poll_restart() and gst_poll_set_flushing() respectively.
545 * Free-function: gst_poll_free
547 * Returns: (transfer full): a new #GstPoll, or %NULL in case of an error.
548 * Free with gst_poll_free().
553 gst_poll_new (gboolean controllable)
557 GST_DEBUG ("controllable : %d", controllable);
559 nset = g_slice_new0 (GstPoll);
560 nset->lock = g_mutex_new ();
562 nset->mode = GST_POLL_MODE_AUTO;
563 nset->fds = g_array_new (FALSE, FALSE, sizeof (struct pollfd));
564 nset->active_fds = g_array_new (FALSE, FALSE, sizeof (struct pollfd));
565 nset->control_read_fd.fd = -1;
566 nset->control_write_fd.fd = -1;
568 gint control_sock[2];
570 if (socketpair (PF_UNIX, SOCK_STREAM, 0, control_sock) < 0)
573 fcntl (control_sock[0], F_SETFL, O_NONBLOCK);
574 fcntl (control_sock[1], F_SETFL, O_NONBLOCK);
576 nset->control_read_fd.fd = control_sock[0];
577 nset->control_write_fd.fd = control_sock[1];
579 gst_poll_add_fd_unlocked (nset, &nset->control_read_fd);
580 gst_poll_fd_ctl_read_unlocked (nset, &nset->control_read_fd, TRUE);
583 nset->mode = GST_POLL_MODE_WINDOWS;
584 nset->fds = g_array_new (FALSE, FALSE, sizeof (WinsockFd));
585 nset->active_fds = g_array_new (FALSE, FALSE, sizeof (WinsockFd));
586 nset->active_fds_ignored = g_array_new (FALSE, FALSE, sizeof (WinsockFd *));
587 nset->events = g_array_new (FALSE, FALSE, sizeof (HANDLE));
588 nset->active_events = g_array_new (FALSE, FALSE, sizeof (HANDLE));
590 nset->wakeup_event = CreateEvent (NULL, TRUE, FALSE, NULL);
593 /* ensure (re)build, though already sneakily set in non-windows case */
596 nset->controllable = controllable;
604 GST_WARNING ("%p: can't create socket pair !", nset);
605 gst_poll_free (nset);
612 * gst_poll_new_timer:
614 * Create a new poll object that can be used for scheduling cancellable
617 * A timeout is performed with gst_poll_wait(). Multiple timeouts can be
618 * performed from different threads.
620 * Free-function: gst_poll_free
622 * Returns: (transfer full): a new #GstPoll, or %NULL in case of an error.
623 * Free with gst_poll_free().
628 gst_poll_new_timer (void)
632 /* make a new controllable poll set */
633 if (!(poll = gst_poll_new (TRUE)))
645 * @set: (transfer full): a file descriptor set.
647 * Free a file descriptor set.
652 gst_poll_free (GstPoll * set)
654 g_return_if_fail (set != NULL);
656 GST_DEBUG ("%p: freeing", set);
659 if (set->control_write_fd.fd >= 0)
660 close (set->control_write_fd.fd);
661 if (set->control_read_fd.fd >= 0)
662 close (set->control_read_fd.fd);
664 CloseHandle (set->wakeup_event);
669 for (i = 0; i < set->events->len; i++)
670 gst_poll_free_winsock_event (set, i);
673 g_array_free (set->active_events, TRUE);
674 g_array_free (set->events, TRUE);
675 g_array_free (set->active_fds_ignored, TRUE);
678 g_array_free (set->active_fds, TRUE);
679 g_array_free (set->fds, TRUE);
680 g_mutex_free (set->lock);
681 g_slice_free (GstPoll, set);
685 * gst_poll_get_read_gpollfd:
689 * Get a GPollFD for the reading part of the control socket. This is useful when
690 * integrating with a GSource and GMainLoop.
695 gst_poll_get_read_gpollfd (GstPoll * set, GPollFD * fd)
697 g_return_if_fail (set != NULL);
698 g_return_if_fail (fd != NULL);
701 fd->fd = set->control_read_fd.fd;
703 #if GLIB_SIZEOF_VOID_P == 8
704 fd->fd = (gint64) set->wakeup_event;
706 fd->fd = (gint) set->wakeup_event;
709 fd->events = G_IO_IN | G_IO_HUP | G_IO_ERR;
717 * Initializes @fd. Alternatively you can initialize it with
723 gst_poll_fd_init (GstPollFD * fd)
725 g_return_if_fail (fd != NULL);
732 gst_poll_add_fd_unlocked (GstPoll * set, GstPollFD * fd)
736 GST_DEBUG ("%p: fd (fd:%d, idx:%d)", set, fd->fd, fd->idx);
738 idx = find_index (set->fds, fd);
744 nfd.events = POLLERR | POLLNVAL | POLLHUP;
747 g_array_append_val (set->fds, nfd);
749 fd->idx = set->fds->len - 1;
755 wfd.event_mask = FD_CLOSE;
756 memset (&wfd.events, 0, sizeof (wfd.events));
757 wfd.ignored_event_mask = 0;
758 event = WSACreateEvent ();
760 g_array_append_val (set->fds, wfd);
761 g_array_append_val (set->events, event);
763 fd->idx = set->fds->len - 1;
767 GST_WARNING ("%p: couldn't find fd !", set);
775 * @set: a file descriptor set.
776 * @fd: a file descriptor.
778 * Add a file descriptor to the file descriptor set.
780 * Returns: %TRUE if the file descriptor was successfully added to the set.
785 gst_poll_add_fd (GstPoll * set, GstPollFD * fd)
789 g_return_val_if_fail (set != NULL, FALSE);
790 g_return_val_if_fail (fd != NULL, FALSE);
791 g_return_val_if_fail (fd->fd >= 0, FALSE);
793 g_mutex_lock (set->lock);
795 ret = gst_poll_add_fd_unlocked (set, fd);
797 g_mutex_unlock (set->lock);
803 * gst_poll_remove_fd:
804 * @set: a file descriptor set.
805 * @fd: a file descriptor.
807 * Remove a file descriptor from the file descriptor set.
809 * Returns: %TRUE if the file descriptor was successfully removed from the set.
814 gst_poll_remove_fd (GstPoll * set, GstPollFD * fd)
818 g_return_val_if_fail (set != NULL, FALSE);
819 g_return_val_if_fail (fd != NULL, FALSE);
820 g_return_val_if_fail (fd->fd >= 0, FALSE);
823 GST_DEBUG ("%p: fd (fd:%d, idx:%d)", set, fd->fd, fd->idx);
825 g_mutex_lock (set->lock);
827 /* get the index, -1 is an fd that is not added */
828 idx = find_index (set->fds, fd);
831 gst_poll_free_winsock_event (set, idx);
832 g_array_remove_index_fast (set->events, idx);
835 /* remove the fd at index, we use _remove_index_fast, which copies the last
836 * element of the array to the freed index */
837 g_array_remove_index_fast (set->fds, idx);
839 /* mark fd as removed by setting the index to -1 */
843 GST_WARNING ("%p: couldn't find fd !", set);
846 g_mutex_unlock (set->lock);
852 * gst_poll_fd_ctl_write:
853 * @set: a file descriptor set.
854 * @fd: a file descriptor.
855 * @active: a new status.
857 * Control whether the descriptor @fd in @set will be monitored for
860 * Returns: %TRUE if the descriptor was successfully updated.
865 gst_poll_fd_ctl_write (GstPoll * set, GstPollFD * fd, gboolean active)
869 g_return_val_if_fail (set != NULL, FALSE);
870 g_return_val_if_fail (fd != NULL, FALSE);
871 g_return_val_if_fail (fd->fd >= 0, FALSE);
873 GST_DEBUG ("%p: fd (fd:%d, idx:%d), active : %d", set,
874 fd->fd, fd->idx, active);
876 g_mutex_lock (set->lock);
878 idx = find_index (set->fds, fd);
881 struct pollfd *pfd = &g_array_index (set->fds, struct pollfd, idx);
884 pfd->events |= POLLOUT;
886 pfd->events &= ~POLLOUT;
888 GST_LOG ("pfd->events now %d (POLLOUT:%d)", pfd->events, POLLOUT);
890 gst_poll_update_winsock_event_mask (set, idx, FD_WRITE | FD_CONNECT,
895 GST_WARNING ("%p: couldn't find fd !", set);
898 g_mutex_unlock (set->lock);
904 gst_poll_fd_ctl_read_unlocked (GstPoll * set, GstPollFD * fd, gboolean active)
908 GST_DEBUG ("%p: fd (fd:%d, idx:%d), active : %d", set,
909 fd->fd, fd->idx, active);
911 idx = find_index (set->fds, fd);
915 struct pollfd *pfd = &g_array_index (set->fds, struct pollfd, idx);
918 pfd->events |= (POLLIN | POLLPRI);
920 pfd->events &= ~(POLLIN | POLLPRI);
922 gst_poll_update_winsock_event_mask (set, idx, FD_READ | FD_ACCEPT, active);
926 GST_WARNING ("%p: couldn't find fd !", set);
933 * gst_poll_fd_ctl_read:
934 * @set: a file descriptor set.
935 * @fd: a file descriptor.
936 * @active: a new status.
938 * Control whether the descriptor @fd in @set will be monitored for
941 * Returns: %TRUE if the descriptor was successfully updated.
946 gst_poll_fd_ctl_read (GstPoll * set, GstPollFD * fd, gboolean active)
950 g_return_val_if_fail (set != NULL, FALSE);
951 g_return_val_if_fail (fd != NULL, FALSE);
952 g_return_val_if_fail (fd->fd >= 0, FALSE);
954 g_mutex_lock (set->lock);
956 ret = gst_poll_fd_ctl_read_unlocked (set, fd, active);
958 g_mutex_unlock (set->lock);
964 * gst_poll_fd_ignored:
965 * @set: a file descriptor set.
966 * @fd: a file descriptor.
968 * Mark @fd as ignored so that the next call to gst_poll_wait() will yield
969 * the same result for @fd as last time. This function must be called if no
970 * operation (read/write/recv/send/etc.) will be performed on @fd before
971 * the next call to gst_poll_wait().
973 * The reason why this is needed is because the underlying implementation
974 * might not allow querying the fd more than once between calls to one of
975 * the re-enabling operations.
980 gst_poll_fd_ignored (GstPoll * set, GstPollFD * fd)
985 g_return_if_fail (set != NULL);
986 g_return_if_fail (fd != NULL);
987 g_return_if_fail (fd->fd >= 0);
989 g_mutex_lock (set->lock);
991 idx = find_index (set->fds, fd);
993 WinsockFd *wfd = &g_array_index (set->fds, WinsockFd, idx);
995 wfd->ignored_event_mask = wfd->event_mask & (FD_READ | FD_WRITE);
999 g_mutex_unlock (set->lock);
1004 * gst_poll_fd_has_closed:
1005 * @set: a file descriptor set.
1006 * @fd: a file descriptor.
1008 * Check if @fd in @set has closed the connection.
1010 * Returns: %TRUE if the connection was closed.
1015 gst_poll_fd_has_closed (const GstPoll * set, GstPollFD * fd)
1017 gboolean res = FALSE;
1020 g_return_val_if_fail (set != NULL, FALSE);
1021 g_return_val_if_fail (fd != NULL, FALSE);
1022 g_return_val_if_fail (fd->fd >= 0, FALSE);
1024 GST_DEBUG ("%p: fd (fd:%d, idx:%d)", set, fd->fd, fd->idx);
1026 g_mutex_lock (set->lock);
1028 idx = find_index (set->active_fds, fd);
1031 struct pollfd *pfd = &g_array_index (set->active_fds, struct pollfd, idx);
1033 res = (pfd->revents & POLLHUP) != 0;
1035 WinsockFd *wfd = &g_array_index (set->active_fds, WinsockFd, idx);
1037 res = (wfd->events.lNetworkEvents & FD_CLOSE) != 0;
1040 GST_WARNING ("%p: couldn't find fd !", set);
1043 g_mutex_unlock (set->lock);
1049 * gst_poll_fd_has_error:
1050 * @set: a file descriptor set.
1051 * @fd: a file descriptor.
1053 * Check if @fd in @set has an error.
1055 * Returns: %TRUE if the descriptor has an error.
1060 gst_poll_fd_has_error (const GstPoll * set, GstPollFD * fd)
1062 gboolean res = FALSE;
1065 g_return_val_if_fail (set != NULL, FALSE);
1066 g_return_val_if_fail (fd != NULL, FALSE);
1067 g_return_val_if_fail (fd->fd >= 0, FALSE);
1069 GST_DEBUG ("%p: fd (fd:%d, idx:%d)", set, fd->fd, fd->idx);
1071 g_mutex_lock (set->lock);
1073 idx = find_index (set->active_fds, fd);
1076 struct pollfd *pfd = &g_array_index (set->active_fds, struct pollfd, idx);
1078 res = (pfd->revents & (POLLERR | POLLNVAL)) != 0;
1080 WinsockFd *wfd = &g_array_index (set->active_fds, WinsockFd, idx);
1082 res = (wfd->events.iErrorCode[FD_CLOSE_BIT] != 0) ||
1083 (wfd->events.iErrorCode[FD_READ_BIT] != 0) ||
1084 (wfd->events.iErrorCode[FD_WRITE_BIT] != 0) ||
1085 (wfd->events.iErrorCode[FD_ACCEPT_BIT] != 0) ||
1086 (wfd->events.iErrorCode[FD_CONNECT_BIT] != 0);
1089 GST_WARNING ("%p: couldn't find fd !", set);
1092 g_mutex_unlock (set->lock);
1098 gst_poll_fd_can_read_unlocked (const GstPoll * set, GstPollFD * fd)
1100 gboolean res = FALSE;
1103 GST_DEBUG ("%p: fd (fd:%d, idx:%d)", set, fd->fd, fd->idx);
1105 idx = find_index (set->active_fds, fd);
1108 struct pollfd *pfd = &g_array_index (set->active_fds, struct pollfd, idx);
1110 res = (pfd->revents & (POLLIN | POLLPRI)) != 0;
1112 WinsockFd *wfd = &g_array_index (set->active_fds, WinsockFd, idx);
1114 res = (wfd->events.lNetworkEvents & (FD_READ | FD_ACCEPT)) != 0;
1117 GST_WARNING ("%p: couldn't find fd !", set);
1124 * gst_poll_fd_can_read:
1125 * @set: a file descriptor set.
1126 * @fd: a file descriptor.
1128 * Check if @fd in @set has data to be read.
1130 * Returns: %TRUE if the descriptor has data to be read.
1135 gst_poll_fd_can_read (const GstPoll * set, GstPollFD * fd)
1137 gboolean res = FALSE;
1139 g_return_val_if_fail (set != NULL, FALSE);
1140 g_return_val_if_fail (fd != NULL, FALSE);
1141 g_return_val_if_fail (fd->fd >= 0, FALSE);
1143 g_mutex_lock (set->lock);
1145 res = gst_poll_fd_can_read_unlocked (set, fd);
1147 g_mutex_unlock (set->lock);
1153 * gst_poll_fd_can_write:
1154 * @set: a file descriptor set.
1155 * @fd: a file descriptor.
1157 * Check if @fd in @set can be used for writing.
1159 * Returns: %TRUE if the descriptor can be used for writing.
1164 gst_poll_fd_can_write (const GstPoll * set, GstPollFD * fd)
1166 gboolean res = FALSE;
1169 g_return_val_if_fail (set != NULL, FALSE);
1170 g_return_val_if_fail (fd != NULL, FALSE);
1171 g_return_val_if_fail (fd->fd >= 0, FALSE);
1173 GST_DEBUG ("%p: fd (fd:%d, idx:%d)", set, fd->fd, fd->idx);
1175 g_mutex_lock (set->lock);
1177 idx = find_index (set->active_fds, fd);
1180 struct pollfd *pfd = &g_array_index (set->active_fds, struct pollfd, idx);
1182 res = (pfd->revents & POLLOUT) != 0;
1184 WinsockFd *wfd = &g_array_index (set->active_fds, WinsockFd, idx);
1186 res = (wfd->events.lNetworkEvents & FD_WRITE) != 0;
1189 GST_WARNING ("%p: couldn't find fd !", set);
1192 g_mutex_unlock (set->lock);
1200 * @timeout: a timeout in nanoseconds.
1202 * Wait for activity on the file descriptors in @set. This function waits up to
1203 * the specified @timeout. A timeout of #GST_CLOCK_TIME_NONE waits forever.
1205 * For #GstPoll objects created with gst_poll_new(), this function can only be
1206 * called from a single thread at a time. If called from multiple threads,
1207 * -1 will be returned with errno set to EPERM.
1209 * This is not true for timer #GstPoll objects created with
1210 * gst_poll_new_timer(), where it is allowed to have multiple threads waiting
1213 * Returns: The number of #GstPollFD in @set that have activity or 0 when no
1214 * activity was detected after @timeout. If an error occurs, -1 is returned
1220 gst_poll_wait (GstPoll * set, GstClockTime timeout)
1222 gboolean restarting;
1227 g_return_val_if_fail (set != NULL, -1);
1229 GST_DEBUG ("timeout :%" GST_TIME_FORMAT, GST_TIME_ARGS (timeout));
1231 is_timer = set->timer;
1233 /* add one more waiter */
1234 old_waiting = INC_WAITING (set);
1236 /* we cannot wait from multiple threads unless we are a timer */
1237 if (G_UNLIKELY (old_waiting > 0 && !is_timer))
1238 goto already_waiting;
1240 /* flushing, exit immediately */
1241 if (G_UNLIKELY (IS_FLUSHING (set)))
1250 mode = choose_mode (set, timeout);
1252 if (TEST_REBUILD (set)) {
1253 g_mutex_lock (set->lock);
1255 g_array_set_size (set->active_fds, set->fds->len);
1256 memcpy (set->active_fds->data, set->fds->data,
1257 set->fds->len * sizeof (struct pollfd));
1259 if (!gst_poll_prepare_winsock_active_sets (set))
1262 g_mutex_unlock (set->lock);
1266 case GST_POLL_MODE_AUTO:
1267 g_assert_not_reached ();
1269 case GST_POLL_MODE_PPOLL:
1273 struct timespec *tsptr;
1275 if (timeout != GST_CLOCK_TIME_NONE) {
1276 GST_TIME_TO_TIMESPEC (timeout, ts);
1283 ppoll ((struct pollfd *) set->active_fds->data,
1284 set->active_fds->len, tsptr, NULL);
1286 g_assert_not_reached ();
1291 case GST_POLL_MODE_POLL:
1296 if (timeout != GST_CLOCK_TIME_NONE) {
1297 t = GST_TIME_AS_MSECONDS (timeout);
1303 poll ((struct pollfd *) set->active_fds->data,
1304 set->active_fds->len, t);
1306 g_assert_not_reached ();
1311 case GST_POLL_MODE_PSELECT:
1312 #ifndef HAVE_PSELECT
1314 g_assert_not_reached ();
1319 case GST_POLL_MODE_SELECT:
1327 max_fd = pollfd_to_fd_set (set, &readfds, &writefds, &errorfds);
1329 if (mode == GST_POLL_MODE_SELECT) {
1331 struct timeval *tvptr;
1333 if (timeout != GST_CLOCK_TIME_NONE) {
1334 GST_TIME_TO_TIMEVAL (timeout, tv);
1340 GST_DEBUG ("Calling select");
1341 res = select (max_fd + 1, &readfds, &writefds, &errorfds, tvptr);
1342 GST_DEBUG ("After select, res:%d", res);
1346 struct timespec *tsptr;
1348 if (timeout != GST_CLOCK_TIME_NONE) {
1349 GST_TIME_TO_TIMESPEC (timeout, ts);
1355 GST_DEBUG ("Calling pselect");
1357 pselect (max_fd + 1, &readfds, &writefds, &errorfds, tsptr, NULL);
1358 GST_DEBUG ("After pselect, res:%d", res);
1363 fd_set_to_pollfd (set, &readfds, &writefds, &errorfds);
1365 #else /* G_OS_WIN32 */
1366 g_assert_not_reached ();
1371 case GST_POLL_MODE_WINDOWS:
1374 gint ignore_count = set->active_fds_ignored->len;
1377 if (G_LIKELY (ignore_count == 0)) {
1378 if (timeout != GST_CLOCK_TIME_NONE)
1379 t = GST_TIME_AS_MSECONDS (timeout);
1383 /* already one or more ignored fds, so we quickly sweep the others */
1387 if (set->active_events->len != 0) {
1388 wait_ret = WSAWaitForMultipleEvents (set->active_events->len,
1389 (HANDLE *) set->active_events->data, FALSE, t, FALSE);
1391 wait_ret = WSA_WAIT_FAILED;
1392 WSASetLastError (WSA_INVALID_PARAMETER);
1395 if (ignore_count == 0 && wait_ret == WSA_WAIT_TIMEOUT) {
1397 } else if (wait_ret == WSA_WAIT_FAILED) {
1399 errno = gst_poll_winsock_error_to_errno (WSAGetLastError ());
1401 /* the first entry is the wakeup event */
1402 if (wait_ret - WSA_WAIT_EVENT_0 >= 1) {
1403 res = gst_poll_collect_winsock_events (set);
1405 res = 1; /* wakeup event */
1409 g_assert_not_reached ();
1417 /* Applications needs to clear the control socket themselves for timer
1419 * For other polls, we need to clear the control socket. If there was only
1420 * one socket with activity and it was the control socket, we need to
1422 if (release_all_wakeup (set) > 0 && res == 1)
1426 if (G_UNLIKELY (IS_FLUSHING (set))) {
1427 /* we got woken up and we are flushing, we need to stop */
1432 } while (G_UNLIKELY (restarting));
1454 g_mutex_unlock (set->lock);
1462 * gst_poll_set_controllable:
1464 * @controllable: new controllable state.
1466 * When @controllable is %TRUE, this function ensures that future calls to
1467 * gst_poll_wait() will be affected by gst_poll_restart() and
1468 * gst_poll_set_flushing().
1470 * Returns: %TRUE if the controllability of @set could be updated.
1475 gst_poll_set_controllable (GstPoll * set, gboolean controllable)
1477 g_return_val_if_fail (set != NULL, FALSE);
1479 GST_LOG ("%p: controllable : %d", set, controllable);
1481 set->controllable = controllable;
1490 * Restart any gst_poll_wait() that is in progress. This function is typically
1491 * used after adding or removing descriptors to @set.
1493 * If @set is not controllable, then this call will have no effect.
1498 gst_poll_restart (GstPoll * set)
1500 g_return_if_fail (set != NULL);
1502 if (set->controllable && GET_WAITING (set) > 0) {
1503 /* we are controllable and waiting, wake up the waiter. The socket will be
1504 * cleared by the _wait() thread and the poll will be restarted */
1510 * gst_poll_set_flushing:
1512 * @flushing: new flushing state.
1514 * When @flushing is %TRUE, this function ensures that current and future calls
1515 * to gst_poll_wait() will return -1, with errno set to EBUSY.
1517 * Unsetting the flushing state will restore normal operation of @set.
1522 gst_poll_set_flushing (GstPoll * set, gboolean flushing)
1524 g_return_if_fail (set != NULL);
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);