[vlc-devel] [PATCH] win32/inhibit: simplify, avoid cancellation

Thomas Guillem thomas at gllm.fr
Mon Apr 13 13:47:21 CEST 2020


LGTM

On Sun, Apr 12, 2020, at 15:10, RĂ©mi Denis-Courmont wrote:
> ---
>  modules/video_output/win32/inhibit.c | 43 +++++++++++-----------------
>  1 file changed, 17 insertions(+), 26 deletions(-)
> 
> diff --git a/modules/video_output/win32/inhibit.c 
> b/modules/video_output/win32/inhibit.c
> index 8bd7604e30..3dac8113dd 100644
> --- a/modules/video_output/win32/inhibit.c
> +++ b/modules/video_output/win32/inhibit.c
> @@ -35,6 +35,7 @@ struct vlc_inhibit_sys
>      vlc_cond_t cond;
>      vlc_thread_t thread;
>      unsigned int mask;
> +    bool exit;
>  };
>  
>  static void Inhibit (vlc_inhibit_t *ih, unsigned mask)
> @@ -46,47 +47,36 @@ static void Inhibit (vlc_inhibit_t *ih, unsigned mask)
>      vlc_mutex_unlock(&sys->mutex);
>  }
>  
> -static void RestoreStateOnCancel( void* p_opaque )
> -{
> -    VLC_UNUSED(p_opaque);
> -    SetThreadExecutionState( ES_CONTINUOUS );
> -}
> -
>  static void* Run(void* obj)
>  {
>      vlc_inhibit_t *ih = (vlc_inhibit_t*)obj;
>      vlc_inhibit_sys_t *sys = ih->p_sys;
> -    EXECUTION_STATE prev_state = ES_CONTINUOUS;
>  
> -    for  (unsigned int mask = 0;;)
> +    vlc_mutex_lock(&sys->mutex);
> +    while (!sys->exit)
>      {
> -        vlc_mutex_lock(&sys->mutex);
> -        mutex_cleanup_push(&sys->mutex);
> -        vlc_cleanup_push(RestoreStateOnCancel, ih);
> -        while (mask == sys->mask)
> -            vlc_cond_wait(&sys->cond, &sys->mutex);
> -        mask = sys->mask;
> -        vlc_mutex_unlock(&sys->mutex);
> -        vlc_cleanup_pop();
> -        vlc_cleanup_pop();
> +        EXECUTION_STATE state = ES_CONTINUOUS;
>  
> -        bool suspend = (mask & VLC_INHIBIT_DISPLAY) != 0;
> -        if (suspend)
> +        if (sys->mask & VLC_INHIBIT_DISPLAY)
>              /* Prevent monitor from powering off */
> -            prev_state = SetThreadExecutionState( ES_DISPLAY_REQUIRED |
> -                                                  ES_SYSTEM_REQUIRED |
> -                                                  ES_CONTINUOUS );
> -        else
> -            SetThreadExecutionState( prev_state );
> +            state |= ES_DISPLAY_REQUIRED | ES_SYSTEM_REQUIRED;
> +
> +        SetThreadExecutionState(state);
> +        vlc_cond_wait(&sys->cond, &sys->mutex);
>      }
> -    vlc_assert_unreachable();
> +    vlc_mutex_unlock(&sys->mutex);
> +    SetThreadExecutionState(ES_CONTINUOUS);
> +    return NULL;
>  }
>  
>  static void CloseInhibit (vlc_object_t *obj)
>  {
>      vlc_inhibit_t *ih = (vlc_inhibit_t*)obj;
>      vlc_inhibit_sys_t* sys = ih->p_sys;
> -    vlc_cancel(sys->thread);
> +    vlc_mutex_lock(&sys->mutex);
> +    sys->exit = true;
> +    vlc_cond_signal(&sys->cond);
> +    vlc_mutex_unlock(&sys->mutex);
>      vlc_join(sys->thread, NULL);
>  }
>  
> @@ -101,6 +91,7 @@ static int OpenInhibit (vlc_object_t *obj)
>      vlc_mutex_init(&sys->mutex);
>      vlc_cond_init(&sys->cond);
>      sys->mask = 0;
> +    sys->exit = false;
>  
>      /* SetThreadExecutionState always needs to be called from the same 
> thread */
>      if (vlc_clone(&sys->thread, Run, ih, VLC_THREAD_PRIORITY_LOW))
> -- 
> 2.26.0
> 
> _______________________________________________
> vlc-devel mailing list
> To unsubscribe or modify your subscription options:
> https://mailman.videolan.org/listinfo/vlc-devel


More information about the vlc-devel mailing list