Are you trying to roll your own spin lock implementation?
the acquire path needs a loop of InterlockedCompareExchange with a stall of some kind in the body. At least REP NOOP aka PAUSE so the CPU knows that this is a busy loop and not useful work it should prioritize. And you should use the acquire version so that the correct memory barrier is used.
The release path should use InterlockedExchangeRelease. No comparison needed and the correct memory barrier.
If you can, I recommend that you just use a spin lock. locking privatives are hard to get exactly right, and unless there is a specific reason not to use the standard ones, you will spend a lot of time for not much
@Phil_Barila said:
Just use a spinlock and be done with it.
to use a spin lock above DISPATCH_LEVEL use the xxxAtDpcLevel APIs (the name is misleading, it is essentially the spin lock API for when the IRQL has already been set). all acquisitions of the lock have to be at the same IRQL.
forgot to tell im using it with KeInterlockedSetProcessorAffinityEx update use InterlockedExchange64 over InterlockedCompareExchangeRelease64 fixed it thanks !