1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-08 03:28:20 +02:00

Fixed ability to cancel downloads

This commit is contained in:
Brian Fiete 2019-09-04 10:27:37 -07:00
parent 43c82515fe
commit d26e2957b0
7 changed files with 82 additions and 17 deletions

View file

@ -8,6 +8,7 @@ static BF_TLS_DECLSPEC ThreadPool* gPoolParent;
ThreadPool::Thread::Thread() ThreadPool::Thread::Thread()
{ {
mCurJobThreadId = -1; mCurJobThreadId = -1;
mActiveJob = NULL;
} }
ThreadPool::Thread::~Thread() ThreadPool::Thread::~Thread()
@ -34,9 +35,10 @@ void ThreadPool::Thread::Proc()
{ {
job = mThreadPool->mJobs[0]; job = mThreadPool->mJobs[0];
job->mProcessing = true; job->mProcessing = true;
mThreadPool->mJobs.RemoveAt(0); mThreadPool->mJobs.RemoveAt(0);
} }
mActiveJob = job;
if (job == NULL) if (job == NULL)
mCurJobThreadId = -1; mCurJobThreadId = -1;
else else
@ -76,6 +78,7 @@ void ThreadPool::Thread::Proc()
// Run dtor synchronized // Run dtor synchronized
AutoCrit autoCrit(mThreadPool->mCritSect); AutoCrit autoCrit(mThreadPool->mCritSect);
mActiveJob = NULL;
delete job; delete job;
} }
} }
@ -176,4 +179,14 @@ void ThreadPool::AddJob(BfpThreadStartProc proc, void* param, int maxWorkersPerP
bool ThreadPool::IsInJob() bool ThreadPool::IsInJob()
{ {
return gPoolParent == this; return gPoolParent == this;
}
void ThreadPool::CancelAll()
{
AutoCrit autoCrit(mCritSect);
for (auto job : mJobs)
job->Cancel();
for (auto thread : mThreads)
if (thread->mActiveJob != NULL)
thread->mActiveJob->Cancel();
} }

View file

@ -9,12 +9,15 @@ NS_BF_BEGIN
class ThreadPool class ThreadPool
{ {
public: public:
class Job;
class Thread class Thread
{ {
public: public:
ThreadPool* mThreadPool; ThreadPool* mThreadPool;
BfpThread* mBfpThread; BfpThread* mBfpThread;
BfpThreadId mCurJobThreadId; BfpThreadId mCurJobThreadId;
Job* mActiveJob;
public: public:
Thread(); Thread();
@ -85,6 +88,7 @@ public:
void AddJob(Job* job, int maxWorkersPerProviderThread = 0x7FFFFFFF); void AddJob(Job* job, int maxWorkersPerProviderThread = 0x7FFFFFFF);
void AddJob(BfpThreadStartProc proc, void* param, int maxWorkersPerProviderThread = 0x7FFFFFFF); void AddJob(BfpThreadStartProc proc, void* param, int maxWorkersPerProviderThread = 0x7FFFFFFF);
bool IsInJob(); bool IsInJob();
void CancelAll();
}; };
NS_BF_END NS_BF_END

View file

@ -27,7 +27,7 @@ TargetDirectory = "$(WorkspaceDir)/dist"
TargetName = "BeefIDE_d" TargetName = "BeefIDE_d"
OtherLinkFlags = "$(LinkFlags) Comdlg32.lib kernel32.lib user32.lib advapi32.lib shell32.lib IDEHelper64_d.lib" OtherLinkFlags = "$(LinkFlags) Comdlg32.lib kernel32.lib user32.lib advapi32.lib shell32.lib IDEHelper64_d.lib"
CLibType = "Dynamic" CLibType = "Dynamic"
DebugCommandArguments = "-test=scripts\\Minidump.txt -testNoExit -verbosity=Diagnostic" DebugCommandArguments = "-test=scripts\\Minidump.txt -testNoExit -verbosity=diagnostic"
DebugWorkingDirectory = "c:\\Beef\\IDE\\Tests\\EmptyTest" DebugWorkingDirectory = "c:\\Beef\\IDE\\Tests\\EmptyTest"
EnvironmentVars = ["_NO_DEBUG_HEAP=1"] EnvironmentVars = ["_NO_DEBUG_HEAP=1"]

View file

@ -1,6 +1,5 @@
SetSymSrvOptions("C:/SymCache", "http://symbols.beeflang.org/\nhttps://msdl.microsoft.com/download/symbols", "TempCache") SetSymSrvOptions("C:/SymCache", "http://symbols.beeflang.org/\nhttps://msdl.microsoft.com/download/symbols", "TempCache")
#SetSymSrvOptions("C:/SymCache", "https://msdl.microsoft.com/download/symbols\nhttps://chromium-browser-symsrv.commondatastorage.googleapis.com", "TempCache") #SetSymSrvOptions("C:/SymCache", "http://127.0.0.1:8042", "TempCache")
#SetSymSrvOptions("C:/SymCache", "https://msdl.microsoft.com/download/symbols\nhttps://chromium-browser-symsrv.commondatastorage.googleapis.com", "None")
OpenCrashDump("dumps/Chrome1.dmp") OpenCrashDump("dumps/Chrome1.dmp")
WaitForPaused() WaitForPaused()

View file

@ -6292,6 +6292,8 @@ namespace IDE
mVerbosity = .Detailed; mVerbosity = .Detailed;
else if (value == "diagnostic") else if (value == "diagnostic")
mVerbosity = .Diagnostic; mVerbosity = .Diagnostic;
else
Fail(scope String()..AppendF("Invalid verbosity option: {}", value));
case "-workspace","-proddir": case "-workspace","-proddir":
var relDir = scope String(value); var relDir = scope String(value);
if ((relDir.EndsWith("\\")) || relDir.EndsWith("\"")) if ((relDir.EndsWith("\\")) || relDir.EndsWith("\""))
@ -10465,7 +10467,7 @@ namespace IDE
bool isOutput = (cmd == "msg") || (cmd == "dbgEvalMsg") || (cmd == "log"); bool isOutput = (cmd == "msg") || (cmd == "dbgEvalMsg") || (cmd == "log");
if (cmd == "msgLo") if (cmd == "msgLo")
{ {
if (mVerbosity <= .Diagnostic) if (mVerbosity < .Diagnostic)
continue; continue;
isOutput = true; isOutput = true;
} }

View file

@ -9,6 +9,7 @@ USING_NS_BF;
#ifdef BF_CURL #ifdef BF_CURL
#define CURL_STATICLIB #define CURL_STATICLIB
#include "curl/curl.h" #include "curl/curl.h"
#include "curl/multi.h"
static int TransferInfoCallback(void* userp, static int TransferInfoCallback(void* userp,
curl_off_t dltotal, curl_off_t dlnow, curl_off_t dltotal, curl_off_t dlnow,
@ -80,8 +81,18 @@ static size_t WriteMemoryCallback(void* contents, size_t size, size_t nmemb, voi
void NetRequest::Cleanup() void NetRequest::Cleanup()
{ {
if (mCURLMulti != NULL)
{
curl_multi_remove_handle(mCURLMulti, mCURL);
}
if (mCURL != NULL) if (mCURL != NULL)
curl_easy_cleanup(mCURL); curl_easy_cleanup(mCURL);
if (mCURLMulti != NULL)
{
curl_multi_cleanup(mCURLMulti);
}
} }
void NetRequest::Perform() void NetRequest::Perform()
@ -98,7 +109,9 @@ void NetRequest::Perform()
mNetManager->mDebugManager->OutputRawMessage(StrFormat("msgLo Getting '%s'\n", mURL.c_str())); mNetManager->mDebugManager->OutputRawMessage(StrFormat("msgLo Getting '%s'\n", mURL.c_str()));
mOutTempPath = mOutPath + "__partial"; mOutTempPath = mOutPath + "__partial";
mCURLMulti = curl_multi_init();
mCURL = curl_easy_init(); mCURL = curl_easy_init();
if (mShowTracking) if (mShowTracking)
@ -115,13 +128,37 @@ void NetRequest::Perform()
curl_easy_setopt(mCURL, CURLOPT_XFERINFOFUNCTION, TransferInfoCallback); curl_easy_setopt(mCURL, CURLOPT_XFERINFOFUNCTION, TransferInfoCallback);
curl_easy_setopt(mCURL, CURLOPT_FOLLOWLOCATION, 1L); curl_easy_setopt(mCURL, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(mCURL, CURLOPT_NOPROGRESS, 0L); curl_easy_setopt(mCURL, CURLOPT_NOPROGRESS, 0L);
auto result = curl_easy_perform(mCURL); //auto result = curl_easy_perform(mCURL);
if (result != CURLE_OK) CURLMcode mcode = curl_multi_add_handle(mCURLMulti, mCURL);
if (mcode != CURLM_OK)
{ {
mFailed = true; mFailed = true;
return; return;
} }
while (true)
{
int activeCount = 0;
curl_multi_perform(mCURLMulti, &activeCount);
if (activeCount == 0)
break;
int waitRet = 0;
curl_multi_wait(mCURLMulti, NULL, 0, 20, &waitRet);
if (mCancelling)
{
mFailed = true;
return;
}
}
// if (result != CURLE_OK)
// {
// mFailed = true;
// return;
// }
long response_code = 0; long response_code = 0;
curl_easy_getinfo(mCURL, CURLINFO_RESPONSE_CODE, &response_code); curl_easy_getinfo(mCURL, CURLINFO_RESPONSE_CODE, &response_code);
@ -375,6 +412,7 @@ void NetManagerThread()
NetManager::NetManager() : mThreadPool(8, 1*1024*1024) NetManager::NetManager() : mThreadPool(8, 1*1024*1024)
{ {
mWaitingResult = NULL; mWaitingResult = NULL;
mWaitingRequest = NULL;
} }
NetManager::~NetManager() NetManager::~NetManager()
@ -458,7 +496,7 @@ bool NetManager::Get(const StringImpl& url, const StringImpl& destPath)
break; break;
} }
mWaitingResult = netResult; mWaitingResult = netResult;
netResult->mCurRequest->ShowTracking(); netResult->mCurRequest->ShowTracking();
} }
} }
@ -474,10 +512,16 @@ bool NetManager::Get(const StringImpl& url, const StringImpl& destPath)
} }
// Perform this in the requesting thread // Perform this in the requesting thread
{
AutoCrit autoCrit(mThreadPool.mCritSect);
mWaitingRequest = netRequest;
}
netRequest->mShowTracking = true; netRequest->mShowTracking = true;
netRequest->Perform(); netRequest->Perform();
AutoCrit autoCrit(mThreadPool.mCritSect); AutoCrit autoCrit(mThreadPool.mCritSect);
mWaitingRequest = NULL;
auto netResult = netRequest->mResult; auto netResult = netRequest->mResult;
delete netRequest; delete netRequest;
@ -490,11 +534,9 @@ bool NetManager::Get(const StringImpl& url, const StringImpl& destPath)
void NetManager::CancelAll() void NetManager::CancelAll()
{ {
AutoCrit autoCrit(mThreadPool.mCritSect); AutoCrit autoCrit(mThreadPool.mCritSect);
for (auto job : mThreadPool.mJobs) if (mWaitingRequest != NULL)
{ mWaitingRequest->Cancel();
auto netRequest = (NetRequest*)job; mThreadPool.CancelAll();
netRequest->Cancel();
}
} }
void NetManager::Clear() void NetManager::Clear()
@ -528,8 +570,9 @@ void NetManager::Clear()
void NetManager::CancelCurrent() void NetManager::CancelCurrent()
{ {
AutoCrit autoCrit(mThreadPool.mCritSect); AutoCrit autoCrit(mThreadPool.mCritSect);
if (mWaitingRequest != NULL)
if ((mWaitingResult != NULL) && (mWaitingResult->mCurRequest != NULL)) mWaitingRequest->Cancel();
else if ((mWaitingResult != NULL) && (mWaitingResult->mCurRequest != NULL))
mWaitingResult->mCurRequest->Cancel(); mWaitingResult->mCurRequest->Cancel();
} }

