Skip to content

Commit

Permalink
Fix hooks_map initialization to be fully thread-safe
Browse files Browse the repository at this point in the history
Fixes issue described here: ruby-concurrency/concurrent-ruby#970
  • Loading branch information
mensfeld authored and piotrmurach committed Oct 8, 2023
1 parent 0d0f08b commit bed686e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
by Vadim Kononov(@vkononov)
* Fix message queue shutdown to raise valid error
* Fix any_event and any_state methods in the define method and definition class
* Fix hooks_map initialization to be fully thread-safe
by Maciej Mensfeld(@mensfeld)

## [v0.14.0] - 2020-09-12

Expand Down
11 changes: 8 additions & 3 deletions lib/finite_machine/hooks.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require "concurrent/array"
require "concurrent/map"

require_relative "hook_event"
Expand All @@ -12,13 +13,17 @@ class Hooks
# Initialize a hooks_map of hooks
#
# @example
# Hoosk.new(machine)
# Hooks.new
#
# @api public
def initialize
@hooks_map = Concurrent::Map.new do |events_hash, hook_event|
events_hash[hook_event] = Concurrent::Map.new do |state_hash, name|
state_hash[name] = []
events_hash.compute_if_absent(hook_event) do
Concurrent::Map.new do |state_hash, name|
state_hash.compute_if_absent(name) do
Concurrent::Array.new
end
end
end
end
end
Expand Down

0 comments on commit bed686e

Please sign in to comment.