Revert "Bug 501997 - Initialize the workspace in a background job"

The job.join method the original change introduced is unsafe due to suspension of the job manager during Eclipse startup. 

This reverts commit 8d90fb030df310a974aae5d27d0bc2610c1a14ad.

Change-Id: I59497601821543bf53ae611db8789c5a1d39cdfb
diff --git a/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/Workspace.java b/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/Workspace.java
index a074078..5faf3dc 100644
--- a/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/Workspace.java
+++ b/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/Workspace.java
@@ -2152,10 +2152,10 @@
 		// create root location
 		localMetaArea.locationFor(getRoot()).toFile().mkdirs();
 
-		SubMonitor subMonitor = SubMonitor.convert(monitor, 100);
-		startup(subMonitor.split(95));
+		IProgressMonitor nullMonitor = Policy.monitorFor(null);
+		startup(nullMonitor);
 		//restart the notification manager so it is initialized with the right tree
-		notificationManager.startup(subMonitor.split(5));
+		notificationManager.startup(null);
 		openFlag = true;
 		if (crashed || refreshRequested()) {
 			try {
@@ -2376,42 +2376,41 @@
 	 * Starts all the workspace manager classes.
 	 */
 	protected void startup(IProgressMonitor monitor) throws CoreException {
-		SubMonitor subMonitor = SubMonitor.convert(monitor, 14);
 		// ensure the tree is locked during the startup notification
 		try {
 			_workManager = new WorkManager(this);
-			_workManager.startup(subMonitor.split(1));
+			_workManager.startup(null);
 			fileSystemManager = new FileSystemResourceManager(this);
-			fileSystemManager.startup(subMonitor.split(1));
+			fileSystemManager.startup(monitor);
 			pathVariableManager = new PathVariableManager();
-			pathVariableManager.startup(subMonitor.split(1));
+			pathVariableManager.startup(null);
 			natureManager = new NatureManager();
-			natureManager.startup(subMonitor.split(1));
+			natureManager.startup(null);
 			filterManager = new FilterTypeManager();
-			filterManager.startup(subMonitor.split(1));
+			filterManager.startup(null);
 			buildManager = new BuildManager(this, getWorkManager().getLock());
-			buildManager.startup(subMonitor.split(1));
+			buildManager.startup(null);
 			notificationManager = new NotificationManager(this);
-			notificationManager.startup(subMonitor.split(1));
+			notificationManager.startup(null);
 			markerManager = new MarkerManager(this);
-			markerManager.startup(subMonitor.split(1));
+			markerManager.startup(null);
 			synchronizer = new Synchronizer(this);
 			saveManager = new SaveManager(this);
-			saveManager.startup(subMonitor.split(1));
+			saveManager.startup(null);
 			propertyManager = new PropertyManager2((Workspace) ResourcesPlugin.getWorkspace());
-			propertyManager.startup(subMonitor.split(1));
+			propertyManager.startup(monitor);
 			charsetManager = new CharsetManager(this);
-			charsetManager.startup(subMonitor.split(1));
+			charsetManager.startup(null);
 			contentDescriptionManager = new ContentDescriptionManager();
-			contentDescriptionManager.startup(subMonitor.split(1));
+			contentDescriptionManager.startup(null);
 			//must start after save manager, because (read) access to tree is needed
 			//must start after other managers to avoid potential cyclic dependency on uninitialized managers (see bug 316182)
 			//must start before alias manager (see bug 94829)
 			refreshManager = new RefreshManager(this);
-			refreshManager.startup(subMonitor.split(1));
+			refreshManager.startup(null);
 			//must start at the end to avoid potential cyclic dependency on other uninitialized managers (see bug 369177)
 			aliasManager = new AliasManager(this);
-			aliasManager.startup(subMonitor.split(1));
+			aliasManager.startup(null);
 		} finally {
 			//unlock tree even in case of failure, otherwise shutdown will also fail
 			treeLocked = null;
diff --git a/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/utils/Messages.java b/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/utils/Messages.java
index f8f5f88..4e3cb16 100644
--- a/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/utils/Messages.java
+++ b/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/utils/Messages.java
@@ -209,7 +209,6 @@
 	public static String resources_initHook;
 	public static String resources_initTeamHook;
 	public static String resources_initValidator;
-	public static String resources_initWorkspace;
 	public static String resources_invalidCharInName;
 	public static String resources_invalidCharInPath;
 	public static String resources_invalidName;
diff --git a/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/utils/messages.properties b/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/utils/messages.properties
index e40e586..978ec63 100644
--- a/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/utils/messages.properties
+++ b/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/utils/messages.properties
@@ -208,7 +208,6 @@
 resources_initValidator = Unable to instantiate validator.
 resources_initHook = Unable to instantiate move/delete hook.
 resources_initTeamHook = Unable to instantiate team hook.
-resources_initWorkspace=Initializing workspace.
 resources_invalidCharInName = {0} is an invalid character in resource name ''{1}''.
 resources_invalidCharInPath = {0} is an invalid character in path ''{1}''.
 resources_invalidName = ''{0}'' is an invalid name on this platform.
diff --git a/bundles/org.eclipse.core.resources/src/org/eclipse/core/resources/ResourcesPlugin.java b/bundles/org.eclipse.core.resources/src/org/eclipse/core/resources/ResourcesPlugin.java
index 72e154a..59d44a9 100644
--- a/bundles/org.eclipse.core.resources/src/org/eclipse/core/resources/ResourcesPlugin.java
+++ b/bundles/org.eclipse.core.resources/src/org/eclipse/core/resources/ResourcesPlugin.java
@@ -327,12 +327,6 @@
 	public static final boolean DEFAULT_PREF_SEPARATE_DERIVED_ENCODINGS = false;
 
 	/**
-	 * The time in milliseconds before a warning is logged when a thread is blocked
-	 * in {@link #getWorkspace()}
-	 */
-	private static final int GET_WORKSPACE_TIMEOUT_BEFORE_WARNING_MILLIS = 20000;
-
-	/**
 	 * The single instance of this plug-in runtime class.
 	 */
 	private static ResourcesPlugin plugin;
@@ -341,15 +335,10 @@
 	 * The workspace managed by the single instance of this
 	 * plug-in runtime class, or <code>null</code> is there is none.
 	 */
-	private static volatile Workspace workspace = null;
+	private static Workspace workspace = null;
 
-	/**
-	 * The job that starts the workspace.
-	 */
-	private static volatile Job initWorkspaceJob;
-
-	private volatile ServiceRegistration<IWorkspace> workspaceRegistration;
-	private volatile ServiceRegistration<DebugOptionsListener> debugRegistration;
+	private ServiceRegistration<IWorkspace> workspaceRegistration;
+	private ServiceRegistration<DebugOptionsListener> debugRegistration;
 
 	/**
 	 * Constructs an instance of this plug-in runtime class.
@@ -419,53 +408,11 @@
 	 *   plug-in class.
 	 */
 	public static IWorkspace getWorkspace() {
-		Job job = initWorkspaceJob;
-		if (workspace == null && job == null) {
-			// This should only happen when running out of an OSGi framework instance
-			// or when the initialization ended with fatal error
+		if (workspace == null)
 			throw new IllegalStateException(Messages.resources_workspaceClosed);
-		} else if (job != null && Thread.currentThread() != job.getThread()) {
-			// workspace is currently being initialized and #getWorkspace() is called from a thread
-			// different from the initialization one: we block until the init is over.
-			try {
-				while (!job.join(GET_WORKSPACE_TIMEOUT_BEFORE_WARNING_MILLIS, new NullProgressMonitor())) {
-					logLongWorkspaceInitWarning(job);
-				}
-			} catch (InterruptedException e) {
-				Thread.currentThread().interrupt();
-			}
-			if (workspace == null) {
-				throw new IllegalStateException(Messages.resources_workspaceClosed);
-			}
-		}
 		return workspace;
 	}
 
-	private static void logLongWorkspaceInitWarning(Job job) {
-		class SeverityMultiStatus extends MultiStatus {
-			public SeverityMultiStatus(int severity, String pluginId, String message, Throwable exception) {
-				super(pluginId, OK, message, exception);
-				setSeverity(severity);
-			}
-		}
-		MultiStatus status = new SeverityMultiStatus(IStatus.WARNING, PI_RESOURCES, String.format("Workspace initialization takes longer than usual. Thread ''%s'' is blocked until the initialization finishes.", Thread.currentThread()), null); //$NON-NLS-1$
-		Exception exception = new Exception();
-		status.add(new Status(IStatus.INFO, PI_RESOURCES, "Sample stacktrace of the blocked thread.", exception)); //$NON-NLS-1$
-
-		try {
-			SecurityManager securityManager = System.getSecurityManager();
-			if (securityManager != null) {
-				securityManager.checkPermission(new RuntimePermission("getStackTrace")); //$NON-NLS-1$
-			}
-			exception = new Exception();
-			exception.setStackTrace(job.getThread().getStackTrace());
-			status.add(new Status(IStatus.INFO, PI_RESOURCES, "Sample stacktrace of the initialization workspace job.", exception)); //$NON-NLS-1$
-		} catch (SecurityException throwable) {
-			// do not log anything
-		}
-		getPlugin().getLog().log(status);
-	}
-
 	/**
 	 * This implementation of the corresponding {@link BundleActivator} method
 	 * closes the workspace without saving.
@@ -501,36 +448,26 @@
 	 * @see BundleActivator#start(BundleContext)
 	 */
 	@Override
-	public void start(final BundleContext context) throws Exception {
+	public void start(BundleContext context) throws Exception {
 		super.start(context);
 
-		initWorkspaceJob = Job.createSystem(Messages.resources_initWorkspace, new ICoreRunnable() {
-			@Override
-			public void run(IProgressMonitor monitor) throws CoreException {
-				try {
-					// register debug options listener
-					Hashtable<String, String> properties = new Hashtable<>(2);
-					properties.put(DebugOptions.LISTENER_SYMBOLICNAME, PI_RESOURCES);
-					debugRegistration = context.registerService(DebugOptionsListener.class, Policy.RESOURCES_DEBUG_OPTIONS_LISTENER, properties);
+		// register debug options listener
+		Hashtable<String, String> properties = new Hashtable<>(2);
+		properties.put(DebugOptions.LISTENER_SYMBOLICNAME, PI_RESOURCES);
+		debugRegistration = context.registerService(DebugOptionsListener.class, Policy.RESOURCES_DEBUG_OPTIONS_LISTENER, properties);
 
-					if (!new LocalMetaArea().hasSavedWorkspace()) {
-						constructWorkspace();
-					}
-					// Remember workspace before opening, to
-					// make it easier to debug cases where open() is failing.
-					workspace = new Workspace();
-					PlatformURLResourceConnection.startup(workspace.getRoot().getLocation());
-					initializePreferenceLookupOrder();
-					IStatus result = workspace.open(monitor);
-					if (!result.isOK())
-						getLog().log(result);
-					workspaceRegistration = context.registerService(IWorkspace.class, workspace, null);
-				} finally {
-					initWorkspaceJob = null;
-				}
-			}
-		});
-		initWorkspaceJob.schedule();
+		if (!new LocalMetaArea().hasSavedWorkspace()) {
+			constructWorkspace();
+		}
+		// Remember workspace before opening, to
+		// make it easier to debug cases where open() is failing.
+		workspace = new Workspace();
+		PlatformURLResourceConnection.startup(workspace.getRoot().getLocation());
+		initializePreferenceLookupOrder();
+		IStatus result = workspace.open(null);
+		if (!result.isOK())
+			getLog().log(result);
+		workspaceRegistration = context.registerService(IWorkspace.class, workspace, null);
 	}
 
 	/*