[r322] Bug 259107 - [console] Console Deadlock when too much information written
diff --git a/org.eclipse.ui.console/META-INF/MANIFEST.MF b/org.eclipse.ui.console/META-INF/MANIFEST.MF
index 1aa8894..91f49bd 100644
--- a/org.eclipse.ui.console/META-INF/MANIFEST.MF
+++ b/org.eclipse.ui.console/META-INF/MANIFEST.MF
@@ -2,7 +2,7 @@
 Bundle-ManifestVersion: 2
 Bundle-Name: %pluginName
 Bundle-SymbolicName: org.eclipse.ui.console; singleton:=true
-Bundle-Version: 3.1.100.qualifier
+Bundle-Version: 3.1.101.qualifier
 Bundle-Activator: org.eclipse.ui.console.ConsolePlugin
 Bundle-Vendor: %providerName
 Bundle-Localization: plugin
diff --git a/org.eclipse.ui.console/src/org/eclipse/ui/internal/console/IOConsolePartitioner.java b/org.eclipse.ui.console/src/org/eclipse/ui/internal/console/IOConsolePartitioner.java
index 7d8acd2..a38516d 100644
--- a/org.eclipse.ui.console/src/org/eclipse/ui/internal/console/IOConsolePartitioner.java
+++ b/org.eclipse.ui.console/src/org/eclipse/ui/internal/console/IOConsolePartitioner.java
@@ -30,6 +30,7 @@
 import org.eclipse.jface.text.ITypedRegion;
 import org.eclipse.jface.text.Region;
 import org.eclipse.swt.custom.StyleRange;
+import org.eclipse.swt.widgets.Display;
 import org.eclipse.ui.console.ConsolePlugin;
 import org.eclipse.ui.console.IConsoleDocumentPartitioner;
 import org.eclipse.ui.console.IOConsole;
@@ -462,11 +463,19 @@
                 }
 			}
             
-            if (fBuffer > 160000) { 
-                try {
-                    pendingPartitions.wait();
-                } catch (InterruptedException e) {
-                }
+            if (fBuffer > 160000) {
+            	if(Display.getCurrent() == null){
+					try {
+						pendingPartitions.wait();
+					} catch (InterruptedException e) {
+					}
+            	} else {
+					/*
+					 * if we are in UI thread we cannot lock it, so process
+					 * queued output.
+					 */
+            		processQueue();
+            	}
             }
 		}
 	}
@@ -506,47 +515,7 @@
          * @see org.eclipse.core.internal.jobs.InternalJob#run(org.eclipse.core.runtime.IProgressMonitor)
          */
         public IStatus runInUIThread(IProgressMonitor monitor) {
-        	synchronized (overflowLock) {
-        		ArrayList pendingCopy = new ArrayList();
-        		StringBuffer buffer = null;
-        		boolean consoleClosed = false;
-        		while (pendingPartitions.size() > 0) {
-        			synchronized(pendingPartitions) {
-        				pendingCopy.addAll(pendingPartitions);
-        				pendingPartitions.clear();
-        				fBuffer = 0;
-        				pendingPartitions.notifyAll();
-        			}
-
-        			buffer = new StringBuffer();
-        			for (Iterator i = pendingCopy.iterator(); i.hasNext(); ) {
-        				PendingPartition pp = (PendingPartition) i.next();
-        				if (pp != consoleClosedPartition) { 
-        					buffer.append(pp.text);
-        				} else {
-        					consoleClosed = true;
-        				}
-        			}
-        		}
-
-        		if (connected) {
-        			setUpdateInProgress(true);
-        			updatePartitions = pendingCopy;
-        			firstOffset = document.getLength();
-        			try {
-        				document.replace(firstOffset, 0, buffer.toString());
-        			} catch (BadLocationException e) {
-        			}
-        			updatePartitions = null;
-        			setUpdateInProgress(false);
-        		}
-        		if (consoleClosed) {
-        			console.partitionerFinished();
-        		}
-        		checkBufferSize();
-
-        	}
-
+        	processQueue();
         	return Status.OK_STATUS;
         }        
 		
@@ -564,7 +533,55 @@
 	}
 
  
-    
+	void processQueue() {
+    	synchronized (overflowLock) {
+    		ArrayList pendingCopy = new ArrayList();
+    		StringBuffer buffer = null;
+    		boolean consoleClosed = false;
+    		while (pendingPartitions.size() > 0) {
+    			synchronized(pendingPartitions) {
+    				pendingCopy.addAll(pendingPartitions);
+    				pendingPartitions.clear();
+    				fBuffer = 0;
+    				pendingPartitions.notifyAll();
+    			}
+    			// determine buffer size
+    			int size = 0;
+    			for (Iterator i = pendingCopy.iterator(); i.hasNext(); ) {
+    				PendingPartition pp = (PendingPartition) i.next();
+    				if (pp != consoleClosedPartition) { 
+    					size+= pp.text.length();
+    				} 
+    			}
+    			buffer = new StringBuffer(size);
+    			for (Iterator i = pendingCopy.iterator(); i.hasNext(); ) {
+    				PendingPartition pp = (PendingPartition) i.next();
+    				if (pp != consoleClosedPartition) { 
+    					buffer.append(pp.text);
+    				} else {
+    					consoleClosed = true;
+    				}
+    			}
+    		}
+
+    		if (connected) {
+    			setUpdateInProgress(true);
+    			updatePartitions = pendingCopy;
+    			firstOffset = document.getLength();
+    			try {
+    				document.replace(firstOffset, 0, buffer.toString());
+    			} catch (BadLocationException e) {
+    			}
+    			updatePartitions = null;
+    			setUpdateInProgress(false);
+    		}
+    		if (consoleClosed) {
+    			console.partitionerFinished();
+    		}
+    		checkBufferSize();
+    	}
+
+	}