vm worker: remove worker thread from vm worker
authorjihye424.kim <jihye424.kim@samsung.com>
Mon, 23 Nov 2015 07:59:41 +0000 (16:59 +0900)
committerSeokYeon Hwang <syeon.hwang@samsung.com>
Fri, 27 Nov 2015 01:56:10 +0000 (10:56 +0900)
- fix: remove worker thread from vm worker
-- we need worker thread to prevent ui blocking beacuse to create base image job take long time
-- UI module use worker thread, beacuse UI module need to prevent blocking
-- vm worker not use thread, vm worker need not to know blocking issue

- bug: thread timing issue
-- thread1: wait to thread2 (check isAlive)
(error status)
-- thread2: notify to thread1
-- thread1: wake up -> check isAlive() -> thread2 isAlive -> thread1 wait forever
-- thread2: terminate
(normal status)
-- thread2: notify to thread1 -> terminate
-- thread1: wake up -> check isAlive() -> next step

- can not control thread terminate time

Change-Id: I9b2528d2b0effccd975c58580542682054d299b4
Signed-off-by: jihye424.kim <jihye424.kim@samsung.com>
src/org/tizen/emulator/manager/console/ConsoleProcessor.java
src/org/tizen/emulator/manager/ui/MenuHandling.java
src/org/tizen/emulator/manager/vms/VMWorkerCommon.java

index c7bef73..bafd3c4 100644 (file)
@@ -359,6 +359,7 @@ public class ConsoleProcessor {
                        if (f.isDirectory()) {
                                path = path + File.separator + "emulimg-" + name + "." + prop.getArchInternal().toString(); //$NON-NLS-1$ //$NON-NLS-2$
                        }
+                       System.out.println("Wait for creating base image. It will take about 30 ~ 60 seconds.");
                        prop.getWorker().createNewBaseImage(prop, path);
                } catch (VMWorkerException e) {
                        System.out.println("Error: " + e.getMessage()); //$NON-NLS-1$
index 690d80f..6216ca4 100644 (file)
@@ -33,6 +33,7 @@ package org.tizen.emulator.manager.ui;
 import java.io.File;
 
 import org.eclipse.swt.SWT;
+import org.eclipse.swt.widgets.Display;
 import org.eclipse.swt.widgets.FileDialog;
 import org.eclipse.swt.widgets.Shell;
 import org.tizen.emulator.manager.resources.FilePathResources;
@@ -127,10 +128,42 @@ public class MenuHandling {
                        return false;
                }
 
-               property.getWorker().createNewBaseImage(property, path);
+               // block other function while to create base image
+               property.setRunning(true);
+               WorkerThread worker = new WorkerThread(property, path);
+               worker.start();
+
                return true;
        }
 
