bug-hurd
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH] libports: implement lockless management of threads


From: Samuel Thibault
Subject: Re: [PATCH] libports: implement lockless management of threads
Date: Sat, 16 Nov 2013 11:16:02 +0100
User-agent: Mutt/1.5.21+34 (58baf7c9f32f) (2010-12-30)

Justus Winter, le Fri 15 Nov 2013 20:52:23 +0100, a écrit :
> @@ -224,30 +216,22 @@ ports_manage_port_operations_multithread (struct 
> port_bucket *bucket,
>  
>        if (master)
>       {
> -       pthread_spin_lock (&lock);
> -       if (totalthreads != 1)
> -         {
> -           pthread_spin_unlock (&lock);
> -           goto startover;
> -         }
> +       if (__atomic_load_n (&totalthreads, __ATOMIC_RELAXED) != 1)
> +         goto startover;
>       }
>        else
>       {
> -       pthread_spin_lock (&lock);
> -       if (nreqthreads == 1)
> +       __atomic_sub_fetch (&totalthreads, 1, __ATOMIC_RELAXED);
> +       if (__atomic_sub_fetch (&nreqthreads, 1, __ATOMIC_RELAXED) == 0)
>           {
>             /* No other thread is listening for requests, continue. */
> -           pthread_spin_unlock (&lock);
> +           __atomic_add_fetch (&totalthreads, 1, __ATOMIC_RELAXED);
> +           __atomic_add_fetch (&nreqthreads, 1, __ATOMIC_RELAXED);
>             goto startover;
>           }
> -       nreqthreads--;
> -       totalthreads--;
> -       pthread_spin_unlock (&lock);
>       }

Here the totalthreads update should be done after decrementing and
testing nreqthreads (and thus no reincrement if that gives 0). Otherwise
master might see totalthreads being 1 although there is actually another
thread which will happen to realize it'll actually continue.  Put
another way, a thread mustn't decrement totalthreads unless it is
absolutely sure it will terminate.

With that fixed, Ack.

Samuel



reply via email to

[Prev in Thread] Current Thread [Next in Thread]