|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectse.lth.cs.realtime.event.RTEventBuffer
A thread safe basic queue/buffer for RTEvent
objects.
Essentially a protected bounded buffer with both blocking and non-blocking
methods for posting and fetching messages in terms of references to objects.
For the functionality provided by this basic class, the buffer could have
been typed to contain EventObject
s (the base class of
RTEvent), but to permit subclasses of RTEventBuffer
to utilize
the properties of the RTEvents that class is used here. For instance, one
might need a buffer that keeps track of the total delay of data by referring
to the time-stamps of the newest and oldest RTEvent.
The methods checking or awaiting buffer full/empty are useful for operation supervision, capturing events until a buffer is filled, mode changes, etc. Do not misuse them for polled (concurrently bad) or busy-wait solutions.
While events are stored in the buffer, the buffer is set to be the owner of
the events, see the RTEvent
class. Only events owned by the
current thread are accepted for posting to the buffer. When events are
fetched, the fetching thread is assigned to be the new owner. This is to be
ensured also by subclasses, thereby maintaining the detection of event
producers keeping references to objects already sent. In some cases, however,
it is desirable to resend (typically immutable) objects. Therefore, events
with the owner equal to null
is always accepted. The concept
of owners is from the OSE operating system.
In practice and by default, all methods can be considered being synchronized,
even if the methods as such are not declared to be synchronized. Having the
methods synchronized would agree with common application programming
practice, and together with the fact that most methods are final, those
synchronized methods would remain synchronized is subclasses. However,
synchronization is performed on an internal lock object for the following
reasons:
1) Other object cannot interfere with the internal wait/notify by
synchronizing on an instance of this class (by using a synchronized
statement).
2) An external object (provided via the constructor) can be used as the lock
object, thereby permitting synchronized use of several buffers/object.
If a blocked call is interrupted, an RTInterrupted (subclass of Error, not an exception) is thrown, and that buffer object is most likely not useful anymore.
License
,
RTInterrupted
Nested Class Summary |
Nested classes inherited from class se.lth.cs.realtime.Synchronized |
Synchronized.Lock |
Field Summary | |
protected RTEvent[] |
buffer
The actual buffer. |
protected int |
currSize
Current queue size. |
static int |
DEFAULT_SIZE
|
protected int |
fetchAt
Position of first element to fetch, if any. |
protected Synchronized.Lock |
lock
The lock to this monitor, which can be replaced in constructor. |
protected int |
maxSize
Maximal buffer size. |
protected int |
postAt
Free position to insert events at (the successor of last element) |
Constructor Summary | |
RTEventBuffer()
Initializes the buffer with a size of DEFAULT_SIZE . |
|
RTEventBuffer(int maxSize)
Initializes the queue (buffer) with a given maximal size. |
|
RTEventBuffer(int maxSize,
Synchronized.Lock lock)
Initializes the queue (buffer) with a given maximal size, and with a lock object to be used for synchronization. |
Method Summary | |
abstract int |
currentSize()
The current size of the buffer. |
abstract RTEvent |
doFetch()
Returns the next RTEvent in the queue, blocks if none available. |
abstract void |
doPost(RTEvent e)
Adds an RTEvent to the queue, blocks caller if the queue is full. |
protected void |
error(java.lang.String message)
Throws RTInterrupted and possibly some logging. |
RTEvent |
fetch()
Returns the next event object in the queue, by default by calling doFetch which is synchronized (opposed to this method) via
an internal lock. |
abstract RTEvent[] |
fetchAll()
Get all buffered events leaving the buffer empty. |
abstract void |
flush()
Removes all buffered events. |
abstract int |
getMaxSize()
Gets the maximum size of the buffer. |
abstract boolean |
isEmpty()
Checks if buffer is empty. |
abstract boolean |
isFull()
Checks if buffer is full. |
RTEvent |
post(RTEvent e)
Put the supplied event object last in the queue, by default by calling doPost which is synchronized (opposed to this method). |
abstract RTEvent |
tryFetch()
Returns the next available RTEvent in the queue, or null if the queue is empty. |
abstract RTEvent |
tryFetch(long timeout)
Get the next RTEvent in the queue, or block (up to the specified timeout time) if no event is available. |
abstract RTEvent |
tryPost(RTEvent e)
Adds an EventObject to the queue, without blocking if the queue is full. |
abstract RTEvent |
tryPost(RTEvent e,
long timeout)
Adds an RTEvent to the queue, blocks caller if the queue is full up to the specified timeout time. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
protected RTEvent[] buffer
protected int currSize
public static final int DEFAULT_SIZE
protected int fetchAt
protected Synchronized.Lock lock
protected int maxSize
protected int postAt
Constructor Detail |
public RTEventBuffer()
DEFAULT_SIZE
.
public RTEventBuffer(int maxSize)
maxSize
- maximum queue sizepublic RTEventBuffer(int maxSize, Synchronized.Lock lock)
maxSize
- maximum queue sizelock
- external monitor/object to synchronized withMethod Detail |
public abstract int currentSize()
maxSize
public abstract RTEvent doFetch()
doFetch
in interface RTEventBufferOutput
public abstract void doPost(RTEvent e)
doPost
in interface RTEventBufferInput
e
- the event to enqueueprotected void error(java.lang.String message)
RTInterrupted
and possibly some logging.
public RTEvent fetch()
doFetch
which is synchronized (opposed to this method) via
an internal lock. Subclasses overriding this method should be very
careful to maintain mutual exclusion, normally by simply declaring
fetch
as synchronized
or by synchronizing
on the lock
attribute.
fetch
in interface RTEventBufferOutput
public abstract RTEvent[] fetchAll()
public abstract void flush()
public abstract int getMaxSize()
public abstract boolean isEmpty()
public abstract boolean isFull()
public RTEvent post(RTEvent e)
doPost
which is synchronized (opposed to this method).
Subclasses overriding this method should be very careful to maintain
mutual exclusion, normally by simply declaring post
synchronized or by locking some enclosing monitor/resource.
The return value is not useful for the base-class implementation since
there is nothing to return from calling doPost
.
Therefore, null
is always returned. The intention of the
return value/type is to facilitate the use of tryPost
or
other (for instance GC-saving) subclass implementations.
post
in interface RTEventBufferInput
public abstract RTEvent tryFetch()
tryFetch
in interface RTEventBufferOutput
public abstract RTEvent tryFetch(long timeout)
tryFetch
in interface RTEventBufferOutput
timeout
- the maximum blocking time time in milli-seconds.
public abstract RTEvent tryPost(RTEvent e)
tryPost
in interface RTEventBufferInput
e
- the event to enqueue
public abstract RTEvent tryPost(RTEvent e, long timeout)
tryPost
in interface RTEventBufferInput
e
- the event to enqueuetimeout
- the maximum blocking time time in milli-seconds.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |