-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Mailboxes no longer inherit from `Channel(M)` but use a tailored solution for agent mailboxes that's simpler and with less overall allocations (thanks to Syn::Core).
- Loading branch information
1 parent
cd5d453
commit 89f55f4
Showing
6 changed files
with
124 additions
and
28 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
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,93 @@ | ||
require "syn/core/mutex" | ||
require "syn/core/condition_variable" | ||
require "./errors" | ||
|
||
module Earl | ||
# Earl::Queue is a Channel-like object with a simpler implementation, that | ||
# leverages Syn::Core structs, specifically targetted for Earl mailbox | ||
# requirements. | ||
# | ||
# It's obviously very limited compared to Channel(T). It doesn't support | ||
# `select`, only supports buffered queue (no sync channel with zero capacity), | ||
# but those features aren't needed by Earl mailboxes. | ||
|
||
# :nodoc: | ||
class Queue(M) | ||
property? close_on_stop : Bool = true | ||
property? closed : Bool = false | ||
|
||
def initialize(@backlog = 128) | ||
{% if M == Nil %} | ||
{% raise "Can't create an Earl::Mailbox(M) where M is Nil (or nilable)" %} | ||
{% elsif M.union? && M.union_types.any? { |m| m == Nil } %} | ||
{% raise "Can't create an Earl::Mailbox(M) where M is nilable" %} | ||
{% end %} | ||
|
||
@deque = Deque(M).new(@backlog) | ||
@mutex = Syn::Core::Mutex.new | ||
@readers = Syn::Core::ConditionVariable.new | ||
@writers = Syn::Core::ConditionVariable.new | ||
end | ||
|
||
def send(message : M) : Nil | ||
@mutex.synchronize do | ||
raise ClosedError.new if @closed | ||
|
||
until @deque.size < @backlog | ||
@writers.wait(pointerof(@mutex)) | ||
raise ClosedError.new if @closed | ||
end | ||
|
||
@deque.push(message) | ||
@readers.signal | ||
end | ||
end | ||
|
||
@[AlwaysInline] | ||
def receive : M | ||
do_receive { raise ClosedError.new } | ||
end | ||
|
||
@[AlwaysInline] | ||
def receive? : M? | ||
do_receive { return nil } | ||
end | ||
|
||
private def do_receive(&) : M | ||
@mutex.synchronize do | ||
loop do | ||
if message = @deque.shift? | ||
@writers.signal | ||
return message | ||
end | ||
|
||
yield if @closed | ||
|
||
@readers.wait(pointerof(@mutex)) | ||
end | ||
end | ||
end | ||
|
||
def empty? : Bool | ||
@mutex.synchronize { @deque.empty? } | ||
end | ||
|
||
# def lazy_empty? : Bool | ||
# @deque.empty? | ||
# end | ||
|
||
def close : Nil | ||
return if @closed | ||
|
||
@mutex.synchronize do | ||
@closed = true | ||
@readers.broadcast | ||
@writers.broadcast | ||
end | ||
end | ||
|
||
# def reset : Nil | ||
# @closed = false | ||
# end | ||
end | ||
end |