celix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bpe...@apache.org
Subject svn commit: r1645516 - in /celix/trunk: framework/private/src/bundle.c framework/private/src/framework.c log_service/private/src/log.c remote_shell/private/src/remote_shell.c utils/private/src/celix_threads.c utils/public/include/celix_threads.h
Date Sun, 14 Dec 2014 20:06:43 GMT
Author: bpetri
Date: Sun Dec 14 20:06:43 2014
New Revision: 1645516

URL: http://svn.apache.org/r1645516
Log:
CELIX-193: replaced celix_thread_t with wrapper to support portable initailization

Modified:
    celix/trunk/framework/private/src/bundle.c
    celix/trunk/framework/private/src/framework.c
    celix/trunk/log_service/private/src/log.c
    celix/trunk/remote_shell/private/src/remote_shell.c
    celix/trunk/utils/private/src/celix_threads.c
    celix/trunk/utils/public/include/celix_threads.h

Modified: celix/trunk/framework/private/src/bundle.c
URL: http://svn.apache.org/viewvc/celix/trunk/framework/private/src/bundle.c?rev=1645516&r1=1645515&r2=1645516&view=diff
==============================================================================
--- celix/trunk/framework/private/src/bundle.c (original)
+++ celix/trunk/framework/private/src/bundle.c Sun Dec 14 20:06:43 2014
@@ -72,7 +72,7 @@ celix_status_t bundle_create(bundle_pt *
         	status = CELIX_ILLEGAL_STATE;
         } else {
 			(*bundle)->lockCount = 0;
-			(*bundle)->lockThread = 0;
+			(*bundle)->lockThread = celix_thread_default;
 
 			resolver_addModule(module);
         }
@@ -108,7 +108,7 @@ celix_status_t bundle_createFromArchive(
 			status = CELIX_ILLEGAL_STATE;
 		} else {
 			(*bundle)->lockCount = 0;
-			(*bundle)->lockThread = 0;
+			(*bundle)->lockThread = celix_thread_default;
 
 			resolver_addModule(module);
 		}
@@ -557,7 +557,7 @@ celix_status_t bundle_unlock(bundle_pt b
 			}
 			bundle->lockCount--;
 			if (bundle->lockCount == 0) {
-				bundle->lockThread = 0;
+				bundle->lockThread = celix_thread_default;
 			}
 			*unlocked = true;
 		}

