From 60f1b2e516e4ea7bfa581a77897ce1a8d21b3603 Mon Sep 17 00:00:00 2001 From: Boram Park Date: Tue, 20 Feb 2018 18:17:32 +0900 Subject: [PATCH] config: export tdm_config functions Change-Id: I95bcd81ba94b21a7383f88e99a5b254effb10597 --- include/Makefile.am | 1 + {src => include}/tdm_config.h | 0 packaging/libtdm.spec | 1 + src/tdm.c | 7 ----- src/tdm_config.c | 71 +++++++++++++++++++++++-------------------- 5 files changed, 40 insertions(+), 40 deletions(-) rename {src => include}/tdm_config.h (100%) diff --git a/include/Makefile.am b/include/Makefile.am index aa1d502..ce83e6e 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -4,6 +4,7 @@ libtdminclude_HEADERS = \ tdm_common.h \ tdm_types.h \ tdm_list.h \ + tdm_config.h \ tdm_log.h \ tdm_backend.h \ tdm_helper.h diff --git a/src/tdm_config.h b/include/tdm_config.h similarity index 100% rename from src/tdm_config.h rename to include/tdm_config.h diff --git a/packaging/libtdm.spec b/packaging/libtdm.spec index 7140089..a8f8003 100644 --- a/packaging/libtdm.spec +++ b/packaging/libtdm.spec @@ -128,6 +128,7 @@ rm -f %{_unitdir_user}/basic.target.wants/tdm-socket-user.path %{_includedir}/tdm_list.h %{_includedir}/tdm_log.h %{_includedir}/tdm_types.h +%{_includedir}/tdm_config.h %{_libdir}/pkgconfig/libtdm.pc %{_libdir}/libtdm.so diff --git a/src/tdm.c b/src/tdm.c index 2b0de50..6306966 100644 --- a/src/tdm.c +++ b/src/tdm.c @@ -927,11 +927,6 @@ tdm_display_init(tdm_error *error) start = stamp1 = tdm_helper_get_time(); - /* tdm_config_init should be called first of all tdm apis for tdm_log */ - ret = tdm_config_init(); - if (ret != TDM_ERROR_NONE) - goto failed_config; - stamp2 = tdm_helper_get_time(); TDM_INFO("config init time: %.3f ms", (stamp2 - stamp1) * 1000.0); stamp1 = stamp2; @@ -1064,8 +1059,6 @@ failed_vblank: failed_mutex_init: free(private_display); failed_alloc: - tdm_config_deinit(); -failed_config: if (error) *error = ret; pthread_mutex_unlock(&gLock); diff --git a/src/tdm_config.c b/src/tdm_config.c index 15cc877..4c66e07 100644 --- a/src/tdm_config.c +++ b/src/tdm_config.c @@ -49,8 +49,8 @@ #define TDM_CONFIG_FILENAME "tdm.ini" #define TDM_CONFIG_GENERAL_SECTION "general" +static pthread_mutex_t g_dic_lock = PTHREAD_MUTEX_INITIALIZER; static dictionary *g_dic = NULL; -static pthread_mutex_t g_dic_lock; static int _tdm_config_check_file_owner(const char *filepath) @@ -101,6 +101,8 @@ _tdm_config_check_logs(void) const char *path; int level; + pthread_mutex_unlock(&g_dic_lock); + level = tdm_config_get_int(TDM_CONFIG_KEY_DEBUG_LOG_LEVEL, 3); tdm_log_set_debug_level(level); @@ -116,46 +118,41 @@ _tdm_config_check_logs(void) int dlog = tdm_config_get_int(TDM_CONFIG_KEY_DEBUG_DLOG, 1); tdm_log_enable_dlog(dlog); } + + pthread_mutex_lock(&g_dic_lock); } -INTERN tdm_error -tdm_config_init(void) +static unsigned int +_tdm_config_check_init(void) { - if (g_dic) { - TDM_ERR("init failed: twice"); - return TDM_ERROR_OPERATION_FAILED; - } - - if (pthread_mutex_init(&g_dic_lock, NULL)) { - TDM_ERR("mutex init failed: %m"); - return TDM_ERROR_OUT_OF_MEMORY; - } + if (g_dic) + return 1; g_dic = _tdm_config_load_file(TDM_DATA_PATH, TDM_CONFIG_FILENAME); _tdm_config_check_logs(); - TDM_INFO("tdm config init done (%p)", g_dic); + TDM_INFO("tdm config init %s (%p)", (g_dic) ? "successed" : "failed", g_dic); - return TDM_ERROR_NONE; + return (g_dic) ? 1 : 0; } INTERN void tdm_config_deinit(void) { + pthread_mutex_lock(&g_dic_lock); + if (!g_dic) { + pthread_mutex_unlock(&g_dic_lock); return; } iniparser_freedict(g_dic); g_dic = NULL; - /* we don't need to lock/unlock here because we come here - * after tdm_thread has been destroyed - */ - pthread_mutex_destroy(&g_dic_lock); - TDM_INFO("tdm config deinit done"); + + pthread_mutex_unlock(&g_dic_lock); } static const char* @@ -183,7 +180,7 @@ _tdm_config_get_string_internal(dictionary *dic, const char *key, const char *de return result; } -INTERN int +EXTERN int tdm_config_get_int(const char *key, int default_value) { const char *result; @@ -191,12 +188,14 @@ tdm_config_get_int(const char *key, int default_value) TDM_RETURN_VAL_IF_FAIL(key != NULL, default_value); - if (!g_dic) { + pthread_mutex_lock(&g_dic_lock); + + if (!_tdm_config_check_init()) { TDM_INFO("%s = %d: default", key, default_value); + pthread_mutex_unlock(&g_dic_lock); return default_value; } - pthread_mutex_lock(&g_dic_lock); result = _tdm_config_get_string_internal(g_dic, key, NULL); pthread_mutex_unlock(&g_dic_lock); @@ -212,19 +211,21 @@ tdm_config_get_int(const char *key, int default_value) return value; } -INTERN const char* +EXTERN const char* tdm_config_get_string(const char *key, const char *default_value) { const char *result; TDM_RETURN_VAL_IF_FAIL(key != NULL, default_value); - if (!g_dic) { + pthread_mutex_lock(&g_dic_lock); + + if (!_tdm_config_check_init()) { TDM_INFO("%s = %s: default", key, default_value); + pthread_mutex_unlock(&g_dic_lock); return default_value; } - pthread_mutex_lock(&g_dic_lock); result = _tdm_config_get_string_internal(g_dic, key, default_value); pthread_mutex_unlock(&g_dic_lock); @@ -233,7 +234,7 @@ tdm_config_get_string(const char *key, const char *default_value) return result; } -INTERN tdm_error +EXTERN tdm_error tdm_config_set_int(const char *key, int value) { char temp[TDM_NAME_LEN]; @@ -241,14 +242,16 @@ tdm_config_set_int(const char *key, int value) TDM_RETURN_VAL_IF_FAIL(key != NULL, TDM_ERROR_INVALID_PARAMETER); - if (!g_dic) { + snprintf(temp, sizeof temp, "%d", value); + + pthread_mutex_lock(&g_dic_lock); + + if (!_tdm_config_check_init()) { TDM_INFO("%s = %d set failed", key, value); + pthread_mutex_unlock(&g_dic_lock); return TDM_ERROR_BAD_REQUEST; } - snprintf(temp, sizeof temp, "%d", value); - - pthread_mutex_lock(&g_dic_lock); ret = iniparser_set(g_dic, key, (const char*)temp); _tdm_config_check_logs(); @@ -262,19 +265,21 @@ tdm_config_set_int(const char *key, int value) return TDM_ERROR_NONE; } -INTERN tdm_error +EXTERN tdm_error tdm_config_set_string(const char *key, const char *value) { int ret; TDM_RETURN_VAL_IF_FAIL(key != NULL, TDM_ERROR_INVALID_PARAMETER); - if (!g_dic) { + pthread_mutex_lock(&g_dic_lock); + + if (!_tdm_config_check_init()) { TDM_INFO("%s = %s set failed", key, value); + pthread_mutex_unlock(&g_dic_lock); return TDM_ERROR_BAD_REQUEST; } - pthread_mutex_lock(&g_dic_lock); ret = iniparser_set(g_dic, key, value); _tdm_config_check_logs(); -- 2.7.4