celix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bpe...@apache.org
Subject celix git commit: CELIX-232: Add lock for bundleListener list
Date Thu, 14 May 2015 15:44:01 GMT
Repository: celix
Updated Branches:
  refs/heads/master 614b0bdc2 -> 48e036cfa


CELIX-232: Add lock for bundleListener list


Project: http://git-wip-us.apache.org/repos/asf/celix/repo
Commit: http://git-wip-us.apache.org/repos/asf/celix/commit/48e036cf
Tree: http://git-wip-us.apache.org/repos/asf/celix/tree/48e036cf
Diff: http://git-wip-us.apache.org/repos/asf/celix/diff/48e036cf

Branch: refs/heads/master
Commit: 48e036cfa82d1890c6c34e9d25da3c3261d7a990
Parents: 614b0bd
Author: Bjoern Petri <bpetri@apache.org>
Authored: Thu May 14 17:43:09 2015 +0200
Committer: Bjoern Petri <bpetri@apache.org>
Committed: Thu May 14 17:43:09 2015 +0200

----------------------------------------------------------------------
 framework/private/include/framework_private.h |  4 +-
 framework/private/src/framework.c             | 89 ++++++++++++++--------
 2 files changed, 61 insertions(+), 32 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/celix/blob/48e036cf/framework/private/include/framework_private.h
