Changeset 7113


Ignore:
Timestamp:
12/02/11 12:40:22 (8 years ago)
Author:
ascheibe
Message:

#1631 implemented review comments for ThreadSafeLog

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Core/3.3/ThreadSafeLog.cs

    r6862 r7113  
    3030  [Item("ThreadSafeLog", "A thread-safe log for logging string messages.")]
    3131  [StorableClass]
    32   public class ThreadSafeLog : Log, IDeepCloneable {
    33     protected ReaderWriterLockSlim locker = new ReaderWriterLockSlim();
     32  public sealed class ThreadSafeLog : Log {
     33    private ReaderWriterLockSlim locker = new ReaderWriterLockSlim();
    3434
    3535    public override IEnumerable<string> Messages {
     
    3838        try {
    3939          return messages.ToArray(); // return copy of messages
    40         }
    41         finally { locker.ExitReadLock(); }
     40        } finally { locker.ExitReadLock(); }
    4241      }
    4342    }
    4443
    4544    [StorableConstructor]
    46     protected ThreadSafeLog(bool deserializing) : base(deserializing) { }
     45    private ThreadSafeLog(bool deserializing) : base(deserializing) { }
    4746    public ThreadSafeLog(long maxMessageCount = -1)
    4847      : base(maxMessageCount) {
    4948    }
    5049
    51     protected ThreadSafeLog(ThreadSafeLog original, Cloner cloner) {
    52       original.locker.EnterReadLock();
    53       try {
    54         cloner.RegisterClonedObject(original, this);
    55         this.messages = new List<string>(original.messages);
    56         this.maxMessageCount = original.maxMessageCount;
    57       }
    58       finally { original.locker.ExitReadLock(); }
    59     }
     50    private ThreadSafeLog(ThreadSafeLog original, Cloner cloner)
     51      : base(original, cloner) { }
    6052
    6153    public override IDeepCloneable Clone(Cloner cloner) {
    62       return new ThreadSafeLog(this, cloner);
     54      locker.EnterReadLock();
     55      try {
     56        return new ThreadSafeLog(this, cloner);
     57      } finally { locker.ExitReadLock(); }
    6358    }
    6459
     
    6762      try {
    6863        messages.Clear();
    69       }
    70       finally { locker.ExitWriteLock(); }
     64      } finally { locker.ExitWriteLock(); }
    7165      OnCleared();
    7266    }
     
    7872        messages.Add(s);
    7973        CapMessages();
    80       }
    81       finally { locker.ExitWriteLock(); }
     74      } finally { locker.ExitWriteLock(); }
    8275      OnMessageAdded(s);
    8376    }
     
    8982        messages.Add(s);
    9083        CapMessages();
    91       }
    92       finally { locker.ExitWriteLock(); }
     84      } finally { locker.ExitWriteLock(); }
    9385      OnMessageAdded(s);
    9486    }
Note: See TracChangeset for help on using the changeset viewer.