folly.git
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

8 years agoto_string is not supported everywhere
Michael Lee [Tue, 1 Mar 2016 15:51:30 +0000 (07:51 -0800)]
to_string is not supported everywhere

Summary: Also remove an extra `#include <strstream>` from JsonTest.cpp as it is no longer necessary.

Reviewed By: yfeldblum

Differential Revision: D2991381

fb-gh-sync-id: 386b8281726d034933ebdc26733dd35dc5ace949
shipit-source-id: 386b8281726d034933ebdc26733dd35dc5ace949

8 years agoRemove unecessary int main from MemoryTest
Michael Lee [Tue, 1 Mar 2016 15:48:18 +0000 (07:48 -0800)]
Remove unecessary int main from MemoryTest

Summary: We do not need this main function, so remove it.

Reviewed By: markisaa

Differential Revision: D2992000

fb-gh-sync-id: 30bc0734af0f61e047276dab9639ae29cc999bbb
shipit-source-id: 30bc0734af0f61e047276dab9639ae29cc999bbb

8 years agoAdd conversion constructors for Future
Subodh Iyengar [Tue, 1 Mar 2016 03:38:32 +0000 (19:38 -0800)]
Add conversion constructors for Future

Summary:previously we were not able to do

Future<Base> f = makeFuture<Derived>();

This is because Future did not declare a conversion
constructor.

Adding a proper conversion ctor for Future is very
tricky because of the way Core is managed under the hood.
Core is not movable, and cannot be moved otherwise we
would have to retain pointers to the Future and Promises which
pointed to a particular core. This would be inefficient.

Instead we compromise and allow a very small subset of conversions
from objects whose templated types are convertible and also the
sizes of their Cores are also the same. As a result, we can
convert between types like unique_ptrs however not always between
full objects.

Reviewed By: jsedgwick

Differential Revision: D2943775

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

8 years agoFix SingletonThreadLocal
Pavlo Kushnir [Mon, 29 Feb 2016 20:02:27 +0000 (12:02 -0800)]
Fix SingletonThreadLocal

Summary: it's really thread local at this point :)

Reviewed By: jmswen

Differential Revision: D2989668

fb-gh-sync-id: 423b9214922c92318181a62c583f58cd204b52e3
shipit-source-id: 423b9214922c92318181a62c583f58cd204b52e3

8 years agoUse yield, not wfe, for asm_volatile_pause on ARM and ARM64
Scott Wolchok [Sat, 27 Feb 2016 20:39:23 +0000 (12:39 -0800)]
Use yield, not wfe, for asm_volatile_pause on ARM and ARM64

Summary:Found this hunting down LockFreeRingBuffer iOS perf problems. `wfe` and `yield` are similar, but `yield` is the instruction meant for "hey, I'm doing a spinlock", whereas `wfe` tries to wait for certain processor events. See http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0204j/Cjafcggi.html

Conveniently, it's the same instruction on 32-bit and 64-bit ARM.

Reviewed By: yfeldblum

Differential Revision: D2986160

fb-gh-sync-id: 34671256112e605bf857f9db54a56cf6bb6f1ee2
shipit-source-id: 34671256112e605bf857f9db54a56cf6bb6f1ee2

8 years agoFix folly::ThreadLocal to have unique singleton in dev builds
Andrii Grynenko [Fri, 26 Feb 2016 18:28:31 +0000 (10:28 -0800)]
Fix folly::ThreadLocal to have unique singleton in dev builds

Summary:This re-uses StaticSingletonManager which was previously used to fix the same issue in folly::Singleton.

Because of the same issue we can no longer use static thread_local for the ThreadEntry. We now rely on pthread_getspecific/pthread_setspecific instead and use static thread_local ThreadEntry* only as a cache (to improve perf).

Reviewed By: yfeldblum

Differential Revision: D2978526

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

8 years agoCreate the sys/time.h portability header
Christopher Dykes [Fri, 26 Feb 2016 17:28:51 +0000 (09:28 -0800)]
Create the sys/time.h portability header

Summary: Windows doesn't have it.

Reviewed By: yfeldblum

Differential Revision: D2979215

fb-gh-sync-id: 030bdcaa44deb18834903705a26369ce0b13abc6
shipit-source-id: 030bdcaa44deb18834903705a26369ce0b13abc6

8 years agoSplit tests into test and benchmarks.
Michael Lee [Thu, 25 Feb 2016 22:58:16 +0000 (14:58 -0800)]
Split tests into test and benchmarks.

Summary:Split up

  io/test/IOBufCursorTest
  test/BitsTest
  test/CacheLocalityTest
  test/CallOnceTest
  test/ConvTest
  test/LoggingTest
  test/MemoryIdlerTest
  test/RandomTest
  test/ThreadLocalTest

Reviewed By: yfeldblum

Differential Revision: D2973132

fb-gh-sync-id: 9dadbdf49a31e82c3a2e34c2fdb6a2b47aa0928d
shipit-source-id: 9dadbdf49a31e82c3a2e34c2fdb6a2b47aa0928d

