folly.git
8 years agoNew strings no longer relocatable
Nicholas Ormrod [Thu, 31 Mar 2016 22:09:14 +0000 (15:09 -0700)]
New strings no longer relocatable

Summary:gcc-5.0 introduced non-relocatable strings in libgcc. Only treat gcc < 5 strings as relocatable. Enable relocation for fbstrings via typedef.

Re https://github.com/facebook/folly/issues/316, thanks tomhughes for reporting this.

Reviewed By: snarkmaster, ot

Differential Revision: D3115580

fb-gh-sync-id: d8aff947d55a0a0c57f6b997f651a190e05f2779
fbshipit-source-id: d8aff947d55a0a0c57f6b997f651a190e05f2779

8 years agoSimplify fbstring::insertImpl
Giuseppe Ottaviano [Thu, 31 Mar 2016 20:39:21 +0000 (13:39 -0700)]
Simplify fbstring::insertImpl

Summary:The current implementation of `insertImpl` assumes that
`expand_noinit` does not reallocate if the `size() + delta <=
capacity()`, but D3114022 makes this assumption invalid when compiling
with ASan. It also doesn't guarantee exponential growth, so repeated
inserting at the end could trigger quadratic behavior.

The new implementation fixes the problems above, and it's much
simpler.

Reviewed By: yfeldblum, Orvid

Differential Revision: D3119813

fb-gh-sync-id: 946ebeeaf924a531f7f03fb7e79c75e352a50c58
fbshipit-source-id: 946ebeeaf924a531f7f03fb7e79c75e352a50c58

8 years agoLog SSL alerts received on the server.
Kyle Nekritz [Thu, 31 Mar 2016 18:57:38 +0000 (11:57 -0700)]
Log SSL alerts received on the server.

Summary: Alerts may be sent by clients, potentially letting us know why connections fail.

Reviewed By: siyengar

Differential Revision: D3117395

fb-gh-sync-id: bddf51f2399eb9e7e397981d5440adb3e815d6d2
fbshipit-source-id: bddf51f2399eb9e7e397981d5440adb3e815d6d2

8 years agoCreate the sys/resource.h portability header
Christopher Dykes [Thu, 31 Mar 2016 17:49:17 +0000 (10:49 -0700)]
Create the sys/resource.h portability header

Summary: Windows doesn't have it, so be nice and at least stub it out.

Reviewed By: yfeldblum

Differential Revision: D2984232

fb-gh-sync-id: 3e8871ab78c5d7c8785a52af24548245f842f19b
fbshipit-source-id: 3e8871ab78c5d7c8785a52af24548245f842f19b

8 years agoCreate a malloc.h portability header
Christopher Dykes [Thu, 31 Mar 2016 17:44:33 +0000 (10:44 -0700)]
Create a malloc.h portability header

Summary:Let's break OSX!

Alright, maybe not. Neither OSX nor Windows define malloc_usable_size, so we implement them based on what is available on the respective platforms.
This moves the implementation for OSX out of Portability.h and into the new header, so it likely breaks something on OSX, although I'm not sure what.

Reviewed By: yfeldblum

Differential Revision: D3019938

fb-gh-sync-id: df95faef09535098fb73b7b3479d7a73f6b49712
fbshipit-source-id: df95faef09535098fb73b7b3479d7a73f6b49712

8 years agoAdd create-move-invoke benchmark for folly::Function
Andrii Grynenko [Thu, 31 Mar 2016 17:31:15 +0000 (10:31 -0700)]
Add create-move-invoke benchmark for folly::Function

Summary: Adding a benchmark for one the most common scenarios (used in Futures, EventBase, fibers etc).

Reviewed By: yfeldblum

Differential Revision: D3106365

fb-gh-sync-id: 8cb55959b3803e8836ab5a5bdf43fdfc1db02d4c
fbshipit-source-id: 8cb55959b3803e8836ab5a5bdf43fdfc1db02d4c

8 years agoFix the portability implementation of strndup
Christopher Dykes [Thu, 31 Mar 2016 17:14:42 +0000 (10:14 -0700)]
Fix the portability implementation of strndup

Summary:It was mistakenly assuming the length passed in included the null terminator.
This also makes the portability implementation of `strndup` available to OSX and FreeBSD, where they weren't present, and where HHVM had a wrapper for them.
This also removes the extra pair of conditions around `memrchr`, as the main define should always be getting set.

Reviewed By: yfeldblum

Differential Revision: D3116467

fb-gh-sync-id: 243dd4dace219efab2c2bf2f383202e70fbec4de
fbshipit-source-id: 243dd4dace219efab2c2bf2f383202e70fbec4de

8 years agoSupport SSE 4.2 qfind under MSVC
Christopher Dykes [Thu, 31 Mar 2016 17:12:30 +0000 (10:12 -0700)]
Support SSE 4.2 qfind under MSVC

Summary:MSVC has support in the compiler for the intrinsics required, but both refuses to tell us that, and also gives them proper names.
The code already checks for runtime support, this just enables compiling the SSE 4.2 version in the first place.

Reviewed By: yfeldblum

Differential Revision: D3104296

fb-gh-sync-id: 9143240bede9b756817691fdd86818001267dac1
fbshipit-source-id: 9143240bede9b756817691fdd86818001267dac1

8 years agoinclude glog for CHECK_EQ
Neel Goyal [Thu, 31 Mar 2016 17:10:17 +0000 (10:10 -0700)]
include glog for CHECK_EQ

Summary: OpenSSLPtrTypes.h uses CHECK_EQ which is included in glog/logging.

Reviewed By: knekritz

Differential Revision: D3118577

fb-gh-sync-id: f8a00aa5a523bf88a3f783433c6699d555799225
fbshipit-source-id: f8a00aa5a523bf88a3f783433c6699d555799225

8 years agoUpdate the sys/syscall.h portability header for Windows
Christopher Dykes [Thu, 31 Mar 2016 17:09:38 +0000 (10:09 -0700)]
Update the sys/syscall.h portability header for Windows

Summary: This adds Windows support to the portability header for sys/syscall.h, which was previously named portability/Syscall.h, which is inconsistent with the naming of the other headers, so it's now also been renamed to SysSyscall.h, and the one place that used it updated to reflect the new name.

Reviewed By: yfeldblum

Differential Revision: D2984383

fb-gh-sync-id: c0e4ce55c7bc2aa6db9b084e09d9762ba587002d
fbshipit-source-id: c0e4ce55c7bc2aa6db9b084e09d9762ba587002d

8 years agoDo not use small category in fbstring when in ASan mode
Giuseppe Ottaviano [Wed, 30 Mar 2016 23:39:11 +0000 (16:39 -0700)]
Do not use small category in fbstring when in ASan mode

Summary:`fbstring`'s small string optimization prevents ASan to catch invalid
accesses to the data of a destroyed string, for example if a
`StringPiece` is initialized from a temporary string.

This diff disables building a string with the small category when
compiled with ASan: small strings will be constructed as
`Medium`-category strings and heap-allocated. This is done by only
changing the behavior of construction and resizing, so that the ABI is
preserved and it is still possible to link an ASan-enabled object file
with a library that wasn't compiled with ASan.

The diff also fixes a blind spot in `fbstring_core`'s constructor,
which disabled ASan altogether in order to allow fast word-aligned
copy of small strings. Since small string construction is now disabled
under ASan, we don't need to disable it anymore.

Lastly, it always clears moved-from strings, even when they are small.
This improves the performance of the move constructor (no more conditional
needed) and it uncovers another class of potential bugs.

Reviewed By: luciang

Differential Revision: D3114022

fb-gh-sync-id: 4e180fbf2b8aced3b977afc985d26fdf244d9598
fbshipit-source-id: 4e180fbf2b8aced3b977afc985d26fdf244d9598

8 years agouse unit64_t for numElements in HHWheelTimer
Yang Chi [Wed, 30 Mar 2016 22:46:17 +0000 (15:46 -0700)]
use unit64_t for numElements in HHWheelTimer

Summary: Some compiler will complain about this when calling std::min with a size_t and a uint64_t. So use unit64_t for numElements in HHWheelTimer.

Reviewed By: mzlee

Differential Revision: D3116346

fb-gh-sync-id: 67a9eebf4f9e8fe0e732a7292af55122be04163b
fbshipit-source-id: 67a9eebf4f9e8fe0e732a7292af55122be04163b

8 years agofix stack usage in HHWhileTimer
Misha Shneerson [Wed, 30 Mar 2016 05:42:54 +0000 (22:42 -0700)]
fix stack usage in HHWhileTimer

Summary:We should be able to use HHWheelTimer in fibers. So it should use way less stack.

Alternative solution to D3112305

Reviewed By: haijunz

Differential Revision: D3112558

fb-gh-sync-id: 3a52a37d9f9347639005fdf84524f7f8c3041918
fbshipit-source-id: 3a52a37d9f9347639005fdf84524f7f8c3041918

8 years agoIPAddressV[46]::validate
Yedidya Feldblum [Tue, 29 Mar 2016 19:53:16 +0000 (12:53 -0700)]
IPAddressV[46]::validate

Summary:[Folly] `IPAddressV[46]::validate(StringPiece)`.

Just perform the check without allocations or throws.

Reviewed By: meyering

