J'ai une mise en œuvre adopted d'un simple ReaderWriterLock pour Silverlight (pas de mises à jour ou de dépassements de délai), je me demandais si quelqu'un avec la bonne expertise peut valider si c'est bon ou mauvais. Pour moi ça a l'air plutôt bien, ça marche comme annoncé, mais j'ai une expérience limitée du code multi-threading en tant que tel.Silverlight ReaderWriterLock Mise en œuvre Bonne/Mauvaise?
public sealed class ReaderWriterLock
{
private readonly object syncRoot = new object(); // Internal lock.
private int i = 0; // 0 or greater means readers can pass; -1 is active writer.
private int readWaiters = 0; // Readers waiting for writer to exit.
private int writeWaiters = 0; // Writers waiting for writer lock.
private ConditionVariable conditionVar; // Condition variable.
public ReaderWriterLock()
{
conditionVar = new ConditionVariable(syncRoot);
}
/// <summary>
/// Gets a value indicating if a reader lock is held.
/// </summary>
public bool IsReaderLockHeld
{
get
{
lock (syncRoot)
{
if (i > 0)
return true;
return false;
}
}
}
/// <summary>
/// Gets a value indicating if the writer lock is held.
/// </summary>
public bool IsWriterLockHeld
{
get
{
lock (syncRoot)
{
if (i < 0)
return true;
return false;
}
}
}
/// <summary>
/// Aquires the writer lock.
/// </summary>
public void AcquireWriterLock()
{
lock (syncRoot)
{
writeWaiters++;
while (i != 0)
conditionVar.Wait(); // Wait until existing writer frees the lock.
writeWaiters--;
i = -1; // Thread has writer lock.
}
}
/// <summary>
/// Aquires a reader lock.
/// </summary>
public void AcquireReaderLock()
{
lock (syncRoot)
{
readWaiters++;
// Defer to a writer (one time only) if one is waiting to prevent writer starvation.
if (writeWaiters > 0)
{
conditionVar.Pulse();
Monitor.Wait(syncRoot);
}
while (i < 0)
Monitor.Wait(syncRoot);
readWaiters--;
i++;
}
}
/// <summary>
/// Releases the writer lock.
/// </summary>
public void ReleaseWriterLock()
{
bool doPulse = false;
lock (syncRoot)
{
i = 0;
// Decide if we pulse a writer or readers.
if (readWaiters > 0)
{
Monitor.PulseAll(syncRoot); // If multiple readers waiting, pulse them all.
}
else
{
doPulse = true;
}
}
if (doPulse)
conditionVar.Pulse(); // Pulse one writer if one waiting.
}
/// <summary>
/// Releases a reader lock.
/// </summary>
public void ReleaseReaderLock()
{
bool doPulse = false;
lock (syncRoot)
{
i--;
if (i == 0)
doPulse = true;
}
if (doPulse)
conditionVar.Pulse(); // Pulse one writer if one waiting.
}
/// <summary>
/// Condition Variable (CV) class.
/// </summary>
public class ConditionVariable
{
private readonly object syncLock = new object(); // Internal lock.
private readonly object m; // The lock associated with this CV.
public ConditionVariable(object m)
{
lock (syncLock)
{
this.m = m;
}
}
public void Wait()
{
bool enter = false;
try
{
lock (syncLock)
{
Monitor.Exit(m);
enter = true;
Monitor.Wait(syncLock);
}
}
finally
{
if (enter)
Monitor.Enter(m);
}
}
public void Pulse()
{
lock (syncLock)
{
Monitor.Pulse(syncLock);
}
}
public void PulseAll()
{
lock (syncLock)
{
Monitor.PulseAll(syncLock);
}
}
}
}
S'il est bon, il pourrait être utile pour les autres aussi, comme Silverlight ne dispose pas actuellement d'un type de verrou de lecture-écriture. Merci.
Hey merci pour votre écriture et me pointant vers la mise en œuvre de Vance Morrison. En fait, j'ai implémenté son implémentation sur Silverlight avec un minimum de changements, et il fonctionne aussi très vite. Je vais mettre quelques chiffres pour sauvegarder cela sur mon blog. À votre santé. –