Changeset 1155


Ignore:
Timestamp:
01/18/09 13:03:12 (12 years ago)
Author:
svonolfe
Message:

Replaced the ReaderWriterLock with a ReaderWriterLockSlim (improved version) (#372)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Hive.Server.ADODataAccess/CachedDataAdapter.cs

    r1152 r1155  
    4141    private static bool cacheFilled = false;
    4242
    43     private static ReaderWriterLock cacheLock =
    44       new ReaderWriterLock();
     43    private static ReaderWriterLockSlim cacheLock =
     44      new ReaderWriterLockSlim();
    4545
    4646    protected DataTable dataTable =
     
    5151
    5252    protected CachedDataAdapter() {
    53       cacheLock.AcquireWriterLock(Timeout.Infinite);
     53      cacheLock.EnterWriteLock();
    5454
    5555      if (!cacheFilled) {
     
    5858      }
    5959
    60       cacheLock.ReleaseWriterLock();
     60      cacheLock.ExitWriteLock();
    6161
    6262      ServiceLocator.GetTransactionManager().OnUpdate +=
     
    6666    protected virtual RowT FindSingleRow(Selector dbSelector,
    6767      Selector cacheSelector) {
    68       cacheLock.AcquireReaderLock(Timeout.Infinite);
     68      cacheLock.EnterReadLock();
    6969     
    7070      RowT row =
     
    7777      }
    7878
    79       cacheLock.ReleaseReaderLock();
     79      cacheLock.ExitReadLock();
    8080
    8181      return row;
     
    8484    protected virtual IEnumerable<RowT> FindMultipleRows(Selector dbSelector,
    8585        Selector cacheSelector) {
    86       cacheLock.AcquireReaderLock(Timeout.Infinite);
     86      cacheLock.EnterReadLock();
    8787
    8888      IList<RowT> result =
     
    9898      }
    9999
    100       cacheLock.ReleaseReaderLock();
     100      cacheLock.ExitReadLock();
    101101
    102102      return result;
     
    107107      ObjT obj = default(ObjT);
    108108
    109       cacheLock.AcquireReaderLock(Timeout.Infinite);
     109      cacheLock.EnterReadLock();
    110110
    111111      RowT row = FindSingleRow(dbSelector, cacheSelector);
     
    116116      }
    117117
    118       cacheLock.ReleaseReaderLock();
     118      cacheLock.ExitReadLock();
    119119
    120120      return obj;
     
    123123    protected virtual ICollection<ObjT> FindMultiple(Selector dbSelector,
    124124      Selector cacheSelector) {
    125       cacheLock.AcquireReaderLock(Timeout.Infinite);
     125      cacheLock.EnterReadLock();
    126126
    127127      ICollection<ObjT> result =
     
    131131        FindMultiple(dbSelector);
    132132
    133       cacheLock.ReleaseReaderLock();
     133      cacheLock.ExitReadLock();
    134134
    135135      foreach (ObjT obj in resultDb) {
     
    156156      }
    157157
    158       cacheLock.AcquireReaderLock(Timeout.Infinite);
     158      cacheLock.EnterReadLock();
    159159
    160160      this.SynchronizeWithDb();
    161161
    162       cacheLock.ReleaseReaderLock();
     162      cacheLock.ExitReadLock();
    163163    }
    164164
     
    171171        return false;
    172172      else {
    173         cacheLock.AcquireReaderLock(Timeout.Infinite);
     173        cacheLock.EnterReadLock();
    174174
    175175        bool cached = FindCachedById((long)row[row.Table.PrimaryKey[0]]) != null;
    176176
    177         cacheLock.ReleaseReaderLock();
     177        cacheLock.ExitReadLock();
    178178
    179179        return cached;
     
    182182
    183183    protected override RowT GetRowById(long id) {
    184       cacheLock.AcquireReaderLock(Timeout.Infinite);
     184      cacheLock.EnterReadLock();
    185185
    186186      RowT row =
     
    194194          });
    195195
    196       cacheLock.ReleaseReaderLock();
     196      cacheLock.ExitReadLock();
    197197
    198198      return row;
     
    200200
    201201    private void AddToCache(RowT row) {
    202       cacheLock.AcquireWriterLock(Timeout.Infinite);
     202      cacheLock.EnterWriteLock();
    203203
    204204      cache.ImportRow(row);
    205205      row.Table.Rows.Remove(row);
    206206
    207       cacheLock.ReleaseWriterLock();
     207      cacheLock.ExitWriteLock();
    208208    }
    209209
    210210    private RowT AddToCache(ObjT obj) {
    211       cacheLock.AcquireWriterLock(Timeout.Infinite);
     211      cacheLock.EnterWriteLock();
    212212
    213213      RowT row =  InsertNewRowInCache(obj);
    214214
    215       cacheLock.ReleaseWriterLock();
     215      cacheLock.ExitWriteLock();
    216216
    217217      return row;
     
    219219
    220220    private void RemoveRowFromCache(RowT row) {
    221       cacheLock.AcquireWriterLock(Timeout.Infinite);
     221      cacheLock.EnterWriteLock();
    222222
    223223      dataTable.ImportRow(row);
    224224      cache.Rows.Remove(row);
    225225
    226       cacheLock.ReleaseWriterLock();
     226      cacheLock.ExitWriteLock();
    227227
    228228      UpdateRow(row);
Note: See TracChangeset for help on using the changeset viewer.