Differential Revision: D3103545

fb-gh-sync-id: 2918c1398935738f597b9045cf7dadbe42da2527
fbshipit-source-id: 2918c1398935738f597b9045cf7dadbe42da2527

8 years agoChange SSLContext to use a ThreadLocalPRNG
Neel Goyal [Tue, 29 Mar 2016 17:47:14 +0000 (10:47 -0700)]
Change SSLContext to use a ThreadLocalPRNG

Summary:Use a ThreadLocalPRNG insteaad of a per context RNG.  This avoids
calls to Random::seed on context creation which can get expensive
when many are created in an application.

Reviewed By: siyengar

Differential Revision: D3105501

fb-gh-sync-id: 92d987c27a1f190a98035ca25c23b994ca915007
fbshipit-source-id: 92d987c27a1f190a98035ca25c23b994ca915007

8 years agoMake FBVector faster
Nicholas Ormrod [Tue, 29 Mar 2016 16:28:51 +0000 (09:28 -0700)]
Make FBVector faster

Summary:Per https://github.com/facebook/folly/issues/379, github user pmalek determined that fbvector's insert function was a lot slower than std::vector's. While insert is often imagined to be a second-class vector function (if you're inserting in the middle, don't use a vector!), it is the correct function to use when inserting multiple elements at the back of an array (since the standard lacks an ##append(multiple-elements)## function). The code, therefore, required optimization.

There are three things that were slowing down fbvector:
- Excessive asserts. The fbvector code contains internal asserts that ensure proper calling of private methods. These are mostly unnecessary, given the extensive test suite that batters fbvector, in addition to fbvector's battle tested history. I have removed these.
- Internal data checks. When inserting into a vector, the elements being inserted may not reference the contents of the vector in question. If the client supplies internal references, then the standard allows for undefined behavior. An old design decision for fbvector was to be forgiving of this error; we check for internal data before performing internal data moves. This is expensive. Further, it is unnecessary when the insertion point is at the end of the vector (which is the valid case we are optimizing) or if the vector is being reallocated. I've rearchitected the insert macros to only perform the internal-data-check when it is absolutely necessary. While rearchitecting the checks, I also reorganized the n==0 base case checking.
- The 'window' function is pretty expensive when called with the end() iterator. It is effectively a no-op, but the function (and some of its called functions) are too big to inline, so the call overhead is non-trivial. I've put window calls behind a conditional to save this overhead.

I added a benchmark test to FBVectorTestBenchmark.cpp.h, but the particular pattern in that file caused the results to be completely optimized away, voiding the test. Oh well.

Running the github benchmarks show a 4x speedup for inserting a single element on the back, bringing it quite close to that of std::vector. The multi-insert function got a bit faster, though the exact number of iterations seems to be having an effect on the numbers (running the tests 10,000 times, intead of only 1,000 times, yields results that are 90% as fast as std::vector, instead of 75%). Both of these cases are now looking quite a bit better.

I resurrected the old StlVectorTest suite, since I was mucking with the complicated insert code. The suite had one latent compilation error (caught with newer compiler warnings - fortunately this particular case was benign). The test suite caught an error in shrink_to_fit, where a clean-slate optimization for empty vectors (see D2696314) that trampled the vector's allocator. I have fixed that small bug in this diff, too.

Reviewed By: ot

Differential Revision: D3105962

fb-gh-sync-id: ac9fa9d7ca79335cf0ff6bb9010af1ed8bd7916b
fbshipit-source-id: ac9fa9d7ca79335cf0ff6bb9010af1ed8bd7916b

8 years agofolly/futures: use folly::Function to store callback
Sven Over [Tue, 29 Mar 2016 10:28:31 +0000 (03:28 -0700)]
folly/futures: use folly::Function to store callback

Summary:This diff makes it possible to pass callables (such as lambdas) to
folly::Future::then that are not copy-constructible. As a consequence, move-only
types (such as folly::Promise or std::unique_ptr) can now be captured in lambdas
passed to folly::Future::then.

Using C++14 notation, the following is now possible:
  Future<Unit>().then([promise = std::move(promise)]() mutable {
    promise.setValue(123);
  });
See folly/futures/test/NonCopyableLambdaTest.cpp for more examples.

folly::Future uses std::function to store callback functions. (More precisely,
the callback function is stored in a std::function in folly::detail::Core.)
std::function is a copy-constructible type and it requires that the callable
that it stores is copy constructible as well.

This diff changes the implementation of folly::detail::Core to use
folly::Function instead of std::function. It also simplifies the code in
folly::detail::Core a little bit: Core had a reserved space of size
8*sizeof(void*) to store callbacks in-place. Only larger callbacks (capturing
more data) would be stored in the std::function, which puts those on the heap.
folly::Function has a template parameter to set the size of the in-place
callable storage. In Core, it is set to 8*sizeof(void*), so all callbacks that
used to be stored in-place inside Core, are now stored in-place inside
folly::Function. This even reduces the size of a Core object: the
folly::Function object occupies 80 bytes, which is 16 bytes less than what was
used before for a std::function (32 bytes) and the callback storage (64
bytes). The resulting size of a Core<Unit> goes down from 192 to 176 bytes (on
x86_64).

Reviewed By: fugalh

Differential Revision: D2884868

fb-gh-sync-id: 35548890c392f80e6c680676b0f98e711bc03ca3
fbshipit-source-id: 35548890c392f80e6c680676b0f98e711bc03ca3

8 years agoFix cases of overwriting errno
Subodh Iyengar [Mon, 28 Mar 2016 23:48:51 +0000 (16:48 -0700)]
Fix cases of overwriting errno

Summary:AsyncSocket has a few cases where
we might overwrite errno, with another function
call that is done during use.

For example withAddr could cause an EBADF if the fd
was bad.

This fixes these cases by copying errno, before doing
anything else

Reviewed By: afrind

Differential Revision: D3101932

fb-gh-sync-id: 73d4810314c90cb987936a0a3a9dc977af07243a
fbshipit-source-id: 73d4810314c90cb987936a0a3a9dc977af07243a

8 years agos/MAX_STATIC_CONSTRUCTOR_PRIORITY/FOLLY_STATIC_CTOR_PRIORITY_MAX/g
Yedidya Feldblum [Mon, 28 Mar 2016 23:44:02 +0000 (16:44 -0700)]
s/MAX_STATIC_CONSTRUCTOR_PRIORITY/FOLLY_STATIC_CTOR_PRIORITY_MAX/g

Summary:[Folly] `s/MAX_STATIC_CONSTRUCTOR_PRIORITY/FOLLY_STATIC_CTOR_PRIORITY_MAX/g`.

Namespace it with a `FOLLY_` prefix - when Folly introduces its own symbols, it is best when they are namespaced.

And stick `_MAX` at the end. Reads a bit more hierarchically to me that way.

Reviewed By: andriigrynenko

Differential Revision: D3095964

fb-gh-sync-id: 4ac8c00c03c8bd146fad9be96ce3849830504d96
fbshipit-source-id: 4ac8c00c03c8bd146fad9be96ce3849830504d96

8 years agoAllocate stacks with guard pages by default
Andrii Grynenko [Mon, 28 Mar 2016 22:14:46 +0000 (15:14 -0700)]
Allocate stacks with guard pages by default

Summary: This was on for mcrouter for very long time. We should make it default for everyone.

Reviewed By: yfeldblum

Differential Revision: D3023133

fb-gh-sync-id: 6401f86df754e9490d46beb5fd9d0cf4b3675208
fbshipit-source-id: 6401f86df754e9490d46beb5fd9d0cf4b3675208

8 years agofolly::fibers: do not move out of lvalue references
Sven Over [Mon, 28 Mar 2016 21:13:24 +0000 (14:13 -0700)]
folly::fibers: do not move out of lvalue references

Summary:If you pass an lvalue to folly::fibers::Fiber::setFunction or
setFunctionFinally, you do not expect that those methods move
the function out of your lvalue.

This diff uses std::forward for perfect forwarding, so if an lvalue
is passed, a copy is made.

If and when when start using folly::Function for storing the
functions in the Fiber object, passing an lvalue will trigger a
compiler error because folly::Function is not copyable. That is
a good thing, as it enforces calling setFunction with std::move
if you use a named object, making clear that the named object
gets moved out of. Often people will pass temporary objects
(like a lambda defined i- place), which a rvalues anyway, so
this will not be a problem anyway.

Reviewed By: andriigrynenko

Differential Revision: D3102685

fb-gh-sync-id: 87bf3135f7f6630766f97be351599ff488e4b796
fbshipit-source-id: 87bf3135f7f6630766f97be351599ff488e4b796

8 years agoCreate the string.h portability header
Christopher Dykes [Mon, 28 Mar 2016 18:22:53 +0000 (11:22 -0700)]
Create the string.h portability header

Summary: Windows has it, but some things aren't there, and some have different names.

Reviewed By: yfeldblum

Differential Revision: D2990475

fb-gh-sync-id: 3f30e084eb7789c13b3981ec0fbf5b7b642c3367
fbshipit-source-id: 3f30e084eb7789c13b3981ec0fbf5b7b642c3367

8 years agoMove asm portability to an Asm portability header
Christopher Dykes [Mon, 28 Mar 2016 18:20:18 +0000 (11:20 -0700)]
Move asm portability to an Asm portability header

