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"
61 #include <sys/types.h>
74 #define EINPROGRESS WSAEINPROGRESS
79 #include <sys/socket.h>
82 /* OS/X needs this because of bad headers */
85 /* The poll() emulation on OS/X doesn't handle fds=NULL, nfds=0,
86 * so we prefer our own poll emulation.
88 #if defined(BROKEN_POLL)
94 #define GST_CAT_DEFAULT GST_CAT_POLL
97 typedef struct _WinsockFd WinsockFd;
103 WSANETWORKEVENTS events;
104 glong ignored_event_mask;
111 GST_POLL_MODE_SELECT,
112 GST_POLL_MODE_PSELECT,
115 GST_POLL_MODE_WINDOWS
123 /* array of fds, always written to and read from with lock */
125 /* array of active fds, only written to from the waiting thread with the
126 * lock and read from with the lock or without the lock from the waiting
132 GstPollFD control_read_fd;
133 GstPollFD control_write_fd;
135 GArray *active_fds_ignored;
137 GArray *active_events;
142 gboolean controllable;
143 volatile gint waiting;
144 volatile gint control_pending;
145 volatile gint flushing;
147 volatile gint rebuild;
150 static gboolean gst_poll_fd_ctl_read_unlocked (GstPoll * set, GstPollFD * fd,
152 static gboolean gst_poll_add_fd_unlocked (GstPoll * set, GstPollFD * fd);
154 #define IS_FLUSHING(s) (g_atomic_int_get(&(s)->flushing))
155 #define SET_FLUSHING(s,val) (g_atomic_int_set(&(s)->flushing, (val)))
157 #define INC_WAITING(s) (g_atomic_int_exchange_and_add(&(s)->waiting, 1))
158 #define DEC_WAITING(s) (g_atomic_int_exchange_and_add(&(s)->waiting, -1))
159 #define GET_WAITING(s) (g_atomic_int_get(&(s)->waiting))
161 #define TEST_REBUILD(s) (g_atomic_int_compare_and_exchange(&(s)->rebuild, 1, 0))
162 #define MARK_REBUILD(s) (g_atomic_int_set(&(s)->rebuild, 1))
165 #define WAKE_EVENT(s) (write ((s)->control_write_fd.fd, "W", 1) == 1)
166 #define RELEASE_EVENT(s) (read ((s)->control_read_fd.fd, (s)->buf, 1) == 1)
168 #define WAKE_EVENT(s) (SetEvent ((s)->wakeup_event))
169 #define RELEASE_EVENT(s) (ResetEvent ((s)->wakeup_event))
172 /* the poll/select call is also performed on a control socket, that way
173 * we can send special commands to control it */
174 static inline gboolean
175 raise_wakeup (GstPoll * set)
177 gboolean result = TRUE;
179 if (g_atomic_int_exchange_and_add (&set->control_pending, 1) == 0) {
180 /* raise when nothing pending */
181 result = WAKE_EVENT (set);
186 /* note how bad things can happen when the 2 threads both raise and release the
187 * wakeup. This is however not a problem because you must always pair a raise
189 static inline gboolean
190 release_wakeup (GstPoll * set)
192 gboolean result = TRUE;
194 if (g_atomic_int_dec_and_test (&set->control_pending)) {
195 result = RELEASE_EVENT (set);
201 release_all_wakeup (GstPoll * set)
206 if (!(old = g_atomic_int_get (&set->control_pending)))
207 /* nothing pending, just exit */
210 /* try to remove all pending control messages */
211 if (g_atomic_int_compare_and_exchange (&set->control_pending, old, 0)) {
212 /* we managed to remove all messages, read the control socket */
213 if (RELEASE_EVENT (set))
216 /* retry again until we read it successfully */
217 g_atomic_int_exchange_and_add (&set->control_pending, 1);
224 find_index (GArray * array, GstPollFD * fd)
233 /* start by assuming the index found in the fd is still valid */
234 if (fd->idx >= 0 && fd->idx < array->len) {
236 ifd = &g_array_index (array, struct pollfd, fd->idx);
238 ifd = &g_array_index (array, WinsockFd, fd->idx);
241 if (ifd->fd == fd->fd) {
246 /* the pollfd array has changed and we need to lookup the fd again */
247 for (i = 0; i < array->len; i++) {
249 ifd = &g_array_index (array, struct pollfd, i);
251 ifd = &g_array_index (array, WinsockFd, i);
254 if (ifd->fd == fd->fd) {
264 #if !defined(HAVE_PPOLL) && defined(HAVE_POLL)
265 /* check if all file descriptors will fit in an fd_set */
267 selectable_fds (const GstPoll * set)
271 g_mutex_lock (set->lock);
272 for (i = 0; i < set->fds->len; i++) {
273 struct pollfd *pfd = &g_array_index (set->fds, struct pollfd, i);
275 if (pfd->fd >= FD_SETSIZE)
278 g_mutex_unlock (set->lock);
284 g_mutex_unlock (set->lock);
289 /* check if the timeout will convert to a timeout value used for poll()
290 * without a loss of precision
293 pollable_timeout (GstClockTime timeout)
295 if (timeout == GST_CLOCK_TIME_NONE)
298 /* not a nice multiple of milliseconds */
299 if (timeout % 1000000)
307 choose_mode (const GstPoll * set, GstClockTime timeout)
311 if (set->mode == GST_POLL_MODE_AUTO) {
313 mode = GST_POLL_MODE_PPOLL;
314 #elif defined(HAVE_POLL)
315 if (!selectable_fds (set) || pollable_timeout (timeout)) {
316 mode = GST_POLL_MODE_POLL;
319 mode = GST_POLL_MODE_PSELECT;
321 mode = GST_POLL_MODE_SELECT;
324 #elif defined(HAVE_PSELECT)
325 mode = GST_POLL_MODE_PSELECT;
327 mode = GST_POLL_MODE_SELECT;
337 pollfd_to_fd_set (GstPoll * set, fd_set * readfds, fd_set * writefds,
347 g_mutex_lock (set->lock);
349 for (i = 0; i < set->active_fds->len; i++) {
350 struct pollfd *pfd = &g_array_index (set->fds, struct pollfd, i);
352 if (pfd->fd < FD_SETSIZE) {
353 if (pfd->events & POLLIN)
354 FD_SET (pfd->fd, readfds);
355 if (pfd->events & POLLOUT)
356 FD_SET (pfd->fd, writefds);
358 FD_SET (pfd->fd, errorfds);
359 if (pfd->fd > max_fd && (pfd->events & (POLLIN | POLLOUT)))
364 g_mutex_unlock (set->lock);
370 fd_set_to_pollfd (GstPoll * set, fd_set * readfds, fd_set * writefds,
375 g_mutex_lock (set->lock);
377 for (i = 0; i < set->active_fds->len; i++) {
378 struct pollfd *pfd = &g_array_index (set->active_fds, struct pollfd, i);
380 if (pfd->fd < FD_SETSIZE) {
382 if (FD_ISSET (pfd->fd, readfds))
383 pfd->revents |= POLLIN;
384 if (FD_ISSET (pfd->fd, writefds))
385 pfd->revents |= POLLOUT;
386 if (FD_ISSET (pfd->fd, errorfds))
387 pfd->revents |= POLLERR;
391 g_mutex_unlock (set->lock);
393 #else /* G_OS_WIN32 */
395 * Translate errors thrown by the Winsock API used by GstPoll:
396 * WSAEventSelect, WSAWaitForMultipleEvents and WSAEnumNetworkEvents
399 gst_poll_winsock_error_to_errno (DWORD last_error)
401 switch (last_error) {
402 case WSA_INVALID_HANDLE:
407 case WSA_NOT_ENOUGH_MEMORY:
411 * Anything else, including:
412 * WSA_INVALID_PARAMETER, WSAEFAULT, WSAEINPROGRESS, WSAENETDOWN,
421 gst_poll_free_winsock_event (GstPoll * set, gint idx)
423 WinsockFd *wfd = &g_array_index (set->fds, WinsockFd, idx);
424 HANDLE event = g_array_index (set->events, HANDLE, idx);
426 WSAEventSelect (wfd->fd, event, 0);
431 gst_poll_update_winsock_event_mask (GstPoll * set, gint idx, glong flags,
436 wfd = &g_array_index (set->fds, WinsockFd, idx);
439 wfd->event_mask |= flags;
441 wfd->event_mask &= ~flags;
443 /* reset ignored state if the new mask doesn't overlap at all */
444 if ((wfd->ignored_event_mask & wfd->event_mask) == 0)
445 wfd->ignored_event_mask = 0;
449 gst_poll_prepare_winsock_active_sets (GstPoll * set)
453 g_array_set_size (set->active_fds, 0);
454 g_array_set_size (set->active_fds_ignored, 0);
455 g_array_set_size (set->active_events, 0);
456 g_array_append_val (set->active_events, set->wakeup_event);
458 for (i = 0; i < set->fds->len; i++) {
459 WinsockFd *wfd = &g_array_index (set->fds, WinsockFd, i);
460 HANDLE event = g_array_index (set->events, HANDLE, i);
462 if (wfd->ignored_event_mask == 0) {
465 g_array_append_val (set->active_fds, *wfd);
466 g_array_append_val (set->active_events, event);
468 ret = WSAEventSelect (wfd->fd, event, wfd->event_mask);
469 if (G_UNLIKELY (ret != 0)) {
470 errno = gst_poll_winsock_error_to_errno (WSAGetLastError ());
474 g_array_append_val (set->active_fds_ignored, wfd);
482 gst_poll_collect_winsock_events (GstPoll * set)
487 * We need to check which events are signaled, and call
488 * WSAEnumNetworkEvents for those that are, which resets
489 * the event and clears the internal network event records.
492 for (i = 0; i < set->active_fds->len; i++) {
493 WinsockFd *wfd = &g_array_index (set->active_fds, WinsockFd, i);
494 HANDLE event = g_array_index (set->active_events, HANDLE, i + 1);
497 wait_ret = WaitForSingleObject (event, 0);
498 if (wait_ret == WAIT_OBJECT_0) {
499 gint enum_ret = WSAEnumNetworkEvents (wfd->fd, event, &wfd->events);
501 if (G_UNLIKELY (enum_ret != 0)) {
503 errno = gst_poll_winsock_error_to_errno (WSAGetLastError ());
509 /* clear any previously stored result */
510 memset (&wfd->events, 0, sizeof (wfd->events));
514 /* If all went well we also need to reset the ignored fds. */
516 res += set->active_fds_ignored->len;
518 for (i = 0; i < set->active_fds_ignored->len; i++) {
519 WinsockFd *wfd = g_array_index (set->active_fds_ignored, WinsockFd *, i);
521 wfd->ignored_event_mask = 0;
524 g_array_set_size (set->active_fds_ignored, 0);
533 * @controllable: whether it should be possible to control a wait.
535 * Create a new file descriptor set. If @controllable, it
536 * is possible to restart or flush a call to gst_poll_wait() with
537 * gst_poll_restart() and gst_poll_set_flushing() respectively.
539 * Free-function: gst_poll_free
541 * Returns: (transfer full): a new #GstPoll, or %NULL in case of an error.
542 * Free with gst_poll_free().
547 gst_poll_new (gboolean controllable)
551 GST_DEBUG ("controllable : %d", controllable);
553 nset = g_slice_new0 (GstPoll);
554 nset->lock = g_mutex_new ();
556 nset->mode = GST_POLL_MODE_AUTO;
557 nset->fds = g_array_new (FALSE, FALSE, sizeof (struct pollfd));
558 nset->active_fds = g_array_new (FALSE, FALSE, sizeof (struct pollfd));
559 nset->control_read_fd.fd = -1;
560 nset->control_write_fd.fd = -1;
562 gint control_sock[2];
564 if (socketpair (PF_UNIX, SOCK_STREAM, 0, control_sock) < 0)
567 fcntl (control_sock[0], F_SETFL, O_NONBLOCK);
568 fcntl (control_sock[1], F_SETFL, O_NONBLOCK);
570 nset->control_read_fd.fd = control_sock[0];
571 nset->control_write_fd.fd = control_sock[1];
573 gst_poll_add_fd_unlocked (nset, &nset->control_read_fd);
574 gst_poll_fd_ctl_read_unlocked (nset, &nset->control_read_fd, TRUE);
577 nset->mode = GST_POLL_MODE_WINDOWS;
578 nset->fds = g_array_new (FALSE, FALSE, sizeof (WinsockFd));
579 nset->active_fds = g_array_new (FALSE, FALSE, sizeof (WinsockFd));
580 nset->active_fds_ignored = g_array_new (FALSE, FALSE, sizeof (WinsockFd *));
581 nset->events = g_array_new (FALSE, FALSE, sizeof (HANDLE));
582 nset->active_events = g_array_new (FALSE, FALSE, sizeof (HANDLE));
584 nset->wakeup_event = CreateEvent (NULL, TRUE, FALSE, NULL);
587 /* ensure (re)build, though already sneakily set in non-windows case */
590 nset->controllable = controllable;
598 GST_WARNING ("%p: can't create socket pair !", nset);
599 gst_poll_free (nset);
606 * gst_poll_new_timer:
608 * Create a new poll object that can be used for scheduling cancellable
611 * A timeout is performed with gst_poll_wait(). Multiple timeouts can be
612 * performed from different threads.
614 * Free-function: gst_poll_free
616 * Returns: (transfer full): a new #GstPoll, or %NULL in case of an error.
617 * Free with gst_poll_free().
622 gst_poll_new_timer (void)
626 /* make a new controllable poll set */
627 if (!(poll = gst_poll_new (TRUE)))
639 * @set: (transfer full): a file descriptor set.
641 * Free a file descriptor set.
646 gst_poll_free (GstPoll * set)
648 g_return_if_fail (set != NULL);
650 GST_DEBUG ("%p: freeing", set);
653 if (set->control_write_fd.fd >= 0)
654 close (set->control_write_fd.fd);
655 if (set->control_read_fd.fd >= 0)
656 close (set->control_read_fd.fd);
658 CloseHandle (set->wakeup_event);
663 for (i = 0; i < set->events->len; i++)
664 gst_poll_free_winsock_event (set, i);
667 g_array_free (set->active_events, TRUE);
668 g_array_free (set->events, TRUE);
669 g_array_free (set->active_fds_ignored, TRUE);
672 g_array_free (set->active_fds, TRUE);
673 g_array_free (set->fds, TRUE);
674 g_mutex_free (set->lock);
675 g_slice_free (GstPoll, set);
679 * gst_poll_get_read_gpollfd:
683 * Get a GPollFD for the reading part of the control socket. This is useful when
684 * integrating with a GSource and GMainLoop.
689 gst_poll_get_read_gpollfd (GstPoll * set, GPollFD * fd)
691 g_return_if_fail (set != NULL);
692 g_return_if_fail (fd != NULL);
695 fd->fd = set->control_read_fd.fd;
697 #if GLIB_SIZEOF_VOID_P == 8
698 fd->fd = (gint64) set->wakeup_event;
700 fd->fd = (gint) set->wakeup_event;
703 fd->events = G_IO_IN | G_IO_HUP | G_IO_ERR;
711 * Initializes @fd. Alternatively you can initialize it with
717 gst_poll_fd_init (GstPollFD * fd)
719 g_return_if_fail (fd != NULL);
726 gst_poll_add_fd_unlocked (GstPoll * set, GstPollFD * fd)
730 GST_DEBUG ("%p: fd (fd:%d, idx:%d)", set, fd->fd, fd->idx);
732 idx = find_index (set->fds, fd);
738 nfd.events = POLLERR | POLLNVAL | POLLHUP;
741 g_array_append_val (set->fds, nfd);
743 fd->idx = set->fds->len - 1;
749 wfd.event_mask = FD_CLOSE;
750 memset (&wfd.events, 0, sizeof (wfd.events));
751 wfd.ignored_event_mask = 0;
752 event = WSACreateEvent ();
754 g_array_append_val (set->fds, wfd);
755 g_array_append_val (set->events, event);
757 fd->idx = set->fds->len - 1;
761 GST_WARNING ("%p: couldn't find fd !", set);
769 * @set: a file descriptor set.
770 * @fd: a file descriptor.
772 * Add a file descriptor to the file descriptor set.
774 * Returns: %TRUE if the file descriptor was successfully added to the set.
779 gst_poll_add_fd (GstPoll * set, GstPollFD * fd)
783 g_return_val_if_fail (set != NULL, FALSE);
784 g_return_val_if_fail (fd != NULL, FALSE);
785 g_return_val_if_fail (fd->fd >= 0, FALSE);
787 g_mutex_lock (set->lock);
789 ret = gst_poll_add_fd_unlocked (set, fd);
791 g_mutex_unlock (set->lock);
797 * gst_poll_remove_fd:
798 * @set: a file descriptor set.
799 * @fd: a file descriptor.
801 * Remove a file descriptor from the file descriptor set.
803 * Returns: %TRUE if the file descriptor was successfully removed from the set.
808 gst_poll_remove_fd (GstPoll * set, GstPollFD * fd)
812 g_return_val_if_fail (set != NULL, FALSE);
813 g_return_val_if_fail (fd != NULL, FALSE);
814 g_return_val_if_fail (fd->fd >= 0, FALSE);
817 GST_DEBUG ("%p: fd (fd:%d, idx:%d)", set, fd->fd, fd->idx);
819 g_mutex_lock (set->lock);
821 /* get the index, -1 is an fd that is not added */
822 idx = find_index (set->fds, fd);
825 gst_poll_free_winsock_event (set, idx);
826 g_array_remove_index_fast (set->events, idx);
829 /* remove the fd at index, we use _remove_index_fast, which copies the last
830 * element of the array to the freed index */
831 g_array_remove_index_fast (set->fds, idx);
833 /* mark fd as removed by setting the index to -1 */
837 GST_WARNING ("%p: couldn't find fd !", set);
840 g_mutex_unlock (set->lock);
846 * gst_poll_fd_ctl_write:
847 * @set: a file descriptor set.
848 * @fd: a file descriptor.
849 * @active: a new status.
851 * Control whether the descriptor @fd in @set will be monitored for
854 * Returns: %TRUE if the descriptor was successfully updated.
859 gst_poll_fd_ctl_write (GstPoll * set, GstPollFD * fd, gboolean active)
863 g_return_val_if_fail (set != NULL, FALSE);
864 g_return_val_if_fail (fd != NULL, FALSE);
865 g_return_val_if_fail (fd->fd >= 0, FALSE);
867 GST_DEBUG ("%p: fd (fd:%d, idx:%d), active : %d", set,
868 fd->fd, fd->idx, active);
870 g_mutex_lock (set->lock);
872 idx = find_index (set->fds, fd);
875 struct pollfd *pfd = &g_array_index (set->fds, struct pollfd, idx);
878 pfd->events |= POLLOUT;
880 pfd->events &= ~POLLOUT;
882 GST_LOG ("pfd->events now %d (POLLOUT:%d)", pfd->events, POLLOUT);
884 gst_poll_update_winsock_event_mask (set, idx, FD_WRITE | FD_CONNECT,
889 GST_WARNING ("%p: couldn't find fd !", set);
892 g_mutex_unlock (set->lock);
898 gst_poll_fd_ctl_read_unlocked (GstPoll * set, GstPollFD * fd, gboolean active)
902 GST_DEBUG ("%p: fd (fd:%d, idx:%d), active : %d", set,
903 fd->fd, fd->idx, active);
905 idx = find_index (set->fds, fd);
909 struct pollfd *pfd = &g_array_index (set->fds, struct pollfd, idx);
912 pfd->events |= (POLLIN | POLLPRI);
914 pfd->events &= ~(POLLIN | POLLPRI);
916 gst_poll_update_winsock_event_mask (set, idx, FD_READ | FD_ACCEPT, active);
920 GST_WARNING ("%p: couldn't find fd !", set);
927 * gst_poll_fd_ctl_read:
928 * @set: a file descriptor set.
929 * @fd: a file descriptor.
930 * @active: a new status.
932 * Control whether the descriptor @fd in @set will be monitored for
935 * Returns: %TRUE if the descriptor was successfully updated.
940 gst_poll_fd_ctl_read (GstPoll * set, GstPollFD * fd, gboolean active)
944 g_return_val_if_fail (set != NULL, FALSE);
945 g_return_val_if_fail (fd != NULL, FALSE);
946 g_return_val_if_fail (fd->fd >= 0, FALSE);
948 g_mutex_lock (set->lock);
950 ret = gst_poll_fd_ctl_read_unlocked (set, fd, active);
952 g_mutex_unlock (set->lock);
958 * gst_poll_fd_ignored:
959 * @set: a file descriptor set.
960 * @fd: a file descriptor.
962 * Mark @fd as ignored so that the next call to gst_poll_wait() will yield
963 * the same result for @fd as last time. This function must be called if no
964 * operation (read/write/recv/send/etc.) will be performed on @fd before
965 * the next call to gst_poll_wait().
967 * The reason why this is needed is because the underlying implementation
968 * might not allow querying the fd more than once between calls to one of
969 * the re-enabling operations.
974 gst_poll_fd_ignored (GstPoll * set, GstPollFD * fd)
979 g_return_if_fail (set != NULL);
980 g_return_if_fail (fd != NULL);
981 g_return_if_fail (fd->fd >= 0);
983 g_mutex_lock (set->lock);
985 idx = find_index (set->fds, fd);
987 WinsockFd *wfd = &g_array_index (set->fds, WinsockFd, idx);
989 wfd->ignored_event_mask = wfd->event_mask & (FD_READ | FD_WRITE);
993 g_mutex_unlock (set->lock);
998 * gst_poll_fd_has_closed:
999 * @set: a file descriptor set.
1000 * @fd: a file descriptor.
1002 * Check if @fd in @set has closed the connection.
1004 * Returns: %TRUE if the connection was closed.
1009 gst_poll_fd_has_closed (const GstPoll * set, GstPollFD * fd)
1011 gboolean res = FALSE;
1014 g_return_val_if_fail (set != NULL, FALSE);
1015 g_return_val_if_fail (fd != NULL, FALSE);
1016 g_return_val_if_fail (fd->fd >= 0, FALSE);
1018 GST_DEBUG ("%p: fd (fd:%d, idx:%d)", set, fd->fd, fd->idx);
1020 g_mutex_lock (set->lock);
1022 idx = find_index (set->active_fds, fd);
1025 struct pollfd *pfd = &g_array_index (set->active_fds, struct pollfd, idx);
1027 res = (pfd->revents & POLLHUP) != 0;
1029 WinsockFd *wfd = &g_array_index (set->active_fds, WinsockFd, idx);
1031 res = (wfd->events.lNetworkEvents & FD_CLOSE) != 0;
1034 GST_WARNING ("%p: couldn't find fd !", set);
1037 g_mutex_unlock (set->lock);
1043 * gst_poll_fd_has_error:
1044 * @set: a file descriptor set.
1045 * @fd: a file descriptor.
1047 * Check if @fd in @set has an error.
1049 * Returns: %TRUE if the descriptor has an error.
1054 gst_poll_fd_has_error (const GstPoll * set, GstPollFD * fd)
1056 gboolean res = FALSE;
1059 g_return_val_if_fail (set != NULL, FALSE);
1060 g_return_val_if_fail (fd != NULL, FALSE);
1061 g_return_val_if_fail (fd->fd >= 0, FALSE);
1063 GST_DEBUG ("%p: fd (fd:%d, idx:%d)", set, fd->fd, fd->idx);
1065 g_mutex_lock (set->lock);
1067 idx = find_index (set->active_fds, fd);
1070 struct pollfd *pfd = &g_array_index (set->active_fds, struct pollfd, idx);
1072 res = (pfd->revents & (POLLERR | POLLNVAL)) != 0;
1074 WinsockFd *wfd = &g_array_index (set->active_fds, WinsockFd, idx);
1076 res = (wfd->events.iErrorCode[FD_CLOSE_BIT] != 0) ||
1077 (wfd->events.iErrorCode[FD_READ_BIT] != 0) ||
1078 (wfd->events.iErrorCode[FD_WRITE_BIT] != 0) ||
1079 (wfd->events.iErrorCode[FD_ACCEPT_BIT] != 0) ||
1080 (wfd->events.iErrorCode[FD_CONNECT_BIT] != 0);
1083 GST_WARNING ("%p: couldn't find fd !", set);
1086 g_mutex_unlock (set->lock);
1092 gst_poll_fd_can_read_unlocked (const GstPoll * set, GstPollFD * fd)
1094 gboolean res = FALSE;
1097 GST_DEBUG ("%p: fd (fd:%d, idx:%d)", set, fd->fd, fd->idx);
1099 idx = find_index (set->active_fds, fd);
1102 struct pollfd *pfd = &g_array_index (set->active_fds, struct pollfd, idx);
1104 res = (pfd->revents & (POLLIN | POLLPRI)) != 0;
1106 WinsockFd *wfd = &g_array_index (set->active_fds, WinsockFd, idx);
1108 res = (wfd->events.lNetworkEvents & (FD_READ | FD_ACCEPT)) != 0;
1111 GST_WARNING ("%p: couldn't find fd !", set);
1118 * gst_poll_fd_can_read:
1119 * @set: a file descriptor set.
1120 * @fd: a file descriptor.
1122 * Check if @fd in @set has data to be read.
1124 * Returns: %TRUE if the descriptor has data to be read.
1129 gst_poll_fd_can_read (const GstPoll * set, GstPollFD * fd)
1131 gboolean res = FALSE;
1133 g_return_val_if_fail (set != NULL, FALSE);
1134 g_return_val_if_fail (fd != NULL, FALSE);
1135 g_return_val_if_fail (fd->fd >= 0, FALSE);
1137 g_mutex_lock (set->lock);
1139 res = gst_poll_fd_can_read_unlocked (set, fd);
1141 g_mutex_unlock (set->lock);
1147 * gst_poll_fd_can_write:
1148 * @set: a file descriptor set.
1149 * @fd: a file descriptor.
1151 * Check if @fd in @set can be used for writing.
1153 * Returns: %TRUE if the descriptor can be used for writing.
1158 gst_poll_fd_can_write (const GstPoll * set, GstPollFD * fd)
1160 gboolean res = FALSE;
1163 g_return_val_if_fail (set != NULL, FALSE);
1164 g_return_val_if_fail (fd != NULL, FALSE);
1165 g_return_val_if_fail (fd->fd >= 0, FALSE);
1167 GST_DEBUG ("%p: fd (fd:%d, idx:%d)", set, fd->fd, fd->idx);
1169 g_mutex_lock (set->lock);
1171 idx = find_index (set->active_fds, fd);
1174 struct pollfd *pfd = &g_array_index (set->active_fds, struct pollfd, idx);
1176 res = (pfd->revents & POLLOUT) != 0;
1178 WinsockFd *wfd = &g_array_index (set->active_fds, WinsockFd, idx);
1180 res = (wfd->events.lNetworkEvents & FD_WRITE) != 0;
1183 GST_WARNING ("%p: couldn't find fd !", set);
1186 g_mutex_unlock (set->lock);
1194 * @timeout: a timeout in nanoseconds.
1196 * Wait for activity on the file descriptors in @set. This function waits up to
1197 * the specified @timeout. A timeout of #GST_CLOCK_TIME_NONE waits forever.
1199 * For #GstPoll objects created with gst_poll_new(), this function can only be
1200 * called from a single thread at a time. If called from multiple threads,
1201 * -1 will be returned with errno set to EPERM.
1203 * This is not true for timer #GstPoll objects created with
1204 * gst_poll_new_timer(), where it is allowed to have multiple threads waiting
1207 * Returns: The number of #GstPollFD in @set that have activity or 0 when no
1208 * activity was detected after @timeout. If an error occurs, -1 is returned
1214 gst_poll_wait (GstPoll * set, GstClockTime timeout)
1216 gboolean restarting;
1221 g_return_val_if_fail (set != NULL, -1);
1223 GST_DEBUG ("timeout :%" GST_TIME_FORMAT, GST_TIME_ARGS (timeout));
1225 is_timer = set->timer;
1227 /* add one more waiter */
1228 old_waiting = INC_WAITING (set);
1230 /* we cannot wait from multiple threads unless we are a timer */
1231 if (G_UNLIKELY (old_waiting > 0 && !is_timer))
1232 goto already_waiting;
1234 /* flushing, exit immediatly */
1235 if (G_UNLIKELY (IS_FLUSHING (set)))
1244 mode = choose_mode (set, timeout);
1246 if (TEST_REBUILD (set)) {
1247 g_mutex_lock (set->lock);
1249 g_array_set_size (set->active_fds, set->fds->len);
1250 memcpy (set->active_fds->data, set->fds->data,
1251 set->fds->len * sizeof (struct pollfd));
1253 if (!gst_poll_prepare_winsock_active_sets (set))
1256 g_mutex_unlock (set->lock);
1260 case GST_POLL_MODE_AUTO:
1261 g_assert_not_reached ();
1263 case GST_POLL_MODE_PPOLL:
1267 struct timespec *tsptr;
1269 if (timeout != GST_CLOCK_TIME_NONE) {
1270 GST_TIME_TO_TIMESPEC (timeout, ts);
1277 ppoll ((struct pollfd *) set->active_fds->data,
1278 set->active_fds->len, tsptr, NULL);
1280 g_assert_not_reached ();
1285 case GST_POLL_MODE_POLL:
1290 if (timeout != GST_CLOCK_TIME_NONE) {
1291 t = GST_TIME_AS_MSECONDS (timeout);
1297 poll ((struct pollfd *) set->active_fds->data,
1298 set->active_fds->len, t);
1300 g_assert_not_reached ();
1305 case GST_POLL_MODE_PSELECT:
1306 #ifndef HAVE_PSELECT
1308 g_assert_not_reached ();
1313 case GST_POLL_MODE_SELECT:
1321 max_fd = pollfd_to_fd_set (set, &readfds, &writefds, &errorfds);
1323 if (mode == GST_POLL_MODE_SELECT) {
1325 struct timeval *tvptr;
1327 if (timeout != GST_CLOCK_TIME_NONE) {
1328 GST_TIME_TO_TIMEVAL (timeout, tv);
1334 GST_DEBUG ("Calling select");
1335 res = select (max_fd + 1, &readfds, &writefds, &errorfds, tvptr);
1336 GST_DEBUG ("After select, res:%d", res);
1340 struct timespec *tsptr;
1342 if (timeout != GST_CLOCK_TIME_NONE) {
1343 GST_TIME_TO_TIMESPEC (timeout, ts);
1349 GST_DEBUG ("Calling pselect");
1351 pselect (max_fd + 1, &readfds, &writefds, &errorfds, tsptr, NULL);
1352 GST_DEBUG ("After pselect, res:%d", res);
1357 fd_set_to_pollfd (set, &readfds, &writefds, &errorfds);
1359 #else /* G_OS_WIN32 */
1360 g_assert_not_reached ();
1365 case GST_POLL_MODE_WINDOWS:
1368 gint ignore_count = set->active_fds_ignored->len;
1371 if (G_LIKELY (ignore_count == 0)) {
1372 if (timeout != GST_CLOCK_TIME_NONE)
1373 t = GST_TIME_AS_MSECONDS (timeout);
1377 /* already one or more ignored fds, so we quickly sweep the others */
1381 if (set->active_events->len != 0) {
1382 wait_ret = WSAWaitForMultipleEvents (set->active_events->len,
1383 (HANDLE *) set->active_events->data, FALSE, t, FALSE);
1385 wait_ret = WSA_WAIT_FAILED;
1386 WSASetLastError (WSA_INVALID_PARAMETER);
1389 if (ignore_count == 0 && wait_ret == WSA_WAIT_TIMEOUT) {
1391 } else if (wait_ret == WSA_WAIT_FAILED) {
1393 errno = gst_poll_winsock_error_to_errno (WSAGetLastError ());
1395 /* the first entry is the wakeup event */
1396 if (wait_ret - WSA_WAIT_EVENT_0 >= 1) {
1397 res = gst_poll_collect_winsock_events (set);
1399 res = 1; /* wakeup event */
1403 g_assert_not_reached ();
1411 /* Applications needs to clear the control socket themselves for timer
1413 * For other polls, we need to clear the control socket. If there was only
1414 * one socket with activity and it was the control socket, we need to
1416 if (release_all_wakeup (set) > 0 && res == 1)
1420 if (G_UNLIKELY (IS_FLUSHING (set))) {
1421 /* we got woken up and we are flushing, we need to stop */
1426 } while (G_UNLIKELY (restarting));
1448 g_mutex_unlock (set->lock);
1456 * gst_poll_set_controllable:
1458 * @controllable: new controllable state.
1460 * When @controllable is %TRUE, this function ensures that future calls to
1461 * gst_poll_wait() will be affected by gst_poll_restart() and
1462 * gst_poll_set_flushing().
1464 * Returns: %TRUE if the controllability of @set could be updated.
1469 gst_poll_set_controllable (GstPoll * set, gboolean controllable)
1471 g_return_val_if_fail (set != NULL, FALSE);
1473 GST_LOG ("%p: controllable : %d", set, controllable);
1475 set->controllable = controllable;
1484 * Restart any gst_poll_wait() that is in progress. This function is typically
1485 * used after adding or removing descriptors to @set.
1487 * If @set is not controllable, then this call will have no effect.
1492 gst_poll_restart (GstPoll * set)
1494 g_return_if_fail (set != NULL);
1496 if (set->controllable && GET_WAITING (set) > 0) {
1497 /* we are controllable and waiting, wake up the waiter. The socket will be
1498 * cleared by the _wait() thread and the poll will be restarted */
1504 * gst_poll_set_flushing:
1506 * @flushing: new flushing state.
1508 * When @flushing is %TRUE, this function ensures that current and future calls
1509 * to gst_poll_wait() will return -1, with errno set to EBUSY.
1511 * Unsetting the flushing state will restore normal operation of @set.
1516 gst_poll_set_flushing (GstPoll * set, gboolean flushing)
1518 g_return_if_fail (set != NULL);
1520 /* update the new state first */
1521 SET_FLUSHING (set, flushing);
1523 if (flushing && set->controllable && GET_WAITING (set) > 0) {
1524 /* we are flushing, controllable and waiting, wake up the waiter. When we
1525 * stop the flushing operation we don't clear the wakeup fd here, this will
1526 * happen in the _wait() thread. */
1532 * gst_poll_write_control:
1535 * Write a byte to the control socket of the controllable @set.
1536 * This function is mostly useful for timer #GstPoll objects created with
1537 * gst_poll_new_timer().
1539 * It will make any current and future gst_poll_wait() function return with
1540 * 1, meaning the control socket is set. After an equal amount of calls to
1541 * gst_poll_read_control() have been performed, calls to gst_poll_wait() will
1542 * block again until their timeout expired.
1544 * Returns: %TRUE on success. %FALSE when @set is not controllable or when the
1545 * byte could not be written.
1550 gst_poll_write_control (GstPoll * set)
1554 g_return_val_if_fail (set != NULL, FALSE);
1555 g_return_val_if_fail (set->timer, FALSE);
1557 res = raise_wakeup (set);
1563 * gst_poll_read_control:
1566 * Read a byte from the control socket of the controllable @set.
1567 * This function is mostly useful for timer #GstPoll objects created with
1568 * gst_poll_new_timer().
1570 * Returns: %TRUE on success. %FALSE when @set is not controllable or when there
1571 * was no byte to read.
1576 gst_poll_read_control (GstPoll * set)
1580 g_return_val_if_fail (set != NULL, FALSE);
1581 g_return_val_if_fail (set->timer, FALSE);
1583 res = release_wakeup (set);