Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7680101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:02:04+00:00 2026-05-31T18:02:04+00:00

to use cas, gcc provides some useful functions such as __sync_bool_compare_and_swap but we can

  • 0

to use cas, gcc provides some useful functions such as
__sync_bool_compare_and_swap

but we can also use asm code like cmpxchg

bool  ret;
__asm__ __volatile__(
    "lock cmpxchg16b %1;\n"
    "sete %0;\n"
    :"=m"(ret),"+m" (*(volatile pointer_t *) (addr))
    :"a" (old_value.ptr), "d" (old_value.tag), "b" (new_value.ptr), "c" (new_value.tag));
return ret;

I have grep the source code of gcc 4.6.3, and found that __sync_bool_compare_and_swap is implemented use

typedef int (__kernel_cmpxchg_t) (int oldval, int newval, int *ptr); 
#define __kernel_cmpxchg (*(__kernel_cmpxchg_t *) 0xffff0fc0)

it seems that 0xffff0fc0 is the adress of some kernel helper functions

but in gcc 4.1.2 , there is no such codes like __kernel_cmpxchg_t, and I can’t find the implementation of __sync_bool_compare_and_swap.

so what’s the difference between __sync_bool_compare_and_swap and cmpxchg?

is __sync_bool_compare_and_swap implemented by cmpxchg?

and with kernel helper function __kernel_cmpxchg_t, is it implementd by cmpxchg?

thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T18:02:05+00:00Added an answer on May 31, 2026 at 6:02 pm

    I think the __kernel_cmpxchg is a fallback which Linux makes available on some architectures which don’t have native hardware support for CAS. E.g. ARMv5 or something like that.

    Usually, GCC inline expands the _sync* builtins. Unless you’re really interested in GCC internals, an easier way to find out what it does is to make a simple C example and look at the ASM the compiler generates.

    Consider

    
    #include <stdbool.h>
    
    bool my_cmpchg(int *ptr, int oldval, int newval)
    {
        return __sync_bool_compare_and_swap(ptr, oldval, newval);
    }
    

    Compiling this on an x86_64 Linux machine with GCC 4.4 the following asm is generated:

    
    my_cmpchg:
    .LFB0:
        .cfi_startproc
        movl    %esi, %eax
        lock cmpxchgl   %edx, (%rdi)
        sete    %al
        ret
        .cfi_endproc
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I use external defines such as LONG_MIN and LONG_MAX in ARM assembler
I wonder if I should use the CAS protocol or OAuth + some authentication
So I read that in a 32bit machine, one can use the CAS operation
see also Is Code Access Security of any real world use? I want to
I do not really use any try/catches in my code ever but I'm trying
use this website a lot but first time posting. My program creates a number
Use case: I've just entered insert mode, and typed some text. Now I want
I'm a sql newbie, I use mssql2005 I like to do join Action depnding
I am trying to use CAS as my Single Sign On service and I
Which CAS implementation should i use to enable CAS single sign on to my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.