Can two CPUs hold two "different" spin locks simultaneously at same time?
So...does this mean: a sigle(un开发者_JAVA百科iprocessor) CPU cannot hold two "different" spinlocks at the same time?
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.
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.
精彩评论