-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Before this change, the management of resources that require explicit deallocations were done ad-hoc and inconsistent. This new class provides a unified mechanism for this purpose and ensures the following: - Resource deallocation happens in reverse-allocation order. This is important because an object often depend on another object allocated earlier and cannot outlive the earlier object. - Interrupts during deallocation will be postponed. (Ensured with Thread.handle_interrupt(Object => :never)) - An interrupt won't be handled while an allocated object is assigned to a AutoHCK::ResourceScope. (Ensured with Thread.handle_interrupt(Object => :on_blocking)) Moreover, this class provides transaction feature that facilitates error handling during resource allocation. If an error happens in a transaction, resources assigned to the transaction scope will be safely deallocated. Otherwise, they will escape the transaction scope and be available to the outer scope. Signed-off-by: Akihiko Odaki <[email protected]>
- Loading branch information
1 parent
f55b65d
commit 4b59a08
Showing
17 changed files
with
328 additions
and
372 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# frozen_string_literal: true | ||
|
||
require './lib/exceptions' | ||
|
||
module AutoHCK | ||
# ResourceScope is a class that manages objects with close methods. | ||
class ResourceScope | ||
def initialize(resources) | ||
@resources = resources | ||
end | ||
|
||
def <<(resource) | ||
@resources << resource | ||
self | ||
end | ||
|
||
def transaction | ||
resources = [] | ||
self.class.open(resources) do |scope| | ||
result = yield(scope) | ||
@resources.concat resources | ||
resources.clear | ||
result | ||
end | ||
end | ||
|
||
def self.open(resources = []) | ||
scope = new(resources) | ||
Thread.handle_interrupt(AutoHCKInterrupt => :never) do | ||
Thread.handle_interrupt(AutoHCKInterrupt => :on_blocking) do | ||
yield scope | ||
end | ||
ensure | ||
resources.reverse_each(&:close) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.