Skip to content

Commit 46c27c4

Browse files
committed
Moved mutex unlocking into ensure clauses.
1 parent 9bc1c89 commit 46c27c4

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

lib/concurrent/atomic/copy_on_notify_observer_set.rb

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,40 +33,38 @@ def add_observer(observer=nil, func=:update, &block)
3333
begin
3434
@mutex.lock
3535
@observers[observer] = func
36+
observer
3637
ensure
3738
@mutex.unlock
3839
end
39-
40-
observer
4140
end
4241

4342
# @param [Object] observer the observer to remove
4443
# @return [Object] the deleted observer
4544
def delete_observer(observer)
4645
@mutex.lock
4746
@observers.delete(observer)
48-
@mutex.unlock
49-
5047
observer
48+
ensure
49+
@mutex.unlock
5150
end
5251

5352
# Deletes all observers
5453
# @return [CopyOnWriteObserverSet] self
5554
def delete_observers
5655
@mutex.lock
5756
@observers.clear
58-
@mutex.unlock
59-
6057
self
58+
ensure
59+
@mutex.unlock
6160
end
6261

6362
# @return [Integer] the observers count
6463
def count_observers
6564
@mutex.lock
66-
result = @observers.count
65+
@observers.count
66+
ensure
6767
@mutex.unlock
68-
69-
result
7068
end
7169

7270
# Notifies all registered observers with optional args
@@ -75,7 +73,6 @@ def count_observers
7573
def notify_observers(*args, &block)
7674
observers = duplicate_observers
7775
notify_to(observers, *args, &block)
78-
7976
self
8077
end
8178

@@ -86,7 +83,6 @@ def notify_observers(*args, &block)
8683
def notify_and_delete_observers(*args, &block)
8784
observers = duplicate_and_clear_observers
8885
notify_to(observers, *args, &block)
89-
9086
self
9187
end
9288

@@ -96,17 +92,17 @@ def duplicate_and_clear_observers
9692
@mutex.lock
9793
observers = @observers.dup
9894
@observers.clear
99-
@mutex.unlock
100-
10195
observers
96+
ensure
97+
@mutex.unlock
10298
end
10399

104100
def duplicate_observers
105101
@mutex.lock
106102
observers = @observers.dup
107-
@mutex.unlock
108-
109103
observers
104+
ensure
105+
@mutex.unlock
110106
end
111107

112108
def notify_to(observers, *args)

0 commit comments

Comments
 (0)