[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
- Re: [PATCH] libports: fix the thread counts in case the thread creation fails, (continued)
- Re: [PATCH] libports: fix the thread counts in case the thread creation fails, Samuel Thibault, 2013/11/10
- Re: [PATCH] libports: fix the thread counts in case the thread creation fails, Neal H. Walfield, 2013/11/10
- lockless thread management in libports (was: [PATCH] libports: fix the thread counts in case the thread creation fails), Justus Winter, 2013/11/11
- [PATCH] libports: implement lockless management of threads, Justus Winter, 2013/11/11
- Re: [PATCH] libports: implement lockless management of threads, Neal H. Walfield, 2013/11/11
- Re: [PATCH] libports: implement lockless management of threads, Justus Winter, 2013/11/12
- Re: [PATCH] libports: implement lockless management of threads, Samuel Thibault, 2013/11/12
- Re: [PATCH] libports: implement lockless management of threads, Emilio Pozuelo Monfort, 2013/11/13
- Re: [PATCH] libports: implement lockless management of threads, Justus Winter, 2013/11/15
- [PATCH] libports: implement lockless management of threads, Justus Winter, 2013/11/15
- Re: [PATCH] libports: implement lockless management of threads,
Samuel Thibault <=
- Re: [PATCH] libports: implement lockless management of threads, Justus Winter, 2013/11/16
- Re: [PATCH] libports: implement lockless management of threads, Richard Braun, 2013/11/16