From 764a13a196fb66bf4b3fe4fcf98625a385e99e6e Mon Sep 17 00:00:00 2001 From: Dave Parks Date: Thu, 21 Jul 2011 17:35:04 -0500 Subject: SH-2031 Don't do network I/O from the main thread in llcurl. Reviewed by Kelly --- indra/llmessage/llcurl.cpp | 65 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 55 insertions(+), 10 deletions(-) (limited to 'indra/llmessage/llcurl.cpp') diff --git a/indra/llmessage/llcurl.cpp b/indra/llmessage/llcurl.cpp index 7c8b7e3584..453ffa9db9 100644 --- a/indra/llmessage/llcurl.cpp +++ b/indra/llmessage/llcurl.cpp @@ -579,11 +579,18 @@ void LLCurl::Easy::prepRequest(const std::string& url, //////////////////////////////////////////////////////////////////////////// -class LLCurl::Multi +class LLCurl::Multi : public LLThread { LOG_CLASS(Multi); public: - + + typedef enum + { + PERFORM_STATE_READY=0, + PERFORM_STATE_PERFORMING=1, + PERFORM_STATE_COMPLETED=2 + } ePerformState; + Multi(); ~Multi(); @@ -593,13 +600,17 @@ public: void removeEasy(Easy* easy); S32 process(); - S32 perform(); + void perform(); + virtual void run(); + CURLMsg* info_read(S32* msgs_in_queue); S32 mQueued; S32 mErrorCount; + S32 mPerformState; + private: void easyFree(Easy*); @@ -614,8 +625,10 @@ private: }; LLCurl::Multi::Multi() - : mQueued(0), - mErrorCount(0) + : LLThread("Curl Multi"), + mQueued(0), + mErrorCount(0), + mPerformState(PERFORM_STATE_READY) { mCurlMultiHandle = curl_multi_init(); if (!mCurlMultiHandle) @@ -630,6 +643,7 @@ LLCurl::Multi::Multi() LLCurl::Multi::~Multi() { + llassert(isStopped()); // Clean up active for(easy_active_list_t::iterator iter = mEasyActiveList.begin(); iter != mEasyActiveList.end(); ++iter) @@ -655,8 +669,16 @@ CURLMsg* LLCurl::Multi::info_read(S32* msgs_in_queue) return curlmsg; } +void LLCurl::Multi::perform() +{ + if (mPerformState == PERFORM_STATE_READY) + { + mPerformState = PERFORM_STATE_PERFORMING; + start(); + } +} -S32 LLCurl::Multi::perform() +void LLCurl::Multi::run() { S32 q = 0; for (S32 call_count = 0; @@ -672,13 +694,18 @@ S32 LLCurl::Multi::perform() } mQueued = q; - return q; + mPerformState = PERFORM_STATE_COMPLETED; } S32 LLCurl::Multi::process() { perform(); - + + if (mPerformState != PERFORM_STATE_COMPLETED) + { + return 0; + } + CURLMsg* msg; int msgs_in_queue; @@ -709,6 +736,8 @@ S32 LLCurl::Multi::process() } } } + + mPerformState = PERFORM_STATE_READY; return processed; } @@ -923,6 +952,12 @@ S32 LLCurlRequest::process() if (multi != mActiveMulti && tres == 0 && multi->mQueued == 0) { mMultiSet.erase(curiter); + + while (!multi->isStopped()) + { + apr_sleep(1000); + } + delete multi; } } @@ -963,6 +998,10 @@ LLCurlEasyRequest::LLCurlEasyRequest() LLCurlEasyRequest::~LLCurlEasyRequest() { + while (!mMulti->isStopped()) + { + apr_sleep(1000); + } delete mMulti; } @@ -1059,14 +1098,20 @@ void LLCurlEasyRequest::requestComplete() } } -S32 LLCurlEasyRequest::perform() +void LLCurlEasyRequest::perform() { - return mMulti->perform(); + mMulti->perform(); } // Usage: Call getRestult until it returns false (no more messages) bool LLCurlEasyRequest::getResult(CURLcode* result, LLCurl::TransferInfo* info) { + if (mMulti->mPerformState != LLCurl::Multi::PERFORM_STATE_COMPLETED) + { //we're busy, try again later + return false; + } + mMulti->mPerformState = LLCurl::Multi::PERFORM_STATE_READY; + if (!mEasy) { // Special case - we failed to initialize a curl_easy (can happen if too many open files) -- cgit v1.2.3 From 7b6afd1eba69a61fae87e3f1e7b03d03ee4ea15e Mon Sep 17 00:00:00 2001 From: Dave Parks Date: Thu, 21 Jul 2011 23:33:23 -0500 Subject: SH-2031 Followup to curl threading work -- don't start and stop the thread on every request, use a signal (cuts time spent in Pump IO down from 1-2 ms to 0.1ms) --- indra/llmessage/llcurl.cpp | 45 ++++++++++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 13 deletions(-) (limited to 'indra/llmessage/llcurl.cpp') diff --git a/indra/llmessage/llcurl.cpp b/indra/llmessage/llcurl.cpp index 453ffa9db9..2bb36f494c 100644 --- a/indra/llmessage/llcurl.cpp +++ b/indra/llmessage/llcurl.cpp @@ -611,6 +611,9 @@ public: S32 mPerformState; + LLCondition* mSignal; + bool mQuitting; + private: void easyFree(Easy*); @@ -630,6 +633,9 @@ LLCurl::Multi::Multi() mErrorCount(0), mPerformState(PERFORM_STATE_READY) { + mQuitting = false; + mSignal = new LLCondition(NULL); + mCurlMultiHandle = curl_multi_init(); if (!mCurlMultiHandle) { @@ -644,6 +650,10 @@ LLCurl::Multi::Multi() LLCurl::Multi::~Multi() { llassert(isStopped()); + + delete mSignal; + mSignal = NULL; + // Clean up active for(easy_active_list_t::iterator iter = mEasyActiveList.begin(); iter != mEasyActiveList.end(); ++iter) @@ -674,27 +684,31 @@ void LLCurl::Multi::perform() if (mPerformState == PERFORM_STATE_READY) { mPerformState = PERFORM_STATE_PERFORMING; - start(); + mSignal->signal(); } } void LLCurl::Multi::run() { - S32 q = 0; - for (S32 call_count = 0; - call_count < MULTI_PERFORM_CALL_REPEAT; - call_count += 1) + while (!mQuitting) { - CURLMcode code = curl_multi_perform(mCurlMultiHandle, &q); - if (CURLM_CALL_MULTI_PERFORM != code || q == 0) + mSignal->wait(); + S32 q = 0; + for (S32 call_count = 0; + call_count < MULTI_PERFORM_CALL_REPEAT; + call_count += 1) { - check_curl_multi_code(code); - break; - } + CURLMcode code = curl_multi_perform(mCurlMultiHandle, &q); + if (CURLM_CALL_MULTI_PERFORM != code || q == 0) + { + check_curl_multi_code(code); + break; + } + } + mQueued = q; + mPerformState = PERFORM_STATE_COMPLETED; } - mQueued = q; - mPerformState = PERFORM_STATE_COMPLETED; } S32 LLCurl::Multi::process() @@ -823,6 +837,7 @@ void LLCurlRequest::addMulti() { llassert_always(mThreadID == LLThread::currentID()); LLCurl::Multi* multi = new LLCurl::Multi(); + multi->start(); mMultiSet.insert(multi); mActiveMulti = multi; mActiveRequestCount = 0; @@ -952,9 +967,10 @@ S32 LLCurlRequest::process() if (multi != mActiveMulti && tres == 0 && multi->mQueued == 0) { mMultiSet.erase(curiter); - + multi->mQuitting = true; while (!multi->isStopped()) { + multi->mSignal->signal(); apr_sleep(1000); } @@ -988,6 +1004,7 @@ LLCurlEasyRequest::LLCurlEasyRequest() mResultReturned(false) { mMulti = new LLCurl::Multi(); + mMulti->start(); mEasy = mMulti->allocEasy(); if (mEasy) { @@ -998,8 +1015,10 @@ LLCurlEasyRequest::LLCurlEasyRequest() LLCurlEasyRequest::~LLCurlEasyRequest() { + mMulti->mQuitting = true; while (!mMulti->isStopped()) { + mMulti->mSignal->signal(); apr_sleep(1000); } delete mMulti; -- cgit v1.2.3 From e4a8ef4ce2572db98d08233c58e23d3ec75f30d7 Mon Sep 17 00:00:00 2001 From: Dave Parks Date: Fri, 22 Jul 2011 02:33:55 -0500 Subject: SH-2031 Cleanup from threaded curl implementation (remove errors/loops on shutdown). --- indra/llmessage/llcurl.cpp | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) (limited to 'indra/llmessage/llcurl.cpp') diff --git a/indra/llmessage/llcurl.cpp b/indra/llmessage/llcurl.cpp index 2bb36f494c..1e735c4bbd 100644 --- a/indra/llmessage/llcurl.cpp +++ b/indra/llmessage/llcurl.cpp @@ -693,21 +693,25 @@ void LLCurl::Multi::run() while (!mQuitting) { mSignal->wait(); - S32 q = 0; - for (S32 call_count = 0; - call_count < MULTI_PERFORM_CALL_REPEAT; - call_count += 1) + + if (!mQuitting) { - CURLMcode code = curl_multi_perform(mCurlMultiHandle, &q); - if (CURLM_CALL_MULTI_PERFORM != code || q == 0) + S32 q = 0; + for (S32 call_count = 0; + call_count < MULTI_PERFORM_CALL_REPEAT; + call_count += 1) { - check_curl_multi_code(code); - break; - } + CURLMcode code = curl_multi_perform(mCurlMultiHandle, &q); + if (CURLM_CALL_MULTI_PERFORM != code || q == 0) + { + check_curl_multi_code(code); + break; + } + } + mQueued = q; + mPerformState = PERFORM_STATE_COMPLETED; } - mQueued = q; - mPerformState = PERFORM_STATE_COMPLETED; } } @@ -830,6 +834,18 @@ LLCurlRequest::LLCurlRequest() : LLCurlRequest::~LLCurlRequest() { llassert_always(mThreadID == LLThread::currentID()); + + //stop all Multi handle background threads + for (curlmulti_set_t::iterator iter = mMultiSet.begin(); iter != mMultiSet.end(); ++iter) + { + LLCurl::Multi* multi = *iter; + multi->mQuitting = true; + while (!multi->isStopped()) + { + multi->mSignal->signal(); + apr_sleep(1000); + } + } for_each(mMultiSet.begin(), mMultiSet.end(), DeletePointer()); } -- cgit v1.2.3 From 26a9a6929c23eabfef0a53355a392fef0ad26b83 Mon Sep 17 00:00:00 2001 From: Dave Parks Date: Fri, 22 Jul 2011 16:22:51 -0500 Subject: SH-2031 Fix for sometimes deadlocking a curl thread. --- indra/llmessage/llcurl.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'indra/llmessage/llcurl.cpp') diff --git a/indra/llmessage/llcurl.cpp b/indra/llmessage/llcurl.cpp index 1e735c4bbd..0735842dcd 100644 --- a/indra/llmessage/llcurl.cpp +++ b/indra/llmessage/llcurl.cpp @@ -683,7 +683,6 @@ void LLCurl::Multi::perform() { if (mPerformState == PERFORM_STATE_READY) { - mPerformState = PERFORM_STATE_PERFORMING; mSignal->signal(); } } @@ -693,7 +692,7 @@ void LLCurl::Multi::run() while (!mQuitting) { mSignal->wait(); - + mPerformState = PERFORM_STATE_PERFORMING; if (!mQuitting) { S32 q = 0; -- cgit v1.2.3