5

I want to use "_test_and_set lock" assembly language implementation with atomic swap assembly instruction in my C/C++ program.

class LockImpl 
{
  public:
  static void lockResource(DWORD resourceLock )
  {
    __asm 
    {
      InUseLoop:  mov     eax, 0;0=In Use
                  xchg    eax, resourceLock
                  cmp     eax, 0
                  je      InUseLoop
    }

  }

  static void unLockResource(DWORD resourceLock )
  {
    __asm 
    {
      mov resourceLock , 1 
    }   

  }
};

This works but there is a bug in here.

The problem is that i want to pass DWORD * resourceLock instead of DWORD resourceLock.

So question is that how to pass a pointer from C/C++ to assembly and get it back. ?

thanks in advance.

Regards, -Jay.

P.S. this is done to avoid context switches between user space and kernel space.

8
  • 1
    By the way, there is no standard assembly language. Also, the parameter passing between assembly and C or C++ varies by the compiler. So you will have to supply the compiler and the platform identification (or see my answer for a platform independent process). Dec 23, 2009 at 19:22
  • What is the problem with lockResource(DWORD *resourceLock )? i.e. just declaring resourceLock as a pointer and otherwise keeping the code the same?
    – x4u
    Dec 23, 2009 at 19:22
  • I don't understand the problem. did you try static void lockResource(DWORD * resourceLock) and it didn't work? Dec 23, 2009 at 19:24
  • @gf : Right now my targeted platform is Windows Server 2008 but eventually its gonna be Monta Vista : Carrer Grade 5.0 . So currently i am using the Visual studio 2008 's inbuilt compiler
    – Jay D
    Dec 23, 2009 at 19:30
  • @x4u : did'nt get what you want to say .. The bug is that the resource has a single instance and all the threads get a pointer to the resource. The resource lock is part of the resource. THe bug here is that i am sending the resource lock by value . I MUST send it by reference.
    – Jay D
    Dec 23, 2009 at 19:32

7 Answers 7

6

If you're writing this for Windows, you should seriously consider using a critical section object. The critical section API functions are optimised such that they won't transition into kernel mode unless they really need to, so the normal case of no contention has very little overhead.

The biggest problem with your spin lock is that if you're on a single CPU system and you're waiting for the lock, then you're using all the cycles you can and whatever is holding the lock won't even get a chance to run until your timeslice is up and the kernel preempts your thread.

Using a critical section will be more successful than trying to roll your own user mode spin lock.

5
  • I do NOT want to write windows specific code. That's the reason why i didn't used Interlocked and other stuff. I want to make it as much generic as possible. I am desperately waiting for the Linux move but that's not gonna happen at least for next couple of months.
    – Jay D
    Dec 23, 2009 at 20:09
  • 9
    Writing assembly isn't going to help portability. If you want portable critical sections, code to a neutral API. Consider boost synchronization: boost.org/doc/libs/1_41_0/doc/html/thread/synchronization.html
    – plinth
    Dec 23, 2009 at 20:19
  • 2
    I'm voting this down for the simple reason that it does not answer the question asked. The question is a valid one, and an answer to it would be good to see.
    – xcut
    Dec 24, 2009 at 0:32
  • I'm voting it up because it's a BAD idea to spinlock on a system that can only run one thread at a time. Downvoting this is like saying "I don't care if it's a bad idea to shoot with kids downrange, I'm only going to hit the target I asked you to put up!"... Lastly, I don't understand why this logic can't be abstracted away so that whatever you change only has to be done once, in one little place. Obviously the plan is to do this with the current plan because otherwise the OP wouldn't be using a DWORD and writing inline assembly. Dec 24, 2009 at 1:38
  • 1
    It's OK to write a spinlock if you know your platform has multiple hardware threads. The default Window CS is basically a combination mutex + spinlock with a spin count. It tries to spin for the spin count before falling back to the OS mutex. On a single processor system, the spin count used is always zero which makes the default behavior there just a mutex.
    – Adisak
    Dec 24, 2009 at 19:17
4

In terms of your actual question, it's pretty simple: just change the function headers to use volatile DWORD *resourceLock, and change the assembly lines that touch resourceLock to use indirection:

