-
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 facilitates error handling during resource allocation. A resource allocation procedure can be wrapped with AutoHCK::ResourceScope.open, and end with AutoHCK::ResourceScope#move_to call with the outer scope as an argument; if an error happens during the allocation, resources already allocated will be safely deallocated. If no error occurs, the allocated resources will escape the inner scope with AutoHCK::ResourceScope#move_to call. Signed-off-by: Akihiko Odaki <[email protected]>
- Loading branch information
1 parent
f55b65d
commit 1b276f3
Showing
17 changed files
with
334 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,41 @@ | ||
# frozen_string_literal: true | ||
|
||
require './lib/exceptions' | ||
|
||
module AutoHCK | ||
# ResourceScope is a class that manages objects with close methods. | ||
class ResourceScope | ||
def initialize | ||
@resources = [] | ||
end | ||
|
||
def <<(resource) | ||
@resources << resource | ||
end | ||
|
||
def close | ||
@resources.reverse_each(&:close) | ||
@resources.clear | ||
end | ||
|
||
def concat(...) | ||
@resources.concat(...) | ||
end | ||
|
||
def move_to(another) | ||
another.concat @resources | ||
@resources.clear | ||
end | ||
|
||
def self.open | ||
scope = new | ||
Thread.handle_interrupt(AutoHCKInterrupt => :never) do | ||
Thread.handle_interrupt(AutoHCKInterrupt => :on_blocking) do | ||
yield scope | ||
end | ||
ensure | ||
scope.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.