(wangle) s/State/Core/

Summary:
codemod
`State` is such an overloaded term, and not really the best to describe this backing future/promise object. Yes, it holds the state but it's more than that and it gets in the way of calling the states of the state machines `State`s.

Test Plan: builds and tests pass

Reviewed By: davejwatson@fb.com

Subscribers: trunkagent, net-systems@, fugalh, exa, njormrod

FB internal diff: D1615707
wangle::detail::FSM

Summary:
spaghetti-monster

Finagle uses a nifty state machine pattern in `Promise.scala`. Each state carries its data with it, then you switch statements with a simple state `cas`, within the functions (which are the transition inputs). See https://github.com/twitter/util/blob/master/util-core/src/main/scala/com/twitter/util/Promise.scala#L133-L139 and https://github.com/twitter/util/blob/master/util-core/src/main/scala/com/twitter/util/Promise.scala#L604-L635 for example.

I was thinking we could do something similar in C++, though we don't quite have the same luxury of type cases like scala. (Although as an aside I found this really cool paper on implementing type cases in C++, it's kind of evil but very cool. http://www.stroustrup.com/OOPSLA-typeswitch-draft.pdf)

I was looking at having a union of the different state classes, e.g.

union U {
State base;
Done done;
Waiting waiting;
...
};

and then you could use the memoized `dynamic_cast` trick in that paper to make a fairly efficient type case (and fairly readable, depending on how evil you want to get with the macros). However, `dynamic_cast<Done*>(&u.base)` blissfully segfaults. I'm not sure if this is something that should work and it's a compiler bug, or whether trying to (ab)use a union this way is against some arcane C++ rules. @hannesr suggested maybe a variant type might work. We can also do memory mangling on our own if it comes to it - however those are all more advanced techniques to play with at a later optimization time.

So instead, I went for a this-is-context helper base class. The mutable context is just `this`, and you inherit from `FSM`.

The magic macros make it more succint to lay out state transitions. See the tests for examples. Maybe in the future we can get really clever and find a way to generate state machine diagrams from code using this, especially when macros are being used.

Test Plan: unit tests were written and pass

Reviewed By: davejwatson@fb.com

Subscribers: meisner, trunkagent, net-systems@, fugalh, exa, njormrod, hannesr

FB internal diff: D1613497
Fix thread local random number generator static issue

Summary:
See https://gcc.gnu.org/onlinedocs/gcc-4.8.3/gcc/Thread-Local.html
Thanks to @subodh for finding the first instance of this problem in the
proxygen codebase.

Test Plan:
unit tests, tested proxygen unit tests on my laptop and I don't
get segfaults on program exit now.

Reviewed By: subodh@fb.com

Subscribers: trunkagent, doug, njormrod, subodh

FB internal diff: D1616149

Tasks: 5217022
Fix opensource build

Summary: D1615707 moved State.h to Core.h, but didn't update Makefile.am

Test Plan: visual

Reviewed By: davejwatson@fb.com

Subscribers: fugalh, njormrod

FB internal diff: D1617562

Blame Revision: D1615707
Defer query string parsing from ctor to getQueryParams()

Summary:
Query string parsing uses a lot of CPU, it happens in ctor killing CPU
even in users that are not interested in query string. Move it to
getQueryParams().

Test Plan:
fbconfig folly/test
fbmake runtests_dbg

Reviewed By: ldemailly@fb.com

Subscribers: trunkagent, njormrod, zellux

FB internal diff: D1604973

Tasks: 5304484

Blame Revision: https://phabricator.fb.com/D1455158
(wangle) Use MicroSpinLock

Summary: For great speed

Test Plan:
Before
============================================================================
folly/wangle/test/Benchmark.cpp                 relative  time/iter  iters/s
============================================================================
constantFuture                                             241.19ns    4.15M
promiseAndFuture                                 100.32%   240.42ns    4.16M
withThen                                          44.63%   540.47ns    1.85M
----------------------------------------------------------------------------
oneThen                                                    519.20ns    1.93M
twoThens                                          62.83%   826.41ns    1.21M
fourThens                                         36.80%     1.41us  708.75K
hundredThens                                       1.79%    29.05us   34.42K
----------------------------------------------------------------------------
no_contention                                                4.82ms   207.27
contention                                        62.91%     7.67ms   130.39
============================================================================

After
============================================================================
folly/wangle/test/Benchmark.cpp                 relative  time/iter  iters/s
============================================================================
constantFuture                                             159.79ns    6.26M
promiseAndFuture                                 101.23%   157.84ns    6.34M
withThen                                          41.78%   382.47ns    2.61M
----------------------------------------------------------------------------
oneThen                                                    358.23ns    2.79M
twoThens                                          63.07%   568.00ns    1.76M
fourThens                                         36.89%   971.07ns    1.03M
hundredThens                                       1.76%    20.34us   49.17K
----------------------------------------------------------------------------
no_contention                                                3.75ms   266.75
contention                                        59.83%     6.27ms   159.59
============================================================================

That's a 150% speedup.

Reviewed By: davejwatson@fb.com

Subscribers: net-systems@, fugalh, exa, njormrod

FB internal diff: D1617363

Tasks: 5278220
Fix clang test compilation failures

Summary:
While working on other code I noticed folly stopped compiling
with clang. Fix that.

Test Plan: fbmake runtests (clang and gcc)

Reviewed By: lucian@fb.com

Subscribers: mathieubaudet, njormrod, bmatheny

FB internal diff: D1618189
Schedule destroyInstances only when first singleton is created

Summary:
Right now destroyInstances is scheduled when SingletonVault is requested. This may change singleton destruction order (folly::Singleton-managed vs unmanaged singletons) when new singleton is registered with folly::Singleton (no matter if it is even used).
This diff changes it to be more stable.

Test Plan: servicerouter unittests

Reviewed By: chip@fb.com

Subscribers: njormrod, mshneer

FB internal diff: D1615213

Tasks: 5353022
merge wangle/Executor.h and experimental/wangle/concurrent/Executor.h

Summary:
the one in concurrent/ is a bit more generic, so I kept that as Executor and renamed the existing one ScheduledExecutor

because Hans is surfing I took the liberty of renaming Action->Func as an alias for std::function<void()>, because I think it's more reflective
also kept the version of add() that doesn't force rvalue-reference as it's more user friendly and probably not less performant in common cases (insert reference to "want speed? pass by value" here)

Test Plan: compiled some major relevant bits, will let contbuild show me anything I missed

Reviewed By: hans@fb.com

Subscribers: trunkagent, rushix, fbcode-common-diffs@, fugalh, msk, njormrod

FB internal diff: D1591237

Tasks: 5279196
support for move-only types

Summary: universal references and perfect forwarding to the rescue

Test Plan: added demonstrative unit tests

Reviewed By: hans@fb.com

Subscribers: trunkagent, fugalh, njormrod, bmatheny

FB internal diff: D1592032

Tasks: 5283342
Remove global init of ThreadLocal in Random

Summary:
Remove global initialization of ThreadLocal in Random
See https://gcc.gnu.org/onlinedocs/gcc-4.8.3/gcc/Thread-Local.html

Static initializers and thread locals don't mix well, and we're seeing
some crashes which we think might be related to this.

Test Plan: Unit tests

Reviewed By: seanc@fb.com

Subscribers: trunkagent, seanc, njormrod

FB internal diff: D1617455
Don't throw in Singleton::get() if singleton is alive

Summary:
This is a fix for situations where you know that something is keeping singleton alive (by getting a weak_ptr and locking it), and request a singleton instance via get() method (if e.g. it's hard to pass a pointer to singleton instance from a method which locked it). If shutdown was scheduled - an exception was previously thrown even though the object was not destroyed yet.

This simplifies conversion of legacy code to folly::Singleton.

Test Plan:
fbconfig -r gatehouse/usersets/tests
fbmake runtests

Reviewed By: chip@fb.com

Subscribers: trunkagent, njormrod

FB internal diff: D1619311
Conditionals for iOS / Android compilation

Summary:
Conditional changes required to compile on iOS and Android.

Test Plan: compiled on all platforms

Reviewed By: meyering@fb.com

Subscribers: kmccray, trunkagent, shilin, njormrod, ranjeeth, subodh, bmatheny

FB internal diff: D1618028

Tasks: 5183325
move shutdown socket set

Summary:
Move shutdownsocketset to folly, since it is a dep of the asyncsockets

Previoulsy tried moving it in to the server directly: D1583629, but had issues - close(fd) is called before the error callback, so we can't remove the fd before the close, which is essential to it working properly.

Just move it to folly instead.

Test Plan: fbconfig -r thrift/lib/cpp thrift/lib/cpp2; fbmake runtests

Reviewed By: dcsommer@fb.com

Subscribers: mshneer, trunkagent, fugalh, jsedgwick, doug, alandau, bmatheny, njormrod

FB internal diff: D1594950
Allow Optional<T> {=,!}= T comparisons in folly::Optional

Summary:
I can see how {<,>}{,=} comparisons might be ambiguous, but equality
should never be, so let's allow it.

Test Plan: tests

Reviewed By: tjackson@fb.com

Subscribers: trunkagent, njormrod

FB internal diff: D1618088
Fixing broken bits test

Summary: Buffer size calculation had fallen out of sync with the actual test code. Good thing we have ASAN!

Test Plan: Run the test with address santizer.

Reviewed By: mpawlowski@fb.com

Subscribers: mpawlowski, trunkagent, njormrod

FB internal diff: D1619472

Tasks: 5325399
Fix one missing conditional for Android

Summary:
pthread_atfork isn't defined in the Android NDK (and therefore is not actually supported even though it's in bionic/libc) so don't call it there.

Test Plan: compiled on Linux, Android

Reviewed By: meyering@fb.com

Subscribers: njormrod, shikong, kmdent, fma

FB internal diff: D1620584

Tasks: 5183325
Fix typo in folly::Optional

Test Plan: yes

Reviewed By: delong.j@fb.com
Fix mcrouter opensource build

Summary: mcrouter now uses folly::Singleton.

Test Plan: visual

Reviewed By: andrii@fb.com

Subscribers: njormrod

FB internal diff: D1622647
Bump version to 13:0