mov ecx, dword ptr [resourceLock]
xchg eax, dword ptr [ecx]

and

mov ecx, dword ptr [resourceLock]
lock mov dword ptr [ecx], 1

However, note that you've got a couple of other problems looming:

  • You say you're developing this on Windows, but want to switch to Linux. However, you're using MSVC-specific inline assembly - this will have to be ported to gcc-style when you move to Linux (in particular that involves switching from Intel syntax to AT&T syntax). You will be much better off developing with gcc even on Windows; that will minimise the pain of migration (see mingw for gcc for Windows).

  • Greg Hewgill is absolutely right about spinning uselessly, stopping the lock-holder from getting CPU. Consider yielding the CPU if you've been spinning for too long.

  • On a multiprocessor x86, you might well have a problem with memory loads and stores being re-ordered around your lock - mfence instructions in the lock and unlock procedures might be necessary.


Really, if you're worrying about locking that means you're using threading, which probably means you're using the platform-specific threading APIs already. So use the native synchronisation primitives, and switch out to the pthreads versions when you switch to Linux.

7
  • This doesn't work for VC++ inline asm - at least not on my test in a debug build. You actually need a mov ebx, resourceLock followed by a xchg eax, [ebx] to do what he wants to do. Just xchg eax, [resourceLock] will actually swap eax with the value stored in the pointer (or address of the reference).
    – Adisak
    Dec 23, 2009 at 22:03
  • 2
    Explanation: Let's actually say our pointer has a value (and it's dereferenced memory has a value too) - say DWORD *resourceLock == 0x493004 and *(DWORD*)(0x00493004) == 1. Then after mov ebx, resourceLock; xchg eax, [ebx], eax will equal 1. After xchg eax, [resourceLock], eax will equal 0x493004 which is most definitely not what you want.
    – Adisak
    Dec 23, 2009 at 22:10
  • Thanks, fixed it. It's been a long while since I used Intel syntax assembly, it's a bit rusty!
    – caf
    Dec 23, 2009 at 23:01
  • @CAF: FWIW, the lock mov you have in the unlock code can just be a mov. X86 enforces visible write ordering so the second lock is unnecessary and in fact, only slows things down. The X86 only reorders 1) load-to-load and 2) loads-above-stores to a different address. There is no store reordering and stores are never moved above loads. Since this code uses a xchg (which has implicit lock/mfence) there is no need to add an mfence to the code. Any modifications while the lock is held will be visible before the unlock write is visible.
    – Adisak
    Dec 24, 2009 at 3:59
  • @CAF: In the unlock code, since the mov doesn't have to be bus-locked it can be done in C with a simple assignment rather than ASM. However, taking it out of ASM means no compiler read-write barrier so you have to add one. Basically this works with the pointer: _ReadWriteBarrier(); *resourceLock=1; (with a reference, you eliminate the * before resourceLock).
    – Adisak
    Dec 24, 2009 at 22:33
3

Apparently, you are compiling with MSVC using inline assembly blocks in your C++ code.

As a general remark, you should really use compiler intrinsics as inline assembly has no future: it's no more supported my MS compilers when compiling for x64.

If you need to have functions fine tuned in assembly, you will have to implement them in separate files.