View file

@ -17,6 +17,7 @@ class NetResult;
#ifdef BF_CURL #ifdef BF_CURL
typedef void CURL; typedef void CURL;
typedef void CURLM;
#endif #endif
class NetRequest : public ThreadPool::Job class NetRequest : public ThreadPool::Job
@ -30,9 +31,10 @@ public:
FileStream mOutFile; FileStream mOutFile;
#ifdef BF_CURL #ifdef BF_CURL
CURL* mCURL; CURL* mCURL;
CURLM* mCURLMulti;
#else #else
#endif #endif
bool mCancelling; volatile bool mCancelling;
bool mFailed; bool mFailed;
String mError; String mError;
uint32 mLastUpdateTick; uint32 mLastUpdateTick;
@ -45,6 +47,7 @@ public:
mLastUpdateTick = 0; mLastUpdateTick = 0;
#ifdef BF_CURL #ifdef BF_CURL
mCURL = NULL; mCURL = NULL;
mCURLMulti = NULL;
#else #else
#endif #endif
mCancelling = false; mCancelling = false;
@ -90,6 +93,7 @@ public:
Array<NetResult*> mOldResults; Array<NetResult*> mOldResults;
SyncEvent mRequestDoneEvent; SyncEvent mRequestDoneEvent;
NetResult* mWaitingResult; NetResult* mWaitingResult;
NetRequest* mWaitingRequest;
public: public:
NetManager(); NetManager();