+       private class WorkerThread extends Thread {
+               VMProperty property;
+               String path;
+
+               public WorkerThread(VMProperty property, String path) {
+                       this.property = property;
+                       this.path = path;
+               }
+
+               @Override
+               public void run() {
+                       try {
+                               property.getWorker().createNewBaseImage(property, path);
+                       } catch (VMWorkerException e) {
+                               final String error = e.getMessage();
+                               Display.getDefault().syncExec(new Runnable() {
+                                       @Override
+                                       public void run() {
+                                               new MessageDialog(null).openWarningDialog(error);
+
+                                       }
+                               });
+                       } finally {
+                               property.setRunning(false);
+                       }
+               }
+       }
+
        public void cloneVirtualMachine(VMProperty property) {
                MessageDialog msg = new MessageDialog();
 
index 3ccf24e..8057097 100644 (file)
@@ -52,10 +52,6 @@ import org.tizen.emulator.manager.vms.option.IOption;
 
 public class VMWorkerCommon implements IVMWorker {
        private VMProperty property;
-       private WorkerThread worker = null;
-       private String baseImagePath = null;
-       private String baseImagePartPath = null;
-       private Object lock = new Object();
        private ILauncher launcher;
        private boolean isModified;
 
@@ -83,14 +79,6 @@ public class VMWorkerCommon implements IVMWorker {
                return property;
        }
 
-       public String getBaseImagePath() {
-               return baseImagePath;
-       }
-
-       public String getBaseImagePartPath() {
-               return baseImagePartPath;
-       }
-
        @Override
        public void launchVM() throws VMLauncherException, VMWorkerException {
                launchVM(false, null);
@@ -198,8 +186,8 @@ public class VMWorkerCommon implements IVMWorker {
                        checkPropertyModification();
                        isRunningNow();
 
-                       baseImagePath = dest;
-                       baseImagePartPath = dest + ".part"; //$NON-NLS-1$
+                       String baseImagePath = dest;
+                       String baseImagePartPath = dest + ".part"; //$NON-NLS-1$
 
                        File childImage = new File(property.getConfiguration()
                                        .getBaseInformation().getDiskImage().getCurrentDiskImage()
@@ -228,88 +216,7 @@ public class VMWorkerCommon implements IVMWorker {
                        cmd.add(baseImagePartPath);
                        final QemuImgProc proc = new QemuImgProc(cmd);
 
-                       /*
-                        * long childSize = childImage.length(); long baseSize =
-                        * baseImage.length(); final long maxSize = childSize + baseSize;
-                        * final File destFile = new File(dest); final CreateBaseImageDialog
-                        * dialog = new
-                        * CreateBaseImageDialog(EmulatorManager.getInstance().getMainDialog
-                        * ().getShell()); dialog.Open(destFile, maxSize);
-                        */
-
-                       worker = new WorkerThread(proc, lock);
-                       worker.setDaemon(false);
-                       worker.start();
-                       synchronized (lock) {
-                               try {
-                                       lock.wait(2000);
-                               } catch (InterruptedException e) {
-                                       e.printStackTrace();
-                               }
-                               if (worker.isError()) {
-                                       throw new VMWorkerException(
-                                                       Messages.getString("VMWorkerCommon.Qemu-imgError.0") //$NON-NLS-1$
-                                                                       + StringResources.NEW_LINE
-                                                                       + Messages.getString("VMWorkerCommon.Qemu-imgError.1") //$NON-NLS-1$
-                                                                       + FilePathResources.getTizenVmsPath()
-                                                                       + File.separator + "emulator-manager)"); //$NON-NLS-1$
-                               }
-                       }
-               } catch (VMWorkerException e) {
-                       throw e;
-               } finally {
-                       // lock.release
-                       WorkerLock.release();
-               }
-
-               // TODO: This code will be moved console source
-               if (EmulatorManager.isConsoleMode()) {
-                       System.out.println(Messages.getString("VMWorkerCommon.ExportAsInfo.0") //$NON-NLS-1$
-                                       + StringResources.NEW_LINE
-                                       + Messages.getString("VMWorkerCommon.ExportAsInfo.1")); //$NON-NLS-1$
-                       synchronized (lock) {
-                               while (worker.isAlive()) {
-                                       try {
-                                               lock.wait();
-                                       } catch (InterruptedException e) {
-                                               e.printStackTrace();
-                                       }
-                               }
-                               if (worker.isError()) {
-                                       throw new VMWorkerException(
-                                                       Messages.getString("VMWorkerCommon.Qemu-imgError.0") //$NON-NLS-1$
-                                                                       + StringResources.NEW_LINE
-                                                                       + Messages.getString("VMWorkerCommon.Qemu-imgError.1") //$NON-NLS-1$
-                                                                       + FilePathResources.getTizenVmsPath()
-                                                                       + File.separator + "emulator-manager)"); //$NON-NLS-1$
-                               }
-                       }
-               }
-
-               /*
-                * if (! proc.Running()) { //dialog.Close(); throw new
-                * VMsWorkerException(
-                * "Failed to create new base image becauese of failed qemu-img processing!"
-                * + StringResources.NEW_LINE +
-                * "You can get more information in log file (" +
-                * FileIO.getInstance().getTizenVmsArchPath() + File.separator +
-                * "emulator-manager)"); } //dialog.Close();
-                */
-       }
-
-       class WorkerThread extends Thread {
-               QemuImgProc proc;
-               boolean isError;
-               Object lock;
-
-               public WorkerThread(QemuImgProc proc, Object lock) {
-                       this.proc = proc;
-                       this.lock = lock;
-                       isError = false;
-               }
-
-               public void run() {
-                       //
+                       // start make custom base image
                        File file = new File(FilePathResources.getTizenVmsPath()
                                        + File.separator + property.getName() + ".lock"); //$NON-NLS-1$
                        try {
@@ -317,24 +224,47 @@ public class VMWorkerCommon implements IVMWorker {
                        } catch (IOException e) {
                                EMLogger.getLogger().warning(e.getMessage());
                        }
-                       //
 
                        if (!proc.Running()) {
-                               isError = true;
+                               EMLogger.getLogger().info("Failed to create base image."
+                                               + StringResources.NEW_LINE
+                                               + "Base image path: " + baseImagePath);
+                               deleteFile(new File(baseImagePartPath));
+                               throw new VMWorkerException(
+                                               Messages.getString("VMWorkerCommon.Qemu-imgError.0") //$NON-NLS-1$
+                                                               + StringResources.NEW_LINE
+                                                               + Messages.getString("VMWorkerCommon.Qemu-imgError.1") //$NON-NLS-1$
+                                                               + FilePathResources.getTizenVmsPath()
+                                                               + File.separator + "emulator-manager)"); //$NON-NLS-1$
                        }
-                       synchronized (lock) {
-                               if (file != null) {
-                                       file.delete();
-                               }
-                               File baseFile = new File(baseImagePath);
-                               File partFile = new File(baseImagePartPath);
-                               partFile.renameTo(baseFile);
-                               lock.notifyAll();
+
+                       // success to create custom base image
+                       File baseFile = new File(baseImagePath);
+                       File partFile = new File(baseImagePartPath);
+                       if (baseFile.exists()) {
+                               EMLogger.getLogger().fine("Base image file is already exist. So, this file will be deleted.");
+                               baseFile.delete();
                        }
-               }
 
-               public boolean isError() {
-                       return isError;
+                       if (!partFile.renameTo(baseFile)) {
+                               deleteFile(baseFile);
+                               deleteFile(partFile);
+                               String error ="Failed to rename base image file. Please, try again to create base image.";
+                               EMLogger.getLogger().warning(error);
+                               throw new VMWorkerException(error);
+                       }
+
+                       if (file != null) {
+                               EMLogger.getLogger().fine("Delete property lock file.");
+                               file.delete();
+                       }
+
+                       EMLogger.getLogger().info("Success to create base image."
+                                       + StringResources.NEW_LINE
+                                       + "Base image path: " + baseImagePath);
+               } finally {
+                       // lock.release
+                       WorkerLock.release();
                }
        }
 
@@ -464,14 +394,6 @@ public class VMWorkerCommon implements IVMWorker {
 
        // check if the emulator instance with the same name is running now.
        public boolean isRunningNow() throws VMWorkerException {
-               // to check running 'create base image' routine
-               if (worker != null) {
-                       if (worker.isAlive()) {
-                               throw new VMWorkerException("[" + property.getName() + "]" //$NON-NLS-1$ //$NON-NLS-2$
-                                               + Messages.getString("VMWorkerCommon.WorkingError.0")); //$NON-NLS-1$
-                       }
-               }
-
                File file = new File(FilePathResources.getTizenVmsPath()
                                + File.separator + property.getName() + ".lock"); //$NON-NLS-1$
                if (file.exists()) {