actors :: Mailbox :: defaultinit
# A Blocking queue implemented from a `ConcurrentList`
# `shift` is blocking if there isn't any element in `self`
# `push` or `unshift` releases every blocking threads
# Corresponds to the mailbox of an actor
class Mailbox[E]
super BlockingQueue[E]
# The associated actor
var actor: Actor is noautoinit
# init self with an associated actor
init with_actor(actor: Actor) do
self.actor = actor
sys.active_actors.increment
end
# Adding the signal to release eventual waiting thread(s)
redef fun push(e) do
mutex.lock
if real_collection.is_empty and not actor.working then
actor.working = true
sys.active_actors.increment
real_collection.push(e)
self.cond.signal
else
real_collection.push(e)
end
mutex.unlock
end
redef fun unshift(e) do
mutex.lock
real_collection.unshift(e)
self.cond.signal
mutex.unlock
end
# If empty, blocks until an item is inserted with `push` or `unshift`
redef fun shift do
mutex.lock
if real_collection.is_empty then
actor.working = false
sys.active_actors.decrement
while real_collection.is_empty do self.cond.wait(mutex)
end
var r = real_collection.shift
mutex.unlock
return r
end
redef fun is_empty do
mutex.lock
var r = real_collection.is_empty
mutex.unlock
return r
end
end
lib/actors/actors.nit:62,1--118,3