Summary: Portabilty.h is just a mashup of everything that was needed up-to now, however, with the Windows port significantly expanding the scope of portability in Folly, it no longer needs to be a massive mashup, so start things by splitting out the asm stubs.

Reviewed By: yfeldblum

Differential Revision: D3007018

fb-gh-sync-id: f32913f74660aaa8889a553ae5d6f7182a4f5789
fbshipit-source-id: f32913f74660aaa8889a553ae5d6f7182a4f5789

8 years agoMove event_base_new out of critical region
Haijun Zhu [Sun, 27 Mar 2016 07:12:23 +0000 (00:12 -0700)]
Move event_base_new out of critical region

Summary:event_base_new does not change the global current_base so no need to
call it with the mutex held.

Reviewed By: chadparry

Differential Revision: D3055963

fb-gh-sync-id: df420b189ef0d7d56eda5ad6c32e03b195804e6b
fbshipit-source-id: df420b189ef0d7d56eda5ad6c32e03b195804e6b

8 years agofolly::Function: improve conversion of return types
Sven Over [Sat, 26 Mar 2016 09:10:46 +0000 (02:10 -0700)]
folly::Function: improve conversion of return types

Summary:Treat any return type as convertible to void:

As of C++17, std::function<void(Args...)> can be set to callables
returning non-void types when called with parameters Args....
This diff adds that capability to folly::Function. It also adds
unit tests, not only for ignoring return types, but also for
correctly converting between the return type of the embedded
callabled and the return type of the encapsulating folly::Function.

Allow conversion of one folly::Function type to another one which
declares a return type the original one can be converted to:

E.g. allow to construct a Function<double()> from a
Function<int()> or a Function<Base*()> from a
Function<Derived*()>.

Reviewed By: yfeldblum

Differential Revision: D3095583

fb-gh-sync-id: 6d924dc6e97f759d8109db4200e1cb9333a98d31
fbshipit-source-id: 6d924dc6e97f759d8109db4200e1cb9333a98d31

8 years agoFix portability/Time.h on OSX
Bert Maher [Sat, 26 Mar 2016 03:36:30 +0000 (20:36 -0700)]
Fix portability/Time.h on OSX

Summary: It needs stdint to define uint8_t

Reviewed By: yfeldblum, mzlee

Differential Revision: D3101140

fb-gh-sync-id: 308507af1ccd81e2eb389a400376556a6cc597b2
fbshipit-source-id: 308507af1ccd81e2eb389a400376556a6cc597b2

8 years agofolly: avoid using atomic operations android can't handle
Kevin McCray [Thu, 24 Mar 2016 19:54:28 +0000 (12:54 -0700)]
folly: avoid using atomic operations android can't handle

Summary:On Android, clang 3.8 + libstdc++ doesn't support the full
range of atomic operations.  This diff fixes up the ones we
ran into when building fb4a.

Reviewed By: mzlee

Differential Revision: D3092352

fb-gh-sync-id: 7fea8513e23425fc37050ad14d82aabaceb00352
shipit-source-id: 7fea8513e23425fc37050ad14d82aabaceb00352

8 years agoFix Build: folly after replacing FOLLY_NORETURN
Yedidya Feldblum [Thu, 24 Mar 2016 01:07:02 +0000 (18:07 -0700)]
Fix Build: folly after replacing FOLLY_NORETURN

Summary:[Folly] Fix Build: `folly` after replacing `FOLLY_NORETURN`.

Problem 1:
* Clang does not treat `[[noreturn]]` and `__attribute__((__noreturn__))` as the same attribute. When a function is declared twice with the noreturn attribute, both declarations must use either the one syntax or the other; Clang fails if the two declarations mix-and-match. When both `folly/detail/FunctionalExcept.h` and gcc49's `bits/functexcept.h` are both included from the same source, they (now) mix-and-match the attribute syntaxes and Clang emits an error.
* `folly/detail/FunctionalExcept.h` should be included only when `bits/functexcept.h` is unavailable - somehow, both headers were being included.

We fix the latter problem and keep our `[[noreturn]]` syntax.

Found from WDT build failures in Travis CI.

Reviewed By: ldemailly

Differential Revision: D3089987

fb-gh-sync-id: 705a087fc2a9629739d6cedd8315d56111204e6d
shipit-source-id: 705a087fc2a9629739d6cedd8315d56111204e6d

8 years agoUse C++'s standardized [[noreturn]] attribute
Yedidya Feldblum [Wed, 23 Mar 2016 19:19:45 +0000 (12:19 -0700)]
Use C++'s standardized [[noreturn]] attribute

Summary:Use C++'s standardized `[[noreturn]]` attribute.

All supported compilers (and some unsupported compilers) also support the standardized syntax.

GCC >= 4.8, Clang >= 3.0, and MSVC >= 2015 have direct support for the C++'s standardized way of writing the attribute.

Clang - http://goo.gl/ftJGVM
GCC 4.8.2 - http://goo.gl/ORCVOD
ICC 13.0.1 - http://goo.gl/I5tn5I
MSVC 2015 - https://msdn.microsoft.com/en-us/library/hh567368.aspx

