Fix double unlock of slot mutex in user stops scneario
[platform/core/multimedia/libmm-sound.git] / server / mm_sound_mgr_codec.c
index 1bb36f1..c2c968b 100644 (file)
@@ -56,8 +56,6 @@ typedef struct {
 
        int pluginid;
        int status;
-       bool stop_by_user;
-
 } __mmsound_mgr_codec_handle_t;
 
 static MMSoundPluginType *g_codec_plugins = NULL;
@@ -67,11 +65,24 @@ static pthread_mutex_t g_slot_mutex;
 static GSourceFunc g_shutdown_cb;
 static guint g_timer_id = 0;
 
-#define SLOT_LOCK() do { pthread_mutex_lock(&g_slot_mutex); /* debug_msg("After Slot_mutex LOCK"); */ } while (0)
-#define SLOT_UNLOCK() do { pthread_mutex_unlock(&g_slot_mutex); /* debug_msg("After Slot_mutex UNLOCK"); */ } while (0)
+#ifdef DEBUG_DETAIL
+#define SLOT_LOCK() do { \
+       debug_msg("Before Slot_mutex LOCK"); \
+       pthread_mutex_lock(&g_slot_mutex); \
+       debug_msg("After Slot_mutex LOCK"); \
+} while (0)
+#define SLOT_UNLOCK() do { \
+       debug_msg("Before Slot_mutex UNLOCK"); \
+       pthread_mutex_unlock(&g_slot_mutex); \
+       debug_msg("After Slot_mutex UNLOCK"); \
+} while (0)
+#else
+#define SLOT_LOCK() do { pthread_mutex_lock(&g_slot_mutex); } while (0)
+#define SLOT_UNLOCK() do { pthread_mutex_unlock(&g_slot_mutex); } while (0)
+#endif
 
 static int _MMSoundMgrCodecRegisterInterface(MMSoundPluginType *plugin);
-static int _MMSoundMgrCodecStopCallback(int param);
+static int _MMSoundMgrCodecStopCallback(int param, bool stop_by_user);
 static gboolean _mm_sound_mgr_codec_slot_is_empty();
 
 static gboolean _idle_cb(gpointer user_data)
@@ -107,7 +118,7 @@ static void _mm_sound_mgr_codec_shutdown_timer_start()
        }
        if (g_shutdown_cb) {
                g_timer_id = g_timeout_add_seconds(SHUTDOWN_TIMEOUT_SEC, _timeout_cb, NULL);
-               debug_error("TIMER : new timer [%d]", g_timer_id);
+               debug_warning("TIMER : new timer [%d]", g_timer_id);
        } else {
                debug_warning("No Timer started due to invalid shutdown callback");
        }
@@ -116,7 +127,7 @@ static void _mm_sound_mgr_codec_shutdown_timer_start()
 static void _mm_sound_mgr_codec_shutdown_timer_stop()
 {
        if (g_timer_id > 0) {
-               debug_error("TIMER : remove timer id [%d]", g_timer_id);
+               debug_warning("TIMER : remove timer id [%d]", g_timer_id);
                g_source_remove(g_timer_id);
                g_timer_id = 0;
        } else {
@@ -169,12 +180,13 @@ static gboolean _mm_sound_mgr_codec_slot_is_empty()
        return (slotid == MANAGER_HANDLE_MAX) ? TRUE : FALSE;
 }
 
-static void _mm_sound_mgr_codec_slot_clear(int slotid)
+static void _mm_sound_mgr_codec_slot_clear(int slotid, bool dump)
 {
        memset(&g_slots[slotid], 0, sizeof(__mmsound_mgr_codec_handle_t));
        g_slots[slotid].status = STATUS_IDLE;
 
-       debug_error("SlotID [%d] cleared", slotid);
+       if (dump)
+               debug_warning("SlotID [%d] cleared", slotid);
 }
 
 int MMSoundMgrCodecInit(const char *targetdir, GSourceFunc _shutdown_cb)
@@ -192,7 +204,7 @@ int MMSoundMgrCodecInit(const char *targetdir, GSourceFunc _shutdown_cb)
        }
 
        for (slotid = 0; slotid < MANAGER_HANDLE_MAX; slotid++)
-               _mm_sound_mgr_codec_slot_clear(slotid);
+               _mm_sound_mgr_codec_slot_clear(slotid, false);
 
        if (g_codec_plugins) {
                debug_warning("Please Check Init twice");
@@ -419,8 +431,6 @@ int MMSoundMgrCodecStop(const int slotid)
        debug_msg("Found slot, Slotid [%d] State [%d]", slotid, g_slots[slotid].status);
 #endif
 
-       g_slots[slotid].stop_by_user = true;
-
        err = g_plugins[g_slots[slotid].pluginid].Stop(g_slots[slotid].plughandle);
        if (err != MM_ERROR_NONE)
                debug_error("Fail to STOP Code : 0x%08X", err);
@@ -432,18 +442,18 @@ cleanup:
        return err;
 }
 
-static int _MMSoundMgrCodecStopCallback(int param)
+static int _MMSoundMgrCodecStopCallback(int param, bool stop_by_user)
 {
        int err = MM_ERROR_NONE;
 
        if (param < 0 || param >= MANAGER_HANDLE_MAX) {
-               debug_error("Slot index param [%d] is invalid", param);
+               debug_error("Slot(%d) is invalid", param);
                return MM_ERROR_INVALID_ARGUMENT;
        }
 
-       debug_enter("(Slot : %d) stop-by-user : %d", param, g_slots[param].stop_by_user);
+       debug_enter("Slot(%d) : stop-by-user(%d)", param, stop_by_user);
 
-       if (!g_slots[param].stop_by_user) {
+       if (!stop_by_user) {
                SLOT_LOCK();
                __mm_sound_mgr_ipc_notify_play_file_end(param);
                debug_msg("Client callback msg_type (instance) : [%d]", (int)g_slots[param].param);
@@ -452,13 +462,13 @@ static int _MMSoundMgrCodecStopCallback(int param)
        debug_msg("Handle allocated handle : [%p]", g_slots[param].plughandle);
        err = g_plugins[g_slots[param].pluginid].Destroy(g_slots[param].plughandle);
        if (err < 0)
-               debug_critical("[CODEC MGR] Fail to destroy slot number : [%d] err [0x%x]", param, err);
+               debug_critical("Slot(%d) : Fail to destroy slot, err [0x%x]", param, err);
 
-       _mm_sound_mgr_codec_slot_clear(param);
+       _mm_sound_mgr_codec_slot_clear(param, true);
        if (_mm_sound_mgr_codec_slot_is_empty())
                _mm_sound_mgr_codec_shutdown_timer_start();
 
-       if (!g_slots[param].stop_by_user)
+       if (!stop_by_user)
                SLOT_UNLOCK();
 
        debug_fleave();