/* Performs an atomic compare exchange operation to release semaphore. The compare exchange operation must be performed using MP safe mechanisms. */
STATIC UINT32 InternalReleaseSemaphore(IN OUT volatile UINT32 *Sem)
/* Performs an atomic compare exchange operation to release semaphore. The compare exchange operation must be performed using MP safe mechanisms. */
STATIC UINT32 InternalReleaseSemaphore(IN OUT volatile UINT32 *Sem)
{
  UINT32  Value;
  do {
    Value = *Sem;
  } while (Value + 1 != 0 &&
           InterlockedCompareExchange32 (
             (UINT32 *)Sem,
             Value,
             Value + 1
             ) != Value);
  if (Value == MAX_UINT32) {
    return Value;
  }
  return Value + 1;
}