Package kilim
Class Mailbox<T>
java.lang.Object
kilim.Mailbox<T>
- All Implemented Interfaces:
EventPublisher,PauseReason
This is a typed buffer that supports multiple producers and a single
consumer. It is the basic construct used for tasks to interact and
synchronize with each other (as opposed to direct java calls or static member
variables). put() and get() are the two essential functions.
We use the term "block" to mean thread block, and "pause" to mean
fiber pausing. The suffix "nb" on some methods (such as getnb())
stands for non-blocking. Both put() and get() have blocking and
non-blocking variants in the form of putb(), putnb
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate intprivate intprivate intstatic final Eventstatic final int(package private) T[]private int(package private) EventSubscriberstatic final intstatic final Event(package private) LinkedList<EventSubscriber> static final intstatic final Event -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidvoidget()get(long timeoutMillis) get(EventSubscriber eo) Non-blocking, nonpausing get.getb()retrieve a message, blocking the thread indefinitely.getb(long timeoutMillis) retrieve a msg, and block the Java thread for the time given.getnb()Get, don't pause or block.booleanbooleanhasMessage(Task eo) booleanhasMessages(int num, Task eo) booleanhasSpace()booleanTrue if the given task's reason for pausing is still valid.Object[]messages()peek(int idx) voidput a non-null message in the mailbox, and pause the calling task until the mailbox has spacebooleanput a non-null message in the mailbox, and pause the calling task for timeoutMillis if the mailbox is full.booleanput(T msg, EventSubscriber eo) Non-blocking, nonpausing put.voidbooleanput a non-null message in the mailbox, and block the calling thread for timeoutMillis if the mailbox is fullbooleanAttempt to put a message, and return true if successful.remove(int idx) voidvoidstatic intTakes an array of mailboxes and returns the index of the first mailbox that has a message.intsize()toString()voidBlock caller until at least one message is available.voiduntilHasMessages(int num) Block caller untilnummessages are available.
-
Field Details
-
msgs
T[] msgs -
iprod
private int iprod -
icons
private int icons -
numMsgs
private int numMsgs -
maxMsgs
private int maxMsgs -
sink
EventSubscriber sink -
SPACE_AVAILABLE
public static final int SPACE_AVAILABLE- See Also:
-
MSG_AVAILABLE
public static final int MSG_AVAILABLE- See Also:
-
TIMED_OUT
public static final int TIMED_OUT- See Also:
-
spaceAvailble
-
messageAvailable
-
timedOut
-
srcs
LinkedList<EventSubscriber> srcs
-
-
Constructor Details
-
Mailbox
public Mailbox() -
Mailbox
public Mailbox(int initialSize) -
Mailbox
public Mailbox(int initialSize, int maxSize)
-
-
Method Details
-
get
Non-blocking, nonpausing get.- Parameters:
eo- . If non-null, registers this observer and calls it with a MessageAvailable event when a put() is done.- Returns:
- buffered message if there's one, or null
-
put
Non-blocking, nonpausing put.- Parameters:
eo- . If non-null, registers this observer and calls it with an SpaceAvailable event when there's space.- Returns:
- buffered message if there's one, or null
- See Also:
-
getnb
Get, don't pause or block.- Returns:
- stored message, or null if no message found.
-
get
- Returns:
- non-null message.
- Throws:
Pausable
-
get
- Returns:
- non-null message, or null if timed out.
- Throws:
Pausable
-
untilHasMessage
Block caller until at least one message is available.- Throws:
Pausable
-
untilHasMessages
Block caller untilnummessages are available.- Parameters:
num-- Throws:
Pausable
-
hasMessage
-
hasMessages
-
peek
-
remove
-
messages
-
select
Takes an array of mailboxes and returns the index of the first mailbox that has a message. It is possible that because of race conditions, an earlier mailbox in the list may also have received a message.- Throws:
Pausable
-
addSpaceAvailableListener
-
removeSpaceAvailableListener
-
addMsgAvailableListener
-
removeMsgAvailableListener
-
putnb
Attempt to put a message, and return true if successful. The thread is not blocked, nor is the task paused under any circumstance. -
put
put a non-null message in the mailbox, and pause the calling task until the mailbox has space- Throws:
Pausable
-
put
put a non-null message in the mailbox, and pause the calling task for timeoutMillis if the mailbox is full.- Throws:
Pausable
-
putb
-
putb
put a non-null message in the mailbox, and block the calling thread for timeoutMillis if the mailbox is full- Returns:
- true if the message was successfully put in the mailbox
-
size
public int size() -
hasMessage
public boolean hasMessage() -
hasSpace
public boolean hasSpace() -
getb
retrieve a message, blocking the thread indefinitely. Note, this is a heavyweight block, unlike #get() that pauses the Fiber but doesn't block the thread. -
getb
retrieve a msg, and block the Java thread for the time given.- Parameters:
millis- . max wait time- Returns:
- null if timed out.
-
toString
-
isValid
Description copied from interface:PauseReasonTrue if the given task's reason for pausing is still valid.- Specified by:
isValidin interfacePauseReason
-