Merge pull request #435 from apache/fix-changes

Fix indentation for CHANGES.md
diff --git a/bundles/remote_services/discovery_common/include/discovery.h b/bundles/remote_services/discovery_common/include/discovery.h
index ee9f298..82770f5 100644
--- a/bundles/remote_services/discovery_common/include/discovery.h
+++ b/bundles/remote_services/discovery_common/include/discovery.h
@@ -38,6 +38,7 @@
 #include "endpoint_discovery_poller.h"
 
 #include "celix_log_helper.h"
+#include <stdbool.h>
 
 #define DISCOVERY_SERVER_INTERFACE  "DISCOVERY_CFG_SERVER_INTERFACE"
 #define DISCOVERY_SERVER_IP         "DISCOVERY_CFG_SERVER_IP"
@@ -64,9 +65,8 @@
 struct discovery {
     celix_bundle_context_t *context;
 
-    celix_thread_mutex_t listenerReferencesMutex;
-    celix_thread_mutex_t discoveredServicesMutex;
-
+    celix_thread_mutex_t mutex;// projects: closed, listenerReferences, discoveredServices
+    bool stopped;//is discovery stopped
     hash_map_t *listenerReferences; //key=serviceReference, value=nop
     hash_map_t *discoveredServices; //key=endpointId (string), value=endpoint_description_t *
 
diff --git a/bundles/remote_services/discovery_common/src/discovery.c b/bundles/remote_services/discovery_common/src/discovery.c
index 4300a84..f8c80a2 100644
--- a/bundles/remote_services/discovery_common/src/discovery.c
+++ b/bundles/remote_services/discovery_common/src/discovery.c
@@ -41,6 +41,12 @@
 celix_status_t discovery_endpointAdded(void *handle, endpoint_description_t *endpoint, char *matchedFilter) {
 	celix_status_t status;
 	discovery_t *discovery = handle;
+	celixThreadMutex_lock(&discovery->mutex);
+	if (discovery->stopped) {// we should not try use discovery->server when discovery is stopped
+		celixThreadMutex_unlock(&discovery->mutex);
+		return CELIX_SUCCESS;
+	}
+	celixThreadMutex_unlock(&discovery->mutex);
 
 	celix_logHelper_info(discovery->loghelper, "Endpoint for %s, with filter \"%s\" added...", endpoint->service, matchedFilter);
 
@@ -53,6 +59,13 @@
 	celix_status_t status;
 	discovery_t *discovery = handle;
 
+	celixThreadMutex_lock(&discovery->mutex);
+	if (discovery->stopped) {// we should not try use discovery->server when discovery is stopped
+		celixThreadMutex_unlock(&discovery->mutex);
+		return CELIX_SUCCESS;
+	}
+	celixThreadMutex_unlock(&discovery->mutex);
+
     celix_logHelper_info(discovery->loghelper, "Endpoint for %s, with filter \"%s\" removed...", endpoint->service, matchedFilter);
 
 	status = endpointDiscoveryServer_removeEndpoint(discovery->server, endpoint);
@@ -83,7 +96,7 @@
 	if (discoveryListener != NULL && strcmp(discoveryListener, "true") == 0) {
         celix_logHelper_info(discovery->loghelper, "EndpointListener Ignored - Discovery listener");
 	} else {
-		celixThreadMutex_lock(&discovery->discoveredServicesMutex);
+		celixThreadMutex_lock(&discovery->mutex);
 
 		hash_map_iterator_pt iter = hashMapIterator_create(discovery->discoveredServices);
 		while (hashMapIterator_hasNext(iter)) {
@@ -101,13 +114,9 @@
 		}
 		hashMapIterator_destroy(iter);
 
-		celixThreadMutex_unlock(&discovery->discoveredServicesMutex);
-
-		celixThreadMutex_lock(&discovery->listenerReferencesMutex);
-
 		hashMap_put(discovery->listenerReferences, reference, NULL);
 
-		celixThreadMutex_unlock(&discovery->listenerReferencesMutex);
+		celixThreadMutex_unlock(&discovery->mutex);
 	}
 
 	celix_filter_destroy(filter);
@@ -130,7 +139,7 @@
     celix_status_t status;
     discovery_t *discovery = handle;
 
-    status = celixThreadMutex_lock(&discovery->listenerReferencesMutex);
+    status = celixThreadMutex_lock(&discovery->mutex);
 
     if (status == CELIX_SUCCESS) {
         if (discovery->listenerReferences != NULL) {
@@ -139,52 +148,47 @@
             }
         }
 
-        status = celixThreadMutex_unlock(&discovery->listenerReferencesMutex);
+        celixThreadMutex_unlock(&discovery->mutex);
     }
 
 	return status;
 }
 
 celix_status_t discovery_informEndpointListeners(discovery_t *discovery, endpoint_description_t *endpoint, bool endpointAdded) {
-	celix_status_t status;
+	celix_status_t status = CELIX_SUCCESS;
 
 	// Inform listeners of new endpoint
-	status = celixThreadMutex_lock(&discovery->listenerReferencesMutex);
 
-    if (status == CELIX_SUCCESS) {
-        if (discovery->listenerReferences != NULL) {
-            hash_map_iterator_pt iter = hashMapIterator_create(discovery->listenerReferences);
-            while (hashMapIterator_hasNext(iter)) {
-                hash_map_entry_pt entry = hashMapIterator_nextEntry(iter);
+    if (discovery->listenerReferences != NULL) {
+        hash_map_iterator_pt iter = hashMapIterator_create(discovery->listenerReferences);
+        while (hashMapIterator_hasNext(iter)) {
+            hash_map_entry_pt entry = hashMapIterator_nextEntry(iter);
 
-                service_reference_pt reference = hashMapEntry_getKey(entry);
-                endpoint_listener_t *listener = NULL;
+            service_reference_pt reference = hashMapEntry_getKey(entry);
+            endpoint_listener_t *listener = NULL;
 
-                const char* scope = NULL;
-                serviceReference_getProperty(reference, OSGI_ENDPOINT_LISTENER_SCOPE, &scope);
+            const char* scope = NULL;
+            serviceReference_getProperty(reference, OSGI_ENDPOINT_LISTENER_SCOPE, &scope);
 
-                celix_filter_t *filter = celix_filter_create(scope);
-                bool matchResult = celix_filter_match(filter, endpoint->properties);
-                if (matchResult) {
-                    bundleContext_getService(discovery->context, reference, (void **) &listener);
-                    if (endpointAdded) {
-                        celix_logHelper_debug(discovery->loghelper, "Adding service (%s)", endpoint->service);
+            celix_filter_t *filter = celix_filter_create(scope);
+            bool matchResult = celix_filter_match(filter, endpoint->properties);
+            if (matchResult) {
+                bundleContext_getService(discovery->context, reference, (void **) &listener);
+                if (endpointAdded) {
+                    celix_logHelper_debug(discovery->loghelper, "Adding service (%s)", endpoint->service);
 
-                        listener->endpointAdded(listener->handle, endpoint, (char*)scope);
-                    } else {
-                        celix_logHelper_debug(discovery->loghelper, "Removing service (%s)", endpoint->service);
+                    listener->endpointAdded(listener->handle, endpoint, (char*)scope);
+                } else {
+                    celix_logHelper_debug(discovery->loghelper, "Removing service (%s)", endpoint->service);
 
-                        listener->endpointRemoved(listener->handle, endpoint, (char*)scope);
-                    }
-                    bundleContext_ungetService(discovery->context, reference, NULL);
+                    listener->endpointRemoved(listener->handle, endpoint, (char*)scope);
                 }
-
-                celix_filter_destroy(filter);
+                bundleContext_ungetService(discovery->context, reference, NULL);
             }
-            hashMapIterator_destroy(iter);
-        }
 
-        status = celixThreadMutex_unlock(&discovery->listenerReferencesMutex);
+            celix_filter_destroy(filter);
+        }
+        hashMapIterator_destroy(iter);
     }
 
 	return status;
@@ -193,7 +197,7 @@
 celix_status_t discovery_addDiscoveredEndpoint(discovery_t *discovery, endpoint_description_t *endpoint) {
 	celix_status_t status;
 
-	status = celixThreadMutex_lock(&discovery->discoveredServicesMutex);
+	status = celixThreadMutex_lock(&discovery->mutex);
 
     if (status == CELIX_SUCCESS) {
         char *endpointId = endpoint->id;
@@ -202,12 +206,11 @@
             hashMap_put(discovery->discoveredServices, endpointId, endpoint);
         }
 
-        status = celixThreadMutex_unlock(&discovery->discoveredServicesMutex);
-
         if (!exists) {
             // notify our listeners that a new endpoint is available...
             discovery_informEndpointListeners(discovery, endpoint, true /* addingService */);
         }
+        celixThreadMutex_unlock(&discovery->mutex);
     }
 
 	return status;
@@ -216,17 +219,17 @@
 celix_status_t discovery_removeDiscoveredEndpoint(discovery_t *discovery, endpoint_description_t *endpoint) {
 	celix_status_t status;
 
-	status = celixThreadMutex_lock(&discovery->discoveredServicesMutex);
+	status = celixThreadMutex_lock(&discovery->mutex);
 
     if (status == CELIX_SUCCESS) {
         char *endpointId = endpoint->id;
         void *oldValue = hashMap_remove(discovery->discoveredServices, endpointId);
 
-        status = celixThreadMutex_unlock(&discovery->discoveredServicesMutex);
-
         if (oldValue) {
             status = discovery_informEndpointListeners(discovery, endpoint, false /* removeService */);
         }
+
+        celixThreadMutex_unlock(&discovery->mutex);
     }
 
 	return status;
diff --git a/bundles/remote_services/discovery_configured/src/discovery_impl.c b/bundles/remote_services/discovery_configured/src/discovery_impl.c
index c4c7328..b0a013a 100644
--- a/bundles/remote_services/discovery_configured/src/discovery_impl.c
+++ b/bundles/remote_services/discovery_configured/src/discovery_impl.c
@@ -39,7 +39,7 @@
 
 
 celix_status_t discovery_create(celix_bundle_context_t *context, discovery_t **discovery) {
-	celix_status_t status;
+	celix_status_t status = CELIX_SUCCESS;
 
 	*discovery = malloc(sizeof(struct discovery));
 	if (!*discovery) {
@@ -49,12 +49,12 @@
 		(*discovery)->context = context;
 		(*discovery)->poller = NULL;
 		(*discovery)->server = NULL;
+		(*discovery)->stopped = false;
 
 		(*discovery)->listenerReferences = hashMap_create(serviceReference_hashCode, NULL, serviceReference_equals2, NULL);
 		(*discovery)->discoveredServices = hashMap_create(utils_stringHash, NULL, utils_stringEquals, NULL);
 
-		status = celixThreadMutex_create(&(*discovery)->listenerReferencesMutex, NULL);
-		status = celixThreadMutex_create(&(*discovery)->discoveredServicesMutex, NULL);
+		celixThreadMutex_create(&(*discovery)->mutex, NULL);
 
         (*discovery)->loghelper = celix_logHelper_create(context, "celix_rsa_discovery");
 	}
@@ -81,6 +81,10 @@
 celix_status_t discovery_stop(discovery_t *discovery) {
 	celix_status_t status;
 
+	celixThreadMutex_lock(&discovery->mutex);
+	discovery->stopped = true;
+	celixThreadMutex_unlock(&discovery->mutex);
+
 	status = endpointDiscoveryServer_destroy(discovery->server);
 	status = endpointDiscoveryPoller_destroy(discovery->poller);
 
@@ -94,23 +98,17 @@
 	discovery->poller = NULL;
 	discovery->server = NULL;
 
-	celixThreadMutex_lock(&discovery->discoveredServicesMutex);
+	celixThreadMutex_lock(&discovery->mutex);
 
 	hashMap_destroy(discovery->discoveredServices, false, false);
 	discovery->discoveredServices = NULL;
 
-	celixThreadMutex_unlock(&discovery->discoveredServicesMutex);
-
-	celixThreadMutex_destroy(&discovery->discoveredServicesMutex);
-
-	celixThreadMutex_lock(&discovery->listenerReferencesMutex);
-
 	hashMap_destroy(discovery->listenerReferences, false, false);
 	discovery->listenerReferences = NULL;
 
-	celixThreadMutex_unlock(&discovery->listenerReferencesMutex);
+	celixThreadMutex_unlock(&discovery->mutex);
 
-	celixThreadMutex_destroy(&discovery->listenerReferencesMutex);
+	celixThreadMutex_destroy(&discovery->mutex);
 
     celix_logHelper_destroy(discovery->loghelper);
 
diff --git a/bundles/remote_services/discovery_etcd/src/discovery_impl.c b/bundles/remote_services/discovery_etcd/src/discovery_impl.c
index 0294e80..4503a8f 100644
--- a/bundles/remote_services/discovery_etcd/src/discovery_impl.c
+++ b/bundles/remote_services/discovery_etcd/src/discovery_impl.c
@@ -52,13 +52,13 @@
         discovery->context = context;
         discovery->poller = NULL;
         discovery->server = NULL;
+        discovery->stopped = false;
 
         discovery->listenerReferences = hashMap_create(serviceReference_hashCode, NULL, serviceReference_equals2,
                                                           NULL);
         discovery->discoveredServices = hashMap_create(utils_stringHash, NULL, utils_stringEquals, NULL);
 
-        status = celixThreadMutex_create(&discovery->listenerReferencesMutex, NULL);
-        status = celixThreadMutex_create(&discovery->discoveredServicesMutex, NULL);
+        celixThreadMutex_create(&discovery->mutex, NULL);
 
         discovery->loghelper = celix_logHelper_create(context, "celix_rsa_discovery");
     } else {
@@ -83,23 +83,17 @@
     discovery->poller = NULL;
     discovery->server = NULL;
 
-    celixThreadMutex_lock(&discovery->discoveredServicesMutex);
+    celixThreadMutex_lock(&discovery->mutex);
 
     hashMap_destroy(discovery->discoveredServices, false, false);
     discovery->discoveredServices = NULL;
 
-    celixThreadMutex_unlock(&discovery->discoveredServicesMutex);
-
-    celixThreadMutex_destroy(&discovery->discoveredServicesMutex);
-
-    celixThreadMutex_lock(&discovery->listenerReferencesMutex);
-
     hashMap_destroy(discovery->listenerReferences, false, false);
     discovery->listenerReferences = NULL;
 
-    celixThreadMutex_unlock(&discovery->listenerReferencesMutex);
+    celixThreadMutex_unlock(&discovery->mutex);
 
-    celixThreadMutex_destroy(&discovery->listenerReferencesMutex);
+    celixThreadMutex_destroy(&discovery->mutex);
 
     celix_logHelper_destroy(discovery->loghelper);
 
@@ -144,6 +138,10 @@
 celix_status_t discovery_stop(discovery_t *discovery) {
     celix_status_t status;
 
+    celixThreadMutex_lock(&discovery->mutex);
+    discovery->stopped = true;
+    celixThreadMutex_unlock(&discovery->mutex);
+
     status = etcdWatcher_destroy(discovery->pImpl->watcher);
     if (status != CELIX_SUCCESS) {
         return CELIX_BUNDLE_EXCEPTION;
@@ -160,7 +158,7 @@
     }
     hash_map_iterator_pt iter;
 
-    celixThreadMutex_lock(&discovery->discoveredServicesMutex);
+    celixThreadMutex_lock(&discovery->mutex);
 
     iter = hashMapIterator_create(discovery->discoveredServices);
     while (hashMapIterator_hasNext(iter)) {
@@ -171,7 +169,7 @@
     }
     hashMapIterator_destroy(iter);
 
-    celixThreadMutex_unlock(&discovery->discoveredServicesMutex);
+    celixThreadMutex_unlock(&discovery->mutex);
 
     return status;
 }
diff --git a/bundles/remote_services/discovery_shm/src/discovery_impl.c b/bundles/remote_services/discovery_shm/src/discovery_impl.c
index cc6751d..71824e1 100644
--- a/bundles/remote_services/discovery_shm/src/discovery_impl.c
+++ b/bundles/remote_services/discovery_shm/src/discovery_impl.c
@@ -59,13 +59,13 @@
         discovery->context = context;
         discovery->poller = NULL;
         discovery->server = NULL;
+        discovery->stopped = false;
+
+        celixThreadMutex_create(&discovery->mutex, NULL);
 
         discovery->listenerReferences = hashMap_create(serviceReference_hashCode, NULL, serviceReference_equals2, NULL);
         discovery->discoveredServices = hashMap_create(utils_stringHash, NULL, utils_stringEquals, NULL);
 
-        celixThreadMutex_create(&discovery->listenerReferencesMutex, NULL);
-        celixThreadMutex_create(&discovery->discoveredServicesMutex, NULL);
-
         discovery->loghelper = celix_logHelper_create(context, "rsa_discovery");
     } else {
         status = CELIX_ENOMEM;
@@ -89,23 +89,18 @@
 	discovery->poller = NULL;
 	discovery->server = NULL;
 
-	celixThreadMutex_lock(&discovery->discoveredServicesMutex);
+	celixThreadMutex_lock(&discovery->mutex);
 
 	hashMap_destroy(discovery->discoveredServices, false, false);
 	discovery->discoveredServices = NULL;
 
-	celixThreadMutex_unlock(&discovery->discoveredServicesMutex);
-
-	celixThreadMutex_destroy(&discovery->discoveredServicesMutex);
-
-	celixThreadMutex_lock(&discovery->listenerReferencesMutex);
-
 	hashMap_destroy(discovery->listenerReferences, false, false);
 	discovery->listenerReferences = NULL;
 
-	celixThreadMutex_unlock(&discovery->listenerReferencesMutex);
+	celixThreadMutex_unlock(&discovery->mutex);
 
-	celixThreadMutex_destroy(&discovery->listenerReferencesMutex);
+    celixThreadMutex_destroy(&discovery->mutex);
+
 
     free(discovery->pImpl);
 	free(discovery);
@@ -131,6 +126,10 @@
 celix_status_t discovery_stop(discovery_t *discovery) {
 	celix_status_t status;
 
+    celixThreadMutex_lock(&discovery->mutex);
+    discovery->stopped = true;
+    celixThreadMutex_unlock(&discovery->mutex);
+
     status = discoveryShmWatcher_destroy(discovery);
 
     if (status == CELIX_SUCCESS) {
@@ -142,7 +141,7 @@
 	if (status == CELIX_SUCCESS) {
         hash_map_iterator_pt iter;
 
-        celixThreadMutex_lock(&discovery->discoveredServicesMutex);
+        celixThreadMutex_lock(&discovery->mutex);
 
         iter = hashMapIterator_create(discovery->discoveredServices);
         while (hashMapIterator_hasNext(iter)) {
@@ -153,7 +152,7 @@
         }
         hashMapIterator_destroy(iter);
 
-        celixThreadMutex_unlock(&discovery->discoveredServicesMutex);
+        celixThreadMutex_unlock(&discovery->mutex);
 
         celix_logHelper_destroy(discovery->loghelper);
 	}