Modified: celix/trunk/framework/private/src/framework.c
URL: http://svn.apache.org/viewvc/celix/trunk/framework/private/src/framework.c?rev=1645516&r1=1645515&r2=1645516&view=diff
==============================================================================
--- celix/trunk/framework/private/src/framework.c (original)
+++ celix/trunk/framework/private/src/framework.c Sun Dec 14 20:06:43 2014
@@ -186,7 +186,7 @@ celix_status_t framework_create(framewor
             (*framework)->shutdown = false;
             (*framework)->globalLockWaitersList = NULL;
             (*framework)->globalLockCount = 0;
-            (*framework)->globalLockThread = 0;
+            (*framework)->globalLockThread = celix_thread_default;
             (*framework)->nextBundleId = 1l;
             (*framework)->cache = NULL;
             (*framework)->installRequestMap = hashMap_create(utils_stringHash, utils_stringHash,
utils_stringEquals, utils_stringEquals);
@@ -1797,7 +1797,7 @@ celix_status_t framework_acquireBundleLo
 	celix_status_t status = CELIX_SUCCESS;
 
 	bool locked;
-	celix_thread_t lockingThread = 0;
+	celix_thread_t lockingThread = celix_thread_default;
 
 	int err = celixThreadMutex_lock(&framework->bundleLock);
 	if (err != CELIX_SUCCESS) {
@@ -1811,7 +1811,7 @@ celix_status_t framework_acquireBundleLo
 		thread_equalsSelf(framework->globalLockThread, &isSelf);
 
 		while (!lockable
-				|| ((framework->globalLockThread != 0)
+				|| (( celixThread_initalized(framework->globalLockThread) == true)
 				&& !isSelf)) {
 			bundle_state_e state;
 			bundle_getState(bundle, &state);
@@ -1821,7 +1821,7 @@ celix_status_t framework_acquireBundleLo
 			} else
 				bundle_getLockingThread(bundle, &lockingThread);
 				if (isSelf
-					&& (lockingThread != 0)
+					&& (celixThread_initalized(lockingThread) == true)
 					&& arrayList_contains(framework->globalLockWaitersList, &lockingThread))
{
 				framework->interrupted = true;
 //				celixThreadCondition_signal_thread_np(&framework->condition, bundle_getLockingThread(bundle));
@@ -1859,7 +1859,7 @@ celix_status_t framework_acquireBundleLo
 
 bool framework_releaseBundleLock(framework_pt framework, bundle_pt bundle) {
     bool unlocked;
-    celix_thread_t lockingThread = 0;
+    celix_thread_t lockingThread = celix_thread_default;
 
     celixThreadMutex_lock(&framework->bundleLock);
 
@@ -1869,7 +1869,7 @@ bool framework_releaseBundleLock(framewo
 		return false;
 	}
 	bundle_getLockingThread(bundle, &lockingThread);
-	if (lockingThread == 0) {
+	if (celixThread_initalized(lockingThread) == false) {
 	    celixThreadCondition_broadcast(&framework->condition);
 	}
 
@@ -1887,7 +1887,7 @@ bool framework_acquireGlobalLock(framewo
 	thread_equalsSelf(framework->globalLockThread, &isSelf);
 
 	while (!interrupted
-			&& (framework->globalLockThread != 0)
+			&& (celixThread_initalized(framework->globalLockThread) == true)
 			&& (!isSelf)) {
 		celix_thread_t currentThread = celixThread_self();
 		arrayList_add(framework->globalLockWaitersList, &currentThread);
@@ -1919,11 +1919,11 @@ celix_status_t framework_releaseGlobalLo
 		return CELIX_FRAMEWORK_EXCEPTION;
 	}
 
-	if (framework->globalLockThread == celixThread_self()) {
+	if (celixThread_equals(framework->globalLockThread, celixThread_self())) {
 		framework->globalLockCount--;
 		if (framework->globalLockCount == 0) {
-			framework->globalLockThread = 0;
-			if (celixThreadCondition_broadcast(&framework->condition) != 0) {
+			framework->globalLockThread = celix_thread_default;
+ 			if (celixThreadCondition_broadcast(&framework->condition) != 0) {
 				fw_log(framework->logger, OSGI_FRAMEWORK_LOG_ERROR,  "Failed to broadcast global lock
release.");
 				status = CELIX_FRAMEWORK_EXCEPTION;
 				// still need to unlock before returning

Modified: celix/trunk/log_service/private/src/log.c
URL: http://svn.apache.org/viewvc/celix/trunk/log_service/private/src/log.c?rev=1645516&r1=1645515&r2=1645516&view=diff
==============================================================================
--- celix/trunk/log_service/private/src/log.c (original)
+++ celix/trunk/log_service/private/src/log.c Sun Dec 14 20:06:43 2014
@@ -62,7 +62,7 @@ celix_status_t log_create(log_pt *logger
 
 		(*logger)->listeners = NULL;
 		(*logger)->listenerEntries = NULL;
-		(*logger)->listenerThread = NULL;
+		(*logger)->listenerThread = celix_thread_default;
 		(*logger)->running = false;
 
 		arrayList_create(&(*logger)->listeners);
@@ -229,10 +229,6 @@ celix_status_t log_removeLogListener(log
 		if (last) {
 		    status = CELIX_DO_IF(status, celixThread_join(logger->listenerThread, NULL));
 		}
-
-		if (status == CELIX_SUCCESS) {
-			logger->listenerThread = NULL;
-		}
 	}
 
 	if (status != CELIX_SUCCESS) {

Modified: celix/trunk/remote_shell/private/src/remote_shell.c
URL: http://svn.apache.org/viewvc/celix/trunk/remote_shell/private/src/remote_shell.c?rev=1645516&r1=1645515&r2=1645516&view=diff
==============================================================================
--- celix/trunk/remote_shell/private/src/remote_shell.c (original)
+++ celix/trunk/remote_shell/private/src/remote_shell.c Sun Dec 14 20:06:43 2014
@@ -104,7 +104,7 @@ celix_status_t remoteShell_addConnection
 		celixThreadMutex_lock(&instance->mutex);
 
 		if (arrayList_size(instance->connections) < instance->maximumConnections) {
-			celix_thread_t connectionRunThread = NULL;
+			celix_thread_t connectionRunThread = celix_thread_default;
 			arrayList_add(instance->connections, connection);
 			status = celixThread_create(&connectionRunThread, NULL, &remoteShell_connection_run,
connection);
 		} else {

Modified: celix/trunk/utils/private/src/celix_threads.c
URL: http://svn.apache.org/viewvc/celix/trunk/utils/private/src/celix_threads.c?rev=1645516&r1=1645515&r2=1645516&view=diff
==============================================================================
--- celix/trunk/utils/private/src/celix_threads.c (original)
+++ celix/trunk/utils/private/src/celix_threads.c Sun Dec 14 20:06:43 2014
@@ -23,11 +23,21 @@
  *  \author     <a href="mailto:celix-dev@incubator.apache.org">Apache Celix Project
Team</a>
  *  \copyright  Apache License, Version 2.0
  */
-
+#include <stdlib.h>
 #include "celix_threads.h"
 
+
 celix_status_t celixThread_create(celix_thread_t *new_thread, celix_thread_attr_t *attr,
celix_thread_start_t func, void *data) {
-    return pthread_create(new_thread, attr, func, data);
+    celix_status_t status = CELIX_SUCCESS;
+
+	if (pthread_create(&(*new_thread).thread, attr, func, data) != 0) {
+		status = CELIX_BUNDLE_EXCEPTION;
+	}
+	else {
+		(*new_thread).threadInitialized = true;
+	}
+
+	return status;
 }
 
 celix_status_t celixThread_exit(void *exitStatus) {
@@ -37,21 +47,39 @@ celix_status_t celixThread_exit(void *ex
 }
 
 celix_status_t celixThread_detach(celix_thread_t thread) {
-    return pthread_detach(thread);
+    return pthread_detach(thread.thread);
 }
 
-celix_status_t celixThread_join(celix_thread_t thread, void **status) {
-    return pthread_join(thread, status);
+celix_status_t celixThread_join(celix_thread_t thread, void **retVal) {
+	celix_status_t status = CELIX_SUCCESS;
+
+	if (pthread_join(thread.thread, retVal) != 0) {
+		status = CELIX_BUNDLE_EXCEPTION;
+	}
+
+	thread.threadInitialized = false;
+
+    return status;
 }
 
 celix_thread_t celixThread_self() {
-    return pthread_self();
+	celix_thread_t thread;
+
+	thread.thread = pthread_self();
+	thread.threadInitialized = true;
+
+	return thread;
 }
 
 int celixThread_equals(celix_thread_t thread1, celix_thread_t thread2) {
-    return pthread_equal(thread1, thread2);
+    return pthread_equal(thread1.thread, thread2.thread);
 }
 
+bool celixThread_initalized(celix_thread_t thread) {
+    return thread.threadInitialized;
+}
+
+
 celix_status_t celixThreadMutex_create(celix_thread_mutex_t *mutex, celix_thread_mutexattr_t
*attr) {
     return pthread_mutex_init(mutex, attr);
 }

Modified: celix/trunk/utils/public/include/celix_threads.h
URL: http://svn.apache.org/viewvc/celix/trunk/utils/public/include/celix_threads.h?rev=1645516&r1=1645515&r2=1645516&view=diff
==============================================================================
--- celix/trunk/utils/public/include/celix_threads.h (original)
+++ celix/trunk/utils/public/include/celix_threads.h Sun Dec 14 20:06:43 2014
@@ -28,20 +28,33 @@
 #define CELIX_THREADS_H_
 
 #include <pthread.h>
+#include <stdbool.h>
 
 #include "celix_errno.h"
 
-typedef pthread_t celix_thread_t;
+
+struct celix_thread {
+	bool threadInitialized;
+	pthread_t thread;
+};
+
+
+typedef struct celix_thread celix_thread_t;
 typedef pthread_attr_t celix_thread_attr_t;
 
 typedef void *(*celix_thread_start_t)(void*);
 
+static const celix_thread_t celix_thread_default = { .threadInitialized = false };
+
+
 celix_status_t celixThread_create(celix_thread_t *new_thread, celix_thread_attr_t *attr,
celix_thread_start_t func, void *data);
 celix_status_t celixThread_exit(void *exitStatus);
 celix_status_t celixThread_detach(celix_thread_t thread);
 celix_status_t celixThread_join(celix_thread_t thread, void **status);
 celix_thread_t celixThread_self();
 int celixThread_equals(celix_thread_t thread1, celix_thread_t thread2);
+bool celixThread_initalized(celix_thread_t thread);
+
 
 typedef pthread_mutex_t celix_thread_mutex_t;
 typedef pthread_mutexattr_t celix_thread_mutexattr_t;



Mime
View raw message