Advertisement
Guest User

ReaderWriterLockSlimExtensions

a guest
Nov 22nd, 2012
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.59 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7.  
  8. namespace Allodium.Threading {
  9.     /// <summary>
  10.     /// Contains extension methods for <c>System.Threading.ReaderWriterLockSlim</c> class.
  11.     /// </summary>
  12.     public static class ReaderWriterLockSlimExtensions {
  13.         /// <summary>
  14.         /// Returns value from <c>readFunc</c> delegate during claimed read lock on instance of <c>ReaderWriterLockSlim</c>.
  15.         /// Will call EnterReadLock().
  16.         /// </summary>
  17.         /// <typeparam name="T">Type of return value.</typeparam>
  18.         /// <param name="self"><c>ReaderWriterLockSlim</c> used as synchronization object.</param>
  19.         /// <param name="readFunc">Delegate for reading value.</param>
  20.         /// <returns></returns>
  21.         public static T ReadOnly<T>(this ReaderWriterLockSlim self, Func<T> readFunc) {
  22.             if (object.ReferenceEquals(self, null)) { throw new ArgumentNullException("self"); }
  23.             if (object.ReferenceEquals(readFunc, null)) { throw new ArgumentNullException("readFunc"); }
  24.  
  25.             T result;
  26.  
  27.             // flag, if lock was entered
  28.             bool lockEntered = false;
  29.             try {
  30.                 // I don't want the ThreadAbortException during claiming the lock
  31.                 Thread.BeginCriticalRegion();
  32.                 try {
  33.                     self.EnterReadLock();
  34.                     lockEntered = true;
  35.                 }
  36.                 finally {
  37.                     Thread.EndCriticalRegion();
  38.                 }
  39.  
  40.                 result = readFunc();
  41.             }
  42.             finally {
  43.                 // I don't want the ThreadAbortException during releasing the lock
  44.                 Thread.BeginCriticalRegion();
  45.                 try {
  46.                     if (lockEntered) {
  47.                         self.ExitReadLock();
  48.                     }
  49.                 }
  50.                 finally {
  51.                     Thread.EndCriticalRegion();
  52.                 }
  53.             }
  54.  
  55.             return result;
  56.         }
  57.  
  58.         /// <summary>
  59.         /// Returns value from <c>readFunc</c> delegate during claimed upgradeable read lock on instance of <c>ReaderWriterLockSlim</c>.
  60.         /// Will call EnterUpgradeableReadLock().
  61.         /// </summary>
  62.         /// <typeparam name="T">Type of return value.</typeparam>
  63.         /// <param name="self"><c>ReaderWriterLockSlim</c> used as synchronization object.</param>
  64.         /// <param name="readFunc">Delegate for reading value.</param>
  65.         /// <returns></returns>
  66.         public static T Read<T>(this ReaderWriterLockSlim self, Func<T> readFunc) {
  67.             if (object.ReferenceEquals(self, null)) { throw new ArgumentNullException("self"); }
  68.             if (object.ReferenceEquals(readFunc, null)) { throw new ArgumentNullException("readFunc"); }
  69.  
  70.             T result;
  71.  
  72.             // flag, if lock was entered
  73.             bool lockEntered = false;
  74.             try {
  75.                 // I don't want the ThreadAbortException during claiming the lock
  76.                 Thread.BeginCriticalRegion();
  77.                 try {
  78.                     self.EnterUpgradeableReadLock();
  79.                     lockEntered = true;
  80.                 }
  81.                 finally {
  82.                     Thread.EndCriticalRegion();
  83.                 }
  84.  
  85.                 result = readFunc();
  86.             }
  87.             finally {
  88.                 // I don't want the ThreadAbortException during releasing the lock
  89.                 Thread.BeginCriticalRegion();
  90.                 try {
  91.                     if (lockEntered) {
  92.                         self.ExitUpgradeableReadLock();
  93.                     }
  94.                 }
  95.                 finally {
  96.                     Thread.EndCriticalRegion();
  97.                 }
  98.             }
  99.  
  100.             return result;
  101.         }
  102.  
  103.         /// <summary>
  104.         /// Call <c>writeFunc</c> delegate during claimed write lock on instance of <c>ReaderWriterLockSlim</c>.
  105.         /// Will call EnterWriteLock().
  106.         /// </summary>
  107.         /// <param name="self"><c>ReaderWriterLockSlim</c> used as synchronization object.</param>
  108.         /// <param name="writeFunc">Delegate for reading value.</param>
  109.         /// <returns></returns>
  110.         public static void Write(this ReaderWriterLockSlim self, Action writeAction) {
  111.             if (object.ReferenceEquals(self, null)) { throw new ArgumentNullException("self"); }
  112.             if (object.ReferenceEquals(writeAction, null)) { throw new ArgumentNullException("writeAction"); }
  113.  
  114.             // flag, if lock was entered
  115.             bool lockEntered = false;
  116.             try {
  117.                 // I don't want the ThreadAbortException during claiming the lock
  118.                 Thread.BeginCriticalRegion();
  119.                 try {
  120.                     self.EnterWriteLock();
  121.                     lockEntered = true;
  122.                 }
  123.                 finally {
  124.                     Thread.EndCriticalRegion();
  125.                 }
  126.  
  127.                 writeAction();
  128.             }
  129.             finally {
  130.                 // I don't want the ThreadAbortException during releasing the lock
  131.                 Thread.BeginCriticalRegion();
  132.                 try {
  133.                     if (lockEntered) {
  134.                         self.ExitWriteLock();
  135.                     }
  136.                 }
  137.                 finally {
  138.                     Thread.EndCriticalRegion();
  139.                 }
  140.             }
  141.         }
  142.  
  143.         /// <summary>
  144.         /// Returns value from <c>writeFunc</c> delegate during claimed write lock on instance of <c>ReaderWriterLockSlim</c>.
  145.         /// Will call EnterWriteLock().
  146.         /// </summary>
  147.         /// <typeparam name="T">Type of return value.</typeparam>
  148.         /// <param name="self"><c>ReaderWriterLockSlim</c> used as synchronization object.</param>
  149.         /// <param name="writeFunc">Delegate for writing value.</param>
  150.         /// <returns></returns>
  151.         public static T Write<T>(this ReaderWriterLockSlim self, Func<T> writeFunc) {
  152.             if (object.ReferenceEquals(self, null)) { throw new ArgumentNullException("self"); }
  153.             if (object.ReferenceEquals(writeFunc, null)) { throw new ArgumentNullException("writeFunc"); }
  154.  
  155.             T result;
  156.  
  157.             // flag, if lock was entered
  158.             bool lockEntered = false;
  159.             try {
  160.                 // I don't want the ThreadAbortException during claiming the lock
  161.                 Thread.BeginCriticalRegion();
  162.                 try {
  163.                     self.EnterWriteLock();
  164.                     lockEntered = true;
  165.                 }
  166.                 finally {
  167.                     Thread.EndCriticalRegion();
  168.                 }
  169.  
  170.                 result = writeFunc();
  171.             }
  172.             finally {
  173.                 // I don't want the ThreadAbortException during releasing the lock
  174.                 Thread.BeginCriticalRegion();
  175.                 try {
  176.                     if (lockEntered) {
  177.                         self.ExitWriteLock();
  178.                     }
  179.                 }
  180.                 finally {
  181.                     Thread.EndCriticalRegion();
  182.                 }
  183.             }
  184.  
  185.             return result;
  186.         }
  187.     }
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement