From: Yedidya Feldblum Date: Sun, 10 Apr 2016 21:51:09 +0000 (-0700) Subject: Some cleanups to folly::EventBase after converting to folly::Function X-Git-Tag: 2016.07.26~360 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=652d34b9f9c22fcceafe2ab05eb5d1387a93c876;p=folly.git Some cleanups to folly::EventBase after converting to folly::Function Summary:[Folly] Some cleanups to `folly::EventBase` after converting to `folly::Function`. * Fix up some comments referring to `std::function`. * Remove the `SmallFunctor` bits - `folly::Function` takes over for that. * Remove `runFunctionPtr` - it's unused. Reviewed By: spacedentist Differential Revision: D3155511 fb-gh-sync-id: 38c75d1254993f59c8eaa7826dc8d9facb50a3a1 fbshipit-source-id: 38c75d1254993f59c8eaa7826dc8d9facb50a3a1 --- diff --git a/folly/io/async/EventBase.cpp b/folly/io/async/EventBase.cpp index 72cd7644..4c11511d 100644 --- a/folly/io/async/EventBase.cpp +++ b/folly/io/async/EventBase.cpp @@ -712,28 +712,6 @@ bool EventBase::nothingHandledYet() { return (nextLoopCnt_ != latestLoopCnt_); } -/* static */ -void EventBase::runFunctionPtr(Cob* fn) { - // The function should never throw an exception, because we have no - // way of knowing what sort of error handling to perform. - // - // If it does throw, log a message and abort the program. - try { - (*fn)(); - } catch (const std::exception &ex) { - LOG(ERROR) << "runInEventBaseThread() std::function threw a " - << typeid(ex).name() << " exception: " << ex.what(); - abort(); - } catch (...) { - LOG(ERROR) << "runInEventBaseThread() std::function threw an exception"; - abort(); - } - - // The function object was allocated by runInEventBaseThread(). - // Delete it once it has been run. - delete fn; -} - void EventBase::attachTimeoutManager(AsyncTimeout* obj, InternalEnum internal) { diff --git a/folly/io/async/EventBase.h b/folly/io/async/EventBase.h index 909d9657..a7958d22 100644 --- a/folly/io/async/EventBase.h +++ b/folly/io/async/EventBase.h @@ -294,13 +294,12 @@ class EventBase : private boost::noncopyable, void runInLoop(LoopCallback* callback, bool thisIteration = false); /** - * Convenience function to call runInLoop() with a std::function. + * Convenience function to call runInLoop() with a folly::Function. * - * This creates a LoopCallback object to wrap the std::function, and invoke - * the std::function when the loop callback fires. This is slightly more + * This creates a LoopCallback object to wrap the folly::Function, and invoke + * the folly::Function when the loop callback fires. This is slightly more * expensive than defining your own LoopCallback, but more convenient in - * areas that aren't performance sensitive where you just want to use - * std::bind. (std::bind is fairly slow on even by itself.) + * areas that aren't too performance sensitive. * * This method may only be called from the EventBase's thread. This * essentially allows an event handler to schedule an additional callback to @@ -370,10 +369,11 @@ class EventBase : private boost::noncopyable, /** * Run the specified function in the EventBase's thread * - * This version of runInEventBaseThread() takes a std::function object. - * Note that this is less efficient than the version that takes a plain - * function pointer and void* argument, as it has to allocate memory to copy - * the std::function object. + * This version of runInEventBaseThread() takes a folly::Function object. + * Note that this may be less efficient than the version that takes a plain + * function pointer and void* argument, if moving the function is expensive + * (e.g., if it wraps a lambda which captures some values with expensive move + * constructors). * * If the loop is terminated (and never later restarted) before it has a * chance to run the requested function, the function will be run upon the @@ -608,10 +608,6 @@ class EventBase : private boost::noncopyable, */ bool nothingHandledYet(); - // --------- libevent callbacks (not for client use) ------------ - - static void runFunctionPtr(Cob* fn); - // small object used as a callback arg with enough info to execute the // appropriate client-provided Cob class CobTimeout : public AsyncTimeout { @@ -730,80 +726,21 @@ class EventBase : private boost::noncopyable, std::unordered_set localStorageToDtor_; }; -namespace detail { - -/** - * Define a small functor (2 pointers) and specialize - * std::__is_location_invariant so that std::function does not require - * memory allocation. - * - * std::function func = SmallFunctor{f, p}; - * - * TODO(lucian): remove this hack once GCC <= 4.9 are deprecated. - * In GCC >= 5.0 just use a lambda like: - * - * std::function func = [=] { f(p); }; - * - * See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61909 - */ -template -struct SmallFunctor { - void (*fn)(T*); - T* p; - void operator()() { fn(p); } -}; - -} // detail - template bool EventBase::runInEventBaseThread(void (*fn)(T*), T* arg) { - return runInEventBaseThread(detail::SmallFunctor{fn, arg}); + return runInEventBaseThread([=] { fn(arg); }); } template bool EventBase::runInEventBaseThreadAndWait(void (*fn)(T*), T* arg) { - return runInEventBaseThreadAndWait(detail::SmallFunctor{fn, arg}); + return runInEventBaseThreadAndWait([=] { fn(arg); }); } template -bool EventBase::runImmediatelyOrRunInEventBaseThreadAndWait(void (*fn)(T*), - T* arg) { - return runImmediatelyOrRunInEventBaseThreadAndWait( - detail::SmallFunctor{fn, arg}); +bool EventBase::runImmediatelyOrRunInEventBaseThreadAndWait( + void (*fn)(T*), + T* arg) { + return runImmediatelyOrRunInEventBaseThreadAndWait([=] { fn(arg); }); } } // folly - -FOLLY_NAMESPACE_STD_BEGIN - -/** - * GCC's libstdc++ uses __is_location_invariant to decide wether to - * use small object optimization and embed the functor's contents in - * the std::function object. - * - * (gcc 4.9) $ libstdc++-v3/include/std/functional - * template - * struct __is_location_invariant - * : integral_constant::value - * || is_member_pointer<_Tp>::value)> - * { }; - * - * (gcc 5.0) $ libstdc++-v3/include/std/functional - * - * template - * struct __is_location_invariant - * : is_trivially_copyable<_Tp>::type - * { }; - * - * - * NOTE: Forward declare so this doesn't break when using other - * standard libraries: it just wont have any effect. - */ -template -struct __is_location_invariant; - -template -struct __is_location_invariant> - : public std::true_type {}; - -FOLLY_NAMESPACE_STD_END