From b262ded7e0cf21314524bf702b0e4fe28a3c3060 Mon Sep 17 00:00:00 2001 From: Nat Goodspeed Date: Wed, 1 Jul 2015 18:33:29 -0400 Subject: MAINT-5351: Remove 'self' parameter from coroutine functions. lleventcoro_test.cpp runs clean (as modified for new API), and all the rest builds clean, but the resulting viewer is as yet untested. --- indra/llcommon/llcoros.cpp | 86 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 79 insertions(+), 7 deletions(-) (limited to 'indra/llcommon/llcoros.cpp') diff --git a/indra/llcommon/llcoros.cpp b/indra/llcommon/llcoros.cpp index baaddcaed1..2d0c419ae0 100755 --- a/indra/llcommon/llcoros.cpp +++ b/indra/llcommon/llcoros.cpp @@ -34,11 +34,65 @@ // std headers // external library headers #include +#include // other Linden headers #include "llevents.h" #include "llerror.h" #include "stringize.h" +namespace { + +// do nothing, when we need nothing done +void no_cleanup(LLCoros::coro::self*) {} + +// When the dcoroutine library calls a top-level callable, it implicitly +// passes coro::self& as the first parameter. All our consumer code used to +// explicitly pass coro::self& down through all levels of call stack, because +// at the leaf level we need it for context-switching. But since coroutines +// are based on cooperative switching, we can cause the top-level entry point +// to stash a static pointer to the currently-running coroutine, and manage it +// appropriately as we switch out and back in. That eliminates the need to +// pass it as an explicit parameter down through every level, which is +// unfortunately viral in nature. Finding it implicitly rather than explicitly +// allows minor maintenance in which a leaf-level function adds a new async +// I/O call that suspends the calling coroutine, WITHOUT having to propagate +// coro::self& through every function signature down to that point -- and of +// course through every other caller of every such function. +// We use a boost::thread_specific_ptr because each thread potentially has its +// own distinct pool of coroutines. +// This thread_specific_ptr does NOT own the 'self' object! It merely +// identifies it. For this reason we instantiate it with a no-op cleanup +// function. +static boost::thread_specific_ptr +sCurrentSelf(no_cleanup); + +} // anonymous + +//static +LLCoros::coro::self& LLCoros::get_self() +{ + coro::self* current_self = sCurrentSelf.get(); + if (! current_self) + { + LL_ERRS("LLCoros") << "Calling get_self() from non-coroutine context!" << LL_ENDL; + } + return *current_self; +} + +LLCoros::Suspending::Suspending(): + mSuspended(sCurrentSelf.get()) +{ + // For the duration of our time away from this coroutine, sCurrentSelf + // must NOT refer to this coroutine. + sCurrentSelf.reset(); +} + +LLCoros::Suspending::~Suspending() +{ + // Okay, we're back, reinstate previous value of sCurrentSelf. + sCurrentSelf.reset(mSuspended); +} + LLCoros::LLCoros(): // MAINT-2724: default coroutine stack size too small on Windows. // Previously we used @@ -60,7 +114,7 @@ bool LLCoros::cleanup(const LLSD&) // since last tick? if (mi->second->exited()) { - LL_INFOS("LLCoros") << "LLCoros: cleaning up coroutine " << mi->first << LL_ENDL; + LL_INFOS("LLCoros") << "LLCoros: cleaning up coroutine " << mi->first << LL_ENDL; // The erase() call will invalidate its passed iterator value -- // so increment mi FIRST -- but pass its original value to // erase(). This is what postincrement is all about. @@ -94,7 +148,7 @@ std::string LLCoros::generateDistinctName(const std::string& prefix) const { if (mCoros.find(name) == mCoros.end()) { - LL_INFOS("LLCoros") << "LLCoros: launching coroutine " << name << LL_ENDL; + LL_INFOS("LLCoros") << "LLCoros: launching coroutine " << name << LL_ENDL; return name; } } @@ -114,10 +168,10 @@ bool LLCoros::kill(const std::string& name) return true; } -std::string LLCoros::getNameByID(const void* self_id) const +std::string LLCoros::getName() const { - // Walk the existing coroutines, looking for one from which the 'self_id' - // passed to us comes. + // Walk the existing coroutines, looking for the current one. + void* self_id = get_self().get_id(); for (CoroMap::const_iterator mi(mCoros.begin()), mend(mCoros.end()); mi != mend; ++mi) { namespace coro_private = boost::dcoroutines::detail; @@ -136,10 +190,24 @@ void LLCoros::setStackSize(S32 stacksize) mStackSize = stacksize; } +namespace { + +// Top-level wrapper around caller's coroutine callable. This function accepts +// the coroutine library's implicit coro::self& parameter and sets sCurrentSelf +// but does not pass it down to the caller's callable. +void toplevel(LLCoros::coro::self& self, const LLCoros::callable_t& callable) +{ + sCurrentSelf.reset(&self); + callable(); + sCurrentSelf.reset(); +} + +} // anonymous + /***************************************************************************** * MUST BE LAST *****************************************************************************/ -// Turn off MSVC optimizations for just LLCoros::launchImpl() -- see +// Turn off MSVC optimizations for just LLCoros::launch() -- see // DEV-32777. But MSVC doesn't support push/pop for optimization flags as it // does for warning suppression, and we really don't want to force // optimization ON for other code even in Debug or RelWithDebInfo builds. @@ -150,9 +218,13 @@ void LLCoros::setStackSize(S32 stacksize) #pragma optimize("", off) #endif // LL_MSVC -std::string LLCoros::launchImpl(const std::string& prefix, coro* newCoro) +std::string LLCoros::launch(const std::string& prefix, const callable_t& callable) { std::string name(generateDistinctName(prefix)); + // Wrap the caller's callable in our toplevel() function so we can manage + // sCurrentSelf appropriately at startup and shutdown of each coroutine. + coro* newCoro = new coro(boost::bind(toplevel, _1, callable), mStackSize); + // Store it in our pointer map mCoros.insert(name, newCoro); /* Run the coroutine until its first wait, then return here */ (*newCoro)(std::nothrow); -- cgit v1.2.3 From f90023fc0b3b61fd346a2b56e30e5f3c35814192 Mon Sep 17 00:00:00 2001 From: Nat Goodspeed Date: Thu, 2 Jul 2015 17:00:32 -0400 Subject: MAINT-5357: Introduce and populate llcoro:: namespace. To date, the coroutine helper functions in lleventcoro.h have been in the global namespace. Migrate them into llcoro namespace, and fix references. Specifically, LLVoidListener => llcoro::VoidListener, and voidlistener(), postAndWait(), both waitForEventOn(), postAndWait2(), errorException() and errorLog() have been moved into llcoro. Also migrate new LLCoros::get_self() and Suspending to llcoro:: namespace. While at it, I realized that -- having converted several lleventcoro.h functions from templates (for arbitrary 'self' parameter type) to ordinary functions, having moved them from lleventcoro.h to lleventcoro.cpp, we can now migrate their helpers from lleventcoro.h to lleventcoro.cpp as well. This eliminates the need for the LLEventDetail namespace; the relevant helpers are now in an anonymous namespace in the .cpp file: listenerNameForCoro(), storeToLLSDPath(), WaitForEventOnHelper and wfeoh(). --- indra/llcommon/llcoros.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'indra/llcommon/llcoros.cpp') diff --git a/indra/llcommon/llcoros.cpp b/indra/llcommon/llcoros.cpp index 2d0c419ae0..957fe034e1 100755 --- a/indra/llcommon/llcoros.cpp +++ b/indra/llcommon/llcoros.cpp @@ -69,9 +69,9 @@ sCurrentSelf(no_cleanup); } // anonymous //static -LLCoros::coro::self& LLCoros::get_self() +LLCoros::coro::self& llcoro::get_self() { - coro::self* current_self = sCurrentSelf.get(); + LLCoros::coro::self* current_self = sCurrentSelf.get(); if (! current_self) { LL_ERRS("LLCoros") << "Calling get_self() from non-coroutine context!" << LL_ENDL; @@ -79,7 +79,7 @@ LLCoros::coro::self& LLCoros::get_self() return *current_self; } -LLCoros::Suspending::Suspending(): +llcoro::Suspending::Suspending(): mSuspended(sCurrentSelf.get()) { // For the duration of our time away from this coroutine, sCurrentSelf @@ -87,7 +87,7 @@ LLCoros::Suspending::Suspending(): sCurrentSelf.reset(); } -LLCoros::Suspending::~Suspending() +llcoro::Suspending::~Suspending() { // Okay, we're back, reinstate previous value of sCurrentSelf. sCurrentSelf.reset(mSuspended); @@ -171,7 +171,7 @@ bool LLCoros::kill(const std::string& name) std::string LLCoros::getName() const { // Walk the existing coroutines, looking for the current one. - void* self_id = get_self().get_id(); + void* self_id = llcoro::get_self().get_id(); for (CoroMap::const_iterator mi(mCoros.begin()), mend(mCoros.end()); mi != mend; ++mi) { namespace coro_private = boost::dcoroutines::detail; -- cgit v1.2.3 From 247eb0c9c3418c10be8f2a0e3c8116758efa702f Mon Sep 17 00:00:00 2001 From: Rider Linden Date: Tue, 7 Jul 2015 19:41:27 +0100 Subject: Backout selfles merge 738255dbbfd679d9e615baab3398e5e345bbb3c5 --- indra/llcommon/llcoros.cpp | 86 ++++------------------------------------------ 1 file changed, 7 insertions(+), 79 deletions(-) (limited to 'indra/llcommon/llcoros.cpp') diff --git a/indra/llcommon/llcoros.cpp b/indra/llcommon/llcoros.cpp index 2d0c419ae0..baaddcaed1 100755 --- a/indra/llcommon/llcoros.cpp +++ b/indra/llcommon/llcoros.cpp @@ -34,65 +34,11 @@ // std headers // external library headers #include -#include // other Linden headers #include "llevents.h" #include "llerror.h" #include "stringize.h" -namespace { - -// do nothing, when we need nothing done -void no_cleanup(LLCoros::coro::self*) {} - -// When the dcoroutine library calls a top-level callable, it implicitly -// passes coro::self& as the first parameter. All our consumer code used to -// explicitly pass coro::self& down through all levels of call stack, because -// at the leaf level we need it for context-switching. But since coroutines -// are based on cooperative switching, we can cause the top-level entry point -// to stash a static pointer to the currently-running coroutine, and manage it -// appropriately as we switch out and back in. That eliminates the need to -// pass it as an explicit parameter down through every level, which is -// unfortunately viral in nature. Finding it implicitly rather than explicitly -// allows minor maintenance in which a leaf-level function adds a new async -// I/O call that suspends the calling coroutine, WITHOUT having to propagate -// coro::self& through every function signature down to that point -- and of -// course through every other caller of every such function. -// We use a boost::thread_specific_ptr because each thread potentially has its -// own distinct pool of coroutines. -// This thread_specific_ptr does NOT own the 'self' object! It merely -// identifies it. For this reason we instantiate it with a no-op cleanup -// function. -static boost::thread_specific_ptr -sCurrentSelf(no_cleanup); - -} // anonymous - -//static -LLCoros::coro::self& LLCoros::get_self() -{ - coro::self* current_self = sCurrentSelf.get(); - if (! current_self) - { - LL_ERRS("LLCoros") << "Calling get_self() from non-coroutine context!" << LL_ENDL; - } - return *current_self; -} - -LLCoros::Suspending::Suspending(): - mSuspended(sCurrentSelf.get()) -{ - // For the duration of our time away from this coroutine, sCurrentSelf - // must NOT refer to this coroutine. - sCurrentSelf.reset(); -} - -LLCoros::Suspending::~Suspending() -{ - // Okay, we're back, reinstate previous value of sCurrentSelf. - sCurrentSelf.reset(mSuspended); -} - LLCoros::LLCoros(): // MAINT-2724: default coroutine stack size too small on Windows. // Previously we used @@ -114,7 +60,7 @@ bool LLCoros::cleanup(const LLSD&) // since last tick? if (mi->second->exited()) { - LL_INFOS("LLCoros") << "LLCoros: cleaning up coroutine " << mi->first << LL_ENDL; + LL_INFOS("LLCoros") << "LLCoros: cleaning up coroutine " << mi->first << LL_ENDL; // The erase() call will invalidate its passed iterator value -- // so increment mi FIRST -- but pass its original value to // erase(). This is what postincrement is all about. @@ -148,7 +94,7 @@ std::string LLCoros::generateDistinctName(const std::string& prefix) const { if (mCoros.find(name) == mCoros.end()) { - LL_INFOS("LLCoros") << "LLCoros: launching coroutine " << name << LL_ENDL; + LL_INFOS("LLCoros") << "LLCoros: launching coroutine " << name << LL_ENDL; return name; } } @@ -168,10 +114,10 @@ bool LLCoros::kill(const std::string& name) return true; } -std::string LLCoros::getName() const +std::string LLCoros::getNameByID(const void* self_id) const { - // Walk the existing coroutines, looking for the current one. - void* self_id = get_self().get_id(); + // Walk the existing coroutines, looking for one from which the 'self_id' + // passed to us comes. for (CoroMap::const_iterator mi(mCoros.begin()), mend(mCoros.end()); mi != mend; ++mi) { namespace coro_private = boost::dcoroutines::detail; @@ -190,24 +136,10 @@ void LLCoros::setStackSize(S32 stacksize) mStackSize = stacksize; } -namespace { - -// Top-level wrapper around caller's coroutine callable. This function accepts -// the coroutine library's implicit coro::self& parameter and sets sCurrentSelf -// but does not pass it down to the caller's callable. -void toplevel(LLCoros::coro::self& self, const LLCoros::callable_t& callable) -{ - sCurrentSelf.reset(&self); - callable(); - sCurrentSelf.reset(); -} - -} // anonymous - /***************************************************************************** * MUST BE LAST *****************************************************************************/ -// Turn off MSVC optimizations for just LLCoros::launch() -- see +// Turn off MSVC optimizations for just LLCoros::launchImpl() -- see // DEV-32777. But MSVC doesn't support push/pop for optimization flags as it // does for warning suppression, and we really don't want to force // optimization ON for other code even in Debug or RelWithDebInfo builds. @@ -218,13 +150,9 @@ void toplevel(LLCoros::coro::self& self, const LLCoros::callable_t& callable) #pragma optimize("", off) #endif // LL_MSVC -std::string LLCoros::launch(const std::string& prefix, const callable_t& callable) +std::string LLCoros::launchImpl(const std::string& prefix, coro* newCoro) { std::string name(generateDistinctName(prefix)); - // Wrap the caller's callable in our toplevel() function so we can manage - // sCurrentSelf appropriately at startup and shutdown of each coroutine. - coro* newCoro = new coro(boost::bind(toplevel, _1, callable), mStackSize); - // Store it in our pointer map mCoros.insert(name, newCoro); /* Run the coroutine until its first wait, then return here */ (*newCoro)(std::nothrow); -- cgit v1.2.3 From 0c915913fd05007bb3fb6cc84be89799b7d85ae5 Mon Sep 17 00:00:00 2001 From: Nat Goodspeed Date: Fri, 10 Jul 2015 16:01:15 -0400 Subject: MAINT-5351: Improve management of "current" coroutine information. Our first cut at tracking the "current" coroutine simply reset the pointer to NULL every time we context-switched away. But that strategy doesn't handle the case of coroutine A launching coroutine B. Introduce LLCoros::CoroData to track, among other things, the previous value of the current-coroutine pointer each time we switch into a coroutine. Restore THAT value when we switch back out. --- indra/llcommon/llcoros.cpp | 111 +++++++++++++++++++++------------------------ 1 file changed, 52 insertions(+), 59 deletions(-) (limited to 'indra/llcommon/llcoros.cpp') diff --git a/indra/llcommon/llcoros.cpp b/indra/llcommon/llcoros.cpp index 957fe034e1..d76401d01b 100755 --- a/indra/llcommon/llcoros.cpp +++ b/indra/llcommon/llcoros.cpp @@ -34,63 +34,47 @@ // std headers // external library headers #include -#include // other Linden headers #include "llevents.h" #include "llerror.h" #include "stringize.h" -namespace { - // do nothing, when we need nothing done -void no_cleanup(LLCoros::coro::self*) {} - -// When the dcoroutine library calls a top-level callable, it implicitly -// passes coro::self& as the first parameter. All our consumer code used to -// explicitly pass coro::self& down through all levels of call stack, because -// at the leaf level we need it for context-switching. But since coroutines -// are based on cooperative switching, we can cause the top-level entry point -// to stash a static pointer to the currently-running coroutine, and manage it -// appropriately as we switch out and back in. That eliminates the need to -// pass it as an explicit parameter down through every level, which is -// unfortunately viral in nature. Finding it implicitly rather than explicitly -// allows minor maintenance in which a leaf-level function adds a new async -// I/O call that suspends the calling coroutine, WITHOUT having to propagate -// coro::self& through every function signature down to that point -- and of -// course through every other caller of every such function. -// We use a boost::thread_specific_ptr because each thread potentially has its -// own distinct pool of coroutines. -// This thread_specific_ptr does NOT own the 'self' object! It merely -// identifies it. For this reason we instantiate it with a no-op cleanup -// function. -static boost::thread_specific_ptr -sCurrentSelf(no_cleanup); - -} // anonymous +void LLCoros::no_cleanup(CoroData*) {} + +// CoroData for the currently-running coroutine. Use a thread_specific_ptr +// because each thread potentially has its own distinct pool of coroutines. +// This thread_specific_ptr does NOT own the CoroData object! That's owned by +// LLCoros::mCoros. It merely identifies it. For this reason we instantiate +// it with a no-op cleanup function. +boost::thread_specific_ptr +LLCoros::sCurrentCoro(LLCoros::no_cleanup); //static -LLCoros::coro::self& llcoro::get_self() +LLCoros::coro::self& LLCoros::get_self() { - LLCoros::coro::self* current_self = sCurrentSelf.get(); - if (! current_self) + CoroData* current = sCurrentCoro.get(); + if (! current) { LL_ERRS("LLCoros") << "Calling get_self() from non-coroutine context!" << LL_ENDL; } - return *current_self; + return *current->mSelf; } llcoro::Suspending::Suspending(): - mSuspended(sCurrentSelf.get()) + mSuspended(LLCoros::sCurrentCoro.get()) { - // For the duration of our time away from this coroutine, sCurrentSelf - // must NOT refer to this coroutine. - sCurrentSelf.reset(); + // Revert mCurrentCoro to the value it had at the moment we last switched + // into this coroutine. + LLCoros::sCurrentCoro.reset(mSuspended->mPrev); } llcoro::Suspending::~Suspending() { - // Okay, we're back, reinstate previous value of sCurrentSelf. - sCurrentSelf.reset(mSuspended); + // Okay, we're back, update our mPrev + mSuspended->mPrev = LLCoros::sCurrentCoro.get(); + // and reinstate our sCurrentCoro. + LLCoros::sCurrentCoro.reset(mSuspended); } LLCoros::LLCoros(): @@ -112,7 +96,7 @@ bool LLCoros::cleanup(const LLSD&) { // Has this coroutine exited (normal return, exception, exit() call) // since last tick? - if (mi->second->exited()) + if (mi->second->mCoro.exited()) { LL_INFOS("LLCoros") << "LLCoros: cleaning up coroutine " << mi->first << LL_ENDL; // The erase() call will invalidate its passed iterator value -- @@ -170,18 +154,13 @@ bool LLCoros::kill(const std::string& name) std::string LLCoros::getName() const { - // Walk the existing coroutines, looking for the current one. - void* self_id = llcoro::get_self().get_id(); - for (CoroMap::const_iterator mi(mCoros.begin()), mend(mCoros.end()); mi != mend; ++mi) + CoroData* current = sCurrentCoro.get(); + if (! current) { - namespace coro_private = boost::dcoroutines::detail; - if (static_cast(coro_private::coroutine_accessor::get_impl(const_cast(*mi->second)).get()) - == self_id) - { - return mi->first; - } + // not in a coroutine + return ""; } - return ""; + return current->mName; } void LLCoros::setStackSize(S32 stacksize) @@ -190,20 +169,20 @@ void LLCoros::setStackSize(S32 stacksize) mStackSize = stacksize; } -namespace { - // Top-level wrapper around caller's coroutine callable. This function accepts // the coroutine library's implicit coro::self& parameter and sets sCurrentSelf // but does not pass it down to the caller's callable. -void toplevel(LLCoros::coro::self& self, const LLCoros::callable_t& callable) +void LLCoros::toplevel(coro::self& self, CoroData* data, const callable_t& callable) { - sCurrentSelf.reset(&self); + // capture the 'self' param in CoroData + data->mSelf = &self; + // run the code the caller actually wants in the coroutine callable(); - sCurrentSelf.reset(); + // This cleanup isn't perfectly symmetrical with the way we initially set + // data->mPrev, but this is our last chance to reset mCurrentCoro. + sCurrentCoro.reset(data->mPrev); } -} // anonymous - /***************************************************************************** * MUST BE LAST *****************************************************************************/ @@ -215,19 +194,33 @@ void toplevel(LLCoros::coro::self& self, const LLCoros::callable_t& callable) #if LL_MSVC // work around broken optimizations #pragma warning(disable: 4748) +#pragma warning(disable: 4355) // 'this' used in initializer list: yes, intentionally #pragma optimize("", off) #endif // LL_MSVC +LLCoros::CoroData::CoroData(CoroData* prev, const std::string& name, + const callable_t& callable, S32 stacksize): + mPrev(prev), + mName(name), + // Wrap the caller's callable in our toplevel() function so we can manage + // sCurrentCoro appropriately at startup and shutdown of each coroutine. + mCoro(boost::bind(toplevel, _1, this, callable), stacksize), + mSelf(0) +{ +} + std::string LLCoros::launch(const std::string& prefix, const callable_t& callable) { std::string name(generateDistinctName(prefix)); - // Wrap the caller's callable in our toplevel() function so we can manage - // sCurrentSelf appropriately at startup and shutdown of each coroutine. - coro* newCoro = new coro(boost::bind(toplevel, _1, callable), mStackSize); + // pass the current value of sCurrentCoro as previous context + CoroData* newCoro = new CoroData(sCurrentCoro.get(), name, + callable, mStackSize); // Store it in our pointer map mCoros.insert(name, newCoro); + // also set it as current + sCurrentCoro.reset(newCoro); /* Run the coroutine until its first wait, then return here */ - (*newCoro)(std::nothrow); + (newCoro->mCoro)(std::nothrow); return name; } -- cgit v1.2.3 From efa9a0f99c17b2b937120bcad6e3d45944122ed9 Mon Sep 17 00:00:00 2001 From: Nat Goodspeed Date: Fri, 10 Jul 2015 19:30:10 -0400 Subject: Backed out changeset bab1000e1b2d: restore 'selfless' changes --- indra/llcommon/llcoros.cpp | 86 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 79 insertions(+), 7 deletions(-) (limited to 'indra/llcommon/llcoros.cpp') diff --git a/indra/llcommon/llcoros.cpp b/indra/llcommon/llcoros.cpp index baaddcaed1..2d0c419ae0 100755 --- a/indra/llcommon/llcoros.cpp +++ b/indra/llcommon/llcoros.cpp @@ -34,11 +34,65 @@ // std headers // external library headers #include +#include // other Linden headers #include "llevents.h" #include "llerror.h" #include "stringize.h" +namespace { + +// do nothing, when we need nothing done +void no_cleanup(LLCoros::coro::self*) {} + +// When the dcoroutine library calls a top-level callable, it implicitly +// passes coro::self& as the first parameter. All our consumer code used to +// explicitly pass coro::self& down through all levels of call stack, because +// at the leaf level we need it for context-switching. But since coroutines +// are based on cooperative switching, we can cause the top-level entry point +// to stash a static pointer to the currently-running coroutine, and manage it +// appropriately as we switch out and back in. That eliminates the need to +// pass it as an explicit parameter down through every level, which is +// unfortunately viral in nature. Finding it implicitly rather than explicitly +// allows minor maintenance in which a leaf-level function adds a new async +// I/O call that suspends the calling coroutine, WITHOUT having to propagate +// coro::self& through every function signature down to that point -- and of +// course through every other caller of every such function. +// We use a boost::thread_specific_ptr because each thread potentially has its +// own distinct pool of coroutines. +// This thread_specific_ptr does NOT own the 'self' object! It merely +// identifies it. For this reason we instantiate it with a no-op cleanup +// function. +static boost::thread_specific_ptr +sCurrentSelf(no_cleanup); + +} // anonymous + +//static +LLCoros::coro::self& LLCoros::get_self() +{ + coro::self* current_self = sCurrentSelf.get(); + if (! current_self) + { + LL_ERRS("LLCoros") << "Calling get_self() from non-coroutine context!" << LL_ENDL; + } + return *current_self; +} + +LLCoros::Suspending::Suspending(): + mSuspended(sCurrentSelf.get()) +{ + // For the duration of our time away from this coroutine, sCurrentSelf + // must NOT refer to this coroutine. + sCurrentSelf.reset(); +} + +LLCoros::Suspending::~Suspending() +{ + // Okay, we're back, reinstate previous value of sCurrentSelf. + sCurrentSelf.reset(mSuspended); +} + LLCoros::LLCoros(): // MAINT-2724: default coroutine stack size too small on Windows. // Previously we used @@ -60,7 +114,7 @@ bool LLCoros::cleanup(const LLSD&) // since last tick? if (mi->second->exited()) { - LL_INFOS("LLCoros") << "LLCoros: cleaning up coroutine " << mi->first << LL_ENDL; + LL_INFOS("LLCoros") << "LLCoros: cleaning up coroutine " << mi->first << LL_ENDL; // The erase() call will invalidate its passed iterator value -- // so increment mi FIRST -- but pass its original value to // erase(). This is what postincrement is all about. @@ -94,7 +148,7 @@ std::string LLCoros::generateDistinctName(const std::string& prefix) const { if (mCoros.find(name) == mCoros.end()) { - LL_INFOS("LLCoros") << "LLCoros: launching coroutine " << name << LL_ENDL; + LL_INFOS("LLCoros") << "LLCoros: launching coroutine " << name << LL_ENDL; return name; } } @@ -114,10 +168,10 @@ bool LLCoros::kill(const std::string& name) return true; } -std::string LLCoros::getNameByID(const void* self_id) const +std::string LLCoros::getName() const { - // Walk the existing coroutines, looking for one from which the 'self_id' - // passed to us comes. + // Walk the existing coroutines, looking for the current one. + void* self_id = get_self().get_id(); for (CoroMap::const_iterator mi(mCoros.begin()), mend(mCoros.end()); mi != mend; ++mi) { namespace coro_private = boost::dcoroutines::detail; @@ -136,10 +190,24 @@ void LLCoros::setStackSize(S32 stacksize) mStackSize = stacksize; } +namespace { + +// Top-level wrapper around caller's coroutine callable. This function accepts +// the coroutine library's implicit coro::self& parameter and sets sCurrentSelf +// but does not pass it down to the caller's callable. +void toplevel(LLCoros::coro::self& self, const LLCoros::callable_t& callable) +{ + sCurrentSelf.reset(&self); + callable(); + sCurrentSelf.reset(); +} + +} // anonymous + /***************************************************************************** * MUST BE LAST *****************************************************************************/ -// Turn off MSVC optimizations for just LLCoros::launchImpl() -- see +// Turn off MSVC optimizations for just LLCoros::launch() -- see // DEV-32777. But MSVC doesn't support push/pop for optimization flags as it // does for warning suppression, and we really don't want to force // optimization ON for other code even in Debug or RelWithDebInfo builds. @@ -150,9 +218,13 @@ void LLCoros::setStackSize(S32 stacksize) #pragma optimize("", off) #endif // LL_MSVC -std::string LLCoros::launchImpl(const std::string& prefix, coro* newCoro) +std::string LLCoros::launch(const std::string& prefix, const callable_t& callable) { std::string name(generateDistinctName(prefix)); + // Wrap the caller's callable in our toplevel() function so we can manage + // sCurrentSelf appropriately at startup and shutdown of each coroutine. + coro* newCoro = new coro(boost::bind(toplevel, _1, callable), mStackSize); + // Store it in our pointer map mCoros.insert(name, newCoro); /* Run the coroutine until its first wait, then return here */ (*newCoro)(std::nothrow); -- cgit v1.2.3 From 68f42e4e102bd578386aeec3485233e332cbdcff Mon Sep 17 00:00:00 2001 From: Nat Goodspeed Date: Fri, 10 Jul 2015 19:44:47 -0400 Subject: MAINT-5351: Finish cleaning up messy merge from backing out backout --- indra/llcommon/llcoros.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'indra/llcommon/llcoros.cpp') diff --git a/indra/llcommon/llcoros.cpp b/indra/llcommon/llcoros.cpp index 6cca5e7c60..d76401d01b 100755 --- a/indra/llcommon/llcoros.cpp +++ b/indra/llcommon/llcoros.cpp @@ -61,7 +61,7 @@ LLCoros::coro::self& LLCoros::get_self() return *current->mSelf; } -LLCoros::Suspending::Suspending(): +llcoro::Suspending::Suspending(): mSuspended(LLCoros::sCurrentCoro.get()) { // Revert mCurrentCoro to the value it had at the moment we last switched @@ -69,7 +69,7 @@ LLCoros::Suspending::Suspending(): LLCoros::sCurrentCoro.reset(mSuspended->mPrev); } -LLCoros::Suspending::~Suspending() +llcoro::Suspending::~Suspending() { // Okay, we're back, update our mPrev mSuspended->mPrev = LLCoros::sCurrentCoro.get(); -- cgit v1.2.3 From 2af14639de9f575ac9a2766835206e5c6ffb46c8 Mon Sep 17 00:00:00 2001 From: Rider Linden Date: Fri, 20 Nov 2015 12:24:53 -0800 Subject: MAINT-5835: Cut down on log spam from coros and voice. --- indra/llcommon/llcoros.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) (limited to 'indra/llcommon/llcoros.cpp') diff --git a/indra/llcommon/llcoros.cpp b/indra/llcommon/llcoros.cpp index d76401d01b..548a6d22be 100755 --- a/indra/llcommon/llcoros.cpp +++ b/indra/llcommon/llcoros.cpp @@ -91,6 +91,8 @@ LLCoros::LLCoros(): bool LLCoros::cleanup(const LLSD&) { + static std::string previousName; + static int previousCount = 0; // Walk the mCoros map, checking and removing completed coroutines. for (CoroMap::iterator mi(mCoros.begin()), mend(mCoros.end()); mi != mend; ) { @@ -98,7 +100,24 @@ bool LLCoros::cleanup(const LLSD&) // since last tick? if (mi->second->mCoro.exited()) { - LL_INFOS("LLCoros") << "LLCoros: cleaning up coroutine " << mi->first << LL_ENDL; + if (previousName != mi->first) + { + previousName = mi->first; + previousCount = 1; + } + else + { + ++previousCount; + } + + if ((previousCount < 5) || !(previousCount % 50)) + { + if (previousCount < 5) + LL_INFOS("LLCoros") << "LLCoros: cleaning up coroutine " << mi->first << LL_ENDL; + else + LL_INFOS("LLCoros") << "LLCoros: cleaning up coroutine " << mi->first << "("<< previousCount << ")" << LL_ENDL; + + } // The erase() call will invalidate its passed iterator value -- // so increment mi FIRST -- but pass its original value to // erase(). This is what postincrement is all about. @@ -116,6 +135,9 @@ bool LLCoros::cleanup(const LLSD&) std::string LLCoros::generateDistinctName(const std::string& prefix) const { + static std::string previousName; + static int previousCount = 0; + // Allowing empty name would make getName()'s not-found return ambiguous. if (prefix.empty()) { @@ -132,7 +154,25 @@ std::string LLCoros::generateDistinctName(const std::string& prefix) const { if (mCoros.find(name) == mCoros.end()) { - LL_INFOS("LLCoros") << "LLCoros: launching coroutine " << name << LL_ENDL; + if (previousName != name) + { + previousName = name; + previousCount = 1; + } + else + { + ++previousCount; + } + + if ((previousCount < 5) || !(previousCount % 50)) + { + if (previousCount < 5) + LL_INFOS("LLCoros") << "LLCoros: launching coroutine " << name << LL_ENDL; + else + LL_INFOS("LLCoros") << "LLCoros: launching coroutine " << name << "(" << previousCount << ")" << LL_ENDL; + + } + return name; } } -- cgit v1.2.3 From 6a062089b86c092d6227a64502c4829fc6f0c586 Mon Sep 17 00:00:00 2001 From: Nat Goodspeed Date: Wed, 16 Dec 2015 17:13:04 -0500 Subject: MAINT-5976: Introduce LLCoros::set_consuming(bool). set_consuming(true) tells each postAndSuspend() call to consume the event for which it is suspending. --- indra/llcommon/llcoros.cpp | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) (limited to 'indra/llcommon/llcoros.cpp') diff --git a/indra/llcommon/llcoros.cpp b/indra/llcommon/llcoros.cpp index 548a6d22be..d16bf0160b 100755 --- a/indra/llcommon/llcoros.cpp +++ b/indra/llcommon/llcoros.cpp @@ -51,14 +51,32 @@ boost::thread_specific_ptr LLCoros::sCurrentCoro(LLCoros::no_cleanup); //static -LLCoros::coro::self& LLCoros::get_self() +LLCoros::CoroData& LLCoros::get_CoroData(const std::string& caller) { CoroData* current = sCurrentCoro.get(); if (! current) { - LL_ERRS("LLCoros") << "Calling get_self() from non-coroutine context!" << LL_ENDL; + LL_ERRS("LLCoros") << "Calling " << caller << " from non-coroutine context!" << LL_ENDL; } - return *current->mSelf; + return *current; +} + +//static +LLCoros::coro::self& LLCoros::get_self() +{ + return *get_CoroData("get_self()").mSelf; +} + +//static +void LLCoros::set_consuming(bool consuming) +{ + get_CoroData("set_consuming()").mConsuming = consuming; +} + +//static +bool LLCoros::get_consuming() +{ + return get_CoroData("get_consuming()").mConsuming; } llcoro::Suspending::Suspending(): @@ -245,6 +263,8 @@ LLCoros::CoroData::CoroData(CoroData* prev, const std::string& name, // Wrap the caller's callable in our toplevel() function so we can manage // sCurrentCoro appropriately at startup and shutdown of each coroutine. mCoro(boost::bind(toplevel, _1, this, callable), stacksize), + // don't consume events unless specifically directed + mConsuming(false), mSelf(0) { } -- cgit v1.2.3