History

GSF.Core: Improved null handling for InterprocessReaderWriterLock and InterprocessCache; and general code cleanup.

J. Ritchie Carroll committed 5 years ago

Commit 5ffac611 parent 30e79942 files · +104 −183Browse files

Showing the changes to Source/Libraries/GSF.Core/Threading/InterprocessReaderWriterLock.cs (1 of 2 files). Show all

Unified · Split

Source/Libraries/GSF.Core/Threading/InterprocessReaderWriterLock.cs+27−46
@@ -82,10 +82,8 @@ namespace GSF.Threading · 81 unmodified lines
8282 /// <summary>
8383 /// Releases the unmanaged resources before the <see cref="InterprocessReaderWriterLock"/> object is reclaimed by <see cref="GC"/>.
8484 /// </summary>
85− ~InterprocessReaderWriterLock()
86− {
85+ ~InterprocessReaderWriterLock() =>
8786 Dispose(false);
88− }
8987
9088 #endregion
9189
@@ -94,13 +92,7 @@ namespace GSF.Threading · 2 unmodified lines
9492 /// <summary>
9593 /// Gets the maximum concurrent reader locks allowed.
9694 /// </summary>
97− public int MaximumConcurrentLocks
98− {
99− get
100− {
101− return m_maximumConcurrentLocks;
102− }
103− }
95+ public int MaximumConcurrentLocks => m_maximumConcurrentLocks;
10496
10597 #endregion
10698
@@ -121,30 +113,30 @@ namespace GSF.Threading · 14 unmodified lines
121113 /// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
122114 protected virtual void Dispose(bool disposing)
123115 {
124− if (!m_disposed)
116+ if (m_disposed)
117+ return;
118+
119+ try
125120 {
126− try
121+ if (!disposing)
122+ return;
123+
124+ if (!(m_concurrencyLock is null))
127125 {
128− if (disposing)
129− {
130− if ((object)m_concurrencyLock != null)
131− {
132− m_concurrencyLock.Close();
133− m_concurrencyLock = null;
134− }
135−
136− if ((object)m_semaphoreLock != null)
137− {
138− m_semaphoreLock.Close();
139− m_semaphoreLock = null;
140− }
141− }
126+ m_concurrencyLock.Close();
127+ m_concurrencyLock = null;
142128 }
143− finally
129+
130+ if (!(m_semaphoreLock is null))
144131 {
145− m_disposed = true; // Prevent duplicate dispose.
132+ m_semaphoreLock.Close();
133+ m_semaphoreLock = null;
146134 }
147135 }
136+ finally
137+ {
138+ m_disposed = true; // Prevent duplicate dispose.
139+ }
148140 }
149141
150142 /// <summary>
@@ -154,10 +146,8 @@ namespace GSF.Threading · 3 unmodified lines
154146 /// Upon successful acquisition of a read lock, use the <c>finally</c> block of a <c>try/finally</c> statement to call <see cref="ExitReadLock"/>.
155147 /// One <see cref="ExitReadLock"/> should be called for each <see cref="EnterReadLock"/> or <see cref="TryEnterReadLock"/>.
156148 /// </remarks>
157− public void EnterReadLock()
158− {
149+ public void EnterReadLock() =>
159150 TryEnterReadLock(Timeout.Infinite);
160− }
161151
162152 /// <summary>
163153 /// Tries to enter the lock in write mode.
@@ -166,10 +156,8 @@ namespace GSF.Threading · 2 unmodified lines
166156 /// Upon successful acquisition of a write lock, use the <c>finally</c> block of a <c>try/finally</c> statement to call <see cref="ExitWriteLock"/>.
167157 /// One <see cref="ExitWriteLock"/> should be called for each <see cref="EnterWriteLock"/> or <see cref="TryEnterWriteLock"/>.
168158 /// </remarks>
169− public void EnterWriteLock()
170− {
159+ public void EnterWriteLock() =>
171160 TryEnterWriteLock(Timeout.Infinite);
172− }
173161
174162 /// <summary>
175163 /// Exits read mode and returns the prior read lock count.
@@ -178,11 +166,8 @@ namespace GSF.Threading · 2 unmodified lines
178166 /// Upon successful acquisition of a read lock, use the <c>finally</c> block of a <c>try/finally</c> statement to call <see cref="ExitReadLock"/>.
179167 /// One <see cref="ExitReadLock"/> should be called for each <see cref="EnterReadLock"/> or <see cref="TryEnterReadLock"/>.
180168 /// </remarks>
181− public int ExitReadLock()
182− {
183− // Release the semaphore lock and restore the slot
184− return m_concurrencyLock.Release();
185− }
169+ public int ExitReadLock() =>
170+ m_concurrencyLock.Release(); // Release the semaphore lock and restore the slot
186171
187172 /// <summary>
188173 /// Exits write mode.
@@ -191,11 +176,8 @@ namespace GSF.Threading · 2 unmodified lines
191176 /// Upon successful acquisition of a write lock, use the <c>finally</c> block of a <c>try/finally</c> statement to call <see cref="ExitWriteLock"/>.
192177 /// One <see cref="ExitWriteLock"/> should be called for each <see cref="EnterWriteLock"/> or <see cref="TryEnterWriteLock"/>.
193178 /// </remarks>
194− public void ExitWriteLock()
195− {
196− // Release semaphore synchronization mutex lock
197− m_semaphoreLock.ReleaseMutex();
198− }
179+ public void ExitWriteLock() =>
180+ m_semaphoreLock.ReleaseMutex(); // Release semaphore synchronization mutex lock
199181
200182 /// <summary>
201183 /// Tries to enter the lock in read mode, with an optional time-out.
@@ -260,7 +242,6 @@ namespace GSF.Threading · 58 unmodified lines
260242 public bool TryEnterWriteLock(int millisecondsTimeout)
261243 {
262244 bool success = false;
263− long startTime;
264245
265246 try
266247 {
@@ -279,7 +260,7 @@ namespace GSF.Threading · 12 unmodified lines
279260 // At this point no other threads can acquire read or write access since we own the mutex.
280261 // Other threads may be busy reading, so we wait until all semaphore slots become available.
281262 // The only way to get a semaphore slot count is to execute a successful wait and release.
282− startTime = DateTime.UtcNow.Ticks;
263+ long startTime = DateTime.UtcNow.Ticks;
283264
284265 success = m_concurrencyLock.WaitOne(millisecondsTimeout);
285266

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t