Add the other cases where we need to know if we're on the main thread

or not.
This commit is contained in:
Kristian S. Stangeland 2012-09-29 23:52:28 +02:00
parent 8a26d047b2
commit 63197bbbd5
3 changed files with 21 additions and 5 deletions

View File

@ -143,7 +143,9 @@ public class AsyncFilterManager implements AsynchronousManager {
// Start the process
getSendingQueue(syncPacket).enqueue(newEvent);
getProcessingQueue(syncPacket).enqueue(newEvent);
// We know this is occuring on the main thread, to pass TRUE
getProcessingQueue(syncPacket).enqueue(newEvent, true);
}
@Override

View File

@ -53,14 +53,15 @@ class PacketProcessingQueue extends AbstractConcurrentListenerMultimap<AsyncList
/**
* Enqueue a packet for processing by the asynchronous listeners.
* @param packet - packet to process.
* @param onMainThread - whether or not this is occuring on the main thread.
* @return TRUE if we sucessfully queued the packet, FALSE if the queue ran out if space.
*/
public boolean enqueue(PacketEvent packet) {
public boolean enqueue(PacketEvent packet, boolean onMainThread) {
try {
processingQueue.add(packet);
// Begin processing packets
signalBeginProcessing();
signalBeginProcessing(onMainThread);
return true;
} catch (IllegalStateException e) {
return false;
@ -69,8 +70,9 @@ class PacketProcessingQueue extends AbstractConcurrentListenerMultimap<AsyncList
/**
* Called by the current method and each thread to signal that a packet might be ready for processing.
* @param onMainThread - whether or not this is occuring on the main thread.
*/
public void signalBeginProcessing() {
public void signalBeginProcessing(boolean onMainThread) {
while (concurrentProcessing.tryAcquire()) {
PacketEvent packet = processingQueue.poll();
@ -91,7 +93,7 @@ class PacketProcessingQueue extends AbstractConcurrentListenerMultimap<AsyncList
}
// The packet has no further listeners. Just send it.
sendingQueue.signalPacketUpdate(packet);
sendingQueue.signalPacketUpdate(packet, onMainThread);
signalProcessingDone();
} else {

View File

@ -23,6 +23,10 @@ class PacketSendingQueue {
// Whether or not packet transmission can only occur on the main thread
private final boolean synchronizeMain;
/**
* Create a packet sending queue.
* @param synchronizeMain - whether or not to synchronize with the main thread.
*/
public PacketSendingQueue(boolean synchronizeMain) {
this.synchronizeMain = synchronizeMain;
this.sendingQueue = new PriorityBlockingQueue<PacketEvent>(INITIAL_CAPACITY, new Comparator<PacketEvent>() {
@ -46,6 +50,8 @@ class PacketSendingQueue {
/**
* Invoked when one of the packets have finished processing.
* @param packetUpdated - the packet that has now been updated.
* @param onMainThread - whether or not this is occuring on the main thread.
*/
public synchronized void signalPacketUpdate(PacketEvent packetUpdated, boolean onMainThread) {
// Mark this packet as finished
@ -53,6 +59,11 @@ class PacketSendingQueue {
trySendPackets(onMainThread);
}
/***
* Invoked when a list of packet IDs are no longer associated with any listeners.
* @param packetsRemoved - packets that no longer have any listeners.
* @param onMainThread - whether or not this is occuring on the main thread.
*/
public synchronized void signalPacketUpdate(List<Integer> packetsRemoved, boolean onMainThread) {
Set<Integer> lookup = new HashSet<Integer>(packetsRemoved);
@ -70,6 +81,7 @@ class PacketSendingQueue {
/**
* Attempt to send any remaining packets.
* @param onMainThread - whether or not this is occuring on the main thread.
*/
public void trySendPackets(boolean onMainThread) {