8 years agoSplit FileTest to a smaller test and an extended test
Michael Lee [Thu, 25 Feb 2016 21:51:28 +0000 (13:51 -0800)]
Split FileTest to a smaller test and an extended test

Summary: FileTest pulls in subprocess and does a lot more than just directly test File.h

Reviewed By: yfeldblum

Differential Revision: D2971641

fb-gh-sync-id: 16e1096ab3b0f6434a26f84c889ebb082ee3f210
shipit-source-id: 16e1096ab3b0f6434a26f84c889ebb082ee3f210

8 years agoCreate a portability header for working with environment variables
Christopher Dykes [Thu, 25 Feb 2016 17:41:35 +0000 (09:41 -0800)]
Create a portability header for working with environment variables

Summary:Because `extern char** environ` is not the correct way to access environ on Windows.
This also implements setenv and unsetenv for Windows, which means that TestUtils no longer needs to be disabled for the Windows build.

Reviewed By: mzlee

Differential Revision: D2973704

fb-gh-sync-id: 84db7db3494cf183fcbcc25063cb0482ef84ebf4
shipit-source-id: 84db7db3494cf183fcbcc25063cb0482ef84ebf4

8 years agoshorten SharedMutex tests that timeout under ASAN
Nathan Bronson [Thu, 25 Feb 2016 04:28:37 +0000 (20:28 -0800)]
shorten SharedMutex tests that timeout under ASAN

Summary:New ASAN options are more thorough but slower, resulting
in timeouts for some tests.  Decreasing their internal loop count to
compensate.

Reviewed By: yfeldblum

Differential Revision: D2973982

fb-gh-sync-id: 67391936d0b4930f99d00358ebb93a1b3ee87140
shipit-source-id: 67391936d0b4930f99d00358ebb93a1b3ee87140

8 years agoFixing AtomicHashArrayTest ASan Test Failure
Jonathan Kron [Thu, 25 Feb 2016 02:36:30 +0000 (18:36 -0800)]
Fixing AtomicHashArrayTest ASan Test Failure

Summary: Both of these tests will unavoidably fail on ASan because of shared library loads, and indestructable objects.  Disabling ASAN testing for these test files.

Reviewed By: yfeldblum

Differential Revision: D2968169

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

8 years agoMinor MSVC issues
Christopher Dykes [Thu, 25 Feb 2016 00:29:40 +0000 (16:29 -0800)]
Minor MSVC issues

Summary:MSVC doesn't let you have unicode characters in a narrow string without using a utf-8 literal string.
AsyncTransport needs the definition of IOBuf because of how the standard library implements a few things.
And longs are only 32-bit under Windows.

Reviewed By: yfeldblum

Differential Revision: D2973973

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

8 years agoExplicit vtordisp for MSVC
Orvid King [Thu, 25 Feb 2016 00:28:14 +0000 (16:28 -0800)]
Explicit vtordisp for MSVC

Summary:Because sometimes things are just different.

MSVC warns about the `dynamic_cast` done against this class because the class's declaration doesn't show that it would need a vtordisp. While I don't believe the `dynamic_cast` in question is actually called in either the constructor or destructor paths for the class, better to be safe and force the disp to be created.

Reviewed By: yfeldblum

Differential Revision: D2887490

Pulled By: Orvid

fb-gh-sync-id: 821a6fc820087ac7120fffd9f8b95a26c2b4e71b
shipit-source-id: 821a6fc820087ac7120fffd9f8b95a26c2b4e71b

8 years agofolly/futures: keep Core alive until callback has returned
Sven Over [Wed, 24 Feb 2016 07:46:14 +0000 (23:46 -0800)]
folly/futures: keep Core alive until callback has returned

Summary:Callbacks passed to e.g. folly::Future::then (or
folly::Future::ensure) may delete the promise that keeps the
Core of the same future alive. The Core must protect itself from
destruction during callback execution.

This commit also adds a unit test to check for correct behaviour
in the "self destruction" scenario. The test should usually pass
even without the fix in Core.h. However, when you run the test
in Valgrind or ASAN, it will report problems unless the fix in
Core.h is applied.

Reviewed By: mhx

Differential Revision: D2938094

fb-gh-sync-id: 22796e168e1876ef2e3c7d7619da020be6a22073
shipit-source-id: 22796e168e1876ef2e3c7d7619da020be6a22073

8 years agoFix double definition for templated folly::Singletons
Andrii Grynenko [Wed, 24 Feb 2016 03:49:07 +0000 (19:49 -0800)]
Fix double definition for templated folly::Singletons

Summary: Allow createGlobal() to create dependent objects.

Reviewed By: yfeldblum

Differential Revision: D2963111

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

8 years agoFix Build: folly/test/IndestructibleTest.cpp under LSAN
Yedidya Feldblum [Wed, 24 Feb 2016 00:35:17 +0000 (16:35 -0800)]
Fix Build: folly/test/IndestructibleTest.cpp under LSAN

Summary:[Folly] Fix Build: `folly/test/IndestructibleTest.cpp` under LSAN.

Leaks of Meyers singletons are ignored automatically, while normal locals are not. So we make our instances be Meyers singletons.

