commit f7c1d3a4f1ce4c05413980563d85dfb50ae704a4
parent 4c8c90f14ca86eea4bccc53862fb3e9adfa1f0dd
Author: cfillion <cfillion@users.noreply.github.com>
Date: Mon, 7 Dec 2015 18:31:57 -0500
always call plugin_register from the main thread
Diffstat:
2 files changed, 19 insertions(+), 13 deletions(-)
diff --git a/src/download.cpp b/src/download.cpp
@@ -8,6 +8,7 @@ using namespace std;
Download::Queue Download::s_finished;
WDL_Mutex Download::s_mutex;
+size_t Download::s_running = 0;
static const int DOWNLOAD_TIMEOUT = 5;
static const int CONCURRENT_DOWNLOADS = 3;
@@ -43,12 +44,8 @@ void Download::TimerTick()
{
Download *dl = Download::NextFinished();
- if(!dl) {
- plugin_register("-timer", (void*)TimerTick);
- return;
- }
-
- dl->finishInMainThread();
+ if(dl)
+ dl->finishInMainThread();
}
void Download::start()
@@ -60,6 +57,7 @@ void Download::start()
m_onStart();
+ RegisterStart();
m_threadHandle = CreateThread(nullptr, 0, Worker, (void *)this, 0, nullptr);
}
@@ -127,16 +125,17 @@ size_t Download::WriteData(char *ptr, size_t rawsize, size_t nmemb, void *data)
return size;
}
-void Download::MarkAsFinished(Download *dl)
+void Download::RegisterStart()
{
WDL_MutexLock lock(&s_mutex);
- // I hope it's OK to call plugin_register from another thread
- // if it's not this call should be moved to start() and
- // we should unregister the timer in finishInMainThread()
- // instead of TimerTick()
- if(s_finished.empty())
- plugin_register("timer", (void*)TimerTick);
+ s_running++;
+ plugin_register("timer", (void*)TimerTick);
+}
+
+void Download::MarkAsFinished(Download *dl)
+{
+ WDL_MutexLock lock(&s_mutex);
s_finished.push(dl);
}
@@ -145,12 +144,17 @@ Download *Download::NextFinished()
{
WDL_MutexLock lock(&s_mutex);
+ if(!s_running)
+ plugin_register("-timer", (void*)TimerTick);
+
if(s_finished.empty())
return nullptr;
Download *dl = s_finished.front();
s_finished.pop();
+ s_running--;
+
return dl;
}
diff --git a/src/download.hpp b/src/download.hpp
@@ -37,6 +37,8 @@ public:
private:
static WDL_Mutex s_mutex;
static Queue s_finished;
+ static size_t s_running;
+ static void RegisterStart();
static Download *NextFinished();
static void MarkAsFinished(Download *);