gstelement: protect async state changes against spurious wake ups

When a pipeline is pre-rolling, it waits for all sink elements to report
they have received a buffer before completing the transition to paused.
This async wait is done using a state condition variable. The way this
waits are currently implemented do not protect against spurious conditional
wake ups, which may happen due to external factors in the kernel.

This change implements the wait within a loop that iterates over the protected
variable to reinitiates the wait if the wakeup was spurious. More details in
the [GCond docs](https://docs.gtk.org/glib/struct.Cond.html).

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/4086>
This commit is contained in:
Michael Gruner 2023-02-28 11:48:27 -06:00 committed by GStreamer Marge Bot
parent 6ce4a12f0b
commit 53c145a158

View File

@ -2500,72 +2500,80 @@ gst_element_get_state_func (GstElement * element,
{ {
GstStateChangeReturn ret = GST_STATE_CHANGE_FAILURE; GstStateChangeReturn ret = GST_STATE_CHANGE_FAILURE;
GstState old_pending; GstState old_pending;
gint64 end_time;
GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "getting state, timeout %" GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "getting state, timeout %"
GST_TIME_FORMAT, GST_TIME_ARGS (timeout)); GST_TIME_FORMAT, GST_TIME_ARGS (timeout));
GST_OBJECT_LOCK (element); GST_OBJECT_LOCK (element);
ret = GST_STATE_RETURN (element);
GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "RETURN is %s",
gst_element_state_change_return_get_name (ret));
/* we got an error, report immediately */ if (timeout != GST_CLOCK_TIME_NONE) {
if (ret == GST_STATE_CHANGE_FAILURE) /* make timeout absolute */
goto done; end_time = g_get_monotonic_time () + (timeout / 1000);
}
/* we got no_preroll, report immediately */ do {
if (ret == GST_STATE_CHANGE_NO_PREROLL) ret = GST_STATE_RETURN (element);
goto done; GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "RETURN is %s",
gst_element_state_change_return_get_name (ret));
/* no need to wait async if we are not async */ /* we got an error, report immediately */
if (ret != GST_STATE_CHANGE_ASYNC) if (ret == GST_STATE_CHANGE_FAILURE)
goto done; goto done;
old_pending = GST_STATE_PENDING (element); /* we got no_preroll, report immediately */
if (old_pending != GST_STATE_VOID_PENDING) { if (ret == GST_STATE_CHANGE_NO_PREROLL)
gboolean signaled; goto done;
guint32 cookie;
/* get cookie to detect state changes during waiting */ /* no need to wait async if we are not async */
cookie = element->state_cookie; if (ret != GST_STATE_CHANGE_ASYNC)
goto done;
GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, old_pending = GST_STATE_PENDING (element);
"waiting for element to commit state"); if (old_pending != GST_STATE_VOID_PENDING) {
gboolean signaled = TRUE;
guint32 cookie;
/* we have a pending state change, wait for it to complete */ /* get cookie to detect state changes during waiting */
if (timeout != GST_CLOCK_TIME_NONE) { cookie = element->state_cookie;
gint64 end_time;
/* make timeout absolute */
end_time = g_get_monotonic_time () + (timeout / 1000);
signaled = GST_STATE_WAIT_UNTIL (element, end_time);
} else {
GST_STATE_WAIT (element);
signaled = TRUE;
}
if (!signaled) { GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "timed out"); "waiting for element to commit state");
/* timeout triggered */
ret = GST_STATE_CHANGE_ASYNC;
} else {
if (cookie != element->state_cookie)
goto interrupted;
/* could be success or failure */ /* we have a pending state change, wait for it to complete or for
if (old_pending == GST_STATE (element)) { an interruption */
GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "got success"); if (timeout != GST_CLOCK_TIME_NONE) {
ret = GST_STATE_CHANGE_SUCCESS; signaled = GST_STATE_WAIT_UNTIL (element, end_time);
} else { } else {
GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "got failure"); GST_STATE_WAIT (element);
ret = GST_STATE_CHANGE_FAILURE; signaled = TRUE;
}
if (!signaled) {
GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "timed out");
/* timeout triggered */
ret = GST_STATE_CHANGE_ASYNC;
goto done;
} else {
if (cookie != element->state_cookie)
goto interrupted;
/* could be success or failure */
if (old_pending == GST_STATE (element)) {
GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "got success");
ret = GST_STATE_CHANGE_SUCCESS;
} else {
GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "got failure");
ret = GST_STATE_CHANGE_FAILURE;
}
}
/* if nothing is pending anymore we can return SUCCESS */
if (GST_STATE_PENDING (element) == GST_STATE_VOID_PENDING) {
GST_CAT_LOG_OBJECT (GST_CAT_STATES, element, "nothing pending");
ret = GST_STATE_CHANGE_SUCCESS;
} }
} }
/* if nothing is pending anymore we can return SUCCESS */ } while (old_pending != GST_STATE (element));
if (GST_STATE_PENDING (element) == GST_STATE_VOID_PENDING) {
GST_CAT_LOG_OBJECT (GST_CAT_STATES, element, "nothing pending");
ret = GST_STATE_CHANGE_SUCCESS;
}
}
done: done:
if (state) if (state)