Differential Revision: D2968368

fb-gh-sync-id: 385ac4491d9a105885af82d85354af929d69cc80
shipit-source-id: 385ac4491d9a105885af82d85354af929d69cc80

8 years agoFix SIOF in folly/io/async/Request.cpp
Andrii Grynenko [Tue, 23 Feb 2016 22:56:29 +0000 (14:56 -0800)]
Fix SIOF in folly/io/async/Request.cpp

Summary: Use Meyers singleton instead.

Reviewed By: afrind

Differential Revision: D2963282

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

8 years agoAsyncSocketException: add detailed information in error message
Beny Luo [Tue, 23 Feb 2016 17:44:14 +0000 (09:44 -0800)]
AsyncSocketException: add detailed information in error message

Summary: AsyncSocketException: add detailed information in error message

Reviewed By: yfeldblum

Differential Revision: D2953030

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

8 years agoSplit benchmarks and tests
Michael Lee [Tue, 23 Feb 2016 16:00:49 +0000 (08:00 -0800)]
Split benchmarks and tests

Summary:Benchmarks need their own main function, which means they
aren't really useful in cases we can't use a custom main function.

Reviewed By: yfeldblum

Differential Revision: D2962104

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

8 years agofolly: symbolizer: dwarf: don't fallback to linear scan if address missing from ...
Lucian Grijincu [Tue, 23 Feb 2016 09:50:18 +0000 (01:50 -0800)]
folly: symbolizer: dwarf: don't fallback to linear scan if address missing from .debug_aranges

Summary:Presence of .debug_aranges implies user expects fast address lookup.
Some addresses might not be avaialble in .debug_aranges.

Don't do slow lookup in .debug_info, as it can lead to unexpected slowdowns.

override-unit-failures

Reviewed By: philippv

Differential Revision: D2965323

fb-gh-sync-id: 405daefd57cdff4344fd231c5f5b7ff4dcd9df8c
shipit-source-id: 405daefd57cdff4344fd231c5f5b7ff4dcd9df8c

8 years agoFix OSX build
Neel Goyal [Mon, 22 Feb 2016 22:42:49 +0000 (14:42 -0800)]
Fix OSX build

Summary: Fixed the build using the homebrew instructions

Reviewed By: yfeldblum

Differential Revision: D2960409

fb-gh-sync-id: 218b879f26b3baab09676cac51787bf36d0b4bed
shipit-source-id: 218b879f26b3baab09676cac51787bf36d0b4bed

8 years agofix SIOF in CacheLocality.h's AccessSpreader
Nathan Bronson [Mon, 22 Feb 2016 21:08:30 +0000 (13:08 -0800)]
fix SIOF in CacheLocality.h's AccessSpreader

Summary:This diff moves all data accessed during
AccessSpreader<>::current(x) into the .data segment, avoiding SIOF
without adding indirection or dynamic gating as would be the case for
normal singleton-like constructs.  The diff also trims the AccessSpreader
API to include only those methods that people actually seem to use.

Reviewed By: djwatson

Differential Revision: D2945205

fb-gh-sync-id: 847e31adc4450217f4ed0575686be261fb504d7c
shipit-source-id: 847e31adc4450217f4ed0575686be261fb504d7c

8 years agoRemove extra `int main`s from unit tests.
Michael Lee [Mon, 22 Feb 2016 16:42:18 +0000 (08:42 -0800)]
Remove extra `int main`s from unit tests.

Summary: test/common/TestMain.cpp should provide the necessary initialization.

Reviewed By: yfeldblum

Differential Revision: D2947245

fb-gh-sync-id: 75b9d241673667badf4dc8fb81285c60cbb8166a
shipit-source-id: 75b9d241673667badf4dc8fb81285c60cbb8166a

8 years agoMove RequestContext::getStaticContext to .cpp
Andrii Grynenko [Sat, 20 Feb 2016 09:08:28 +0000 (01:08 -0800)]
Move RequestContext::getStaticContext to .cpp

Summary: All singleton registration has to happen in .cpp. Otherwise singleton may be double registered if inlined.

Reviewed By: yfeldblum

Differential Revision: D2956951

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

8 years agoSupport nested FiberManagers
Andrii Grynenko [Sat, 20 Feb 2016 00:13:42 +0000 (16:13 -0800)]
Support nested FiberManagers

Summary:It's possible to have nested loopUntilNoReady (with different FiberManagers). See unit test for a simple example.
This diff fixes currentFiberManager_ to be a stack.

Reviewed By: meyering

Differential Revision: D2953468

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

8 years agoFix EventBaseOnDestructionCallback to loop FiberManager
Andrii Grynenko [Fri, 19 Feb 2016 19:19:18 +0000 (11:19 -0800)]
Fix EventBaseOnDestructionCallback to loop FiberManager

Summary: EventBase loop should do it as well, but it's possible for LoopCallback to be removed before EventBaseOnDestructionCallback.

Reviewed By: pavlo-fb

Differential Revision: D2951303

