You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@celix.apache.org by pn...@apache.org on 2015/11/17 12:57:51 UTC

[34/38] celix git commit: CELIX-272: Introduce celixThread_once code and call in the framwork to ensure a single logger initialization even when runnign mutiple frameworks in the same process

CELIX-272: Introduce celixThread_once code and call in the framwork to ensure a single logger initialization even when runnign mutiple frameworks in the same process


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

Branch: refs/heads/develop
Commit: a40693f56a416085f2bca95aa4018ea89057cbe1
Parents: cf7acae
Author: Pepijn Noltes <pe...@gmail.com>
Authored: Tue Nov 17 10:15:05 2015 +0100
Committer: Pepijn Noltes <pe...@gmail.com>
Committed: Tue Nov 17 10:15:05 2015 +0100

----------------------------------------------------------------------
 framework/private/src/framework.c    | 10 ++++++++--
 utils/private/src/celix_threads.c    |  4 ++++
 utils/public/include/celix_threads.h |  3 +++
 3 files changed, 15 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/celix/blob/a40693f5/framework/private/src/framework.c
----------------------------------------------------------------------
diff --git a/framework/private/src/framework.c b/framework/private/src/framework.c
index 84f428d..0823781 100644
--- a/framework/private/src/framework.c
+++ b/framework/private/src/framework.c
@@ -152,6 +152,13 @@ typedef struct request *request_pt;
 
 framework_logger_pt logger;
 
+//TODO introduce a counter + mutex to control the freeing of the logger when mutiple threads are running a framework.
+static celix_thread_once_t loggerInit = CELIX_THREAD_ONCE_INIT;
+static void framework_loggerInit(void) {
+    logger = malloc(sizeof(*logger));
+    logger->logFunction = frameworkLogger_log;
+}
+
 #ifdef _WIN32
     #define handle_t HMODULE
     #define fw_openLibrary(path) LoadLibrary(path)
@@ -179,8 +186,7 @@ celix_status_t framework_create(framework_pt *framework, properties_pt config) {
 
     logger = hashMap_get(config, "logger");
     if (logger == NULL) {
-        logger = malloc(sizeof(*logger));
-        logger->logFunction = frameworkLogger_log;
+        celixThread_once(&loggerInit, framework_loggerInit);
     }
 
     *framework = (framework_pt) malloc(sizeof(**framework));

http://git-wip-us.apache.org/repos/asf/celix/blob/a40693f5/utils/private/src/celix_threads.c
----------------------------------------------------------------------
diff --git a/utils/private/src/celix_threads.c b/utils/private/src/celix_threads.c
index bec374b..64bdf5b 100644
--- a/utils/private/src/celix_threads.c
+++ b/utils/private/src/celix_threads.c
@@ -178,3 +178,7 @@ celix_status_t celixThreadRwlockAttr_create(celix_thread_rwlockattr_t *attr) {
 celix_status_t celixThreadRwlockAttr_destroy(celix_thread_rwlockattr_t *attr) {
 	return pthread_rwlockattr_destroy(attr);
 }
+
+celix_status_t celixThread_once(celix_thread_once_t *once_control, void (*init_routine)(void)) {
+	return pthread_once(once_control, init_routine);
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/celix/blob/a40693f5/utils/public/include/celix_threads.h
----------------------------------------------------------------------
diff --git a/utils/public/include/celix_threads.h b/utils/public/include/celix_threads.h
index 1e7a7ad..8fc043e 100644
--- a/utils/public/include/celix_threads.h
+++ b/utils/public/include/celix_threads.h
@@ -38,6 +38,8 @@ struct celix_thread {
 	pthread_t thread;
 };
 
+typedef pthread_once_t celix_thread_once_t;
+#define CELIX_THREAD_ONCE_INIT PTHREAD_ONCE_INIT
 
 typedef struct celix_thread celix_thread_t;
 typedef pthread_attr_t celix_thread_attr_t;
@@ -101,5 +103,6 @@ celix_status_t celixThreadCondition_wait(celix_thread_cond_t *cond, celix_thread
 celix_status_t celixThreadCondition_broadcast(celix_thread_cond_t *cond);
 celix_status_t celixThreadCondition_signal(celix_thread_cond_t *cond);
 
+celix_status_t celixThread_once(celix_thread_once_t *once_control, void (*init_routine)(void));
 
 #endif /* CELIX_THREADS_H_ */