Package kilim
Class MailboxSPSC<T>
- java.lang.Object
-
- kilim.MailboxSPSC<T>
-
- All Implemented Interfaces:
EventPublisher,PauseReason
public class MailboxSPSC<T> extends java.lang.Object implements PauseReason, EventPublisher
This is a typed buffer that supports single 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
-
-
Field Summary
Fields Modifier and Type Field Description static EventmessageAvailablestatic intMSG_AVAILABLE(package private) SPSCQueue<T>msgs(package private) VolatileReferenceCell<EventSubscriber>sinkstatic intSPACE_AVAILABLEstatic EventspaceAvailble(package private) VolatileReferenceCell<EventSubscriber>srcsstatic intTIMED_OUTstatic EventtimedOut
-
Constructor Summary
Constructors Constructor Description MailboxSPSC()MailboxSPSC(int initialSize)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddMsgAvailableListener(EventSubscriber msgSub)voidaddSpaceAvailableListener(EventSubscriber spcSub)voidclear()booleanfill(EventSubscriber eo, T[] msg)Non-blocking, nonpausing fill.voidfill(T[] msg)Pausable fill Pause the caller until at least one message is available.Tget()Tget(long timeoutMillis)Tget(EventSubscriber eo)Non-blocking, nonpausing get.Tgetnb()Get, don't pause or block.private EventSubscribergetProducer()private longgetSize()booleanisValid(Task t)True if the given task's reason for pausing is still valid.voidput(T msg)put a non-null message in the mailbox, and pause the calling task until the mailbox has spacevoidput(T[] buf)put a non-null messages from buffer in the mailbox, and pause the calling task until all the messages put in the mailboxbooleanput(T msg, int timeoutMillis)put a non-null message in the mailbox, and pause the calling task for timeoutMillis if the mailbox is full.booleanput(T msg, EventSubscriber eo)booleanputnb(T msg)Attempt to put a message, and return true if successful.voidremoveMsgAvailableListener(EventSubscriber msgSub)voidremoveSpaceAvailableListener(EventSubscriber spcSub)private booleansrcContains(Task t)voidsubscribe(boolean avail, Task t, boolean set)java.lang.StringtoString()
-
-
-
Field Detail
-
sink
final VolatileReferenceCell<EventSubscriber> sink
-
srcs
final VolatileReferenceCell<EventSubscriber> srcs
-
SPACE_AVAILABLE
public static final int SPACE_AVAILABLE
- See Also:
- Constant Field Values
-
MSG_AVAILABLE
public static final int MSG_AVAILABLE
- See Also:
- Constant Field Values
-
TIMED_OUT
public static final int TIMED_OUT
- See Also:
- Constant Field Values
-
spaceAvailble
public static final Event spaceAvailble
-
messageAvailable
public static final Event messageAvailable
-
timedOut
public static final Event timedOut
-
-
Method Detail
-
fill
public boolean fill(EventSubscriber eo, T[] msg)
Non-blocking, nonpausing fill.- Parameters:
eo- . If non-null, registers this observer and calls it with a MessageAvailable event when a put() is done.- Returns:
- buffered true if there's one, or up to burst size messages else false
-
put
public void put(T[] buf) throws Pausable
put a non-null messages from buffer in the mailbox, and pause the calling task until all the messages put in the mailbox- Throws:
Pausable
-
get
public T get(EventSubscriber eo)
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
public boolean put(T msg, EventSubscriber eo)
-
getnb
public T getnb()
Get, don't pause or block.- Returns:
- stored message, or null if no message found.
-
get
public T get(long timeoutMillis) throws Pausable
- Returns:
- non-null message, or null if timed out.
- Throws:
Pausable
-
putnb
public boolean putnb(T msg)
Attempt to put a message, and return true if successful. The thread is not blocked, nor is the task paused under any circumstance.
-
addSpaceAvailableListener
public void addSpaceAvailableListener(EventSubscriber spcSub)
-
removeSpaceAvailableListener
public void removeSpaceAvailableListener(EventSubscriber spcSub)
-
addMsgAvailableListener
public void addMsgAvailableListener(EventSubscriber msgSub)
-
removeMsgAvailableListener
public void removeMsgAvailableListener(EventSubscriber msgSub)
-
getProducer
private EventSubscriber getProducer()
-
srcContains
private boolean srcContains(Task t)
-
getSize
private long getSize()
-
put
public void put(T msg) throws Pausable
put a non-null message in the mailbox, and pause the calling task until the mailbox has space- Throws:
Pausable
-
put
public boolean put(T msg, int timeoutMillis) throws Pausable
put a non-null message in the mailbox, and pause the calling task for timeoutMillis if the mailbox is full.- Throws:
Pausable
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
clear
public void clear()
-
isValid
public boolean isValid(Task t)
Description copied from interface:PauseReasonTrue if the given task's reason for pausing is still valid.- Specified by:
isValidin interfacePauseReason
-
fill
public void fill(T[] msg) throws Pausable
Pausable fill Pause the caller until at least one message is available.- Throws:
Pausable
-
-