fb-gh-sync-id: 43f5a91e7ecffa7ab7feec32fa45e01b28db66a3
shipit-source-id: 43f5a91e7ecffa7ab7feec32fa45e01b28db66a3

8 years agoFix folly::Singleton to work in dynamically linked binaries
Andrii Grynenko [Fri, 19 Feb 2016 19:02:35 +0000 (11:02 -0800)]
Fix folly::Singleton to work in dynamically linked binaries

Summary:This implements StaticSingletonManager which is then used to create all leaked Meyers singletons.

StaticSingletonManager is a singleton itself, which is created in a separate compilation unit (Singleton.cpp) and so we can be sure that other compilation units will always see a single instance of StaticSingletonManager, even if linked dynamically.

StaticSingletonManager then keeps a dictionary of typeid -> object pointer, which is used to de-duplicate same singleton being re-created from different compilation units (linked dynamically), usually because of code inlining.

override-unit-failures

Reviewed By: yfeldblum

Differential Revision: D2913027

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

8 years agofix dangling pointer bug in IPAddressV6
Tianjiao Yin [Fri, 19 Feb 2016 18:40:46 +0000 (10:40 -0800)]
fix dangling pointer bug in IPAddressV6

Summary: `mask(numBits)` returns a temporary variable. After its lifetime ends, subbytes becomes a dangling pointer.

Reviewed By: yfeldblum

Differential Revision: D2953699

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

8 years agoIndestructible
Yedidya Feldblum [Fri, 19 Feb 2016 07:10:20 +0000 (23:10 -0800)]
Indestructible

Summary:[Folly] `Indestructible`.

For when you need a Meyers singleton that will never be destructed, even at program exit.

Good for large data structures with many heap allocations, as long as they have no behavior. No point in ever destructing them.

HT: Proxygen, and Orvid.

Reviewed By: andriigrynenko

Differential Revision: D2947959

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

8 years agofolly: symbolizer: slow address->{file+line-number} lookup if `.debug_aranges` is...
Lucian Grijincu [Fri, 19 Feb 2016 02:46:44 +0000 (18:46 -0800)]
folly: symbolizer: slow address->{file+line-number} lookup if `.debug_aranges` is missing (e.g. --strip-debug-non-line)

Summary:Binaries linked with `gold` and `--strip-debug-non-line` don't have an `.debug_aranges` section

We still want to map `address->{file+line-number}` to get nice stack
traces even though this might be slower (linear search all compilation unit entries in `.debug_info`).

Before:
```
$ # link with gold + --strip-debug-non-line
$ folly/experimental/exception_tracer/exception_tracer_test
E0217 15:02:13.694947 1321814 ExceptionTracer.cpp:179] Exception type: std::runtime_error (9 frames)
    @ 000000000040ad2d __cxa_throw
    @ 0000000000409df3 bar()
    @ 0000000000409eab baz()
    @ 0000000000407c77 main
    @ 00007f00dd9860f5 __libc_start_main
    @ 000000000040991b (unknown)
```

After (similar to the output without `--strip-debug-non-line`):
```
E0217 18:37:37.579596 1583124 ExceptionTracer.cpp:179] Exception type: std::runtime_error (9 frames)
    @ 000000000040ad6d __cxa_throw
                       ./folly/experimental/exception_tracer/ExceptionTracerLib.cpp:57
    @ 0000000000409e33 bar()
                       ./folly/experimental/exception_tracer/ExceptionTracerTest.cpp:24
    @ 0000000000409eeb baz()
                       ./folly/experimental/exception_tracer/ExceptionTracerTest.cpp:51
    @ 0000000000407c87 main
                       ./folly/experimental/exception_tracer/ExceptionTracerTest.cpp:96
    @ 00007f1d16ff80f5 __libc_start_main
    @ 000000000040995b (unknown)
```

Differential Revision: D2947965

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

8 years agoDon't use a VLA for the double->string buffer.
Christopher Dykes [Thu, 18 Feb 2016 23:24:48 +0000 (15:24 -0800)]
Don't use a VLA for the double->string buffer.

Summary: MSVC is actually smarter about this than GCC, as MSVC doesn't support VLAs, it tries to eval the length at compile time. GCC on the other hand doesn't try to eval it at compile time, resulting in compiles via CMake telling us that this is a VLA.

Reviewed By: yfeldblum

Differential Revision: D2911929

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

8 years agofolly/ApplyTuple.h: fix const-correctness & other issues, simplify
Sven Over [Thu, 18 Feb 2016 13:31:22 +0000 (05:31 -0800)]
folly/ApplyTuple.h: fix const-correctness & other issues, simplify

Summary:The existing implementation of folly::applyTuple does not support
mutable callables (such as mutable lambdas and other functor objects
that only implement non-const operator()).

This commit adds a few more unit tests and changes the implementation
so that new and existing tests pass.

Reviewed By: yfeldblum

Differential Revision: D2942622

fb-gh-sync-id: 82478f290e9fd2020358ff79ef0a6bcf8a43738c
shipit-source-id: 82478f290e9fd2020358ff79ef0a6bcf8a43738c

