views:

46

answers:

1
  1. Can two CPUs hold two "different" spin locks simultaneously at same time?

  2. So...does this mean: a sigle(uniprocessor) CPU cannot hold two "different" spinlocks at the same time?

  3. So...does this mean: the number of spinlocks on a single CPU cannot be > 1.

PS:"different" implying spinlock associated with different memory resources.


Does anybody know how spinlocks work internally? ...I mean, do they freeze bus during test set operations? I have googled but no absolute answer.

+1  A: 

A spin-lock is more or less only a shared int, to which writes are synchronized. There is no special flag for the processor. So you can acquire more then one spin-lock. (You shouldn't ...)

To prevent uni-processor-system from locking up, windows raises the IRQL to DISPATCH_LEVEL. The processor can only have one 'thread' running at DISPATCH_LEVEL, so locking multiple spin-locks at the same time, is safe on these systems.

The implementation should be like this : (not 100% true, and can diverge due to details)

LONG lock = 0;

KeAcquireSpinlock( ... )
{
    // raise irql. etc.
    while( InterlockedExchange( &lock, 1 ) != 0 ) 
        /* do nothing*/;
}

KeReleaseSpinLock( ... )
{
     InterlockedExchange( &lock, 0 );
     // lower irql ... etc.
}

InterlockedExchange guarantees that the exchange happens atomically for all processors on the same memory bus. So it must lock the memory bus, or at least force sole ownership of the specific cache line.

Christopher
"processor can only have one 'thread' running at DISPATCH_LEVEL so locking multiple spin-locks at the same time, is safe on these systems."that was the info. missing.Thanks this clears my doubt about uni processor spinlocks.
bakra