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 6224963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:45:31+00:00 2026-05-24T08:45:31+00:00

I’m new to using gcc inline assembly, and was wondering if, on an x86

  • 0

I’m new to using gcc inline assembly, and was wondering if, on an x86 multi-core machine, a spinlock (without race conditions) could be implemented as (using AT&T syntax):

spin_lock:
mov 0 eax
lock cmpxchg 1 [lock_addr]
jnz spin_lock
ret

spin_unlock:
lock mov 0 [lock_addr]
ret
  • 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-24T08:45:32+00:00Added an answer on May 24, 2026 at 8:45 am

    You have the right idea, but your asm is broken:

    cmpxchg can’t work with an immediate operand, only registers.

    lock is not a valid prefix for mov. mov to an aligned address is atomic on x86, so you don’t need lock anyway.

    It has been some time since I’ve used AT&T syntax, hope I remembered everything:

    spin_lock:
        xorl   %ecx, %ecx
        incl   %ecx            # newVal = 1
    spin_lock_retry:
        xorl   %eax, %eax      # expected = 0
        lock; cmpxchgl %ecx, (lock_addr)
        jnz    spin_lock_retry
        ret
    
    spin_unlock:
        movl   $0,  (lock_addr)    # atomic release-store
        ret
    

    Note that GCC has atomic builtins, so you don’t actually need to use inline asm to accomplish this:

    void spin_lock(int *p)
    {
        while(!__sync_bool_compare_and_swap(p, 0, 1));
    }
    
    void spin_unlock(int volatile *p)
    {
        asm volatile ("":::"memory"); // acts as a memory barrier.
        *p = 0;
    }
    

    As Bo says below, locked instructions incur a cost: every one you use must acquire exclusive access to the cache line and lock it down while lock cmpxchg runs, like for a normal store to that cache line but held for the duration of lock cmpxchg execution. This can delay the unlocking thread especially if multiple threads are waiting to take the lock. Even without many CPUs, it’s still easy and worth it to optimize around:

    void spin_lock(int volatile *p)
    {
        while(!__sync_bool_compare_and_swap(p, 0, 1))
        {
            // spin read-only until a cmpxchg might succeed
            while(*p) _mm_pause();  // or maybe do{}while(*p) to pause first
        }
    }
    

    The pause instruction is vital for performance on HyperThreading CPUs when you’ve got code that spins like this — it lets the second thread execute while the first thread is spinning. On CPUs which don’t support pause, it is treated as a nop.

    pause also prevents memory-order mis-speculation when leaving the spin-loop, when it’s finally time to do real work again. What is the purpose of the "PAUSE" instruction in x86?

    Note that spin locks are actually rarely used: typically, one uses something like a critical section or futex. These integrate a spin lock for performance under low contention, but then fall back to an OS-assisted sleep and notify mechanism. They may also take measures to improve fairness, and lots of other things the cmpxchg / pause loop doesn’t do.


    Also note that cmpxchg is unnecessary for a simple spinlock: you can use xchg and then check whether the old value was 0 or not. Doing less work inside the locked instruction may keep the cache line pinned for less time. See Locks around memory manipulation via inline assembly for a complete asm implementation using xchg and pause (but still with no fallback to OS-assisted sleep, just spinning indefinitely.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

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.