8 years agoSort the Makefile headers a bit
Yedidya Feldblum [Wed, 17 Feb 2016 23:30:46 +0000 (15:30 -0800)]
Sort the Makefile headers a bit

Summary:[Folly] Sort the `Makefile` headers a bit.

Just manually moving a few obviously out-of-order entries.

Reviewed By: Orvid

Differential Revision: D2945668

fb-gh-sync-id: 604976d25e5913ef01d591e23737d61dacfbfc86
shipit-source-id: 604976d25e5913ef01d591e23737d61dacfbfc86

8 years agoclang-format some code in preparation for real changes
Nathan Bronson [Wed, 17 Feb 2016 22:20:19 +0000 (14:20 -0800)]
clang-format some code in preparation for real changes

Reviewed By: djwatson

Differential Revision: D2945770

fb-gh-sync-id: 9e4ee3b052b0bbb33ef796b8070edd24c6942589
shipit-source-id: 9e4ee3b052b0bbb33ef796b8070edd24c6942589

8 years agoAdd call_once, wrapper around std::call_once with a fast path
Lovro Puzar [Wed, 17 Feb 2016 13:22:40 +0000 (05:22 -0800)]
Add call_once, wrapper around std::call_once with a fast path

Summary: std::call_once is a nice API but the current GCC implementation is slower than it needs to be.  Adding a header-only wrapper with an additional atomic bool.

Reviewed By: luciang

Differential Revision: D2938884

fb-gh-sync-id: 5939c94fe62a1523053dcff26c880ecaec9e1150
shipit-source-id: 5939c94fe62a1523053dcff26c880ecaec9e1150

8 years agoHandle wrapvFull when IOV_MAX is not defined.
Michael Lee [Wed, 17 Feb 2016 04:38:20 +0000 (20:38 -0800)]
Handle wrapvFull when IOV_MAX is not defined.

Summary:--Perhaps this should move to Portability.h instead.  There is another instance of this in io/async/AsyncSocket.cpp.--

Added `portability/SysUio.h` to handle the c-macro conversion into a usable value.

Reviewed By: yfeldblum

Differential Revision: D2940778

fb-gh-sync-id: 897e44b430c02e5a7d826f3e8da9e4979b7b898c
shipit-source-id: 897e44b430c02e5a7d826f3e8da9e4979b7b898c

8 years agoECDSA async/offloading support for proxygen
Anirudh Ramachandran [Wed, 17 Feb 2016 01:28:22 +0000 (17:28 -0800)]
ECDSA async/offloading support for proxygen

Summary: Minor changes to AsyncSSLSocket for async crypto

Reviewed By: siyengar

Differential Revision: D2516765

fb-gh-sync-id: 354baeb94e6f63e8d5cdf8455ff5ca49a6aa479c
shipit-source-id: 354baeb94e6f63e8d5cdf8455ff5ca49a6aa479c

8 years agoFix wrapvFull for the case when iovec* has size more than 1024
Iaroslav Tverdokhlib [Tue, 16 Feb 2016 18:21:35 +0000 (10:21 -0800)]
Fix wrapvFull for the case when iovec* has size more than 1024

Summary: `folly::wrapvFull` fail if the passed in `iovec*` has more than 1024 (`IOV_MAX`) elements. In particular, it returns -1 with errno 22 [Invalid argument]. The fix is to limit maximum size of iovec* to IOV_MAX that is passed in to `readv/writev/...` in a single iteration of outer loop.

Reviewed By: yfeldblum

Differential Revision: D2935540

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

8 years agofolly::Optional<T> should be trivially destructible when T is
Brett Simmers [Tue, 16 Feb 2016 05:51:35 +0000 (21:51 -0800)]
folly::Optional<T> should be trivially destructible when T is

Summary:There doesn't appear to be a way to use std::enable_if on a
destructor, so conditionally select from one of two storage types.

Reviewed By: yfeldblum

Differential Revision: D2923902

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

8 years agoModification to futures to remove deadlock in certain use cases for getVia(executor).
Lee Howes [Mon, 15 Feb 2016 21:22:30 +0000 (13:22 -0800)]
Modification to futures to remove deadlock in certain use cases for getVia(executor).

Summary: If getVia was called on a future modified using via, getVia could deadlock if the original future was updated to a new executor and there was no callback chained after the call to via. In effect: f.via(executor).getVia(executor); deadlocks. This can be a problem if the code is hidden in a library and the precise semantics are unclear. This diff adds a test that exposes the problem and a fix by forcing waitVia to add a callback that will satisfy the new exector, ensuring that drive() has a callback to trigger once the future is satisfied.

Reviewed By: andriigrynenko

Differential Revision: D2906858

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

8 years agoUnify runInMainContext for void and non-void
Andrii Grynenko [Mon, 15 Feb 2016 19:32:24 +0000 (11:32 -0800)]
Unify runInMainContext for void and non-void

Summary: This also fixes a bug where exception was not re-thrown for functions returning void.

Reviewed By: spalamarchuk

Differential Revision: D2936887

fb-gh-sync-id: 9828dec131203528c27eae874aba147168f40d0d
shipit-source-id: 9828dec131203528c27eae874aba147168f40d0d