----------------------------------------------------------------------
diff --git a/framework/private/include/framework_private.h b/framework/private/include/framework_private.h
index 40a07d7..77b41cf 100644
--- a/framework/private/include/framework_private.h
+++ b/framework/private/include/framework_private.h
@@ -53,9 +53,11 @@ struct framework {
     hash_map_pt installedBundleMap;
     hash_map_pt installRequestMap;
     array_list_pt serviceListeners;
-    array_list_pt bundleListeners;
     array_list_pt frameworkListeners;
 
+    array_list_pt bundleListeners;
+    celix_thread_mutex_t bundleListenerLock;
+
     long nextBundleId;
     struct serviceRegistry * registry;
     bundle_cache_pt cache;

http://git-wip-us.apache.org/repos/asf/celix/blob/48e036cf/framework/private/src/framework.c
----------------------------------------------------------------------
diff --git a/framework/private/src/framework.c b/framework/private/src/framework.c
index ba3cbaf..95c86e6 100644
--- a/framework/private/src/framework.c
+++ b/framework/private/src/framework.c
@@ -200,6 +200,7 @@ celix_status_t framework_create(framework_pt *framework, apr_pool_t *pool,
prope
         status = CELIX_DO_IF(status, celixThreadMutex_create(&(*framework)->bundleLock,
NULL));
         status = CELIX_DO_IF(status, celixThreadMutex_create(&(*framework)->installRequestLock,
NULL));
         status = CELIX_DO_IF(status, celixThreadMutex_create(&(*framework)->dispatcherLock,
NULL));
+        status = CELIX_DO_IF(status, celixThreadMutex_create(&(*framework)->bundleListenerLock,
NULL));
         status = CELIX_DO_IF(status, celixThreadCondition_init(&(*framework)->dispatcher,
NULL));
         if (status == CELIX_SUCCESS) {
             (*framework)->pool = pool;
@@ -321,6 +322,7 @@ celix_status_t framework_destroy(framework_pt framework) {
 	}
 
 	celixThreadCondition_destroy(&framework->dispatcher);
+	celixThreadMutex_destroy(&framework->bundleListenerLock);
 	celixThreadMutex_destroy(&framework->dispatcherLock);
 	celixThreadMutex_destroy(&framework->installRequestLock);
 	celixThreadMutex_destroy(&framework->bundleLock);
@@ -1497,7 +1499,15 @@ celix_status_t fw_addBundleListener(framework_pt framework, bundle_pt
bundle, bu
 		bundleListener->listener = listener;
 		bundleListener->bundle = bundle;
 
-		arrayList_add(framework->bundleListeners, bundleListener);
+		if (celixThreadMutex_lock(&framework->bundleListenerLock) != CELIX_SUCCESS) {
+			status = CELIX_FRAMEWORK_EXCEPTION;
+		} else {
+			arrayList_add(framework->bundleListeners, bundleListener);
+
+			if (celixThreadMutex_unlock(&framework->bundleListenerLock)) {
+				status = CELIX_FRAMEWORK_EXCEPTION;
+			}
+		}
 	}
 
 	framework_logIfError(framework->logger, status, NULL, "Failed to add bundle listener");
@@ -1511,14 +1521,22 @@ celix_status_t fw_removeBundleListener(framework_pt framework, bundle_pt
bundle,
 	unsigned int i;
 	fw_bundle_listener_pt bundleListener;
 
-	for (i = 0; i < arrayList_size(framework->bundleListeners); i++) {
-		bundleListener = (fw_bundle_listener_pt) arrayList_get(framework->bundleListeners, i);
-		if (bundleListener->listener == listener && bundleListener->bundle == bundle)
{
-			arrayList_remove(framework->bundleListeners, i);
+	if (celixThreadMutex_lock(&framework->bundleListenerLock) != CELIX_SUCCESS) {
+		status = CELIX_FRAMEWORK_EXCEPTION;
+	}
+	else {
+		for (i = 0; i < arrayList_size(framework->bundleListeners); i++) {
+			bundleListener = (fw_bundle_listener_pt) arrayList_get(framework->bundleListeners,
i);
+			if (bundleListener->listener == listener && bundleListener->bundle == bundle)
{
+				arrayList_remove(framework->bundleListeners, i);
 
-			bundleListener->bundle = NULL;
-			bundleListener->listener = NULL;
-			free(bundleListener);
+				bundleListener->bundle = NULL;
+				bundleListener->listener = NULL;
+				free(bundleListener);
+			}
+		}
+		if (celixThreadMutex_unlock(&framework->bundleListenerLock)) {
+			status = CELIX_FRAMEWORK_EXCEPTION;
 		}
 	}
 
@@ -2209,29 +2227,38 @@ static void *fw_eventDispatcher(void *fw) {
 		}
 
 		if (validReq) {
-			int i;
-			int size = arrayList_size(request->listeners);
-			for (i = 0; i < size; i++) {
-				if (request->type == BUNDLE_EVENT_TYPE) {
-					fw_bundle_listener_pt listener = (fw_bundle_listener_pt) arrayList_get(request->listeners,
i);
-					bundle_event_pt event = (bundle_event_pt) malloc(sizeof(*event));
-					event->bundle = request->bundle;
-					event->type = request->eventType;
-
-					fw_invokeBundleListener(framework, listener->listener, event, listener->bundle);
-
-					free(event);
-				} else if (request->type == FRAMEWORK_EVENT_TYPE) {
-					fw_framework_listener_pt listener = (fw_framework_listener_pt) arrayList_get(request->listeners,
i);
-					framework_event_pt event = (framework_event_pt) malloc(sizeof(*event));
-					event->bundle = request->bundle;
-					event->type = request->eventType;
-					event->error = request->error;
-					event->errorCode = request->errorCode;
-
-					fw_invokeFrameworkListener(framework, listener->listener, event, listener->bundle);
-
-					free(event);
+			if (celixThreadMutex_lock(&framework->bundleListenerLock) != CELIX_SUCCESS) {
+				status = CELIX_FRAMEWORK_EXCEPTION;
+			}
+			else {
+				int i;
+				int size = arrayList_size(request->listeners);
+				for (i = 0; i < size; i++) {
+					if (request->type == BUNDLE_EVENT_TYPE) {
+						fw_bundle_listener_pt listener = (fw_bundle_listener_pt) arrayList_get(request->listeners,
i);
+						bundle_event_pt event = (bundle_event_pt) malloc(sizeof(*event));
+						event->bundle = request->bundle;
+						event->type = request->eventType;
+
+						fw_invokeBundleListener(framework, listener->listener, event, listener->bundle);
+
+						free(event);
+					} else if (request->type == FRAMEWORK_EVENT_TYPE) {
+						fw_framework_listener_pt listener = (fw_framework_listener_pt) arrayList_get(request->listeners,
i);
+						framework_event_pt event = (framework_event_pt) malloc(sizeof(*event));
+						event->bundle = request->bundle;
+						event->type = request->eventType;
+						event->error = request->error;
+						event->errorCode = request->errorCode;
+
+						fw_invokeFrameworkListener(framework, listener->listener, event, listener->bundle);
+
+						free(event);
+					}
+				}
+
+				if (celixThreadMutex_unlock(&framework->bundleListenerLock)) {
+					status = CELIX_FRAMEWORK_EXCEPTION;
 				}
 			}
 		}


Mime
View raw message