Re: [PATCH] futex.2: Make the example use C11 atomics rather than GCC builtins.

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

 



On 11/14/18 7:53 AM, Benjamin Peterson wrote:

Thanks, Benjamin. Patch applied.

Cheers,

Michael

> ---
>  man2/futex.2 | 22 ++++++++++------------
>  1 file changed, 10 insertions(+), 12 deletions(-)
> 
> diff --git a/man2/futex.2 b/man2/futex.2
> index 0d5f69fe8..adb8dab89 100644
> --- a/man2/futex.2
> +++ b/man2/futex.2
> @@ -1756,6 +1756,7 @@ Child  (18535) 4
>  #define _GNU_SOURCE
>  #include <stdio.h>
>  #include <errno.h>
> +#include <stdatomic.h>
>  #include <stdlib.h>
>  #include <unistd.h>
>  #include <sys/wait.h>
> @@ -1785,22 +1786,19 @@ fwait(int *futexp)
>  {
>      int s;
>  
> -    /* __sync_bool_compare_and_swap(ptr, oldval, newval) is a gcc
> -       built\-in function.  It atomically performs the equivalent of:
> +    /* atomic_compare_exchange_strong(ptr, oldval, newval)
> +       atomically performs the equivalent of:
>  
> -           if (*ptr == oldval)
> +           if (*ptr == *oldval)
>                 *ptr = newval;
>  
> -       It returns true if the test yielded true and *ptr was updated.
> -       The alternative here would be to employ the equivalent atomic
> -       machine\-language instructions.  For further information, see
> -       the GCC Manual. */
> +       It returns true if the test yielded true and *ptr was updated. */
>  
>      while (1) {
>  
>          /* Is the futex available? */
> -
> -        if (__sync_bool_compare_and_swap(futexp, 1, 0))
> +        const int zero = 0;
> +        if (atomic_compare_exchange_strong(futexp, &zero, 1))
>              break;      /* Yes */
>  
>          /* Futex is not available; wait */
> @@ -1820,10 +1818,10 @@ fpost(int *futexp)
>  {
>      int s;
>  
> -    /* __sync_bool_compare_and_swap() was described in comments above */
> -
> -    if (__sync_bool_compare_and_swap(futexp, 0, 1)) {
> +    /* atomic_compare_exchange_strong() was described in comments above */
>  
> +    const int one = 1;
> +    if (atomic_compare_exchange_strong(futexp, &one, 0)) {
>          s = futex(futexp, FUTEX_WAKE, 1, NULL, NULL, 0);
>          if (s  == \-1)
>              errExit("futex\-FUTEX_WAKE");
> 


-- 
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/



[Index of Archives]     [Kernel Documentation]     [Netdev]     [Linux Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux