IA-32 | reordering issue in a multiprocessor environment

Started by FrEEzE2046, February 05, 2011, 08:35:57 AM

Previous topic - Next topic

dedndave

i didn't think you needed to use LOCK with XCHG reg,mem - that it was implied
well - i have been doing it that way with semaphores and have had no trouble

in this case, if the semaphore = 0, it means that it is being queried or is locked
        xor     eax,eax
        xchg    eax,Semaphore
        or      eax,eax
        jz      busy

        push    eax
;do stuff
        pop     eax

        xchg    eax,Semaphore   ;restore original value

FrEEzE2046

Quote from: dedndave on February 07, 2011, 12:41:03 PMi didn't think you needed to use LOCK with XCHG reg,mem - that it was implied

Yes, I know that, it's meaningless. But, I like to prefix xchg too, in order to be sure that everyone how reads my code will understand what happens.

Quote from: dedndave on February 07, 2011, 12:41:03 PMi have been doing it that way with semaphores and have had no trouble

I don't know what this is related to. The question is "do we need a 'lock' (not the LOCK Signal) or 'mutex' to ensure 'atomic_and' ist a) atomic and b) wait-free (e.g. doesn't block other threads). I think my loop-solution is arguable, because I only promise that this function is 'atomic', not 'wait-free'. If the user of this function want's to ensure that the access to that memory location is also 'wait-free' he can locks the access by himself.

Antariy

Quote from: FrEEzE2046 on February 07, 2011, 05:27:54 AM
And, like I already said, it's just what WinAPI and Intels TBB does.

Yes, WinAPI is obviously user mode code which just cannot execute pieces without being interruped.

FrEEzE2046

Quote from: Antariy on February 07, 2011, 03:39:31 PM
Quote from: FrEEzE2046 on February 07, 2011, 05:27:54 AM
And, like I already said, it's just what WinAPI and Intels TBB does.
Yes, WinAPI is obviously user mode code which just cannot execute pieces without being interruped

I think it is ;) Thanks for your answer.

Slugsnack

you could just do something like this, which is a spinlock.

lock:                        ; The lock variable. 1 = locked, 0 = unlocked.
     dd      0

spin_lock:
     mov     eax, 1          ; Set the EAX register to 1.

loop:
     xchg    eax, [lock]     ; Atomically swap the EAX register with
                             ;  the lock variable.
                             ; This will always store 1 to the lock, leaving
                             ;  previous value in the EAX register.

     test    eax, eax        ; Test EAX with itself. Among other things, this will
                             ;  set the processor's Zero Flag if EAX is 0.
                             ; If EAX is 0, then the lock was unlocked and
                             ;  we just locked it.
                             ; Otherwise, EAX is 1 and we didn't acquire the lock.

     jnz     loop            ; Jump back to the XCHG instruction if the Zero Flag is
                             ;  not set, the lock was locked, and we need to spin.

     do your code here.........

     ret                     ; The lock has been acquired, return to the calling
                             ;  function.

spin_unlock:
     mov     eax, 0          ; Set the EAX register to 0.

     xchg    eax, [lock]     ; Atomically swap the EAX register with
                             ;  the lock variable.

     ret


spinlock code taken from wiki. it is not atomic but deals with interleaving/multithreading properly

sinsi

Which code should take precedence? The '0xff' or the 'and'?
Wrap both in a spinlock but let one control 'var'.
Light travels faster than sound, that's why some people seem bright until you hear them.