package org.jenkinsci.plugins.mesos;

import org.apache.mesos.Scheduler;

public class MesosImpl extends Mesos {
  @Override
  public synchronized void startScheduler(String jenkinsMaster, MesosCloud mesosCloud) {
    stopScheduler();
    scheduler = new JenkinsScheduler(jenkinsMaster, mesosCloud);
    scheduler.init();
  }

  @Override
  public synchronized boolean isSchedulerRunning() {
    return scheduler != null && scheduler.isRunning();
  }

  @Override
  public synchronized void stopScheduler() {
    if (scheduler != null) {
      scheduler.stop();
      scheduler = null;
    }
  }

  @Override
  public synchronized void startJenkinsSlave(SlaveRequest request, SlaveResult result) {
    if (scheduler != null) {
      scheduler.requestJenkinsSlave(request, result);
    }
  }

  @Override
  public synchronized void stopJenkinsSlave(String name) {
    if (scheduler != null) {
      scheduler.terminateJenkinsSlave(name);
    }
  }

  @Override
  public synchronized void updateScheduler(String jenkinsMaster, MesosCloud mesosCloud) {
    scheduler.setMesosCloud(mesosCloud);
    scheduler.setJenkinsMaster(jenkinsMaster);
  }

  private JenkinsScheduler scheduler;

  @Override
  public Scheduler getScheduler() {
    return scheduler;
  }

}

@@ -123,37 +123,24 @@
 de%0A  public 
-synchronized 
 void startSc
@@ -190,24 +190,48 @@
 sosCloud) %7B%0A
+    lock();%0A    try %7B%0A  
     stopSche
@@ -235,24 +235,26 @@
 cheduler();%0A
+  
     schedule
@@ -302,32 +302,34 @@
 esosCloud);%0A    
+  
 scheduler.init()
@@ -326,24 +326,62 @@
 ler.init();%0A
+    %7D finally %7B%0A      unlock();%0A    %7D%0A
   %7D%0A%0A  @Over
@@ -394,29 +394,16 @@
  public 
-synchronized 
 boolean 
@@ -421,24 +421,48 @@
 Running() %7B%0A
+    lock();%0A    try %7B%0A  
     return s
@@ -504,16 +504,54 @@
 ning();%0A
+    %7D finally %7B%0A      unlock();%0A    %7D%0A
   %7D%0A%0A  @
@@ -564,37 +564,24 @@
 de%0A  public 
-synchronized 
 void stopSch
@@ -587,24 +587,48 @@
 heduler() %7B%0A
+    lock();%0A    try %7B%0A  
     if (sche
@@ -642,32 +642,34 @@
 = null) %7B%0A      
+  
 scheduler.stop()
@@ -666,24 +666,26 @@
 ler.stop();%0A
+  
       schedu
@@ -696,16 +696,56 @@
 = null;%0A
+      %7D%0A    %7D finally %7B%0A      unlock();%0A
     %7D%0A  
@@ -764,37 +764,24 @@
 de%0A  public 
-synchronized 
 void startJe
@@ -831,24 +831,48 @@
 t result) %7B%0A
+    lock();%0A    try %7B%0A  
     if (sche
@@ -886,32 +886,34 @@
 = null) %7B%0A      
+ 
+ 
 scheduler.reques
@@ -944,16 +944,56 @@
 esult);%0A
+      %7D%0A    %7D finally %7B%0A      unlock();%0A
     %7D%0A  
@@ -1012,37 +1012,24 @@
 de%0A  public 
-synchronized 
 void stopJen
@@ -1049,24 +1049,48 @@
 ing name) %7B%0A
+    lock();%0A    try %7B%0A  
     if (sche
@@ -1102,24 +1102,26 @@
  != null) %7B%0A
+  
       schedu
@@ -1153,16 +1153,56 @@
 (name);%0A
+      %7D%0A    %7D finally %7B%0A      unlock();%0A
     %7D%0A  
@@ -1229,21 +1229,8 @@
 lic 
-synchronized 
 void
@@ -1289,24 +1289,48 @@
 sosCloud) %7B%0A
+    lock();%0A    try %7B%0A  
     schedule
@@ -1354,24 +1354,26 @@
 esosCloud);%0A
+  
     schedule
@@ -1407,16 +1407,54 @@
 aster);%0A
+    %7D finally %7B%0A      unlock();%0A    %7D%0A
   %7D%0A%0A  p
@@ -1564,10 +1564,161 @@
 r;%0A  %7D%0A%0A
+  private void unlock() %7B%0A    JenkinsScheduler.SUPERVISOR_LOCK.unlock();%0A  %7D%0A%0A  private void lock() %7B%0A    JenkinsScheduler.SUPERVISOR_LOCK.lock();%0A  %7D%0A
 %7D%0A

Fix a deadlock case.