(Regardling Clang, earlier versions may support it. 3.0 was the earliest Clang version listed at godbolt.com, so that's as far back as I went.)

Therefore, we no longer need to use the compiler-specific syntaxes, or use preprocessor macros with per-compiler definitions:

    __attribute__((__noreturn__))
    __attribute__((noreturn))
    __declspec(noreturn)

Reviewed By: Orvid

Differential Revision: D3073621

fb-gh-sync-id: 32d4771d1bf1974693b8574fa2d39c9559872945
shipit-source-id: 32d4771d1bf1974693b8574fa2d39c9559872945

8 years agoMove the clock details over to the Time.h portability header
Christopher Dykes [Wed, 23 Mar 2016 17:10:36 +0000 (10:10 -0700)]
Move the clock details over to the Time.h portability header

Summary: Because things belong places.

Reviewed By: mzlee

Differential Revision: D3077737

fb-gh-sync-id: 966f37df722ae737481d776ff1c9b0d5132b7a58
shipit-source-id: 966f37df722ae737481d776ff1c9b0d5132b7a58

8 years agoAdd APPLE to portability/Config
Michael Lee [Tue, 22 Mar 2016 18:45:43 +0000 (11:45 -0700)]
Add APPLE to portability/Config

Summary: And use that to guard the use of posix_memalign.

Reviewed By: benyl

Differential Revision: D3081743

fb-gh-sync-id: 2c71d9cbeeaeb59d1600d486cccfc2109699ba6b
shipit-source-id: 2c71d9cbeeaeb59d1600d486cccfc2109699ba6b

8 years agoIntroducing folly::Function
Sven Over [Tue, 22 Mar 2016 13:10:19 +0000 (06:10 -0700)]
Introducing folly::Function

Summary:std::function is copy-constructible and requires that the callable that it wraps
is copy-constructible as well, which is a constraint that is often inconvenient.
In most cases when using a std::function we don't make use of its
copy-constructibility.

This diff introduces a templated type called folly::Function that is very
similar to a std::function, except it is not copy-constructible and doesn't
require the callable to be either.

Like std::function, Function is a templated type with template parameters
for return type and argument types of the callable, but not the callable's
specific type. It can store function pointers, static member function pointers,
std::function objects, std::reference_wrapper objects and arbitrary callable
types (functors) with matching return and argument types.

Much like std::function, Function will store small callables in-place, so
that no additional memory allocation is necessary. For larger callables,
Function will allocate memory on the heap.

Function has two more template parameters: firstly, an enum parameter of
type folly::FunctionMoveCtor, which defaults to NO_THROW and determines
whether no-except-movability should be guaranteed. If set to NO_THROW,
callables that are not no-except-movable will be stored on the heap, even
if they would fit into the storage area within Function.

Secondly, a size_t parameter (EmbedFunctorSize), which determines the size of
the internal callable storage. If you know the specific type of the callable you
want to store, you can set EmbedFunctorSize to sizeof(CallableType).

The original motivation of this diff was to allow to pass lambdas to
folly::Future::then that are not copy-constructible because they capture
non-copyable types, such as a promise or a unique pointer.

Another diff will shortly follow that changes folly::Future to use
folly::Function instead of std::function for callbacks, thus allowing to
pass non-copyable lambdas to folly::Future::then.

Reviewed By: fugalh

Differential Revision: D2844587

fb-gh-sync-id: 3bee2af75ef8a4eca4409aaa679cc13762cae0d0
shipit-source-id: 3bee2af75ef8a4eca4409aaa679cc13762cae0d0

8 years agoUse FOLLY_MOBILE to split some functionality
Michael Lee [Tue, 22 Mar 2016 01:02:31 +0000 (18:02 -0700)]
Use FOLLY_MOBILE to split some functionality

Summary: The guards already look for __APPLE__, but __ANDROID__ needs this as well.

Reviewed By: yfeldblum

Differential Revision: D3053976

fb-gh-sync-id: a9ba7a612d29502d4a7e691c6741837d4a8b42f0
shipit-source-id: a9ba7a612d29502d4a7e691c6741837d4a8b42f0

8 years agoRevert "Changed AC_LANG from PROGRAM to SOURCE to prevent double definition of main"
Orvid King [Tue, 22 Mar 2016 17:21:25 +0000 (10:21 -0700)]
Revert "Changed AC_LANG from PROGRAM to SOURCE to prevent double definition of main"

This reverts commit bb733f43b5674294a12c99ac3e686c4b54427917.

8 years agoAdd support for probabilistically choosing server ciphers
Anirudh Ramachandran [Mon, 21 Mar 2016 23:25:46 +0000 (16:25 -0700)]
Add support for probabilistically choosing server ciphers

Summary:Since SSLContextManager sets SSL_OP_CIPHER_SERVER_PREFERENCE on the SSL_CTX
when it creates contexts, we may be unable to accommodate any clients who
prefer a different ciphersuite. Having differently weighted cipher preference
lists allows SSLContext to set a list with a different most-preferred cipher
for some fraction of new handshakes.

Note: resumption will work with the previously negotiated ciphersuite even if
the server doesn't explicitly prefer/support it anymore, provided the cipher is
supported in OpenSSL.

Reviewed By: knekritz

Differential Revision: D3050496

fb-gh-sync-id: 1c3b77ce3af87f939f8b8c6fe72b6a64eeaeeeb4
shipit-source-id: 1c3b77ce3af87f939f8b8c6fe72b6a64eeaeeeb4

8 years agoHave StringTest use PRI*64 instead
Michael Lee [Mon, 21 Mar 2016 22:52:19 +0000 (15:52 -0700)]
Have StringTest use PRI*64 instead

Summary: This helps account for 32-bit vs. 64-bit compilation targets.

Reviewed By: benyl

Differential Revision: D3076523

fb-gh-sync-id: 56f49daaf55d242ac48f8b8d38a40c6d6230818b
shipit-source-id: 56f49daaf55d242ac48f8b8d38a40c6d6230818b

8 years agoAdd option to retrieve hex representation of client ciphers
Anirudh Ramachandran [Mon, 21 Mar 2016 22:46:23 +0000 (15:46 -0700)]
Add option to retrieve hex representation of client ciphers

Summary: A more compact hex representation of ciphers in ClientHello can be useful, e.g., for logging.

Reviewed By: knekritz

Differential Revision: D3052308

fb-gh-sync-id: beaf6fcd4705d4d7fae652d8d8b95b52ca9e07a9
shipit-source-id: beaf6fcd4705d4d7fae652d8d8b95b52ca9e07a9

8 years agoAdd Windows support to portability/Memory.h
Christopher Dykes [Mon, 21 Mar 2016 22:05:49 +0000 (15:05 -0700)]
Add Windows support to portability/Memory.h

Summary: This adds Windows support to portability/Memory.h and also does some refactoring to be more correct about when to use posix_memalign, and also changes it to fall back to memalign rather than posix_memalign.

Reviewed By: mzlee

Differential Revision: D3069990

fb-gh-sync-id: 88861583c6028e9fb02a3e3804bd6d476956c555
shipit-source-id: 88861583c6028e9fb02a3e3804bd6d476956c555

8 years agoOptimize fbstring::append()
Giuseppe Ottaviano [Mon, 21 Mar 2016 03:34:23 +0000 (20:34 -0700)]
Optimize fbstring::append()

Summary:Instead of relying on `push_back()` to guarantee exponential growth,
delegate to `expand_noinit`. This removes the duplication of logic
between the two functions, and significantly speeds up short appends.

```
$ _bin/folly/test/fbstring_benchmark_using_jemalloc --bm_min_usec=100000
                                                          Before:             After:
============================================================================ ===================
./folly/test/FBStringTestBenchmarks.cpp.h       relative  time/iter  iters/s  time/iter  iters/s
============================================================================ ===================
...
BM_push_back_fbstring(1)                                     7.51ns  133.20M     6.87ns  145.49M
BM_push_back_fbstring(23)                                  175.08ns    5.71M   173.92ns    5.75M
BM_push_back_fbstring(127)                                 586.02ns    1.71M   585.70ns    1.71M
BM_push_back_fbstring(1024)                                  3.30us  302.81K     3.41us  293.13K
BM_short_append_fbstring(23)                               367.01ns    2.72M   179.45ns    5.57M
BM_short_append_fbstring(1024)                               9.33us  107.20K     5.72us  174.95K
...
============================================================================ ===================
```

Reviewed By: philippv

Differential Revision: D3075249

fb-gh-sync-id: 497775ba3fc707bf50821a3cf10fb9d247b9352d
shipit-source-id: 497775ba3fc707bf50821a3cf10fb9d247b9352d

8 years agoAdd features to portability/Config.h
Christopher Dykes [Sun, 20 Mar 2016 18:06:56 +0000 (11:06 -0700)]
Add features to portability/Config.h

Summary: It is perfectly reasonable to need to feature macros when checking configuration, and the configuration is needed to know if features.h exists, so put it in the same file.

Reviewed By: yfeldblum

Differential Revision: D3072255

fb-gh-sync-id: 0f253dd1fa2f4a95adb11f11c45e6b86ef9c25bc
shipit-source-id: 0f253dd1fa2f4a95adb11f11c45e6b86ef9c25bc

8 years agoSplit iovec out of SysUio.h
Christopher Dykes [Sun, 20 Mar 2016 18:06:40 +0000 (11:06 -0700)]
Split iovec out of SysUio.h

Summary: This is needed to avoid a circular dependency.

Reviewed By: yfeldblum

Differential Revision: D3072037

fb-gh-sync-id: 546a46fd68f25f0c5b027d4d938335b2711d8fdd
shipit-source-id: 546a46fd68f25f0c5b027d4d938335b2711d8fdd

8 years agofix typo in log printing in AsyncSSLSocket
Mark Reitblatt [Sun, 20 Mar 2016 09:00:17 +0000 (02:00 -0700)]
fix typo in log printing in AsyncSSLSocket

Reviewed By: pritamdamania

Differential Revision: D3068807

fb-gh-sync-id: 0b45ab678e2ec5565eb379540576527cf8b1d308
shipit-source-id: 0b45ab678e2ec5565eb379540576527cf8b1d308

8 years agoUpgrade ScopeGuard for Visual Studio 2015
Jeremy Wright [Sat, 19 Mar 2016 00:11:23 +0000 (17:11 -0700)]
Upgrade ScopeGuard for Visual Studio 2015

Summary:Upgrade UncaughtExceptionCounter to use std::uncaught_exceptions on Visual Studio 2015 Update 1
Closes https://github.com/facebook/folly/pull/358

Reviewed By: yfeldblum

Differential Revision: D3066005

Pulled By: Orvid

fb-gh-sync-id: 6bc7129aa794257b1f6e7a084139d8a3a52fa4a8
shipit-source-id: 6bc7129aa794257b1f6e7a084139d8a3a52fa4a8

8 years agoCreate the sys/mman.h portability header
Christopher Dykes [Fri, 18 Mar 2016 21:57:46 +0000 (14:57 -0700)]
Create the sys/mman.h portability header

Summary: Something that Windows very definitely lacks. This is a fun one.

Reviewed By: yfeldblum

Differential Revision: D2979661

fb-gh-sync-id: e23c1db0cb8655b22308b874bd421a7cc1e4759e
shipit-source-id: e23c1db0cb8655b22308b874bd421a7cc1e4759e

8 years agoRemove pthread_yield support
Yedidya Feldblum [Thu, 17 Mar 2016 21:08:38 +0000 (14:08 -0700)]
Remove pthread_yield support

Summary:[Folly] Remove `pthread_yield` support.

* It is not actually part of POSIX - rather, `sched_yield` is.
* In C++, we should be using `std::this_thread::yield` anyway.

Reviewed By: Gownta

Differential Revision: D3056221

fb-gh-sync-id: 2fea55dd7b715149327987d61e5a573ad0455330
shipit-source-id: 2fea55dd7b715149327987d61e5a573ad0455330

8 years agoAllow override of session context
Subodh Iyengar [Thu, 17 Mar 2016 20:06:21 +0000 (13:06 -0700)]
Allow override of session context

Summary:We currently set the session context to the
default of common name, this allows session
context to be set to a different value
for different applications

Reviewed By: ngoyal

Differential Revision: D3059769

fb-gh-sync-id: 185afeb487c2c62dcf44f96076bd05871692c7ab
shipit-source-id: 185afeb487c2c62dcf44f96076bd05871692c7ab

8 years agoTell ASAN about fiber stacks
Andrew Cox [Thu, 17 Mar 2016 14:09:07 +0000 (07:09 -0700)]
Tell ASAN about fiber stacks

Summary:ASAN needs to know about the stack extents. Currently it has no knowledge of
fibers and so it can give false positives, particularly in cases of no-return
(think exceptions).

See: https://github.com/google/sanitizers/issues/189

This change along with a related ASAN diff fixes that, and I've verified it
fixes false positive test failures I'm seeing when throws happen from fibers.

Also rips out some hacks that attempted to work around the limitations of
ASAN these changes should fix.

This change depends on:
D3017630
D2952854
D3017619

And will also depend on rollout of libasan.so to /usr/local/fbcode platform dirs on all machines.

Reviewed By: andriigrynenko

Differential Revision: D2952899

fb-gh-sync-id: 19da779227c6c0f30c5755806325aa4cba364cfe
shipit-source-id: 19da779227c6c0f30c5755806325aa4cba364cfe

8 years agoCreate portability/Config.h
Christopher Dykes [Wed, 16 Mar 2016 22:15:52 +0000 (15:15 -0700)]
Create portability/Config.h

Summary: Because including the root portability header just to get a couple of defines to be able to enable things is a waste. In addition, it would be nice if the config defines are never needed outside of the portability headers.

Reviewed By: yfeldblum

Differential Revision: D3059651

fb-gh-sync-id: 1cb9910d850ea015616a598808556b4815b3fb74
shipit-source-id: 1cb9910d850ea015616a598808556b4815b3fb74

8 years agomove StaticMeta::onThreadExit into libfolly_thread_local.so
Eric Niebler [Wed, 16 Mar 2016 16:54:38 +0000 (09:54 -0700)]
move StaticMeta::onThreadExit into libfolly_thread_local.so

Summary: Fix folly/test:thread_local_test in the shared library scenario by moving onThreadExit into libfolly_thread_local.so, avoiding the crash when a user's .so is dlclosed.

Reviewed By: andriigrynenko

Differential Revision: D2919287

fb-gh-sync-id: ecc4220fd40203289366eb1a6391b8b6d971e65f
shipit-source-id: ecc4220fd40203289366eb1a6391b8b6d971e65f

8 years agoRemove `using std::make_unique`
Phil Willoughby [Wed, 16 Mar 2016 14:22:26 +0000 (07:22 -0700)]
Remove `using std::make_unique`

Summary: Older compilers do not have have `std::make_unique`. We have `folly::make_unique` which does the same job, and because we already have `using namespace folly` in this file it suffices to erase the `using std::make_unique` line.

Reviewed By: eduardosuarez

Differential Revision: D3058089

fb-gh-sync-id: a5a5eb54e2bc0ba7ef0880f2b5680a79d1f41d37
shipit-source-id: a5a5eb54e2bc0ba7ef0880f2b5680a79d1f41d37

8 years agoRemove an outdated/inapplicable comment
Yedidya Feldblum [Wed, 16 Mar 2016 07:28:24 +0000 (00:28 -0700)]
Remove an outdated/inapplicable comment

Summary: [Folly] Remove an outdated/inapplicable comment from `folly/Portability.h`.

Reviewed By: Gownta

Differential Revision: D3055909

fb-gh-sync-id: c17fdcc7de13fe5a9bb38bb4498a006ec71a61e2
shipit-source-id: c17fdcc7de13fe5a9bb38bb4498a006ec71a61e2

8 years agoAdd portability header for sys/file.h
Christopher Dykes [Tue, 15 Mar 2016 20:23:20 +0000 (13:23 -0700)]
Add portability header for sys/file.h

Summary: We don't have it on Windows :(

Reviewed By: yfeldblum

Differential Revision: D2978397

fb-gh-sync-id: 388bc450ce269559825ebfb78e3646533c2e1153
shipit-source-id: 388bc450ce269559825ebfb78e3646533c2e1153

8 years agofolly/subprocess: fix one -Wsign-conversion and clag 3.9 test
Igor Sugak [Tue, 15 Mar 2016 20:00:36 +0000 (13:00 -0700)]
folly/subprocess: fix one -Wsign-conversion and clag 3.9 test

Summary:easy:
`options.parentDeathSignal_` is `int`, and second argument of `prctl` is `unsigned long`. Adding explicit cast.

hard:
this change is fixing a real problem when building folly with Clang 3.9.0. The [[ https://github.com/llvm-mirror/clang/commit/f4ddf94ecbd9899a497151621f3871545e24e93b | new alignment implementation ]] in `-O3` generates code that breaks folly's `ParentDeathSubprocessTest.ParentDeathSignal`.
For the following snipped of code:
```lang=cpp
  if (options.parentDeathSignal_ != 0) {
    if (prctl(PR_SET_PDEATHSIG, options.parentDeathSignal_, 0, 0, 0) == -1) {
      return errno;
    }
  }
```
LLVM generates the following assembly:
```lang=asm
   0x000000000040a77b <+347>:   mov    0x28(%r14),%rsi
   0x000000000040a77f <+351>:   test   %esi,%esi
   0x000000000040a781 <+353>:   je     0x40a7a1 <folly::Subprocess::prepareChild(folly::Subprocess::Options const&, __sigset_t const*, char const*) const+385>
   0x000000000040a783 <+355>:   mov    $0x1,%edi
   0x000000000040a788 <+360>:   xor    %edx,%edx
   0x000000000040a78a <+362>:   xor    %ecx,%ecx
   0x000000000040a78c <+364>:   xor    %r8d,%r8d
   0x000000000040a78f <+367>:   xor    %eax,%eax
   0x000000000040a791 <+369>:   callq  0x405ad0 <prctl@plt>
```
`%rsi` is a 64-bit register, on the line 1 value of `0x28(%r14)` is loaded to this register. That address points to `options.parentDeathSignal_`, which is a 32-bit value, set to 10 somewhere in a test. After that load:
```
rsi            0x7f000000000a
```
Notice it is not 10 (0xa), there is some garbage value in the upper part of the register. On the line 2, the lower part of this register is checked if it is zero, which corresponds to the first if statement. Then lines 4-8 prepare for `prctl` call. The value of the second argument is passed via `%rsi`, but the upper 32 bits were not cleared. This makes the function call generate `Invalid argument` error.
With the added explicit cast, notice a call `movslq %eax,%rsi`, which moves a signed 32-bit value of `%eax` (which contains `options.parentDeathSignal_`) to unsigned 64-bin `%rsi`:
```
   0x000000000040a77b <+347>:   mov    0x28(%r14),%rax
   0x000000000040a77f <+351>:   test   %eax,%eax
   0x000000000040a781 <+353>:   je     0x40a7a4 <folly::Subprocess::prepareChild(folly::Subprocess::Options const&, __sigset_t const*, char const*) const+388>
   0x000000000040a783 <+355>:   movslq %eax,%rsi
   0x000000000040a786 <+358>:   mov    $0x1,%edi
   0x000000000040a78b <+363>:   xor    %edx,%edx
   0x000000000040a78d <+365>:   xor    %ecx,%ecx
   0x000000000040a78f <+367>:   xor    %r8d,%r8d
   0x000000000040a792 <+370>:   xor    %eax,%eax
   0x000000000040a794 <+372>:   callq  0x405ad0 <prctl@plt>
```

Reviewed By: yfeldblum

Differential Revision: D3051611

fb-gh-sync-id: fc0f809bc4be0eed79dc5a701717efa27fedc022
shipit-source-id: fc0f809bc4be0eed79dc5a701717efa27fedc022

8 years agoConvert a polling loop to a futex wait
Phil Willoughby [Tue, 15 Mar 2016 08:39:48 +0000 (01:39 -0700)]
Convert a polling loop to a futex wait

Summary:Add a new method to MPMCQueue:
```
template <class Clock, typename... Args>
  bool tryWriteUntil(const std::chrono::time_point<Clock>& when,
                     Args&&... args) noexcept
```
This allows you to write producers which terminate reliably in the absence of consumers.

Returns `true` if `args` was enqueued, `false` otherwise.

`Clock` must be one of the types supported by the underlying call to `folly::detail::Futex::futexWaitUntil`; at time of writing these are `std::chrono::steady_clock` and `std::chrono::system_clock`.

Reviewed By: nbronson

Differential Revision: D2895574

fb-gh-sync-id: bdfabcd043191c149f1271e30ffc28476cc8a36e
shipit-source-id: bdfabcd043191c149f1271e30ffc28476cc8a36e

8 years agoAllow httpsession to be movable readCB
Subodh Iyengar [Tue, 15 Mar 2016 06:08:18 +0000 (23:08 -0700)]
Allow httpsession to be movable readCB

Summary:Allow HTTPSession to have a movable read
callback independent of the openssl check.

This allows other downstream transports to
deliver app data more efficiently.

Reviewed By: w-o-o, knekritz

Differential Revision: D3023517

fb-gh-sync-id: ff27c0e14f7364ad7d7b0028ef740b2678dbcb4c
shipit-source-id: ff27c0e14f7364ad7d7b0028ef740b2678dbcb4c

8 years agoMissing guards for FOLLY_TLS in test
Michael Lee [Tue, 15 Mar 2016 01:54:37 +0000 (18:54 -0700)]
Missing guards for FOLLY_TLS in test

Summary:defined(FOLLY_TLS) is guarded once in the test, so we should
guard for it throughout.  Also a missing header for some platforms.

Reviewed By: yfeldblum

Differential Revision: D3049797

fb-gh-sync-id: 5d66c71dda94c57a2e50aee96d55be1c574bf921
shipit-source-id: 5d66c71dda94c57a2e50aee96d55be1c574bf921

8 years agofolly/portability/Constexpr.h: add missing include statement
Sven Over [Mon, 14 Mar 2016 18:43:33 +0000 (11:43 -0700)]
folly/portability/Constexpr.h: add missing include statement

Summary:This commit fixes compiler errors when Constexpr.h was included
on gcc before cstring (or string.h) was included. Also, qualify
the call to strlen with the std namespace.

Reviewed By: yfeldblum

Differential Revision: D3047417

fb-gh-sync-id: 7a50dac2e9449b149062896f34fa5e864f767943
shipit-source-id: 7a50dac2e9449b149062896f34fa5e864f767943

8 years agoFix a typo in portability/Memory.cpp
Michael Lee [Mon, 14 Mar 2016 16:34:01 +0000 (09:34 -0700)]
Fix a typo in portability/Memory.cpp

Summary: Also, add error checking back in, and remove the empty WIN32 implementation.

Reviewed By: francis-ma

Differential Revision: D3047494

fb-gh-sync-id: 5a2a18cd1ca675a081ad2a9252c765ab56527b6b
shipit-source-id: 5a2a18cd1ca675a081ad2a9252c765ab56527b6b

8 years agofolly::FunctionScheduler: Adding capability to reset a function's timer
Jimmy Saade [Mon, 14 Mar 2016 12:07:14 +0000 (05:07 -0700)]
folly::FunctionScheduler: Adding capability to reset a function's timer

Summary:Adding support for resetting a specified function's timer.

"Resetting a function's timer" effectively means "canceling whatever next runs it would have had, and treating it as though it were just added".
When `resetFunctionTimer` is called, the specified function's interval (timer) will be reset, and it will execute after its initially-specified `startDelay`. If the `startDelay` is zero, the function will execute immediately, and then be scheduled as before - once every `interval` milliseconds.

Motivation: batch processing of updates, where both a size and time limit are in play. If the size limit is reached, it makes sense to reset the timer for the scheduled function.

Differential Revision: D3045868

fb-gh-sync-id: a5ceb0069c04a77fdab16b61679987ee55484e89
shipit-source-id: a5ceb0069c04a77fdab16b61679987ee55484e89

8 years agoFix warning in MicroLock initialization
Giuseppe Ottaviano [Sun, 13 Mar 2016 00:12:44 +0000 (16:12 -0800)]
Fix warning in MicroLock initialization

Summary:The `init()` function uses the previous value of `lock_`, but that is
uninitialized and the compiler can issue a warning about it. It is
also potentially undefined behavior because it is not guaranteed that
the address of `lock_` is taken before `init()` (in which case the it
would be just an indeterminate value).

Since it is not very useful to initialize only one slot and leave the
others uninitialized, we can just have a single `init()` that
zero-initializes all the slots.

Reviewed By: dcolascione

Differential Revision: D3042629

fb-gh-sync-id: de1633b02eb1c891e310f2d5d2cfc5376cd41d5f
shipit-source-id: de1633b02eb1c891e310f2d5d2cfc5376cd41d5f

8 years agoExplain union safety in MicroLock comment
Daniel Colascione [Sat, 12 Mar 2016 21:31:46 +0000 (13:31 -0800)]
Explain union safety in MicroLock comment

Reviewed By: ot

Differential Revision: D3045850

fb-gh-sync-id: ee19b146c43410d8d8804c9bfe79c3811394b10e
shipit-source-id: ee19b146c43410d8d8804c9bfe79c3811394b10e

8 years agofolly: fix gflags<->glog configure check ordering
Wez Furlong [Sat, 12 Mar 2016 00:36:37 +0000 (16:36 -0800)]
folly: fix gflags<->glog configure check ordering

Summary:On systems that do not have gflags or glog installed globally,
but only locally as static libraries, our configure check failed for
glog because we were checking it prior to gflags.

glog depends on gflags, but when the libraries are static, there is
no explicit dependency information available and the linker fails.

This resolves the issue by checking for gflags first; this causes
configure to add an implicit `-lgflags` for the subsequent glog
tests.

Reviewed By: yfeldblum

Differential Revision: D3044138

fb-gh-sync-id: 5e07ce52842c6e4cff796560672bf950e2fafe6c
shipit-source-id: 5e07ce52842c6e4cff796560672bf950e2fafe6c

8 years agoRemove portability/Stdlib.{h,cpp}
Michael Lee [Sat, 12 Mar 2016 00:08:55 +0000 (16:08 -0800)]
Remove portability/Stdlib.{h,cpp}

Summary: This was wrong and is like better solved, not with a shim layer, but by not using such a platform-specific function in the first place.

Reviewed By: yfeldblum

Differential Revision: D3001253

fb-gh-sync-id: 460cc46b1c9adc3d229a07cb290270f7afbbb1e0
shipit-source-id: 460cc46b1c9adc3d229a07cb290270f7afbbb1e0

8 years agoAdding portability to gating PicoSpinLock.
Michael Lee [Fri, 11 Mar 2016 00:50:53 +0000 (16:50 -0800)]
Adding portability to gating PicoSpinLock.

Summary:PicoSpinLock only works on x86_64, Arm64, and ppc64 (i.e.,
not 32-bit).  Add a bit of gating so we can continue to run tests and
use headers when compiling for i386.

Reviewed By: yfeldblum

Differential Revision: D2991328

fb-gh-sync-id: b0d0c229508f65dff62b24fdd9d80c799cd97935
shipit-source-id: b0d0c229508f65dff62b24fdd9d80c799cd97935

8 years agoFix an exception safety hole in ScopeGuard
Eric Niebler [Thu, 10 Mar 2016 21:25:07 +0000 (13:25 -0800)]
Fix an exception safety hole in ScopeGuard

Summary: Address the exception safety hole described in https://fburl.com/scopeguard-oops. Addnditional noexcept to the places that need it.

Reviewed By: dcolascione

Differential Revision: D3033015

fb-gh-sync-id: 8dfec103bbc86abef425585371994756d3df0a14
shipit-source-id: 8dfec103bbc86abef425585371994756d3df0a14

8 years agoPort easy instances to folly::dynamic::array
Giuseppe Ottaviano [Wed, 9 Mar 2016 22:35:35 +0000 (14:35 -0800)]
Port easy instances to folly::dynamic::array

Summary:The vast majority of `folly::dynamic` array initializations are
single-line and with no nested arrays, so we can fix them with a
syntactic codemod. This also fixes a couple of singletons.

For empty arrays:
```
codemod '((?:folly::)?)(dynamic\s+\w+\s*=\s*)({\s*})' '\1\2\1dynamic::array'
```

For non-empty ones:
```
codemod '((?:folly::)?)(dynamic\s+\w+\s*=\s*)(?:{\s*([^{}]+?)\s*})' '\1\2\1dynamic::array(\3)'
```

Reviewed By: igorsugak

Differential Revision: D3030338

fb-gh-sync-id: 3e56704a6c6294d6f6270e42a1776d991a7938df
shipit-source-id: 3e56704a6c6294d6f6270e42a1776d991a7938df

8 years agoAdd replay safety connector/handler to delay transactions scheduled on a replay-unsaf...
Kyle Nekritz [Wed, 9 Mar 2016 19:18:37 +0000 (11:18 -0800)]
Add replay safety connector/handler to delay transactions scheduled on a replay-unsafe transport.

Summary: This allows HTTPTransactions to be held back until the transport is replay-safe.

Reviewed By: siyengar

Differential Revision: D2974083

fb-gh-sync-id: 037b14c24a80c828a25e483b6873a8e782af0cb4
shipit-source-id: 037b14c24a80c828a25e483b6873a8e782af0cb4

8 years agoCreate the sys/stat.h portability header
Christopher Dykes [Wed, 9 Mar 2016 18:30:53 +0000 (10:30 -0800)]
Create the sys/stat.h portability header

Summary: Windows has it, but it doesn't define lstat, and a few other functions have other names.

Reviewed By: yfeldblum

Differential Revision: D3003096

fb-gh-sync-id: e7bc210390d18806c8c89cbc18711bb7a728e12f
shipit-source-id: e7bc210390d18806c8c89cbc18711bb7a728e12f

8 years agoMake Bits.h respect FOLLY_HAVE_UNALIGNED_ACCESS.
Kyle Nekritz [Wed, 9 Mar 2016 17:33:14 +0000 (09:33 -0800)]
Make Bits.h respect FOLLY_HAVE_UNALIGNED_ACCESS.

Summary: This was causing a bunch of SIGBUS's on ARM platforms.

Reviewed By: yfeldblum, mzlee

Differential Revision: D3013458

fb-gh-sync-id: 6d3f60c3f59d16cd3454e3a4231b967e5378724a
shipit-source-id: 6d3f60c3f59d16cd3454e3a4231b967e5378724a

8 years agoSSL cleanup: moving some OpenSSL definitions to new dir folly/io/async/ssl
Anirudh Ramachandran [Wed, 9 Mar 2016 02:09:22 +0000 (18:09 -0800)]
SSL cleanup: moving some OpenSSL definitions to new dir folly/io/async/ssl

Summary:SSLContext and AsyncSSLSocket are growing with a lot of code that is
OpenSSL-specific, and it may be good to refactor some of these before it gets
out of hand.

This is also useful to reduce complexity as we some additional features such as ServerHello parsing and TLS Cached Info (D2936570)

Main changes:
 * Created a subdirectory folly/io/async/ssl to refactor code from folly/io/async. We may want to consider moving this out of folly/io/async
 * Moved OpenSSLPtrTypes.h to folly/io/async/ssl/OpenSSLPtrTypes.h
 * Moved 'OpenSSLUtils' from SSLContext to separate file OpenSSLUtils.{h,cpp}
 * Moved TLSExtensions and ClientHelloInfo from AsyncSSLSocket to TLSDefinitions.h

Reviewed By: siyengar

Differential Revision: D2978707

fb-gh-sync-id: a21f02947aeffccc447da2124a91cc99315df1c7
shipit-source-id: a21f02947aeffccc447da2124a91cc99315df1c7

8 years agominor tweak to MicroLock slow path
Nathan Bronson [Mon, 7 Mar 2016 21:36:59 +0000 (13:36 -0800)]
minor tweak to MicroLock slow path

Summary:This diff uses the x86 "pause" instruction or its equivalent when
spinning in the MicroLock slow loop.  This gives a hint to the processor
that it should devote more resources to the other execution contexts
sharing the same core.  This diff also removes an mfence on x86 by using
a slightly stronger memory model on the preceding compare_exchange_weak,
and switches to a more portable way of invoking sched_yield().

Reviewed By: dcolascione

Differential Revision: D3018568

fb-gh-sync-id: 02e0ab3a9d9bb9901eddf54e45b71cbb7758a227
shipit-source-id: 02e0ab3a9d9bb9901eddf54e45b71cbb7758a227

8 years agoDeprecate dynamic::dynamic(std::initializer_list<dynamic>) deprecate-dynamic-initializer
Giuseppe Ottaviano [Mon, 7 Mar 2016 19:53:13 +0000 (11:53 -0800)]
Deprecate dynamic::dynamic(std::initializer_list<dynamic>)

Summary:After DR95 the single braces dispatch to the copy constructor
(http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1467),
so it is not possible anymore to initialize a singleton dynamic array
using the braces syntax. The initializer list constructor already had
a special case for empty lists, which unconditionally called the
default constructor if defined.

This diff deprecates the braces syntax and defines the following alternative:
```
dynamic empty = dynamic::array;
dynamic a = dynamic::array(1, 2, "foo");
```

Reviewed By: luciang, yfeldblum

Differential Revision: D3013423

fb-gh-sync-id: a0cf09dfd96e9806044f7c3bf3592c637a9bc932
shipit-source-id: a0cf09dfd96e9806044f7c3bf3592c637a9bc932

8 years agoGuard Getcpu in test.
Michael Lee [Mon, 7 Mar 2016 15:41:07 +0000 (07:41 -0800)]
Guard Getcpu in test.

Summary:Guarding this the same way it is done in
detail/CacheLocality.cpp

Reviewed By: yfeldblum

Differential Revision: D3013288

fb-gh-sync-id: a1cc65cccc5cdf32339a739853c27a11ecd98ba0
shipit-source-id: a1cc65cccc5cdf32339a739853c27a11ecd98ba0

8 years agoCreate the Windows portability header
Christopher Dykes [Sat, 5 Mar 2016 01:55:27 +0000 (17:55 -0800)]
Create the Windows portability header

Summary: Because Windows' defines seem to get in the way more than they help.

Reviewed By: yfeldblum

Differential Revision: D2989507

fb-gh-sync-id: 148f390ef6adf977bffa2262aa76839b2bf4f507
shipit-source-id: 148f390ef6adf977bffa2262aa76839b2bf4f507

8 years agoA warning for the portability headers
Yedidya Feldblum [Sat, 5 Mar 2016 00:48:27 +0000 (16:48 -0800)]
A warning for the portability headers

Summary: [Folly] A warning for the `portability` headers.

Reviewed By: mzlee, Orvid, meyering

Differential Revision: D3014442

fb-gh-sync-id: f6557d8021dc2a50a656685492d717fdbff1124a
shipit-source-id: f6557d8021dc2a50a656685492d717fdbff1124a

8 years agoSwitch from tmpdir to TemporaryDirectory
Michael Lee [Sat, 5 Mar 2016 00:06:40 +0000 (16:06 -0800)]
Switch from tmpdir to TemporaryDirectory

Summary: tmpdir does not work on all platforms. TemporaryDirectory is more robust.

Reviewed By: yfeldblum

Differential Revision: D3013866

fb-gh-sync-id: 51e2b7ec2348eb95b99c125a27bb2aca2b87ac21
shipit-source-id: 51e2b7ec2348eb95b99c125a27bb2aca2b87ac21

8 years agoCreate the sys/types.h portability header
Christopher Dykes [Fri, 4 Mar 2016 23:43:26 +0000 (15:43 -0800)]
Create the sys/types.h portability header

Summary: Windows has it, but it doesn't define pid_t. It also doesn't define ssize_t, and it's not worth a separate header for a single typedef, so define it here as well.

Reviewed By: yfeldblum

Differential Revision: D3001168

fb-gh-sync-id: 3722270181c200bbcf39043960f81609c854b132
shipit-source-id: 3722270181c200bbcf39043960f81609c854b132

8 years agoFix clang build for MicroLock
Max Warsewa [Fri, 4 Mar 2016 22:32:48 +0000 (14:32 -0800)]
Fix clang build for MicroLock

Summary: clang would complain about losing integer precision. Use unsigned for offset calculation.

Reviewed By: luciang

Differential Revision: D3011253

fb-gh-sync-id: 10cb603708a22bf0e57f41b2486ffca4f5bf7a14
shipit-source-id: 10cb603708a22bf0e57f41b2486ffca4f5bf7a14

8 years agoAdd replay safety method and callback to AsyncTransport.
Kyle Nekritz [Fri, 4 Mar 2016 22:28:22 +0000 (14:28 -0800)]
Add replay safety method and callback to AsyncTransport.

Summary: This allows the transport to signal to a higher layer when it has become replay-safe.

Reviewed By: siyengar

Differential Revision: D2974057

fb-gh-sync-id: 436f0c66b78f72ad34cdf4fe117771ea96b4388b
shipit-source-id: 436f0c66b78f72ad34cdf4fe117771ea96b4388b

8 years agoMake the test portable for different libc
Michael Lee [Fri, 4 Mar 2016 21:01:27 +0000 (13:01 -0800)]
Make the test portable for different libc

Summary:The class name for std::runtime_erorr is sometimes
St13runtime_error instead. And there is simlar mapping for other
classes.

Reviewed By: yfeldblum

Differential Revision: D3013228

fb-gh-sync-id: 6a162de348eeaa51c059f9619e25ecf051615bfc
shipit-source-id: 6a162de348eeaa51c059f9619e25ecf051615bfc

8 years agofix liger contbuild: cast offset_bytes to unsigned
Yang Chi [Fri, 4 Mar 2016 16:54:08 +0000 (08:54 -0800)]
fix liger contbuild: cast offset_bytes to unsigned

Summary: Apparently I don't know what I'm doing. But given lock_ is 8bit and the assert, I suppose cast offset_bytes to unsigned is safe.

Reviewed By: dcolascione

Differential Revision: D3010732

fb-gh-sync-id: 1f3440a72d2f0b2fb145ab36966f0382a3009e0c
shipit-source-id: 1f3440a72d2f0b2fb145ab36966f0382a3009e0c

8 years agoRemove persistentCork in native land
Yang Chi [Fri, 4 Mar 2016 03:47:53 +0000 (19:47 -0800)]
Remove persistentCork in native land

Summary: Remove persistent cork

Reviewed By: francis-ma

Differential Revision: D3008550

fb-gh-sync-id: a5ce492876d98d74f5669c5ec5ff3486c0cb0b2c
shipit-source-id: a5ce492876d98d74f5669c5ec5ff3486c0cb0b2c

8 years agoAdd a missed glog header.
Den Raskovalov [Fri, 4 Mar 2016 01:34:39 +0000 (17:34 -0800)]
Add a missed glog header.

Summary: Instructions.h depends on glog/logging.h because of DCHECK_GT macros.

Reviewed By: ot

Differential Revision: D3009084

fb-gh-sync-id: 05012e635a8c623ca1e653cc41a460778942c39e
shipit-source-id: 05012e635a8c623ca1e653cc41a460778942c39e

8 years agoAdd MicroLock as an alternative to MicroSpinLock
Daniel Colascione [Fri, 4 Mar 2016 00:33:07 +0000 (16:33 -0800)]
Add MicroLock as an alternative to MicroSpinLock

Summary:MicroLock is a full-featured lock that fills the niche that
MicroSpinLock and PicoSpinLock currently inhabit.  Unlike these two
classes, MicroLock is a sleeping lock.  MicroLock requires two bits of
a single word and has no particular alignment requirements.

Reviewed By: ot

Differential Revision: D3004474

fb-gh-sync-id: a9bd28bd3f48e894d5bff407612ee4b228466209
shipit-source-id: a9bd28bd3f48e894d5bff407612ee4b228466209

8 years agoHave internal tests use folly::Random instead of rand_r
Michael Lee [Thu, 3 Mar 2016 23:51:18 +0000 (15:51 -0800)]
Have internal tests use folly::Random instead of rand_r

Summary: rand_r is not provided on all platforms, so use `folly::Random` instead.

Reviewed By: yfeldblum

Differential Revision: D3000351

fb-gh-sync-id: 45df3e1957c4b529ab2d2cb4db13b71d13dcef5d
shipit-source-id: 45df3e1957c4b529ab2d2cb4db13b71d13dcef5d

8 years agoAdding DynamicConverter support for enums
Marcelo Juchem [Thu, 3 Mar 2016 21:48:36 +0000 (13:48 -0800)]
Adding DynamicConverter support for enums

Summary:DynamicConverter doesn't currently support reading enums.
This diff addresses that by adding an enum specialization that uses integers as its underlying implementation.
A follow-up is to handle both integers and strings, but that would require a way to parse strings into enums like Thrift static reflection provides (see https://github.com/facebook/fbthrift/blob/d17b072daddbd318f9afaa5562100eaba0f890e3/thrift/lib/cpp2/fatal/folly_dynamic-inl.h#L88,L92.

Reviewed By: yfeldblum

Differential Revision: D3002569

fb-gh-sync-id: 27bf4a9d5a7844762f5311e2c777606a0e7753f0
shipit-source-id: 27bf4a9d5a7844762f5311e2c777606a0e7753f0

8 years agoFix printing of uint64_t in ConvTest
Michael Lee [Thu, 3 Mar 2016 21:14:30 +0000 (13:14 -0800)]
Fix printing of uint64_t in ConvTest

Summary:uint64_t is an unsigned long for 64-bit, but a long long int
for 32-bit.  `%lu` can cause truncation in certain implementations of
libc.

Reviewed By: ngoyal

Differential Revision: D3007568

fb-gh-sync-id: bbb1896c9727cbff413ac73ff0caa12bae5d8a6a
shipit-source-id: bbb1896c9727cbff413ac73ff0caa12bae5d8a6a

8 years agoAdd service identity to SSL socket and use in ticket cache
Neel Goyal [Thu, 3 Mar 2016 20:29:48 +0000 (12:29 -0800)]
Add service identity to SSL socket and use in ticket cache

Summary:Allow applications to specify a service identity tied to an SSLSocket
that can be used as a ticket cache key.

Further, add the cache key to the SSL_SESSION object and serialize it.

Reviewed By: siyengar

Differential Revision: D2991005

fb-gh-sync-id: 25a5ddbb66bd9da2084159136cbe4d55b9e00f28
shipit-source-id: 25a5ddbb66bd9da2084159136cbe4d55b9e00f28

8 years agoUse false start with ALPN.
Kyle Nekritz [Thu, 3 Mar 2016 18:33:19 +0000 (10:33 -0800)]
Use false start with ALPN.

Summary: All the work we do in SSLContext to check the cipher and NPN usage is actually completely unnecessary since OpenSSL internally checks the cipher and use of ALPN/NPN after you set the SSL_MODE option (see `ssl3_can_cutthrough()` in ssl_lib.c). This just sets the option on the SSLContext instead.

Reviewed By: siyengar

Differential Revision: D3002063

fb-gh-sync-id: 4514faf9ed2eb42a6e41d9e682b2c8aa52c46691
shipit-source-id: 4514faf9ed2eb42a6e41d9e682b2c8aa52c46691

8 years agoFix AsyncSSLSocket handshake error reporting.
Kyle Nekritz [Thu, 3 Mar 2016 16:51:53 +0000 (08:51 -0800)]
Fix AsyncSSLSocket handshake error reporting.

Summary:https://www.openssl.org/docs/manmaster/ssl/SSL_get_error.html
OpenSSL errors are a pain to deal with and we were handling several cases
incorrectly, resulting in a ton of "DH lib" errors when none were likely
actually DH lib errors.

Reviewed By: siyengar

Differential Revision: D2999084

fb-gh-sync-id: b3182be2c199f79ed341af7dbf7524197a838584
shipit-source-id: b3182be2c199f79ed341af7dbf7524197a838584

8 years agoCreate the strings.h portability header
Christopher Dykes [Wed, 2 Mar 2016 21:41:41 +0000 (13:41 -0800)]
Create the strings.h portability header

Summary: Windows doesn't have it.

Reviewed By: yfeldblum

Differential Revision: D2990219

fb-gh-sync-id: e9fe96905ec415874f0e2732b0685f3e7ad5d018
shipit-source-id: e9fe96905ec415874f0e2732b0685f3e7ad5d018

8 years agoAndroid does not always provide posix_memalign
Michael Lee [Wed, 2 Mar 2016 16:28:19 +0000 (08:28 -0800)]
Android does not always provide posix_memalign

Summary: We could provide `posix_memalign` instead as part of portability, but I am not sure how to tell whether or not it will be available.

Reviewed By: yfeldblum

Differential Revision: D2991432

fb-gh-sync-id: 587314d43779f3b8fead2c41ed05016e6350f2ee
shipit-source-id: 587314d43779f3b8fead2c41ed05016e6350f2ee

8 years agoImprove RWSpinLock.h cautionary comment
Daniel Colascione [Wed, 2 Mar 2016 03:48:45 +0000 (19:48 -0800)]
Improve RWSpinLock.h cautionary comment

Reviewed By: yfeldblum

Differential Revision: D2998122

fb-gh-sync-id: a113ba1a474da8a46052091acadb90c95d3c0c28
shipit-source-id: a113ba1a474da8a46052091acadb90c95d3c0c28

8 years agoi386 -fPIC requires `%ebx`
Michael Lee [Wed, 2 Mar 2016 01:32:43 +0000 (17:32 -0800)]
i386 -fPIC requires `%ebx`

Summary:On i368 (until, I'm lead to believe i686) `%ebx` is used for the PIC register. Thus, when we use cpuid, we clobber that register and gcc loudly complains:

  folly/CpuId.h: In member function 'virtual void CpuId_Simple_Test::TestBody()':
  folly/CpuId.h:67:61: error: PIC register clobbered by 'ebx' in 'asm'
       __asm__("cpuid" : "=a"(n) : "a"(0) : "ebx", "edx", "ecx");

Reviewed By: yfeldblum

Differential Revision: D2991229

fb-gh-sync-id: ee8795e36a4c173147d61e26c9590ec5da1d02b5
shipit-source-id: ee8795e36a4c173147d61e26c9590ec5da1d02b5

8 years agoCreate the portability header for time.h
Christopher Dykes [Wed, 2 Mar 2016 00:10:58 +0000 (16:10 -0800)]
Create the portability header for time.h

Summary: Although MSVC does have time.h, and does have equivelents of these functions, those have slightly different semantics so we have to wrap them.

Reviewed By: yfeldblum

Differential Revision: D2983613

fb-gh-sync-id: 676cd524ffa834a4250a2acc76aa1200eefe62cc
shipit-source-id: 676cd524ffa834a4250a2acc76aa1200eefe62cc

8 years agoStern warning about spinlocks
Daniel Colascione [Tue, 1 Mar 2016 22:57:44 +0000 (14:57 -0800)]
Stern warning about spinlocks

Summary: Spinlocks are a bad idea most of the time.

Reviewed By: rickbrew

Differential Revision: D2996558

fb-gh-sync-id: e86f1f0a084bda0c16759e979201db2e18102555
shipit-source-id: e86f1f0a084bda0c16759e979201db2e18102555

8 years agoEnclose global variables in an anonymous namespace.
Michael Lee [Tue, 1 Mar 2016 20:09:28 +0000 (12:09 -0800)]
Enclose global variables in an anonymous namespace.

Summary: This causes a symbol collision tests are compiled into a library.

Differential Revision: D2991357

fb-gh-sync-id: 912be0df42216004a07213d62b68ff3f4713beb0
shipit-source-id: 912be0df42216004a07213d62b68ff3f4713beb0

8 years agoListen to the Windows docs in portability/SysTime.cpp
Christopher Dykes [Tue, 1 Mar 2016 18:01:25 +0000 (10:01 -0800)]
Listen to the Windows docs in portability/SysTime.cpp

Summary:As-per the documenation of FILETIME:

"Do not cast a pointer to a FILETIME structure to either a ULARGE_INTEGER* or __int64* value because it can cause alignment faults on 64-bit Windows."

Reviewed By: yfeldblum

Differential Revision: D2989434

fb-gh-sync-id: cf57d569a785e0eb7225b346730bf2ed4c50dc55
shipit-source-id: cf57d569a785e0eb7225b346730bf2ed4c50dc55

8 years agoAdd timeradd and timersub to the sys/time.h portability header
Christopher Dykes [Tue, 1 Mar 2016 17:59:36 +0000 (09:59 -0800)]
Add timeradd and timersub to the sys/time.h portability header

Summary: I missed these in my initial diff.

Reviewed By: yfeldblum

Differential Revision: D2989977

fb-gh-sync-id: 0efb92286a8aed91ec1d394572cd709e5b6b37ab
shipit-source-id: 0efb92286a8aed91ec1d394572cd709e5b6b37ab

8 years agoFix narenas size
Dave Watson [Tue, 1 Mar 2016 17:52:58 +0000 (09:52 -0800)]
Fix narenas size

Summary:Changed from size_t to unsigned in a recent tp2 update
https://github.com/jemalloc/jemalloc/blame/3c07f803aa282598451eb0664cc94717b769a5e6/test/unit/mallctl.c

Reviewed By: jasone, mhlakhani

Differential Revision: D2991755

fb-gh-sync-id: 46f2be722a30706333eac6428f4183ca9cb85543
shipit-source-id: 46f2be722a30706333eac6428f4183ca9cb85543