Add lock for multi-thread cases 24/89824/1 accepted/tizen/3.0/common/20161114.105319 accepted/tizen/3.0/ivi/20161011.065218 accepted/tizen/3.0/mobile/20161015.032850 accepted/tizen/3.0/tv/20161016.003753 accepted/tizen/3.0/wearable/20161015.081742 accepted/tizen/common/20161021.194000 accepted/tizen/ivi/20160929.001653 accepted/tizen/mobile/20160929.001511 accepted/tizen/tv/20160929.001707 accepted/tizen/wearable/20160929.001627 submit/tizen/20160927.110043 submit/tizen_3.0_common/20161104.104000 submit/tizen_3.0_ivi/20161010.000007 submit/tizen_3.0_mobile/20161015.000001 submit/tizen_3.0_tv/20161015.000001 submit/tizen_3.0_wearable/20161015.000001 submit/tizen_common/20161021.172255
authorpr.jung <pr.jung@samsung.com>
Tue, 27 Sep 2016 06:35:29 +0000 (15:35 +0900)
committerpr.jung <pr.jung@samsung.com>
Tue, 27 Sep 2016 06:35:29 +0000 (15:35 +0900)
Change-Id: I7a362ca4341b9e0c5fdc765980b885050aae9f8c
Signed-off-by: pr.jung <pr.jung@samsung.com>
src/system_info.c

index 1b541c2..a697eca 100644 (file)
@@ -39,6 +39,7 @@
 #define GDBM_CACHE_SIZE 10 /* GDBM default == 100 */
 
 GHashTable *hashtable = NULL;
+static pthread_mutex_t fmutex = PTHREAD_MUTEX_INITIALIZER;
 
 enum tag_type {
        TAG_TYPE_PLATFORM,
@@ -103,12 +104,14 @@ static int db_get_value(enum tag_type tag, const char *key,
                snprintf(key_internal, sizeof(key_internal),
                                "%s%s:%s:%s", KEY_PREFIX, key, type, tag_s);
 
+       pthread_mutex_lock(&fmutex);
        if (!hashtable) {
                hashtable = g_hash_table_new_full(g_str_hash, g_str_equal, destroy_key_value, destroy_key_value);
        } else {
                temp = (char *)g_hash_table_lookup(hashtable, key_internal);
                if (temp) {
                        snprintf(value, len, "%s", temp);
+                       pthread_mutex_unlock(&fmutex);
                        return SYSTEM_INFO_ERROR_NONE;
                }
        }
@@ -116,6 +119,7 @@ static int db_get_value(enum tag_type tag, const char *key,
        db = gdbm_open(SYSTEM_INFO_DB_PATH, 0, GDBM_READER, S_IRUSR | S_IRGRP | S_IROTH, NULL);
        if (!db) {
                _E("Failed to open db (%d, %s)", gdbm_errno, gdbm_strerror(gdbm_errno)); //LCOV_EXCL_LINE
+               pthread_mutex_unlock(&fmutex);
                return SYSTEM_INFO_ERROR_IO_ERROR; //LCOV_EXCL_LINE
        }
 
@@ -140,6 +144,7 @@ static int db_get_value(enum tag_type tag, const char *key,
 out:
        if (db)
                gdbm_close(db);
+       pthread_mutex_unlock(&fmutex);
        return ret;
 }
 
@@ -179,6 +184,7 @@ static int system_info_get_type(enum tag_type tag, const char *key,
                return -EINVAL;
        }
 
+       pthread_mutex_lock(&fmutex);
        if (!hashtable) {
                hashtable = g_hash_table_new_full(g_str_hash, g_str_equal, destroy_key_value, destroy_key_value);
        } else {
@@ -194,6 +200,7 @@ static int system_info_get_type(enum tag_type tag, const char *key,
                                continue;
 
                        *type = info_type[i].type_e;
+                       pthread_mutex_unlock(&fmutex);
                        return SYSTEM_INFO_ERROR_NONE;
                }
        }
@@ -201,6 +208,7 @@ static int system_info_get_type(enum tag_type tag, const char *key,
        db = gdbm_open(SYSTEM_INFO_DB_PATH, 0, GDBM_READER, S_IRUSR | S_IRGRP | S_IROTH, NULL);
        if (!db) {
                _E("Failed to open db (%d, %s)", gdbm_errno, gdbm_strerror(gdbm_errno)); //LCOV_EXCL_LINE
+               pthread_mutex_unlock(&fmutex);
                return SYSTEM_INFO_ERROR_IO_ERROR; //LCOV_EXCL_LINE
        }
 
@@ -237,6 +245,7 @@ static int system_info_get_type(enum tag_type tag, const char *key,
 out:
        if (db)
                gdbm_close(db);
+       pthread_mutex_unlock(&fmutex);
        return ret;
 }