Add count for initialization and deinitialization 24/50324/2
authorpr.jung <pr.jung@samsung.com>
Fri, 3 Jul 2015 09:28:09 +0000 (18:28 +0900)
committerTaeyoung Kim <ty317.kim@samsung.com>
Thu, 12 Nov 2015 05:33:23 +0000 (21:33 -0800)
Change-Id: I57d95946248397b70c4d59c25cc08a32bd89aa39
Signed-off-by: pr.jung <pr.jung@samsung.com>
src/feedback.c

index 7cb65be2ce07c5f8fa19a8b7cac154a255bce276..bee3687efb693f3bca8a8e58418f82ba3ce03adf 100644 (file)
 #define API __attribute__ ((visibility("default")))
 #endif
 
-static bool binit;
+static unsigned int init_cnt;
+static pthread_mutex_t fmutex = PTHREAD_MUTEX_INITIALIZER;
 
 API int feedback_initialize(void)
 {
-       if (binit)
-               return FEEDBACK_ERROR_NONE;
-
+       pthread_mutex_lock(&fmutex);
        if (!profile) {
                _E("there is no valid profile module.");
+               pthread_mutex_unlock(&fmutex);
                return FEEDBACK_ERROR_NOT_SUPPORTED;
        }
 
+       if (init_cnt++ > 0) {
+               pthread_mutex_unlock(&fmutex);
+               return FEEDBACK_ERROR_NONE;
+       }
+
        /* initialize device */
        devices_init();
 
@@ -49,14 +54,22 @@ API int feedback_initialize(void)
        if (profile->init)
                profile->init();
 
-       binit = true;
+       pthread_mutex_unlock(&fmutex);
        return FEEDBACK_ERROR_NONE;
 }
 
 API int feedback_deinitialize(void)
 {
-       if (!binit)
+       pthread_mutex_lock(&fmutex);
+       if (!init_cnt) {
+               pthread_mutex_unlock(&fmutex);
                return FEEDBACK_ERROR_NOT_INITIALIZED;
+       }
+
+       if (init_cnt-- > 1) {
+               pthread_mutex_unlock(&fmutex);
+               return FEEDBACK_ERROR_NONE;
+       }
 
        /* deinitialize device */
        devices_exit();
@@ -65,7 +78,7 @@ API int feedback_deinitialize(void)
        if (profile->exit)
                profile->exit();
 
-       binit = false;
+       pthread_mutex_unlock(&fmutex);
        return FEEDBACK_ERROR_NONE;
 }
 
@@ -76,10 +89,13 @@ API int feedback_play(feedback_pattern_e pattern)
        int switched;
 
        /* check initialize */
-       if (!binit) {
+       pthread_mutex_lock(&fmutex);
+       if (!init_cnt) {
                _E("Not initialized");
+               pthread_mutex_unlock(&fmutex);
                return FEEDBACK_ERROR_NOT_INITIALIZED;
        }
+       pthread_mutex_unlock(&fmutex);
 
        if (pattern <= FEEDBACK_PATTERN_NONE ||
            pattern >= profile->max_pattern) {
@@ -120,10 +136,13 @@ API int feedback_play_type(feedback_type_e type, feedback_pattern_e pattern)
        int switched;
 
        /* check initialize */
-       if (!binit) {
+       pthread_mutex_lock(&fmutex);
+       if (!init_cnt) {
                _E("Not initialized");
+               pthread_mutex_unlock(&fmutex);
                return FEEDBACK_ERROR_NOT_INITIALIZED;
        }
+       pthread_mutex_unlock(&fmutex);
 
        if (type <= FEEDBACK_TYPE_NONE ||
            type >= profile->max_type) {
@@ -170,10 +189,13 @@ API int feedback_stop(void)
        int err;
 
        /* check initialize */
-       if (!binit) {
+       pthread_mutex_lock(&fmutex);
+       if (!init_cnt) {
                _E("Not initialized");
+               pthread_mutex_unlock(&fmutex);
                return FEEDBACK_ERROR_NOT_INITIALIZED;
        }
+       pthread_mutex_unlock(&fmutex);
 
        /* stop all device */
        err = devices_stop();
@@ -196,10 +218,13 @@ API int feedback_is_supported_pattern(feedback_type_e type, feedback_pattern_e p
        int switched;
 
        /* check initialize */
-       if (!binit) {
+       pthread_mutex_lock(&fmutex);
+       if (!init_cnt) {
                _E("Not initialized");
+               pthread_mutex_unlock(&fmutex);
                return FEEDBACK_ERROR_NOT_INITIALIZED;
        }
+       pthread_mutex_unlock(&fmutex);
 
        if (!status) {
                _E("Invalid parameter : status(NULL)");