KHMEXP khm_boolean KHMAPI InitializeOnce(PIONCE pOnce) { if (InterlockedCompareExchangeAcquire(&pOnce->i_initializing, 1, 0) == 0) { InterlockedExchange(&pOnce->i_thrd, GetCurrentThreadId()); return TRUE; } else { DWORD this_thrd; unsigned spin_count = 0; #define MAX_SPIN_COUNT 100 #define FAILOVER_MS 100 this_thrd = GetCurrentThreadId(); while (pOnce->i_done == 0 && (DWORD) pOnce->i_thrd != this_thrd) { Sleep((spin_count / MAX_SPIN_COUNT) * FAILOVER_MS); spin_count++; } return FALSE; } }
inline ::LONG compare_exchange_acquire ( ::LONG z, volatile ::LONG& x, ::LONG y ) { return InterlockedCompareExchangeAcquire(&x, y, z); }