Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace Allodium.Threading {
- /// <summary>
- /// Contains extension methods for <c>System.Threading.ReaderWriterLockSlim</c> class.
- /// </summary>
- public static class ReaderWriterLockSlimExtensions {
- /// <summary>
- /// Returns value from <c>readFunc</c> delegate during claimed read lock on instance of <c>ReaderWriterLockSlim</c>.
- /// Will call EnterReadLock().
- /// </summary>
- /// <typeparam name="T">Type of return value.</typeparam>
- /// <param name="self"><c>ReaderWriterLockSlim</c> used as synchronization object.</param>
- /// <param name="readFunc">Delegate for reading value.</param>
- /// <returns></returns>
- public static T ReadOnly<T>(this ReaderWriterLockSlim self, Func<T> readFunc) {
- if (object.ReferenceEquals(self, null)) { throw new ArgumentNullException("self"); }
- if (object.ReferenceEquals(readFunc, null)) { throw new ArgumentNullException("readFunc"); }
- T result;
- // flag, if lock was entered
- bool lockEntered = false;
- try {
- // I don't want the ThreadAbortException during claiming the lock
- Thread.BeginCriticalRegion();
- try {
- self.EnterReadLock();
- lockEntered = true;
- }
- finally {
- Thread.EndCriticalRegion();
- }
- result = readFunc();
- }
- finally {
- // I don't want the ThreadAbortException during releasing the lock
- Thread.BeginCriticalRegion();
- try {
- if (lockEntered) {
- self.ExitReadLock();
- }
- }
- finally {
- Thread.EndCriticalRegion();
- }
- }
- return result;
- }
- /// <summary>
- /// Returns value from <c>readFunc</c> delegate during claimed upgradeable read lock on instance of <c>ReaderWriterLockSlim</c>.
- /// Will call EnterUpgradeableReadLock().
- /// </summary>
- /// <typeparam name="T">Type of return value.</typeparam>
- /// <param name="self"><c>ReaderWriterLockSlim</c> used as synchronization object.</param>
- /// <param name="readFunc">Delegate for reading value.</param>
- /// <returns></returns>
- public static T Read<T>(this ReaderWriterLockSlim self, Func<T> readFunc) {
- if (object.ReferenceEquals(self, null)) { throw new ArgumentNullException("self"); }
- if (object.ReferenceEquals(readFunc, null)) { throw new ArgumentNullException("readFunc"); }
- T result;
- // flag, if lock was entered
- bool lockEntered = false;
- try {
- // I don't want the ThreadAbortException during claiming the lock
- Thread.BeginCriticalRegion();
- try {
- self.EnterUpgradeableReadLock();
- lockEntered = true;
- }
- finally {
- Thread.EndCriticalRegion();
- }
- result = readFunc();
- }
- finally {
- // I don't want the ThreadAbortException during releasing the lock
- Thread.BeginCriticalRegion();
- try {
- if (lockEntered) {
- self.ExitUpgradeableReadLock();
- }
- }
- finally {
- Thread.EndCriticalRegion();
- }
- }
- return result;
- }
- /// <summary>
- /// Call <c>writeFunc</c> delegate during claimed write lock on instance of <c>ReaderWriterLockSlim</c>.
- /// Will call EnterWriteLock().
- /// </summary>
- /// <param name="self"><c>ReaderWriterLockSlim</c> used as synchronization object.</param>
- /// <param name="writeFunc">Delegate for reading value.</param>
- /// <returns></returns>
- public static void Write(this ReaderWriterLockSlim self, Action writeAction) {
- if (object.ReferenceEquals(self, null)) { throw new ArgumentNullException("self"); }
- if (object.ReferenceEquals(writeAction, null)) { throw new ArgumentNullException("writeAction"); }
- // flag, if lock was entered
- bool lockEntered = false;
- try {
- // I don't want the ThreadAbortException during claiming the lock
- Thread.BeginCriticalRegion();
- try {
- self.EnterWriteLock();
- lockEntered = true;
- }
- finally {
- Thread.EndCriticalRegion();
- }
- writeAction();
- }
- finally {
- // I don't want the ThreadAbortException during releasing the lock
- Thread.BeginCriticalRegion();
- try {
- if (lockEntered) {
- self.ExitWriteLock();
- }
- }
- finally {
- Thread.EndCriticalRegion();
- }
- }
- }
- /// <summary>
- /// Returns value from <c>writeFunc</c> delegate during claimed write lock on instance of <c>ReaderWriterLockSlim</c>.
- /// Will call EnterWriteLock().
- /// </summary>
- /// <typeparam name="T">Type of return value.</typeparam>
- /// <param name="self"><c>ReaderWriterLockSlim</c> used as synchronization object.</param>
- /// <param name="writeFunc">Delegate for writing value.</param>
- /// <returns></returns>
- public static T Write<T>(this ReaderWriterLockSlim self, Func<T> writeFunc) {
- if (object.ReferenceEquals(self, null)) { throw new ArgumentNullException("self"); }
- if (object.ReferenceEquals(writeFunc, null)) { throw new ArgumentNullException("writeFunc"); }
- T result;
- // flag, if lock was entered
- bool lockEntered = false;
- try {
- // I don't want the ThreadAbortException during claiming the lock
- Thread.BeginCriticalRegion();
- try {
- self.EnterWriteLock();
- lockEntered = true;
- }
- finally {
- Thread.EndCriticalRegion();
- }
- result = writeFunc();
- }
- finally {
- // I don't want the ThreadAbortException during releasing the lock
- Thread.BeginCriticalRegion();
- try {
- if (lockEntered) {
- self.ExitWriteLock();
- }
- }
- finally {
- Thread.EndCriticalRegion();
- }
- }
- return result;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement