From 2ee22ed26483381bb13bc840090bad1897b4fd27 Mon Sep 17 00:00:00 2001 From: Nat Goodspeed Date: Fri, 18 Dec 2015 18:06:32 -0500 Subject: MAINT-5976: Fix bug in LLCoros::set_consuming() mechanism. The original implementation of set_consuming() involved a bool* pointing to a local bool in VoidListener::operator()()'s stack frame. postAndSuspend() would set that bool (through the pointer) as soon as it returned from suspension. The trouble with that is that LLEventMailDrop potentially calls its new listener (fulfilling the future) immediately in the listen_impl() override -- in other words, way up at the top of postAndSuspend(), well before the code that sets the relevant bool. Instead, make the adapter formerly known as VoidListener bind the coroutine's get_consuming() value at adapter construction time (before listening on the LLEventPump), so that its operator()() has the coroutine's correct get_consuming() value to return. Eliminating the bool* makes the code both simpler AND more correct! This change makes that adapter very specific to coroutine usage. Rename it FutureListener and migrate it from lleventcoros.h into the .cpp file. Nobody else was using it anyway. Make corresponding changes to postAndSuspend2() and its WaitForEventOnHelper class -- whose name no longer corresponds to the function as it used to. Rename that one FutureListener2. The new FutureListener functionality, common to both these adapters, makes it useful to derive FutureListener2 from FutureListener. Introduce llmake(), a generic function to deduce template type arguments from function parameter types. This allows us to remove the voidlistener() and wfeoh() helper functions. Hiding VoidListener broke one of the lleventcoro_test.cpp tests. But that test was sort of a lame recap of an earlier implementation of postAndSuspend(), based on LLEventPump events. Recast that test to illustrate how to use a coroutine future to suspend a coroutine for something other than an LLEventPump. But that rubbed my nose in the fact that we MUST wrap future's context switching with proper management of the current coroutine. Introduce LLCoros::Future, which wraps boost::dcoroutines::future. Use LLCoros::Future in postAndSuspend() and postAndSuspend2(). --- indra/llcommon/llcoros.h | 48 +++++++++++ indra/llcommon/lleventcoro.cpp | 135 ++++++++++++++++-------------- indra/llcommon/lleventcoro.h | 30 ------- indra/llcommon/llmake.h | 63 ++++++++++++++ indra/llcommon/tests/lleventcoro_test.cpp | 62 +++++++------- 5 files changed, 219 insertions(+), 119 deletions(-) create mode 100644 indra/llcommon/llmake.h diff --git a/indra/llcommon/llcoros.h b/indra/llcommon/llcoros.h index 0b1f58f48e..f2fb7f1e70 100755 --- a/indra/llcommon/llcoros.h +++ b/indra/llcommon/llcoros.h @@ -30,6 +30,7 @@ #define LL_LLCOROS_H #include +#include #include "llsingleton.h" #include #include @@ -162,6 +163,15 @@ public: static void set_consuming(bool consuming); static bool get_consuming(); + /** + * Please do NOT directly use boost::dcoroutines::future! It is essential + * to maintain the "current" coroutine at every context switch. This + * Future wraps the essential boost::dcoroutines::future functionality + * with that maintenance. + */ + template + class Future; + private: LLCoros(); friend class LLSingleton; @@ -233,4 +243,42 @@ private: } // namespace llcoro +template +class LLCoros::Future +{ + typedef boost::dcoroutines::future dfuture; + +public: + Future(): + mFuture(get_self()) + {} + + typedef typename boost::dcoroutines::make_callback_result::type callback_t; + + callback_t make_callback() + { + return boost::dcoroutines::make_callback(mFuture); + } + + explicit operator bool() const + { + return bool(mFuture); + } + + bool operator!() const + { + return ! mFuture; + } + + T get() + { + // instantiate Suspending to manage the "current" coroutine + llcoro::Suspending suspended; + return *mFuture; + } + +private: + dfuture mFuture; +}; + #endif /* ! defined(LL_LLCOROS_H) */ diff --git a/indra/llcommon/lleventcoro.cpp b/indra/llcommon/lleventcoro.cpp index 9b7e8fb65f..578a2b62c8 100755 --- a/indra/llcommon/lleventcoro.cpp +++ b/indra/llcommon/lleventcoro.cpp @@ -34,12 +34,11 @@ #include // std headers // external library headers -#include -#include // other Linden headers #include "llsdserialize.h" #include "llerror.h" #include "llcoros.h" +#include "llmake.h" #include "lleventfilter.h" @@ -145,6 +144,36 @@ void storeToLLSDPath(LLSD& dest, const LLSD& rawPath, const LLSD& value) *pdest = value; } +/// For LLCoros::Future::make_callback(), the callback has a signature +/// like void callback(LLSD), which isn't a valid LLEventPump listener: such +/// listeners must return bool. +template +class FutureListener +{ +public: + // FutureListener is instantiated on the coroutine stack: the stack, in + // other words, that wants to suspend. + FutureListener(const LISTENER& listener): + mListener(listener), + // Capture the suspending coroutine's flag as a consuming or + // non-consuming listener. + mConsume(LLCoros::get_consuming()) + {} + + // operator()() is called on the main stack: the stack on which the + // expected event is fired. + bool operator()(const LLSD& event) + { + mListener(event); + // tell upstream LLEventPump whether listener consumed + return mConsume; + } + +protected: + LISTENER mListener; + bool mConsume; +}; + } // anonymous void llcoro::suspend() @@ -167,13 +196,13 @@ LLSD llcoro::postAndSuspend(const LLSD& event, const LLEventPumpOrPumpName& requ const LLEventPumpOrPumpName& replyPump, const LLSD& replyPumpNamePath) { // declare the future - boost::dcoroutines::future future(LLCoros::get_self()); + LLCoros::Future future; // make a callback that will assign a value to the future, and listen on // the specified LLEventPump with that callback std::string listenerName(listenerNameForCoro()); LLTempBoundListener connection( replyPump.getPump().listen(listenerName, - voidlistener(boost::dcoroutines::make_callback(future)))); + llmake(future.make_callback()))); // skip the "post" part if requestPump is default-constructed if (requestPump) { @@ -192,67 +221,56 @@ LLSD llcoro::postAndSuspend(const LLSD& event, const LLEventPumpOrPumpName& requ LL_DEBUGS("lleventcoro") << "postAndSuspend(): coroutine " << listenerName << " about to wait on LLEventPump " << replyPump.getPump().getName() << LL_ENDL; - // trying to dereference ("resolve") the future makes us wait for it - LLSD_consumed value; - { - // instantiate Suspending to manage the "current" coroutine - llcoro::Suspending suspended; - value = *future; - } // destroy Suspending as soon as we're back + // calling get() on the future makes us wait for it + LLSD value(future.get()); LL_DEBUGS("lleventcoro") << "postAndSuspend(): coroutine " << listenerName - << " resuming with " << value.first << LL_ENDL; - // immediately set consumed according to consuming - *value.second = LLCoros::get_consuming(); + << " resuming with " << value << LL_ENDL; // returning should disconnect the connection - return value.first; + return value; } namespace { -typedef std::pair LLEventWithID_consumed; - /** - * This helper is specifically for the two-pump version of suspendUntilEventOn(). - * We use a single future object, but we want to listen on two pumps with it. - * Since we must still adapt from (the callable constructed by) - * boost::dcoroutines::make_callback() (void return) to provide an event - * listener (bool return), we've adapted VoidListener for the purpose. The - * basic idea is that we construct a distinct instance of WaitForEventOnHelper - * -- binding different instance data -- for each of the pumps. Then, when a - * pump delivers an LLSD value to either WaitForEventOnHelper, it can combine - * that LLSD with its discriminator to feed the future object. + * This helper is specifically for postAndSuspend2(). We use a single future + * object, but we want to listen on two pumps with it. Since we must still + * adapt from the callable constructed by boost::dcoroutines::make_callback() + * (void return) to provide an event listener (bool return), we've adapted + * FutureListener for the purpose. The basic idea is that we construct a + * distinct instance of FutureListener2 -- binding different instance data -- + * for each of the pumps. Then, when a pump delivers an LLSD value to either + * FutureListener2, it can combine that LLSD with its discriminator to feed + * the future object. + * + * DISCRIM is a template argument so we can use llmake() rather than + * having to write our own argument-deducing helper function. */ -template -class WaitForEventOnHelper +template +class FutureListener2: public FutureListener { + typedef FutureListener super; + public: - WaitForEventOnHelper(const LISTENER& listener, int discriminator): - mListener(listener), + // instantiated on coroutine stack: the stack about to suspend + FutureListener2(const LISTENER& listener, DISCRIM discriminator): + super(listener), mDiscrim(discriminator) {} - // this signature is required for an LLEventPump listener + // called on main stack: the stack on which event is fired bool operator()(const LLSD& event) { - bool consumed = false; - // our future object is defined to accept LLEventWithID_consumed - mListener(LLEventWithID_consumed(LLEventWithID(event, mDiscrim), &consumed)); + // our future object is defined to accept LLEventWithID + super::mListener(LLEventWithID(event, mDiscrim)); // tell LLEventPump whether or not event was consumed - return consumed; + return super::mConsume; } + private: - LISTENER mListener; - const int mDiscrim; + const DISCRIM mDiscrim; }; -/// WaitForEventOnHelper type-inference helper -template -WaitForEventOnHelper wfeoh(const LISTENER& listener, int discriminator) -{ - return WaitForEventOnHelper(listener, discriminator); -} - } // anonymous namespace llcoro @@ -266,16 +284,18 @@ LLEventWithID postAndSuspend2(const LLSD& event, const LLSD& replyPump1NamePath) { // declare the future - boost::dcoroutines::future future(LLCoros::get_self()); + LLCoros::Future future; // either callback will assign a value to this future; listen on // each specified LLEventPump with a callback std::string name(listenerNameForCoro()); LLTempBoundListener connection0( - replyPump0.getPump().listen(name + "a", - wfeoh(boost::dcoroutines::make_callback(future), 0))); + replyPump0.getPump().listen( + name + "a", + llmake(future.make_callback(), 0))); LLTempBoundListener connection1( - replyPump1.getPump().listen(name + "b", - wfeoh(boost::dcoroutines::make_callback(future), 1))); + replyPump1.getPump().listen( + name + "b", + llmake(future.make_callback(), 1))); // skip the "post" part if requestPump is default-constructed if (requestPump) { @@ -294,20 +314,13 @@ LLEventWithID postAndSuspend2(const LLSD& event, LL_DEBUGS("lleventcoro") << "postAndSuspend2(): coroutine " << name << " about to wait on LLEventPumps " << replyPump0.getPump().getName() << ", " << replyPump1.getPump().getName() << LL_ENDL; - // trying to dereference ("resolve") the future makes us wait for it - LLEventWithID_consumed value; - { - // instantiate Suspending to manage "current" coroutine - llcoro::Suspending suspended; - value = *future; - } // destroy Suspending as soon as we're back + // calling get() on the future makes us wait for it + LLEventWithID value(future.get()); LL_DEBUGS("lleventcoro") << "postAndSuspend(): coroutine " << name - << " resuming with (" << value.first.first - << ", " << value.first.second << ")" << LL_ENDL; - // tell LLEventPump whether we're consuming - *value.second = LLCoros::get_consuming(); + << " resuming with (" << value.first << ", " << value.second << ")" + << LL_ENDL; // returning should disconnect both connections - return value.first; + return value; } LLSD errorException(const LLEventWithID& result, const std::string& desc) diff --git a/indra/llcommon/lleventcoro.h b/indra/llcommon/lleventcoro.h index acf2ad24a4..2105faf861 100755 --- a/indra/llcommon/lleventcoro.h +++ b/indra/llcommon/lleventcoro.h @@ -76,36 +76,6 @@ private: namespace llcoro { -typedef std::pair LLSD_consumed; - -/// This is an adapter for a signature like void LISTENER(const LLSD&), which -/// isn't a valid LLEventPump listener: such listeners should return bool. -template -class VoidListener -{ -public: - VoidListener(const LISTENER& listener): - mListener(listener) - {} - - bool operator()(const LLSD& event) - { - bool consumed = false; - mListener(LLSD_consumed(event, &consumed)); - // tell upstream LLEventPump whether listener consumed - return consumed; - } -private: - LISTENER mListener; -}; - -/// VoidListener helper function to infer the type of the LISTENER -template -VoidListener voidlistener(const LISTENER& listener) -{ - return VoidListener(listener); -} - /** * Yield control from a coroutine for one "mainloop" tick. If your coroutine * runs without suspending for nontrivial time, sprinkle in calls to this diff --git a/indra/llcommon/llmake.h b/indra/llcommon/llmake.h new file mode 100644 index 0000000000..9a662a0640 --- /dev/null +++ b/indra/llcommon/llmake.h @@ -0,0 +1,63 @@ +/** + * @file llmake.h + * @author Nat Goodspeed + * @date 2015-12-18 + * @brief Generic llmake