singrdk/base/Kernel/System/Threading/SmartSpinlock.cs

56 lines
1.2 KiB
C#
Raw Normal View History

2008-03-05 09:52:00 -05:00
////////////////////////////////////////////////////////////////////////////////
//
// Microsoft Research Singularity
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// File: ProtectionDomain.cs
//
// Note:
//
// SmartSpinlock is an object that wraps the SpinLock struct
// and avoids actually interlocking memory on a single-processor
// box.
//
using Microsoft.Singularity;
using System;
using System.Runtime.CompilerServices;
namespace System.Threading
{
2008-11-17 18:29:00 -05:00
[CLSCompliant(false)]
2008-03-05 09:52:00 -05:00
public class SmartSpinlock
{
#if SINGULARITY_MP
private SpinLock spin;
#endif
2008-11-17 18:29:00 -05:00
public SmartSpinlock(SpinLock.Types type)
2008-03-05 09:52:00 -05:00
{
#if SINGULARITY_MP
2008-11-17 18:29:00 -05:00
this.spin = new SpinLock(type);
2008-03-05 09:52:00 -05:00
#endif
}
[Inline]
public bool Lock()
{
bool enabled = Processor.DisableInterrupts();
#if SINGULARITY_MP
spin.Acquire(Thread.CurrentThread);
#endif // SINGULARITY_MP
return enabled;
}
[Inline]
public void Unlock(bool iflag)
{
#if SINGULARITY_MP
spin.Release(Thread.CurrentThread);
#endif // SINGULARITY_MP
Processor.RestoreInterrupts(iflag);
}
}
}