[MPlayer-dev-eng] [PATCH] threaded cache, round 2

Martin Simmons vyslnqaaxytp at dyxyl.com
Mon Jan 2 23:06:35 CET 2012


>>>>> On Mon, 02 Jan 2012 17:59:19 +0100, Dan Oscarsson said:
> 
> +#ifdef PTHREAD_CACHE
> +    if (!((cache_vars_t *)s->cache_data)->do_work) {
> +        pthread_mutex_lock(&((cache_vars_t *)s->cache_data)->go_ahead_mutex);
> +           ((cache_vars_t *)s->cache_data)->do_work = 1;
> +        pthread_mutex_unlock(&((cache_vars_t *)s->cache_data)->go_ahead_mutex);
> +        pthread_cond_signal(&((cache_vars_t *)s->cache_data)->go_ahead);

The last two lines are in the wrong order -- to avoid race conditions, you
must signal while holding the lock.


> +#ifdef PTHREAD_CACHE
> +    if (!s->do_work) {
> +        pthread_mutex_lock(&s->go_ahead_mutex);
> +           s->do_work = 1;
> +        pthread_mutex_unlock(&s->go_ahead_mutex);
> +        pthread_cond_signal(&s->go_ahead);

Ditto.


> +#ifdef PTHREAD_CACHE
> +            if (!s->do_work) {
> +                pthread_mutex_lock(&s->go_ahead_mutex);
> +                    while (!s->do_work) pthread_cond_wait(&s->go_ahead, &s->go_ahead_mutex);
> +                pthread_mutex_unlock(&s->go_ahead_mutex);
> +            }
> +            s->do_work = 0;

Set s->do_work to 0 while holding the lock inside the if, also to avoid race
conditions.

__Martin


More information about the MPlayer-dev-eng mailing list