Merge remote-tracking branch 'gerrit/master'
diff --git a/plugins/org.eclipse.etrice.generator.c/src/org/eclipse/etrice/generator/c/gen/NodeGen.xtend b/plugins/org.eclipse.etrice.generator.c/src/org/eclipse/etrice/generator/c/gen/NodeGen.xtend
index 1e849d8..798959f 100644
--- a/plugins/org.eclipse.etrice.generator.c/src/org/eclipse/etrice/generator/c/gen/NodeGen.xtend
+++ b/plugins/org.eclipse.etrice.generator.c/src/org/eclipse/etrice/generator/c/gen/NodeGen.xtend
@@ -187,7 +187,7 @@
 				etTime interval;
 
 				/* initialization of all message services */
-				«FOR thread: threads»
+				«FOR thread: threads.sortBy[-prio]»
 					«IF thread.execmode==ExecMode::POLLED || thread.execmode==ExecMode::MIXED»
 						interval.sec = «TimeConverter::split(thread.time, TimeConverter.SEC, true)»;
 						interval.nSec = «TimeConverter::split(thread.time, TimeConverter.MILLI_SEC, false)»;
@@ -212,7 +212,7 @@
 		static void «clsname»_startMessageServices(void) {
 			ET_MSC_LOGGER_SYNC_ENTRY("«clsname»", "startMessageServices")
 
-			«FOR thread: threads.sortBy[getPrio].reverse»
+			«FOR thread: threads.sortBy[-prio]»
 				etMessageService_start(&msgService_«thread.name»);
 			«ENDFOR»
 
diff --git a/plugins/org.eclipse.etrice.generator.c/xtend-gen/org/eclipse/etrice/generator/c/gen/NodeGen.java b/plugins/org.eclipse.etrice.generator.c/xtend-gen/org/eclipse/etrice/generator/c/gen/NodeGen.java
index a945d37..7506505 100644
--- a/plugins/org.eclipse.etrice.generator.c/xtend-gen/org/eclipse/etrice/generator/c/gen/NodeGen.java
+++ b/plugins/org.eclipse.etrice.generator.c/xtend-gen/org/eclipse/etrice/generator/c/gen/NodeGen.java
@@ -80,7 +80,6 @@
 import org.eclipse.xtext.xbase.lib.Functions.Function1;
 import org.eclipse.xtext.xbase.lib.IntegerRange;
 import org.eclipse.xtext.xbase.lib.IterableExtensions;
-import org.eclipse.xtext.xbase.lib.ListExtensions;
 
 @Singleton
 @SuppressWarnings("all")
@@ -412,7 +411,15 @@
       _builder.append("/* initialization of all message services */");
       _builder.newLine();
       {
-        for(final PhysicalThread thread : threads) {
+        final Function1<PhysicalThread, Long> _function_1 = new Function1<PhysicalThread, Long>() {
+          @Override
+          public Long apply(final PhysicalThread it) {
+            long _prio = it.getPrio();
+            return Long.valueOf((-_prio));
+          }
+        };
+        List<PhysicalThread> _sortBy = IterableExtensions.<PhysicalThread, Long>sortBy(threads, _function_1);
+        for(final PhysicalThread thread : _sortBy) {
           {
             if ((Objects.equal(thread.getExecmode(), ExecMode.POLLED) || Objects.equal(thread.getExecmode(), ExecMode.MIXED))) {
               _builder.append("\t\t");
@@ -518,15 +525,15 @@
       _builder.newLineIfNotEmpty();
       _builder.newLine();
       {
-        final Function1<PhysicalThread, Long> _function_1 = new Function1<PhysicalThread, Long>() {
+        final Function1<PhysicalThread, Long> _function_2 = new Function1<PhysicalThread, Long>() {
           @Override
           public Long apply(final PhysicalThread it) {
-            return Long.valueOf(it.getPrio());
+            long _prio = it.getPrio();
+            return Long.valueOf((-_prio));
           }
         };
-        List<PhysicalThread> _sortBy = IterableExtensions.<PhysicalThread, Long>sortBy(threads, _function_1);
-        List<PhysicalThread> _reverse = ListExtensions.<PhysicalThread>reverse(_sortBy);
-        for(final PhysicalThread thread_1 : _reverse) {
+        List<PhysicalThread> _sortBy_1 = IterableExtensions.<PhysicalThread, Long>sortBy(threads, _function_2);
+        for(final PhysicalThread thread_1 : _sortBy_1) {
           _builder.append("\t");
           _builder.append("etMessageService_start(&msgService_");
           String _name_11 = thread_1.getName();
diff --git a/plugins/org.eclipse.etrice.generator.cpp/src/org/eclipse/etrice/generator/cpp/gen/NodeGen.xtend b/plugins/org.eclipse.etrice.generator.cpp/src/org/eclipse/etrice/generator/cpp/gen/NodeGen.xtend
index 7ac206d..48a98f8 100644
--- a/plugins/org.eclipse.etrice.generator.cpp/src/org/eclipse/etrice/generator/cpp/gen/NodeGen.xtend
+++ b/plugins/org.eclipse.etrice.generator.cpp/src/org/eclipse/etrice/generator/cpp/gen/NodeGen.xtend
@@ -234,7 +234,7 @@
 			«ENDIF»
 
 			IMessageMemory* msgMemory;
-			«FOR thread: threads»
+			«FOR thread: threads.sortBy[-prio]»
 				{
 					msgMemory = new StaticMessageMemory(this, "MessageMemory_«thread.name»", «thread.msgblocksize», «thread.msgpoolsize»);
 					«IF thread.execmode==ExecMode::POLLED || thread.execmode==ExecMode::MIXED»
diff --git a/plugins/org.eclipse.etrice.generator.cpp/xtend-gen/org/eclipse/etrice/generator/cpp/gen/NodeGen.java b/plugins/org.eclipse.etrice.generator.cpp/xtend-gen/org/eclipse/etrice/generator/cpp/gen/NodeGen.java
index f93f1f9..8b127a7 100644
--- a/plugins/org.eclipse.etrice.generator.cpp/xtend-gen/org/eclipse/etrice/generator/cpp/gen/NodeGen.java
+++ b/plugins/org.eclipse.etrice.generator.cpp/xtend-gen/org/eclipse/etrice/generator/cpp/gen/NodeGen.java
@@ -574,7 +574,15 @@
       _builder.append("IMessageMemory* msgMemory;");
       _builder.newLine();
       {
-        for(final PhysicalThread thread_3 : threads) {
+        final Function1<PhysicalThread, Long> _function_1 = new Function1<PhysicalThread, Long>() {
+          @Override
+          public Long apply(final PhysicalThread it) {
+            long _prio = it.getPrio();
+            return Long.valueOf((-_prio));
+          }
+        };
+        List<PhysicalThread> _sortBy = IterableExtensions.<PhysicalThread, Long>sortBy(threads, _function_1);
+        for(final PhysicalThread thread_3 : _sortBy) {
           _builder.append("\t");
           _builder.append("{");
           _builder.newLine();
diff --git a/plugins/org.eclipse.etrice.generator.java/src/org/eclipse/etrice/generator/java/gen/NodeGen.xtend b/plugins/org.eclipse.etrice.generator.java/src/org/eclipse/etrice/generator/java/gen/NodeGen.xtend
index ef86670..793c38e 100644
--- a/plugins/org.eclipse.etrice.generator.java/src/org/eclipse/etrice/generator/java/gen/NodeGen.xtend
+++ b/plugins/org.eclipse.etrice.generator.java/src/org/eclipse/etrice/generator/java/gen/NodeGen.xtend
@@ -151,7 +151,7 @@
 			public void instantiateMessageServices() {
 
 				IMessageService msgService;
-				«FOR thread: threads»
+				«FOR thread: threads.sortBy[-prio]»
 					«IF thread.execmode==ExecMode::POLLED || thread.execmode==ExecMode::MIXED»
 						msgService = new MessageService(this, MessageService.ExecMode.«thread.execmode.getName», «thread.time»L, 0, «thread.threadId», "MessageService_«thread.name»" /*, thread_prio */);
 					«ELSE»
diff --git a/plugins/org.eclipse.etrice.generator.java/xtend-gen/org/eclipse/etrice/generator/java/gen/NodeGen.java b/plugins/org.eclipse.etrice.generator.java/xtend-gen/org/eclipse/etrice/generator/java/gen/NodeGen.java
index 9db9a44..d1eba88 100644
--- a/plugins/org.eclipse.etrice.generator.java/xtend-gen/org/eclipse/etrice/generator/java/gen/NodeGen.java
+++ b/plugins/org.eclipse.etrice.generator.java/xtend-gen/org/eclipse/etrice/generator/java/gen/NodeGen.java
@@ -331,7 +331,15 @@
       _builder.append("IMessageService msgService;");
       _builder.newLine();
       {
-        for(final PhysicalThread thread_1 : threads) {
+        final Function1<PhysicalThread, Long> _function_1 = new Function1<PhysicalThread, Long>() {
+          @Override
+          public Long apply(final PhysicalThread it) {
+            long _prio = it.getPrio();
+            return Long.valueOf((-_prio));
+          }
+        };
+        List<PhysicalThread> _sortBy = IterableExtensions.<PhysicalThread, Long>sortBy(threads, _function_1);
+        for(final PhysicalThread thread_1 : _sortBy) {
           {
             if ((Objects.equal(thread_1.getExecmode(), ExecMode.POLLED) || Objects.equal(thread_1.getExecmode(), ExecMode.MIXED))) {
               _builder.append("\t\t");
@@ -487,20 +495,20 @@
       _builder.newLine();
       {
         BasicEList<AbstractInstance> _allSubInstances = this._roomExtensions.getAllSubInstances(comp);
-        final Function1<AbstractInstance, Boolean> _function_1 = new Function1<AbstractInstance, Boolean>() {
+        final Function1<AbstractInstance, Boolean> _function_2 = new Function1<AbstractInstance, Boolean>() {
           @Override
           public Boolean apply(final AbstractInstance inst) {
             return Boolean.valueOf((inst instanceof ActorInterfaceInstance));
           }
         };
-        Iterable<AbstractInstance> _filter = IterableExtensions.<AbstractInstance>filter(_allSubInstances, _function_1);
-        final Function1<AbstractInstance, ActorInterfaceInstance> _function_2 = new Function1<AbstractInstance, ActorInterfaceInstance>() {
+        Iterable<AbstractInstance> _filter = IterableExtensions.<AbstractInstance>filter(_allSubInstances, _function_2);
+        final Function1<AbstractInstance, ActorInterfaceInstance> _function_3 = new Function1<AbstractInstance, ActorInterfaceInstance>() {
           @Override
           public ActorInterfaceInstance apply(final AbstractInstance inst) {
             return ((ActorInterfaceInstance) inst);
           }
         };
-        Iterable<ActorInterfaceInstance> _map = IterableExtensions.<AbstractInstance, ActorInterfaceInstance>map(_filter, _function_2);
+        Iterable<ActorInterfaceInstance> _map = IterableExtensions.<AbstractInstance, ActorInterfaceInstance>map(_filter, _function_3);
         for(final ActorInterfaceInstance aii : _map) {
           _builder.append("\t\t");
           _builder.append("{");
@@ -760,14 +768,14 @@
           {
             EList<ActorClass> _subClasses = root.getSubClasses(oa);
             List<ActorClass> _union = this._roomExtensions.<ActorClass>union(_subClasses, oa);
-            final Function1<ActorClass, Boolean> _function_3 = new Function1<ActorClass, Boolean>() {
+            final Function1<ActorClass, Boolean> _function_4 = new Function1<ActorClass, Boolean>() {
               @Override
               public Boolean apply(final ActorClass s) {
                 boolean _isAbstract = s.isAbstract();
                 return Boolean.valueOf((!_isAbstract));
               }
             };
-            Iterable<ActorClass> _filter_1 = IterableExtensions.<ActorClass>filter(_union, _function_3);
+            Iterable<ActorClass> _filter_1 = IterableExtensions.<ActorClass>filter(_union, _function_4);
             for(final ActorClass subcls : _filter_1) {
               _builder.append("\t\t");
               _builder.append("\t");
diff --git a/plugins/org.eclipse.etrice.ui.common.base/src/org/eclipse/etrice/ui/common/base/dialogs/AbstractPropertyDialog.java b/plugins/org.eclipse.etrice.ui.common.base/src/org/eclipse/etrice/ui/common/base/dialogs/AbstractPropertyDialog.java
index b848266..912f9f3 100644
--- a/plugins/org.eclipse.etrice.ui.common.base/src/org/eclipse/etrice/ui/common/base/dialogs/AbstractPropertyDialog.java
+++ b/plugins/org.eclipse.etrice.ui.common.base/src/org/eclipse/etrice/ui/common/base/dialogs/AbstractPropertyDialog.java
@@ -205,6 +205,9 @@
 		
 		@Override
 		public Object convert(Object fromObject) {
+			if (fromObject==null)
+				return "";
+			
 			return ((Enumerator)fromObject).getLiteral();
 		}
 	}
diff --git a/plugins/org.eclipse.etrice.ui.common.base/src/org/eclipse/etrice/ui/common/base/support/ChangeAwareCreateConnectionFeature.java b/plugins/org.eclipse.etrice.ui.common.base/src/org/eclipse/etrice/ui/common/base/support/ChangeAwareCreateConnectionFeature.java
index e7b4e0e..f328a99 100644
--- a/plugins/org.eclipse.etrice.ui.common.base/src/org/eclipse/etrice/ui/common/base/support/ChangeAwareCreateConnectionFeature.java
+++ b/plugins/org.eclipse.etrice.ui.common.base/src/org/eclipse/etrice/ui/common/base/support/ChangeAwareCreateConnectionFeature.java
@@ -45,6 +45,9 @@
 
 	@Override
 	public final boolean hasDoneChanges() {
+		if (changeRecorder==null)
+			return false;
+		
 		return changeRecorder.hasResourceChanged();
 	}
 	
diff --git a/runtime/org.eclipse.etrice.runtime.cpp/src/common/messaging/MessageServiceController.cpp b/runtime/org.eclipse.etrice.runtime.cpp/src/common/messaging/MessageServiceController.cpp
index 89ab179..7ca18e7 100644
--- a/runtime/org.eclipse.etrice.runtime.cpp/src/common/messaging/MessageServiceController.cpp
+++ b/runtime/org.eclipse.etrice.runtime.cpp/src/common/messaging/MessageServiceController.cpp
@@ -10,12 +10,14 @@
  *
  *******************************************************************************/
 
+#include <algorithm>
 #include "MessageServiceController.h"
 
 namespace etRuntime {
 
 MessageServiceController::MessageServiceController() :
 		m_messageServices(),
+		m_messageServicesOrdered(),
 		m_freeIDs(),
 		m_running(false),
 		m_nextFreeID(0),
@@ -50,12 +52,15 @@
 		m_nextFreeID = msgSvc.getAddress().m_threadID + 1;
 
 	m_messageServices[msgSvc.getAddress().m_threadID] = &msgSvc;
+	m_messageServicesOrdered.push_back(&msgSvc);
 	etMutex_leave(&m_mutex);
 }
 
 void MessageServiceController::removeMsgSvc(IMessageService& msgSvc) {
 	etMutex_enter(&m_mutex);
 	m_messageServices.erase(msgSvc.getAddress().m_threadID);
+	std::vector<IMessageService*>::iterator it = std::find(m_messageServicesOrdered.begin(), m_messageServicesOrdered.end(), &msgSvc);
+	m_messageServicesOrdered.erase(it);
 	etMutex_leave(&m_mutex);
 }
 
@@ -71,12 +76,15 @@
 }
 
 void MessageServiceController::start() {
+	etMutex_enter(&m_mutex);
+
 	// start all message services
-	for (std::map<int, IMessageService*>::iterator it = m_messageServices.begin(); it != m_messageServices.end(); ++it) {
-		(it->second)->start();
-		// TODO TS: start in order of priorities
+	for (std::vector<IMessageService*>::iterator it = m_messageServicesOrdered.begin(); it != m_messageServicesOrdered.end(); ++it) {
+		(*it)->start();
 	}
 	m_running = true;
+
+	etMutex_leave(&m_mutex);
 }
 
 void MessageServiceController::stop() {
@@ -111,12 +119,12 @@
 void MessageServiceController::terminate() {
 	// terminate all message services
 	etMutex_enter(&m_mutex);
-	m_terminateServices = m_messageServices;
+	m_terminateServices = m_messageServicesOrdered;
 
-	std::map<int, IMessageService*>::iterator it = m_terminateServices.begin();
-	for (; it != m_terminateServices.end(); ++it) {
-		(it->second)->terminate();
-		//TODO TS: stop in order of priorities
+	// stop in reverse order
+	std::vector<IMessageService*>::reverse_iterator it = m_terminateServices.rbegin();
+	for (; it != m_terminateServices.rend(); ++it) {
+		(*it)->terminate();
 	}
 	etMutex_leave(&m_mutex);
 }
@@ -147,7 +155,10 @@
 
 void MessageServiceController::setMsgSvcTerminated(const IMessageService& msgSvc){
 	etMutex_enter(&m_mutex);
-	m_terminateServices.erase(msgSvc.getAddress().m_threadID);
+	std::vector<IMessageService*>::iterator it = std::find(m_terminateServices.begin(), m_terminateServices.end(), &msgSvc);
+	if (it!=m_terminateServices.end()) {
+		m_terminateServices.erase(it);
+	}
 	etSema_wakeup(&m_terminateSema);
 	etMutex_leave(&m_mutex);
 }
diff --git a/runtime/org.eclipse.etrice.runtime.cpp/src/common/messaging/MessageServiceController.h b/runtime/org.eclipse.etrice.runtime.cpp/src/common/messaging/MessageServiceController.h
index 4771b38..81724e8 100644
--- a/runtime/org.eclipse.etrice.runtime.cpp/src/common/messaging/MessageServiceController.h
+++ b/runtime/org.eclipse.etrice.runtime.cpp/src/common/messaging/MessageServiceController.h
@@ -17,6 +17,7 @@
 #include "osal/etMutex.h"
 #include "osal/etSema.h"
 #include <map>
+#include <vector>
 #include <queue>
 #include <string>
 
@@ -51,13 +52,14 @@
 	void terminate();
 
 	std::map<int, IMessageService*> m_messageServices;
+	std::vector<IMessageService*> m_messageServicesOrdered;
 	std::queue<int> m_freeIDs;
 	etBool m_running;
 	int m_nextFreeID;
 
 	etMutex m_mutex;
 	etSema m_terminateSema;
-	std::map<int, IMessageService*> m_terminateServices;
+	std::vector<IMessageService*> m_terminateServices;
 
 	MessageServiceController(MessageServiceController const&);
 	MessageServiceController& operator=(MessageServiceController const&);
diff --git a/runtime/org.eclipse.etrice.runtime.java/src/org/eclipse/etrice/runtime/java/messaging/MessageServiceController.java b/runtime/org.eclipse.etrice.runtime.java/src/org/eclipse/etrice/runtime/java/messaging/MessageServiceController.java
index fd1089c..367b804 100644
--- a/runtime/org.eclipse.etrice.runtime.java/src/org/eclipse/etrice/runtime/java/messaging/MessageServiceController.java
+++ b/runtime/org.eclipse.etrice.runtime.java/src/org/eclipse/etrice/runtime/java/messaging/MessageServiceController.java
@@ -16,6 +16,7 @@
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.HashMap;
+import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
@@ -32,6 +33,7 @@
 public class MessageServiceController {
 	
 	private HashMap<Integer, IMessageService> messageServices = new HashMap<Integer, IMessageService>();
+	private LinkedList<IMessageService> orderedMessageServices = new LinkedList<IMessageService>();
 	private LinkedList<Integer> freeIDs = new LinkedList<Integer>();
 	private boolean running = false;
 	private int nextFreeID = 0;
@@ -52,10 +54,12 @@
 			nextFreeID = msgSvc.getAddress().threadID+1;
 		
 		messageServices.put(msgSvc.getAddress().threadID, msgSvc);
+		orderedMessageServices.add(msgSvc);
 	}
 
 	public synchronized void removeMsgSvc(IMessageService msgSvc){
 		messageServices.remove(msgSvc.getAddress().threadID);
+		orderedMessageServices.remove(msgSvc);
 	}
 	
 	public synchronized IMessageService getMsgSvc(int id){
@@ -77,7 +81,7 @@
 		
 		// start all message services
 		List<Thread> threads = new ArrayList<Thread>(messageServices.size());
-		for (IMessageService msgSvc : messageServices.values()){
+		for (IMessageService msgSvc : orderedMessageServices){
 			Thread thread = new Thread(msgSvc, msgSvc.getName());
 			msgSvc.setThread(thread);
 			threads.add(thread);
@@ -124,9 +128,8 @@
 
 	private void terminate() {
 		// terminate all message services
-		for (IMessageService msgSvc : messageServices.values()){
-			msgSvc.terminate();
-			// TODOTS: stop in order of priorities
+		for (Iterator<IMessageService> it = orderedMessageServices.descendingIterator(); it.hasNext(); ) {
+			it.next().terminate();
 		}
 	}
 
@@ -135,8 +138,9 @@
 	 * ! not thread safe !
 	 */
 	public void waitTerminate() {
-		for (IMessageService msgSvc : messageServices.values()) {
+		for (Iterator<IMessageService> it = orderedMessageServices.descendingIterator(); it.hasNext(); ) {
 			try {
+				IMessageService msgSvc = it.next();
 				if (msgSvc.getThread()==null)
 					continue;
 				
@@ -153,6 +157,7 @@
 	public synchronized void resetAll() {
 		stop();
 		messageServices.clear();
+		orderedMessageServices.clear();
 		freeIDs.clear();
 		nextFreeID = 0;
 	}