Files
@ bd55ba5f4fde
Branch filter:
Location: seniordesign-ui/GMap.NET.Core/GMap.NET.Internals/FastReaderWriterLock.cs
bd55ba5f4fde
4.9 KiB
text/x-csharp
data display is 'finished'. charts are working. minor fixes also done
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | #if !MONO && !PocketPC
#define UseFastResourceLock
#endif
namespace GMap.NET.Internals
{
using System;
using System.Threading;
#if !MONO
using System.Runtime.InteropServices;
#endif
/// <summary>
/// custom ReaderWriterLock
/// in Vista and later uses integrated Slim Reader/Writer (SRW) Lock
/// http://msdn.microsoft.com/en-us/library/aa904937(VS.85).aspx
/// http://msdn.microsoft.com/en-us/magazine/cc163405.aspx#S2
/// </summary>
public sealed class FastReaderWriterLock : IDisposable
{
#if !MONO && !PocketPC
private static class NativeMethods
{
// Methods
[DllImport("Kernel32", ExactSpelling = true)]
internal static extern void AcquireSRWLockExclusive(ref IntPtr srw);
[DllImport("Kernel32", ExactSpelling = true)]
internal static extern void AcquireSRWLockShared(ref IntPtr srw);
[DllImport("Kernel32", ExactSpelling = true)]
internal static extern void InitializeSRWLock(out IntPtr srw);
[DllImport("Kernel32", ExactSpelling = true)]
internal static extern void ReleaseSRWLockExclusive(ref IntPtr srw);
[DllImport("Kernel32", ExactSpelling = true)]
internal static extern void ReleaseSRWLockShared(ref IntPtr srw);
}
IntPtr LockSRW = IntPtr.Zero;
public FastReaderWriterLock()
{
if (UseNativeSRWLock)
{
NativeMethods.InitializeSRWLock(out this.LockSRW);
}
else
{
#if UseFastResourceLock
pLock = new FastResourceLock();
#endif
}
}
#if UseFastResourceLock
~FastReaderWriterLock()
{
Dispose(false);
}
void Dispose(bool disposing)
{
if (pLock != null)
{
pLock.Dispose();
pLock = null;
}
}
FastResourceLock pLock;
#endif
static readonly bool UseNativeSRWLock = Stuff.IsRunningOnVistaOrLater() && IntPtr.Size == 4; // works only in 32-bit mode, any ideas on native 64-bit support?
#endif
#if !UseFastResourceLock
Int32 busy = 0;
Int32 readCount = 0;
#endif
public void AcquireReaderLock()
{
#if !MONO && !PocketPC
if (UseNativeSRWLock)
{
NativeMethods.AcquireSRWLockShared(ref LockSRW);
}
else
#endif
{
#if UseFastResourceLock
pLock.AcquireShared();
#else
Thread.BeginCriticalRegion();
while(Interlocked.CompareExchange(ref busy, 1, 0) != 0)
{
Thread.Sleep(1);
}
Interlocked.Increment(ref readCount);
// somehow this fix deadlock on heavy reads
Thread.Sleep(0);
Thread.Sleep(0);
Thread.Sleep(0);
Thread.Sleep(0);
Thread.Sleep(0);
Thread.Sleep(0);
Thread.Sleep(0);
Interlocked.Exchange(ref busy, 0);
#endif
}
}
public void ReleaseReaderLock()
{
#if !MONO && !PocketPC
if (UseNativeSRWLock)
{
NativeMethods.ReleaseSRWLockShared(ref LockSRW);
}
else
#endif
{
#if UseFastResourceLock
pLock.ReleaseShared();
#else
Interlocked.Decrement(ref readCount);
Thread.EndCriticalRegion();
#endif
}
}
public void AcquireWriterLock()
{
#if !MONO && !PocketPC
if (UseNativeSRWLock)
{
NativeMethods.AcquireSRWLockExclusive(ref LockSRW);
}
else
#endif
{
#if UseFastResourceLock
pLock.AcquireExclusive();
#else
Thread.BeginCriticalRegion();
while(Interlocked.CompareExchange(ref busy, 1, 0) != 0)
{
Thread.Sleep(1);
}
while(Interlocked.CompareExchange(ref readCount, 0, 0) != 0)
{
Thread.Sleep(1);
}
#endif
}
}
public void ReleaseWriterLock()
{
#if !MONO && !PocketPC
if (UseNativeSRWLock)
{
NativeMethods.ReleaseSRWLockExclusive(ref LockSRW);
}
else
#endif
{
#if UseFastResourceLock
pLock.ReleaseExclusive();
#else
Interlocked.Exchange(ref busy, 0);
Thread.EndCriticalRegion();
#endif
}
}
#region IDisposable Members
public void Dispose()
{
#if UseFastResourceLock
this.Dispose(true);
GC.SuppressFinalize(this);
#endif
}
#endregion
}
}
|