8 years agoMake Gold more like Core.
Michael Lee [Mon, 15 Feb 2016 14:00:09 +0000 (06:00 -0800)]
Make Gold more like Core.

Summary:Adding padding for Android was wrong, instead, we should have
used `std::aligned_storage`.

Reviewed By: fugalh

Differential Revision: D2934566

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

8 years agofolly/test/json_test: fix heap-buffer-overflow in Json.PrintTo test
Igor Sugak [Fri, 12 Feb 2016 23:54:28 +0000 (15:54 -0800)]
folly/test/json_test: fix heap-buffer-overflow in Json.PrintTo test

Summary:Json.PrintTo test if failing with heap-buffer-overflow asan abort. The problem here comes up
when values of `std::string` and `std::ostrstring::str()` are compared.  `std::ostrstring::str()` returns
*non null* terminated array of `char`s. When compared with `std::string` a read from memory after
the end that array is made. Fixing the test by replacing `std::ostrstream` with `std::ostringstream`,
that does append `\0`.

Reviewed By: yfeldblum

Differential Revision: D2934352

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

8 years agoConvert Thrift1(2)RequestDispatcher::sendMessage()
Andrii Grynenko [Fri, 12 Feb 2016 22:34:00 +0000 (14:34 -0800)]
Convert Thrift1(2)RequestDispatcher::sendMessage()

Summary:This depends on D2897095.

The main goal here is to start moving onRequestSuccess and onRequestFailure calls higher in the stack.

Reviewed By: mmcduff

Differential Revision: D2899959

fb-gh-sync-id: 4e074c2d734f88f5be56000095b892c6d47c0dcc
shipit-source-id: 4e074c2d734f88f5be56000095b892c6d47c0dcc

8 years agofolly copyright 2015 -> copyright 2016
Mark Isaacson [Fri, 12 Feb 2016 20:56:41 +0000 (12:56 -0800)]
folly copyright 2015 -> copyright 2016

Summary: Update copyright for 2016

Reviewed By: igorsugak

Differential Revision: D2828047

fb-gh-sync-id: 8638188aeb694c3ca2a615cc2bc3bc9d251e388d
shipit-source-id: 8638188aeb694c3ca2a615cc2bc3bc9d251e388d

8 years agoFix Singleton/ThreadLocal destruction order crashes in buck dev builds
Andrii Grynenko [Fri, 12 Feb 2016 18:22:25 +0000 (10:22 -0800)]
Fix Singleton/ThreadLocal destruction order crashes in buck dev builds

Summary: This adds a folly::Singleton->folly::ThreadLocal dependency to make sure folly::ThreadLocal is always loaded first. Otherwise PthreadKeyUnregister singleton is created after folly::Singleton storage, even though it has higher priority.

Reviewed By: andrewjcg

Differential Revision: D2931170

fb-gh-sync-id: 36392d76e98201f2b4416b4bbef451d6c0e8c69d
shipit-source-id: 36392d76e98201f2b4416b4bbef451d6c0e8c69d

8 years agoFix MultiLevelTimeseries::getRate()
Drew Hoskins [Thu, 11 Feb 2016 23:53:21 +0000 (15:53 -0800)]
Fix MultiLevelTimeseries::getRate()

Summary:I hit a landmine where rate() returned 0 for my tests where fewer than 60 items were added per minute.  This was because it was truncating it.  And yet, countRate() was working.
It doesn't make sense for a rate (value accumulated per time period) to be integral.
Folly: I changed rate() to return a double by default for folly, as was done by avg() and countRate().  Looked like a typo to me.
Common wrapper: I changed getRate() to allow you to override and make it double, as was done by getAvg().  Defaulting to int (which is usually the value type) is a bad call IMO but it's a riskier change to change it to double, and I want to be consistent with getAvg().

Reviewed By: tracelog

Differential Revision: D2921061

fb-gh-sync-id: 00875f2ab7963ef3ba2db475aedaf6ebd413b38f
shipit-source-id: 00875f2ab7963ef3ba2db475aedaf6ebd413b38f

8 years agofix -Wshadowing in String.cpp
Michael Lee [Thu, 11 Feb 2016 04:22:20 +0000 (20:22 -0800)]
fix -Wshadowing in String.cpp

Summary: Didn't check this with -Wshadow when I first wrote it.

Reviewed By: ranjeeth

Differential Revision: D2926047

fb-gh-sync-id: 8391986dee2d6b5698491bd2995f039468ec684d
shipit-source-id: 8391986dee2d6b5698491bd2995f039468ec684d

8 years agoFix typo in StringTest
Michael Lee [Thu, 11 Feb 2016 04:04:28 +0000 (20:04 -0800)]
Fix typo in StringTest

Summary: It should be UL instead of ULL.

Reviewed By: siyengar

Differential Revision: D2926005

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

8 years agoSwitch stripLeftMargin to not use boost::regex
Michael Lee [Thu, 11 Feb 2016 02:51:43 +0000 (18:51 -0800)]
Switch stripLeftMargin to not use boost::regex

