[libinline-java-perl] 367/398: implementation of new callback interface

Jonas Smedegaard dr at jones.dk
Thu Feb 26 11:43:25 UTC 2015


This is an automated email from the git hooks/post-receive script.

js pushed a commit to tag 0.55
in repository libinline-java-perl.

commit dd2da4e559bfe1683c016b53fe2ca8ad29952cfd
Author: patrick_leb <>
Date:   Sat Jan 21 14:31:19 2006 +0000

    implementation of new callback interface
---
 .../perl/inline/java/InlineJavaCallbackQueue.java  | 109 +++++++++++++++++----
 1 file changed, 92 insertions(+), 17 deletions(-)

diff --git a/Java/sources/org/perl/inline/java/InlineJavaCallbackQueue.java b/Java/sources/org/perl/inline/java/InlineJavaCallbackQueue.java
index de57ef5..a1d7ab1 100644
--- a/Java/sources/org/perl/inline/java/InlineJavaCallbackQueue.java
+++ b/Java/sources/org/perl/inline/java/InlineJavaCallbackQueue.java
@@ -10,57 +10,132 @@ import java.io.* ;
 class InlineJavaCallbackQueue {
 	// private InlineJavaServer ijs = InlineJavaServer.GetInstance() ;
 	private ArrayList queue = new ArrayList() ;
-	private boolean stop_loop = false ;
+	private boolean wait_interrupted = false ;
+	private boolean stream_opened = false ;
 
 
-	InlineJavaCallbackQueue() {
+	InlineJavaCallbackQueue(){
 	}
 
 
-	synchronized void EnqueueCallback(InlineJavaCallback ijc) {
+	synchronized void EnqueueCallback(InlineJavaCallback ijc){
 		queue.add(ijc) ;
 		notify() ;
 	}
 
 
-	synchronized private InlineJavaCallback DequeueCallback() {
-		if (queue.size() > 0){
+	synchronized private InlineJavaCallback DequeueCallback(){
+		if (GetSize() > 0){
 			return (InlineJavaCallback)queue.remove(0) ;
 		}
 		return null ;
 	}
 
 
-	synchronized InlineJavaCallback WaitForCallback(){
-		while ((! stop_loop)&&(IsEmpty())){
+    synchronized int WaitForCallback(double timeout){
+        long secs = (long)Math.floor(timeout) ;
+        double rest = timeout - ((double)secs) ;
+        long millis = (long)Math.floor(rest * 1000.0) ;
+        rest = (rest * 1000.0) - ((double)millis) ;
+        int nanos = (int)Math.floor(rest * 1000000.0) ;
+
+        return WaitForCallback((secs * 1000) + millis, nanos) ;
+    }
+
+
+	/*
+		Blocks up to the specified time for the next callback to arrive.
+		Returns -1 if the wait was interrupted voluntarily, 0 on timeout or
+		> 0 if a callback has arrived before the timeout expired.
+	*/
+	synchronized int WaitForCallback(long millis, int nanos){
+		wait_interrupted = false ;
+		Thread t = Thread.currentThread() ;
+		InlineJavaUtils.debug(3, "waiting for callback request (" + millis + " millis, " + 
+			nanos + " nanos) in " + t.getName() + "...") ;
+
+		if (! stream_opened){
+			return -1 ;
+		}
+
+		while ((stream_opened)&&(! wait_interrupted)&&(IsEmpty())){
 			try {
-				wait() ;
+				wait(millis, nanos) ;
+				// If we reach this code, it means the either we timed out
+				// or that we were notify()ed. 
+				// In the former case, we must break out and return 0.
+				// In the latter case, either the queue will not be empty or 
+                // wait_interrupted will be set. We must therefore also break out.
+				break ;
 			}
 			catch (InterruptedException ie){
 				// Do nothing, return and wait() some more...
 			}
 		}
-		return DequeueCallback() ;
+		InlineJavaUtils.debug(3, "waiting for callback request finished " + t.getName() + "...") ;
+
+		if (wait_interrupted){
+			return -1 ;
+		}
+		else {
+			return GetSize() ;
+		}
+	}
+
+
+	/*
+		Waits indefinetely for the next callback to arrive and executes it.
+		Return true on success of false if the wait was interrupted voluntarily.
+	*/
+	synchronized boolean ProcessNextCallback() throws InlineJavaException, InlineJavaPerlException {
+		int rc = WaitForCallback(0, 0) ;
+		if (rc == -1){
+			// Wait was interrupted
+			return false ;
+		}
+
+		// DequeueCallback can't return null because we explicetely
+		// waited until a callback was there.
+		Thread t = Thread.currentThread() ;
+		InlineJavaUtils.debug(3, "processing callback request in " + t.getName() + "...") ;
+		InlineJavaCallback ijc = DequeueCallback() ;
+		ijc.Process() ;
+		ijc.NotifyOfResponse(t) ;
+
+		return true ;
 	}
 
 
 	private boolean IsEmpty(){
-		return (queue.size() == 0) ;
+		return (GetSize() == 0) ;
 	}
 
 
-	void StartLoop(){
-		stop_loop = false ;
+	void OpenCallbackStream(){
+		stream_opened = true ;
 	}
 
 
-	synchronized void StopLoop(){
-		stop_loop = true ;
-		notify() ;
+	synchronized void CloseCallbackStream(){
+		stream_opened = false ;
+		InterruptWaitForCallback() ;
 	}
 
 
-	boolean IsLoopStopped(){
-		return stop_loop ;
+	boolean IsStreamOpen(){
+		return stream_opened ;
+	}
+
+
+	int GetSize(){
+		return queue.size() ;
+	}
+
+
+	synchronized private void InterruptWaitForCallback(){
+		Thread t = Thread.currentThread() ;
+		InlineJavaUtils.debug(3, "interrupting wait for callback request in " + t.getName() + "...") ;
+		wait_interrupted = true ;
+		notify() ;
 	}
 }

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-perl/packages/libinline-java-perl.git



More information about the Pkg-perl-cvs-commits mailing list