[concurrency-interest] Queue Predicate

Jean Morissette jean.morissette666@videotron.ca
Wed, 08 Dec 2004 01:07:30 -0500


Hi,
I actually contribute to the development of Sandstorm, a Staged 
Event-Driven Architecture (SEDA) based high-performance platform for 
services http://www.eecs.harvard.edu/~mdw/proj/seda/

Presently, each services (stage) has its own queue and a queue can be 
associated with a predicate.  The enqueue operation succeed only if the 
predicate accept the element that we want to enqueue.  There are 
actually two type of predicates: QueueThresholdPredicate and 
RateLimitingPredicate.

But now, I want to add blocking enqueue operations and I don't know how 
to integrate the predicate with the blocking queues implementations.  I 
also want to unify the two types of predicate.

Here is an example of a method enqueue that don't use the predicate API:

public boolean enqueue_lossy(QueueElementIF x, int msecs)
     throws InterruptedException {
   if (x == null) throw new IllegalArgumentException();
   if (Thread.interrupted()) throw new InterruptedException();
   synchronized (putMonitor) {
     long start = (msecs <= 0) ? 0 : System.currentTimeMillis();
     long waitTime = msecs;
     while (emptySlots <= 0) {
       if (waitTime <= 0) return false;
       ++waitingPuts;
       try {
         putMonitor.wait(waitTime);
       } catch (InterruptedException ex) {
         putMonitor.notify();
         throw ex;
       } finally {
         --waitingPuts;
       }
       waitTime = msecs - (System.currentTimeMillis() - start);
     }
     insert(x);
   }
   incUsedSlots();
   return true;
}


Should be something maybe like:

public boolean enqueue_lossy(QueueElementIF x, int msecs)
     throws InterruptedException {
   if (x == null) throw new IllegalArgumentException();
   if (Thread.interrupted()) throw new InterruptedException();
   predicate.blockingAcceptAcquire(x, msecs);
   insert(x);
   predicate.blockingAcceptRelease();
   incUsedSlots();
   return true;
}


So, I'm wondering if we could implement, in a elegant way, the 
predicates like a Semaphore?

Thanks for your advices

Jean