Summary: Remove boost regex from `stripLeftMargin`. We can shrink some binaries by not including it in the core folly library.

Reviewed By: yfeldblum

Differential Revision: D2922415

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

8 years agoAdd MAX_STATIC_CONSTRUCTOR_PRIORITY to PthreadKeyUnregister
Andrii Grynenko [Thu, 11 Feb 2016 02:02:27 +0000 (18:02 -0800)]
Add MAX_STATIC_CONSTRUCTOR_PRIORITY to PthreadKeyUnregister

Summary: We want onThreadExit to be canceled as late as possible. Otherwise accessAllThreads() may crash on shutdown (because ThreadEntry is destroyed, but not removed from the global list).

Reviewed By: igorsugak

Differential Revision: D2923435

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

8 years agoCompensate for -m32 platforms.
Michael Lee [Thu, 11 Feb 2016 00:05:12 +0000 (16:05 -0800)]
Compensate for -m32 platforms.

Summary:
When compiled for -m32, this test fails because of the
difference between longs.

Reviewed By: yfeldblum

Differential Revision: D2924626

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

8 years agoMinor optimizations in ExceptionCounterLib
Giuseppe Ottaviano [Wed, 10 Feb 2016 20:17:35 +0000 (12:17 -0800)]
Minor optimizations in ExceptionCounterLib

Summary: Avoid allocation and logging inside the throw handler, and some other minor tweaks.

Reviewed By: bort, luciang

Differential Revision: D2921073

fb-gh-sync-id: 4491ab2f85a4251e59ba9394bba5abef0bdf7a10
shipit-source-id: 4491ab2f85a4251e59ba9394bba5abef0bdf7a10

8 years agoFix EventBase destruction race in FiberManagerMap
Andrii Grynenko [Wed, 10 Feb 2016 19:14:26 +0000 (11:14 -0800)]
Fix EventBase destruction race in FiberManagerMap

Summary:
Previously we could be reading from thread-local FiberManagerMap while it was modified.
This is now fixed by keeping a per-thread list of EventBases which need to be removed from local maps. On the fast-path no action is taken, since list will be empty.

This is second try, since D2853921 got reverted.
The new implementation is simpler and does not rely on AtomicLinkedList.

Reviewed By: yfeldblum

Differential Revision: D2908018

fb-gh-sync-id: 4d7aed974c19761f7e2732ddbf8694af57c69bd6
shipit-source-id: 4d7aed974c19761f7e2732ddbf8694af57c69bd6

8 years agostripLeftMargin
Yedidya Feldblum [Wed, 10 Feb 2016 00:28:21 +0000 (16:28 -0800)]
stripLeftMargin

Summary:
[Folly] `stripLeftMargin`.

So you can do:

    TEST(MyClass, doSomethingWithString) {
      //  Multiline string literal is indented properly inside the test case
      //  instead of being aligned all the way to the left, which would make
      //  the test case read poorly.
      auto input = folly::stripLeftMargin(R"TEXT(
        first line
        second line
        third line
      )TEXT");
      auto expected = //...
      auto actual = MyClass::doSomethingWithString(input);
      EXPECT_EQ(expected, actual);
    }

Reviewed By: markisaa

Differential Revision: D2909736

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

8 years agoRelax HHWheelTimer::destroy assertion to accommodate SharedPtr
Alan Frindell [Tue, 9 Feb 2016 17:44:21 +0000 (09:44 -0800)]
Relax HHWheelTimer::destroy assertion to accommodate SharedPtr

Summary:
HHWheelTimer's auto-pointers are kind of funny.  You can do something like this:

```
HHWheelTimer::UniquePtr p = ...;
// create a SharedPtr from UniquePtr
HHWheelTimer::SharedPtr s(p);
// create another SharedPtr from raw ptr
HHWheelTimer::SharedPtr s(p.get());
// No problem.

If you do this:

HHWheelTimer::SharedPtr p = ....;
// this leaks
```

Why?  Because SharedPtr is only have of std::shared_ptr.  It's the refcounting half.  But when the last SharedPtr goes out of scope, it **does not** invoke HHWheelTimer::destroy().

So code like this is possible/expected:

```
MySweetObj::MySweetObj(HHWheelTimer::SharedPtr s) {
  s_ = s;
  s_.scheduleTimeout(a, b);
}

{
  HHWheelTimer::UniquePtr p = ...;

  obj = MySweetObj(p)

  // But what if I decide to kill p:
  p.reset();
}
```

Since MySweetObj takes a SharedPtr and holds it, it can reasonbly expect that it can schedule timeouts on it, and the HHWheelTimer will not be deleted until it releases the SharedPtr.  This is true, but the above code would hit the assert that count_ == 0.

Instead, relase the check that count_ == 0 only if there are no extra outstanding SharedPtrs.

Reviewed By: viswanathgs, chadparry

Differential Revision: D2908729

fb-gh-sync-id: 9abd4a7d692fe952c5514dbb8d85dfbad95a3cac
shipit-source-id: 9abd4a7d692fe952c5514dbb8d85dfbad95a3cac

