Fix indefinite wait when trying to stop pjmedia_clock (#3304)

This commit is contained in:
sauwming 2022-12-08 17:22:06 +08:00 committed by GitHub
parent eca0ae23de
commit 950081c737
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 4 deletions

View File

@ -272,11 +272,15 @@ PJ_DECL(pj_status_t) pjmedia_clock_start(pjmedia_clock *clock);
/**
* Stop the clock.
* Stop the clock. When the function returns PJ_SUCCESS, it is guaranteed
* that the clock thread (if any) has stopped and the callback has completed.
* But if the function is called from within the clock's callback itself,
* the callback will still be running until completion. In that case,
* the function will only stop future callbacks and returns PJ_EBUSY.
*
* @param clock The media clock.
*
* @return PJ_SUCCES on success.
* @return PJ_SUCCES on success, or PJ_EBUSY.
*/
PJ_DECL(pj_status_t) pjmedia_clock_stop(pjmedia_clock *clock);

View File

@ -218,7 +218,15 @@ PJ_DEF(pj_status_t) pjmedia_clock_start(pjmedia_clock *clock)
clock->running = PJ_TRUE;
clock->quitting = PJ_FALSE;
if ((clock->options & PJMEDIA_CLOCK_NO_ASYNC) == 0 && !clock->thread) {
if ((clock->options & PJMEDIA_CLOCK_NO_ASYNC) == 0) {
if (clock->thread) {
/* This is probably the leftover thread that failed to
* be cleaned up during the last clock stoppage.
*/
pj_thread_destroy(clock->thread);
clock->thread = NULL;
}
status = pj_thread_create(clock->pool, "clock", &clock_thread, clock,
0, 0, &clock->thread);
if (status != PJ_SUCCESS) {
@ -247,7 +255,13 @@ PJ_DEF(pj_status_t) pjmedia_clock_stop(pjmedia_clock *clock)
clock->thread = NULL;
pj_pool_reset(clock->pool);
} else {
clock->quitting = PJ_FALSE;
/* We are probably called from the clock thread itself.
* Do not cancel the thread's quitting though, since it
* may cause the clock thread to run indefinitely.
*/
// clock->quitting = PJ_FALSE;
return PJ_EBUSY;
}
}