Add critical sections in DownloadManager
authorSunwook Bae <sunwook45.bae@samsung.com>
Wed, 14 Aug 2013 01:17:58 +0000 (10:17 +0900)
committerSunwook Bae <sunwook45.bae@samsung.com>
Wed, 14 Aug 2013 01:40:49 +0000 (10:40 +0900)
Change-Id: Ia99008c644bfb926e45469453013635f7aeb5830
Signed-off-by: Sunwook Bae <sunwook45.bae@samsung.com>
src/FCnt_DownloadManagerImpl.cpp

index 94989e3..24915bd 100644 (file)
@@ -68,6 +68,8 @@ static const int SLP_STATE_COMPLETED = 5;
 static const int SLP_STATE_FAILED = 6;
 static const int SLP_STATE_CANCELLED = 7;
 
+static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
+
 _DownloadManagerImpl* _DownloadManagerImpl::__pInstance = null;
 
 class _DownloadEventArg
@@ -130,6 +132,7 @@ protected:
                                                break;
 
                                        default:
+
                                                break;
                                }
                        }
@@ -160,6 +163,7 @@ OnStateChanged(int download_id, download_state_e state, void* data)
 
                case SLP_STATE_PAUSED:
                {
+                       pthread_mutex_lock(&mutex);
                        if (pDMImpl->__pEvent)
                        {
                                _DownloadEventArg* pEventArg = new (std::nothrow) _DownloadEventArg();
@@ -168,12 +172,14 @@ OnStateChanged(int download_id, download_state_e state, void* data)
 
                                pDMImpl->__pEvent->Fire(*pEventArg);
                        }
+                       pthread_mutex_unlock(&mutex);
 
                        break;
                }
 
                case SLP_STATE_COMPLETED:
                {
+                       pthread_mutex_lock(&mutex);
                        if (pDMImpl->__pEvent)
                        {
                                _DownloadEventArg* pEventArg = new (std::nothrow) _DownloadEventArg();
@@ -188,6 +194,7 @@ OnStateChanged(int download_id, download_state_e state, void* data)
 
                                pDMImpl->__pEvent->Fire(*pEventArg);
                        }
+                       pthread_mutex_unlock(&mutex);
 
                        // Remove the resource from url_download
                        pDMImpl->DestroyResources(reqId);
@@ -197,6 +204,7 @@ OnStateChanged(int download_id, download_state_e state, void* data)
 
                case SLP_STATE_FAILED:
                {
+                       pthread_mutex_lock(&mutex);
                        if (pDMImpl->__pEvent)
                        {
                                _DownloadEventArg* pEventArg = new (std::nothrow) _DownloadEventArg();
@@ -213,6 +221,7 @@ OnStateChanged(int download_id, download_state_e state, void* data)
 
                                pDMImpl->__pEvent->Fire(*pEventArg);
                        }
+                       pthread_mutex_unlock(&mutex);
 
                        // Comment out due to resume the failed request
                        //pDMImpl->DestroyResources(reqId);
@@ -222,6 +231,7 @@ OnStateChanged(int download_id, download_state_e state, void* data)
 
                case SLP_STATE_CANCELLED:
                {
+                       pthread_mutex_lock(&mutex);
                        if (pDMImpl->__pEvent)
                        {
                                _DownloadEventArg* pEventArg = new (std::nothrow) _DownloadEventArg();
@@ -230,6 +240,7 @@ OnStateChanged(int download_id, download_state_e state, void* data)
 
                                pDMImpl->__pEvent->Fire(*pEventArg);
                        }
+                       pthread_mutex_unlock(&mutex);
 
                        // Remove the resource from url_download
                        pDMImpl->DestroyResources(reqId);
@@ -250,6 +261,7 @@ OnProgress(int download_id, unsigned long long received, void* data)
 
        _DownloadManagerImpl* pDMImpl = (_DownloadManagerImpl*)data;
 
+       pthread_mutex_lock(&mutex);
        if (data && pDMImpl->__pEvent)
        {
                _DownloadEventArg* pEventArg = new (std::nothrow) _DownloadEventArg();
@@ -265,6 +277,7 @@ OnProgress(int download_id, unsigned long long received, void* data)
 
                pDMImpl->__pEvent->Fire(*pEventArg);
        }
+       pthread_mutex_unlock(&mutex);
 }
 
 
@@ -624,6 +637,8 @@ _DownloadManagerImpl::SetAllowedNetwork(unsigned long flags)
 void
 _DownloadManagerImpl::SetDownloadListener(IDownloadListener* pListener)
 {
+       pthread_mutex_lock(&mutex);
+
        if (pListener != null)
        {
                _DownloadEvent* pEvent = new (std::nothrow) _DownloadEvent();
@@ -642,6 +657,8 @@ _DownloadManagerImpl::SetDownloadListener(IDownloadListener* pListener)
 
                __pEvent = null;
        }
+
+       pthread_mutex_unlock(&mutex);
 }
 
 void