8 years agoFix bad merge.
Michael Lee [Mon, 8 Feb 2016 20:53:55 +0000 (12:53 -0800)]
Fix bad merge.

Summary:
FBStringTest.cpp was refactored, but the TestUtil and
TemporaryFile was not properly removed.

Reviewed By: yfeldblum

Differential Revision: D2913045

fb-gh-sync-id: 710c3f5b808acb634dfcd65219484ddc257ed52c

8 years agoFix Build: conditionally_existent_test in folly/test/Makefile.am
Yedidya Feldblum [Mon, 8 Feb 2016 02:14:17 +0000 (18:14 -0800)]
Fix Build: conditionally_existent_test in folly/test/Makefile.am

Summary:
[Folly] Fix Build: `conditionally_existent_test` in `folly/test/Makefile.am`.

HT: https://github.com/shindo.

Reviewed By: meyering

Differential Revision: D2910782

fb-gh-sync-id: 9593f026e7c58b8644abc5996da65005bcc2d095

8 years agofolly/futures: fix early release of non-embedded callbacks
Sven Over [Sun, 7 Feb 2016 13:24:01 +0000 (05:24 -0800)]
folly/futures: fix early release of non-embedded callbacks

Summary:
folly::Future (more precisely folly::detail::Core) can store
callback functors (typically lambdas) up to a certain size
(8*sizeof(void*)) inside the main object. Only bigger functors
are stored in the std::function object in folly::detail::Core,
which will put them on the heap.

The behaviour of folly::Future is slightly different depending
on whether the callback can be embedded in the main object
or not. Small functors will be destructed after the callback
is executed. Functors too big to fit in the lambda space in
the Core object will be deleted when Core is deleted.

Some code assumes that functor objects are deleted as soon
as the callback has been executed. The implementations of
folly::Future::collect and variants do so. If you switch
off this optimisation temporarily (which can be done easily
by setting lambdaBufSize in folly/futures/detail/Core.h to
0), a number of unit tests fail.

Given that the lambda buffer is reasonably large, most
functors can be stored in the Core object. The different
behaviour for very large lambdas may not have been observed.

This diff fixes the inconsitent behaviour.

Firstly, it changes the unit test Future:finish to explicitly
check that the callback functor is deleted after the callback
has been executed. This should be tested, because this
behaviour is assumed in other parts of the futures
implementation (e.g. Future::collect et al.).

Secondly, it adds another unit test Future:finishBigLambda,
similar to Future:finish. The lambda captures much more data
to make sure the lambda won't be stored in the Core object,
but in the std::function object inside Core. The test verifies
that the behaviour is the same, i.e. the callback functor
is destructed after the callback has been executed.

Thirdly, it fixes Core and makes sure that functors of any
size are destructued after the callback has been called. The
new unit test fails without this.

Reviewed By: fugalh

Differential Revision: D2883772

fb-gh-sync-id: 21a410f6592b3e090772a7b55bef6729d8739922

8 years agoCall destructor for non-trivial destructors if arena allocator is used in ConcurrentS...
Den Raskovalov [Sat, 6 Feb 2016 05:31:32 +0000 (21:31 -0800)]
Call destructor for non-trivial destructors if arena allocator is used in ConcurrentSkipList.

Reviewed By: philippv

Differential Revision: D2900534

fb-gh-sync-id: c711a160d541d937ada24f2b524016dbceb40ee2

8 years agoLoosen restriction to get folly::future::CoreTest pass on android
Francis Ma [Fri, 5 Feb 2016 23:00:39 +0000 (15:00 -0800)]
Loosen restriction to get folly::future::CoreTest pass on android

Summary: Due to different way of padding, CoreTest failed on android. Loosen the check to make it pass.

Reviewed By: mzlee

Differential Revision: D2907987

fb-gh-sync-id: 81d715725d6908ec7b259b0a67789a91ed63df71

8 years agoMake folly/future portable on android
Francis Ma [Fri, 5 Feb 2016 22:42:28 +0000 (14:42 -0800)]
Make folly/future portable on android

Summary: Some small changes to port folly/futures onto android

Reviewed By: mzlee

Differential Revision: D2787564

fb-gh-sync-id: 8c161942c24c2b7b0484339eaa51c5a356f17de5

8 years agoConditionallyExistent<typename>
Yedidya Feldblum [Fri, 5 Feb 2016 20:27:56 +0000 (12:27 -0800)]
ConditionallyExistent<typename>

Summary:
[Folly] `ConditionallyExistent<typename>`.

For when we need extra member fields in dbg builds, but want to save the space in opt builds. Or other situations along similar lines, but with perhaps another the statically-known condition.

Conditional compilation can have some nasty side-effects. Best to avoid it. Let the compiler see everything, rather than having the preprocessor ruin our day. Let the optimizer remove code that will never be called, after the compiler has seen it. Let us have a single AST in our source file, not one AST for dbg and one for opt, or other statically-known conditions.

Reviewed By: andriigrynenko

Differential Revision: D2879397

fb-gh-sync-id: d631141a984eebd46674f27a40a97f670eb33f54