4
  • inline assembly (that is assembly blocks in C/C++ code) is no more supported by MS. and as far as I can see, it really looks like his code is inline assembly compiled with MSVC Dec 23, 2009 at 19:32
  • @Ken do you prefer it rephrased like this? Dec 23, 2009 at 19:35
  • @Ken , @Gregory : Yes .. that's correct. ! I hate Windowz .. but my management thinks that it helps speed up the development .. WTF .. I have seeing lot of scheduling related issues where the Win OS Scheduler doesn't do the right thing predictably and that makes some threads run wild and other sit around.. :( I tried enforcing CPU Affinity but that screws up the performance badly..
    – Jay D
    Dec 23, 2009 at 19:43
  • @Ken well inline assembly blocks, with the given syntax could only be MS, that's why I originally answered like this :) Dec 25, 2009 at 10:24
1

The main problems with the original version in the question is that it needs to use register indirect addressing and take a reference (or pointer parameter) rather than a by-value parameter for the lock DWORD.

Here's a working solution for Visual C++. EDIT: I have worked offline with the author and we have verified the code in this answer works in his test harness correctly.

But if you're using Windows, you should really by using the Interlocked API (i.e. InterlockedExchange).

Edit: As noted by CAF, lock xchg is not required because xchg automatically asserts a BusLock.

I also added a faster version that does a non-locking read before attempting to do the xchg. This significantly reduces BusLock contention on the memory interface. The algorithm can be sped up quite a bit more (in a contentious multithreaded case) by doing backoffs (yield then sleep) for locks held a long time. For the single-threaded-CPU case, using a OS lock that sleeps immediately on held-locks will be fastest.

class LockImpl
{
    // This is a simple SpinLock
    //  0 - in use / busy
    //  1 - free / available
public:
    static void lockResource(volatile DWORD &resourceLock )
    {
        __asm 
        {
            mov     ebx, resourceLock
InUseLoop:
            mov     eax, 0           ;0=In Use
            xchg    eax, [ebx]
            cmp     eax, 0
            je      InUseLoop
        }

    }

    static void lockResource_FasterVersion(DWORD &resourceLock )
    {
        __asm 
        {
            mov     ebx, resourceLock
InUseLoop:
            mov     eax, [ebx]    ;// Read without BusLock 
            cmp     eax, 0
            je      InUseLoop     ;// Retry Read if Busy

            mov     eax, 0
            xchg    eax, [ebx]    ;// XCHG with BusLock
            cmp     eax, 0
            je      InUseLoop     ;// Retry if Busy
        }
    }

    static void unLockResource(volatile DWORD &resourceLock)
    {
        __asm 
        {
            mov     ebx, resourceLock
            mov     [ebx], 1 
        }       

    }
};

// A little testing code here
volatile DWORD aaa=1;
void test()
{
 LockImpl::lockResource(aaa);
 LockImpl::unLockResource(aaa);
}
16
  • xchg is supposed to imply a bus lock. However if you don't put a lock prefix on the mov in unLockResource, I think you will need an mfence instruction before it, to ensure all sideeffects from the critical section you just left are seen by the other processors before they're allowed to enter their critical sections.
    – caf
    Dec 23, 2009 at 22:47
  • I tried your code and it does NOT work . What happens is that only first thread picks up the lock and its not able to release it. Secondly I was trying out the mfense/sfense + lock combination but that too does not seem to work.
    – Jay D
    Dec 24, 2009 at 0:43
  • Have you verified your Program using this lock works with any other type of lock? I have run the above code on a QuadCore in a highly contentious testbed that does multithread modifications and independent verify under locks using 8 threads. This test bed is strong enough that it found a bug in the common Ruediger-Asche-ReaderWriterLock from the MSDN website. This code passed "correctness" functionality with flying colors although it was several orders of magnitude slower than the locking mechanism I actually use. It's possible your calling code is not correct ?
    – Adisak
    Dec 24, 2009 at 2:03
  • lockTestThreadStatus_ = 1; while(lockTestThreadStatus_ ==1 ) { LockImpl::lockResource(commonResource->lockCnt); //===========Critical Section === commonResource->resource++; printf("The Thread ID : %ld resource value :%ld \n", GetCurrentThreadId(), commonResource->resource); //===========Critical Section End==== LockImpl::unLockResource(commonResource->lockCnt); }
    – Jay D
    Dec 24, 2009 at 2:30
  • This is my calling code .. This is basically the thread function. I expect to see multiple THread ID incrementing this resource Counter one after the other. But with above code I only see one thread ID continuously doing the increment. Ironically the code that have posted shows the correct behavior thought i know the bug in it. :) This test i am running on dual core machine without hyperthreading
    – Jay D
    Dec 24, 2009 at 2:33
1

You should be using something like this:

volatile LONG resourceLock = 1;

if(InterlockedCompareExchange(&resourceLock, 0, 1) == 1) {
    // success!
    // do something, and then
    resourceLock = 1;
} else {
    // failed, try again later
}

See InterlockedCompareExchange.

4
  • His code should work perfectly well with just InterlockedExchange(). For an unchecked value swap, InterlockedExchange is a bit faster than InterlockedCompareExchange.
    – Adisak
    Dec 24, 2009 at 3:28
  • Yes but you usually use CAS in this kind of situation and I wanted to bump him in the right direction.
    – Fozi
    Dec 24, 2009 at 14:32
  • Oh and as far as I know both the intrinsic InterlockedCompareExchange and CompareExchange boil down to one single assembly instruction, so I wouldn't say that one is faster than the other. Well, ok, CAS might need a cycle more.
    – Fozi
    Dec 24, 2009 at 14:34
  • Actually, CAS and XCHG are considerably different. For one, XCHG ALWAYS succeeds while CAS may fail. CAS usually requires additional code to pre-read the value before setting and if it fails you must do a retry or other code. And despite the fact that both single ASM instructions that are fairly slow due to BusLock, the XCHG can execute more quickly than CAS simply because you ALWAYS have to check if CAS succeeded. If your algorithm is simple enough to use XCHG, then XCHG is the preferred operation.
    – Adisak
    Dec 24, 2009 at 22:50
0

Look at your compiler documentation to find out how to print the generated assembly language for functions.

Print the assembly language for this function:

static void unLockResource(DWORD resourceLock )
{
  resourceLock = 0;
  return;
}

This may not work because the compiler can optimize the function and remove all the code. You should change the above function to pass a pointer to resourceLock and then have the function set the lock. Print the assembly of this working function.

-1

I already provided a working version which answered the original poster's question both on how to get the parameters passed in ASM and how to get his lock working correctly.

Many other answers have questioned the wiseness of using ASM at all and mentioned that either intrinsics or C OS calls should be used. The following works as well and is a C++ version of my ASM answer. There is a snippet of ASM in there that only needs to be used if your platform does not support InterlockedExchange().

class LockImpl
{
    // This is a simple SpinLock
    //  0 - in use / busy
    //  1 - free / available
public:
#if 1
    static DWORD MyInterlockedExchange(volatile DWORD *variable,DWORD newval)
    {
        // InterlockedExchange() uses LONG / He wants to use DWORD
        return((DWORD)InterlockedExchange(
            (volatile LONG *)variable,(LONG)newval));
    }
#else
    // You can use this if you don't have InterlockedExchange()
    // on your platform. Otherwise no ASM is required.
    static DWORD MyInterlockedExchange(volatile DWORD *variable,DWORD newval)
    {
        DWORD old;
        __asm 
        {
            mov     ebx, variable
            mov     eax, newval
            xchg    eax, [ebx]  ;// XCHG with BusLock
            mov     old, eax
        }
        return(old);
    }
#endif
    static void lockResource(volatile DWORD &resourceLock )
    {
        DWORD oldval;
        do 
        {
            while(0==resourceLock)
            {
                // Could have a yield, spin count, exponential 
                // backoff, OS CS fallback, etc. here
            }
            oldval=MyInterlockedExchange(&resourceLock,0);
        } while (0==oldval);
    }
    static void unLockResource(volatile DWORD &resourceLock)
    {
        // _ReadWriteBarrier() is a VC++ intrinsic that generates
        // no instructions / only prevents compiler reordering.
        // GCC uses __sync_synchronize() or __asm__ ( :::"memory" )
        _ReadWriteBarrier();
        resourceLock=1;
    }
};
2
  • The questions was not about whether to use inline assembly, it was about passing a variable by reference to assembly from C instead of by value. Thanks for your extended code though.. !
    – Jay D
    Dec 25, 2009 at 23:19
  • Wow... considering the top answer just says "Use Critical Sections" and doesn't answer the question at all, getting down-voted here seems a bit odd. Especially when many of the other answers are gripes about not using ASM and this is not just a follow-up but an ACTUAL working example that addresses those responses. I actually put some thought and work into this, contacting the author offline to help get things functional in his own code testbed, and then provided an additional non-ASM working version as well. I guess it doesn't pay to go out of your way to try to be helpful.
    – Adisak
    Dec 26, 2009 at 10:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.