Skip to content

Commit

Permalink
Hide Channel internal implementation methods
Browse files Browse the repository at this point in the history
  • Loading branch information
j8r committed Jul 2, 2020
1 parent 2f8092c commit 37b055b
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/channel.cr
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@ require "crystal/pointer_linked_list"
class Channel(T)
@lock = Crystal::SpinLock.new
@queue : Deque(T)?


# :nodoc:
record NotReady
# :nodoc:
record UseDefault


# :nodoc:
module SelectAction(S)
abstract def execute : DeliveryState
abstract def wait(context : SelectContext(S))
Expand Down Expand Up @@ -273,19 +276,19 @@ class Channel(T)
# end
# channel.receive # => 1
# ```
def receive
def receive : T
receive_impl { raise ClosedError.new }
end

# Receives a value from the channel.
# If there is a value waiting, it is returned immediately. Otherwise, this method blocks until a value is sent to the channel.
#
# Returns `nil` if the channel is closed or closes while waiting for receive.
def receive?
def receive? : T?
receive_impl { return nil }
end

def receive_impl
private def receive_impl
receiver = Receiver(T).new

@lock.lock
Expand Down Expand Up @@ -318,7 +321,7 @@ class Channel(T)
end
end

def receive_internal
protected def receive_internal
if (queue = @queue) && !queue.empty?
deque_value = queue.shift
if sender_ptr = dequeue_sender
Expand Down Expand Up @@ -422,7 +425,7 @@ class Channel(T)
select_impl(ops, true)
end

def self.select_impl(ops : Indexable(SelectAction), non_blocking)
private def self.select_impl(ops : Indexable(SelectAction), non_blocking)
# Sort the operations by the channel they contain
# This is to avoid deadlocks between concurrent `select` calls
ops_locks = ops
Expand Down

0 comments on commit 37b055b

Please sign in to comment.