folly.git
6 years agoUpdate SSLContext to use folly::Random and discrete_distribution
Neel Goyal [Tue, 15 Dec 2015 14:35:14 +0000 (06:35 -0800)]
Update SSLContext to use folly::Random and discrete_distribution

Summary:
Change next pick logic to use `std::discrete_distribution`.  Use folly::Random::DefaultGenerator as the generator.
Update the TARGETS to have sslcontext depend on random (which was the build break in D2741855)

Reviewed By: siyengar

Differential Revision: D2749166

fb-gh-sync-id: 7a6b400173b1e8c69a961f9e75e97d4777d5467a

6 years agoAdd a per-socket buffer callback
Yang Chi [Mon, 14 Dec 2015 23:33:49 +0000 (15:33 -0800)]
Add a per-socket buffer callback

Summary: this is way simpler than D2623385 + D2709121. There will be a followup diff to clean the existing per-write call BufferCallback. The new one is on per-socket basis, much straightforward. I will only setup this in HTTPUpstreamSession.

Reviewed By: afrind

Differential Revision: D2723493

fb-gh-sync-id: 6b1c21a719281b9693330b6a4074f7149d7c342a

6 years agoRemove per-write buffer callback from AsyncSocket
Yang Chi [Mon, 14 Dec 2015 23:33:46 +0000 (15:33 -0800)]
Remove per-write buffer callback from AsyncSocket

Summary: Remove per-write buffer callback from AsyncSocket

Reviewed By: afrind

Differential Revision: D2733142

fb-gh-sync-id: 91214a8c833bbd479bf049c2bb72d660e0c30f50

6 years agofolly::dynamic::operator= for std::initializer_list
Louis Brandy [Fri, 11 Dec 2015 17:21:43 +0000 (09:21 -0800)]
folly::dynamic::operator= for std::initializer_list

Summary:
If we're going to have a constructor for it, we should have operator=.

Otherwise, gcc-4.9 goes via the copy constructor effectively "ignoring" the attempted nesting for e.g..

```
d = { other_dynamic };  // Should be ARRAY containing dynamic
```

NOTE: this only fixes gcc-4.9, there's still issues in clang.

Reviewed By: yfeldblum

Differential Revision: D2745180

fb-gh-sync-id: 667787c788fc7c131d8a34c608c355f5b875be50

6 years agoFix RefCountTest and RCURefCount race
Andrii Grynenko [Fri, 11 Dec 2015 02:16:51 +0000 (18:16 -0800)]
Fix RefCountTest and RCURefCount race

Reviewed By: alikhtarov

Differential Revision: D2741459

fb-gh-sync-id: c4bd068cf735ae25364edba40960096fb35e8c43

6 years agosetup signal handler with SA_ONSTACK
Philip Pronin [Fri, 11 Dec 2015 00:15:37 +0000 (16:15 -0800)]
setup signal handler with SA_ONSTACK

Summary:
By default signal handlers are run on the signaled thread's stack.
In case of stack overflow running the `SIGSEGV` signal handler on
the same stack leads to another `SIGSEGV` and crashes the program
Use `SA_ONSTACK`, so alternate stack is used (only if configured via
`sigaltstack`).

Reviewed By: luciang

Differential Revision: D2747021

fb-gh-sync-id: 48388acd6147e2919412ec32acfca1ca76f22a16

6 years agoUse SharedMutex as the default mutex type in Synchronized
Yedidya Feldblum [Thu, 10 Dec 2015 20:15:41 +0000 (12:15 -0800)]
Use SharedMutex as the default mutex type in Synchronized

Summary:
[Folly] Use `SharedMutex` as the default mutex type in `Synchronized`.

`folly::SharedMutex` is, overall, a faster mutex than `boost::shared_mutex`. Let's upgrade.

Reviewed By: nbronson

Differential Revision: D2743132

fb-gh-sync-id: e36881495e8e1002cd481607e05e555c5da19b9a

6 years agoD2741855 broke my wangle. Reverting
Neel Goyal [Thu, 10 Dec 2015 15:10:49 +0000 (07:10 -0800)]
D2741855 broke my wangle. Reverting

Summary: Revert D2741855

Reviewed By: mzlee

Differential Revision: D2744015

fb-gh-sync-id: b1e9b0a5ab95cb988d2b5c08c86139452b092465

6 years agoUnit test showing array assignment problem on gcc 4.9
Barney Moss [Thu, 10 Dec 2015 10:37:18 +0000 (02:37 -0800)]
Unit test showing array assignment problem on gcc 4.9

Summary: Unit test showing array assignment problem on gcc 4.9

Reviewed By: lbrandy

Differential Revision: D2739900

fb-gh-sync-id: 6b0fde956672b8248fbd4e620fd112195c45c646

6 years agoAdd ReadMostlySharedPtr and TLRefCount to Makefile.am
Andrii Grynenko [Thu, 10 Dec 2015 04:03:34 +0000 (20:03 -0800)]
Add ReadMostlySharedPtr and TLRefCount to Makefile.am

Reviewed By: pavlo-fb

Differential Revision: D2743230

fb-gh-sync-id: 4a52d1758a43fd103318bbe0913776340eaa6af7

6 years agoFix automake build
Arjen Roodselaar [Thu, 10 Dec 2015 01:51:27 +0000 (17:51 -0800)]
Fix automake build

Summary: There is slight drift in external deps between the TARGETS and automake files, keeping tests from linking in the 'make check' step when building folly outside of fbcode.

Reviewed By: yfeldblum

Differential Revision: D2740482

fb-gh-sync-id: ae6243f9bb112a7d2680017b3d577a1f23ef4f60

6 years agoUpdate SSLContext to use discrete_distribution
Neel Goyal [Thu, 10 Dec 2015 00:21:19 +0000 (16:21 -0800)]
Update SSLContext to use discrete_distribution

Summary: Update the protocol pick logic to use discrete_distribution

Reviewed By: siyengar

Differential Revision: D2741855

fb-gh-sync-id: 244bd087124a7a9584a1108fe8f8150093275878

6 years agoSwitch folly::Singleton to ReadMostlySharedPtr
Andrii Grynenko [Wed, 9 Dec 2015 23:09:05 +0000 (15:09 -0800)]
Switch folly::Singleton to ReadMostlySharedPtr

Summary:
This also introduces a new try_get_fast() API which returns ReadMostlySharedPtr.
We should eventually migrate all users onto this API.

Reviewed By: elsteveogrande

Differential Revision: D2706745

fb-gh-sync-id: d558d705f431fc6147334c188fd0c6156644ba5c

6 years agoAdd support for ALPN
Alan Frindell [Wed, 9 Dec 2015 19:55:40 +0000 (11:55 -0800)]
Add support for ALPN

Summary: With openssl-1.0.2 and later add support for ALPN.  Clients can request NPN only, but the default is to support either (client will send ALPN list, server will send NPN advertisement if ALPN is not negotiated).

Reviewed By: siyengar

Differential Revision: D2710441

fb-gh-sync-id: a8efe69e1869bbecb4ed9e0a513448fcfdb21ca6

6 years agonotification queue read -> readNoInt
Shijin Kong [Tue, 8 Dec 2015 23:29:19 +0000 (15:29 -0800)]
notification queue read -> readNoInt

Summary: EINTR should be retried

Reviewed By: afrind

Differential Revision: D2735879

fb-gh-sync-id: 6f25cd26e29ab8e7e13a3d63f24bf2d104c14809

6 years agoAvoid cost of cancelAll
Chad Parry [Tue, 8 Dec 2015 22:37:00 +0000 (14:37 -0800)]
Avoid cost of cancelAll

Summary:
This is an experimental diff that may improve the performance of `HHWheelTimer::cancelAll`. If there are no timers to cancel, then it can bail early.

Since perflab is unlikely to be conclusive, and since this diff looks like a strict improvement, I'll proceed with landing it.

Reviewed By: djwatson

Differential Revision: D2735297

fb-gh-sync-id: 9f5a811ee6d9fa9434576e9abd3ef3443a7579b7

6 years agoFix TLRefCount race around thread local destruction and fix RefCount unit test
Andrii Grynenko [Tue, 8 Dec 2015 20:51:40 +0000 (12:51 -0800)]
Fix TLRefCount race around thread local destruction and fix RefCount unit test

Reviewed By: pavlo-fb

Differential Revision: D2708425

fb-gh-sync-id: 665d077210503df4f4e8aa8f88ce5b9b277582f3

6 years agoUpdate zstd to 0.4.2
Chip Turner [Tue, 8 Dec 2015 15:52:02 +0000 (07:52 -0800)]
Update zstd to 0.4.2

Summary: New version.

Reviewed By: cyan

Differential Revision: D2730844

fb-gh-sync-id: 4305bdfba2e8d25eba295bbf3f13a140c0d04a1c

6 years agoMark some methods virtual on SSL Context.
Neel Goyal [Tue, 8 Dec 2015 13:18:02 +0000 (05:18 -0800)]
Mark some methods virtual on SSL Context.

Summary:
Mark some methods as virtual.  This makes writing some
better unit tests on the associated task possible.

Reviewed By: siyengar

Differential Revision: D2730126

fb-gh-sync-id: f79f7632e873cb218bb49883d9b53beea7bb96e7

6 years agoadd type info to broken promise what()
John Sherwood [Mon, 7 Dec 2015 20:05:28 +0000 (12:05 -0800)]
add type info to broken promise what()

Summary: when a promise is broken, add type info to the message. This will allow for some small improvements to debugging errors from broken promises, namely giving developers a slightly more refined target to grep for.

Reviewed By: fugalh

Differential Revision: D2725575

fb-gh-sync-id: b92edbde874e63eaeab97395da7bb52f76968800

6 years agoFix segfaults in AsyncSSLSocket when used in unencrypted mode.
Kyle Nekritz [Mon, 7 Dec 2015 19:04:13 +0000 (11:04 -0800)]
Fix segfaults in AsyncSSLSocket when used in unencrypted mode.

Reviewed By: djwatson

Differential Revision: D2720027

fb-gh-sync-id: 1d56aabedeaf65a5d48e152eaee5c81695be7620

6 years agojson: Add option to parse numbers as strings
Bartek Ryniec [Fri, 4 Dec 2015 21:07:15 +0000 (13:07 -0800)]
json: Add option to parse numbers as strings

Summary: Adding a flag in `folly::json::serialization_opts` that allows you to request all numbers to be parsed into strings. This saves you from exceptions being thrown by `folly::parseJson` if it encounters numbers that wouldn't fit in int64.

Reviewed By: yfeldblum

Differential Revision: D2713039

fb-gh-sync-id: 4f3d0d20f3012efd6229adf895200a3901bb4493

6 years agoMake getWrappedTransport/getUnderlyingTransport const.
Kyle Nekritz [Fri, 4 Dec 2015 17:18:44 +0000 (09:18 -0800)]
Make getWrappedTransport/getUnderlyingTransport const.

Reviewed By: mzlee

Differential Revision: D2709222

fb-gh-sync-id: 67a00a49bbcef5572b6092d7bfba1842d985e643

6 years agoRemove getBytesRead and getBytesWritten from folly::AsyncSSLSocket.
Kyle Nekritz [Fri, 4 Dec 2015 17:18:39 +0000 (09:18 -0800)]
Remove getBytesRead and getBytesWritten from folly::AsyncSSLSocket.

Summary: getRawBytesReceived and getRawBytesWritten should be used instead.

Reviewed By: mzlee

Differential Revision: D2708684

fb-gh-sync-id: 2de87dc2b8c3466f94293a3d7df89fc5fce3a00f

6 years agoAdd an "after fork, before exec" callback
Alexey Spiridonov [Fri, 4 Dec 2015 09:35:41 +0000 (01:35 -0800)]
Add an "after fork, before exec" callback

Summary:
In rare cases, it is required to run code **in the child process**, before it calls `exec()` because you need to change the state of the process **before** the child binary gets to run. This diff adds a callback to permit this. The callback is deliberately harder to use than an `std::function`, because you **REALLY HAVE TO KNOW WHAT YOU ARE DOING** to use this. And you have to check your work three times. Even glog `LOG()` must not be called from inside this callback. Your random library of choice is also probably unsafe.

This diff is primarily applicable to job managers and shells. For example, these tasks benefit from this callback: adding a child to a Linux `cgroup`, twiddling its various POSIX process attributes.

Implementing a correct callback for the post-vfork environment is fraught with peril. Read http://ewontfix.com/7/, and the docstrings in this diff.

Reviewed By: yfeldblum

Differential Revision: D2688323

fb-gh-sync-id: aae49e2b3957ca845895acca26e9cb44df1afc07

6 years agofix for negative delay in runAfterDelay
Umair Sadiq [Fri, 4 Dec 2015 05:38:07 +0000 (21:38 -0800)]
fix for negative delay in runAfterDelay

Summary: Currently, the tryRunAfterDelay implicitly converts a negative delay in int to unsigned int. With this change, the api makes it clear than only unsinged int is accepted as valid delay

Reviewed By: yfeldblum

Differential Revision: D2710034

fb-gh-sync-id: cb3c6b91bb86bbe07fa0e590b2ea871242d19745

6 years agoThreadLocalDetail: OSX fixes
Steve O'Brien [Thu, 3 Dec 2015 03:13:22 +0000 (19:13 -0800)]
ThreadLocalDetail: OSX fixes

Summary:
Change mutex to MicroSpinLock, for constexpr-ctor-ness on OSX.

Fixes this error seen on mac:

  ThreadLocalDetail.h:202:38: note: non-literal type 'std::mutex' cannot be used in a constant expression

Reviewed By: ldemailly

Differential Revision: D2713511

fb-gh-sync-id: bc1c34b4a0ee21347278aa368b408f286345e050

6 years agoThreadLocalDetail: fix bug just introduced w/ recent change to constexpr-ctor style...
Steve O'Brien [Wed, 2 Dec 2015 17:17:38 +0000 (09:17 -0800)]
ThreadLocalDetail: fix bug just introduced w/ recent change to constexpr-ctor style class

Summary: Small, silly bug which is triggered when there are no keys to unregister, invalidating the current size of the structure.

Reviewed By: yzhan

Differential Revision: D2712604

fb-gh-sync-id: f6f031936d8e4e458b49c08714f42cb2eec4c4fe

6 years agoAdd Optional::value_type
Tudor Bosman [Wed, 2 Dec 2015 16:57:22 +0000 (08:57 -0800)]
Add Optional::value_type

Summary: Comes in handy, and std::experimental::optional has it.

Reviewed By: yfeldblum

Differential Revision: D2708324

fb-gh-sync-id: 87cdf2449f356d3baf9fb56a608e9c43d8fe79ae

6 years agofolly: ThreadLocalDetail: make PthreadKeyUnregister constexpr-constructible, avoid...
Steve O'Brien [Wed, 2 Dec 2015 14:06:32 +0000 (06:06 -0800)]
folly: ThreadLocalDetail: make PthreadKeyUnregister constexpr-constructible, avoid SIOF

Summary:
Since this is used in so many places during the program's static-initialization (at startup), this class itself could be (has been demonstrated to be) a point of SIOF problems itself.

Made this class constexpr-constructible, so it doesn't need to be part of static initialization, making it SIOF-proof.

Reviewed By: luciang

Differential Revision: D2709231

fb-gh-sync-id: f248c9f2848c09045e000cfdc03636d847e522c9

6 years agoRecommend a different exception_handler idiom
Chad Parry [Wed, 2 Dec 2015 07:36:55 +0000 (23:36 -0800)]
Recommend a different exception_handler idiom

Summary: This is a //tiny// documentation change. I noticed that there are lots of places in the codebase where people follow this recommended usage of `with_exception` in an if–else chain, (e.g., https://fburl.com/181606454). IMHO, a short-circuiting `||` operator is easier to read and write.

Reviewed By: yfeldblum

Differential Revision: D2687840

fb-gh-sync-id: 6354555aeba81dcfe7adf17e7e573de0b1206b37

6 years agoProperly init collectDone_
Andrii Grynenko [Wed, 2 Dec 2015 01:23:48 +0000 (17:23 -0800)]
Properly init collectDone_

Summary: facepaw

Reviewed By: djwatson

Differential Revision: D2709030

fb-gh-sync-id: 20fe50bc70e97c4fedeae8fd271a4de70fa0e805

6 years agoAvoid requiring a suppression (unevaluated-expression)
Yedidya Feldblum [Tue, 1 Dec 2015 20:04:24 +0000 (12:04 -0800)]
Avoid requiring a suppression (unevaluated-expression)

Summary: [Folly] Avoid requiring a suppression (`unevaluated-expression`).

I am not a fan of suppressing warnings. This warning comes from the internals of gtest, but anyway.

Replace with something that does not trigger the warning.

Reviewed By: Gownta

Differential Revision: D2706279

fb-gh-sync-id: 2ecac1d230308a8fd21a69565193c52585526448

6 years agoEnable find/emplace for key types other than KeyT.
Daniel Andersson [Tue, 1 Dec 2015 15:46:00 +0000 (07:46 -0800)]
Enable find/emplace for key types other than KeyT.

Summary: Add template parameters to support arbitrary types when looking up a key.

This is useful to avoid the cost of 'materializing' a key which is likely to be present in the array (or, alternatively, switching on a tagged union KeyT).

Use the new capability to greatly simplify the lookup logic in HHVMs static string table.

Reviewed By: nbronson

Differential Revision: D2662451

fb-gh-sync-id: 707fa033f350b80ca8080af17f1a8a74c59f2e88

6 years agofolly/Conv.h: suppress -Wfloat-conversion warnings
Jim Meyering [Tue, 1 Dec 2015 15:27:54 +0000 (07:27 -0800)]
folly/Conv.h: suppress -Wfloat-conversion warnings

Summary: Using -Werror and -Wfloat-conversion was causing failure in code that is deliberately
performing that conversion.  Add explicit casts to avoid the warnings/errors.
Here are two of the diagnostics:

  ./folly/Conv.h: In instantiation of "typename std::enable_if<std::is_floating_point<_Tp>::value, Tgt>::type folly::to(folly::StringPiece) [with Tgt = float; typename std::enable_if<std::is_floating_point<_Tp>::value, Tgt>::type = float; folly::StringPiece = folly::Range<const char*>]":
  ./thrift/lib/cpp2/protocol/SimpleJSONProtocol.tcc:687:28:   required from "T apache::thrift::SimpleJSONProtocolReader::castIntegral(const string&) [with T = float; std::string = std::basic_fbstring<char>]"
  ./thrift/lib/cpp2/protocol/SimpleJSONProtocol.tcc:680:26:   required from "uint32_t apache::thrift::SimpleJSONProtocolReader::readJSONKey(T&) [with T = float; uint32_t = unsigned int]"
  ./thrift/lib/cpp2/protocol/SimpleJSONProtocol.tcc:654:16:   required from "uint32_t apache::thrift::SimpleJSONProtocolReader::readInContext(T&) [with T = float; uint32_t = unsigned int]"
  ./thrift/lib/cpp2/protocol/SimpleJSONProtocol.tcc:1035:34:   required from here
  ./folly/Conv.h:1222:31: error: conversion to "float" from "std::enable_if<true, double>::type {aka double}" may alter its value [-Werror=float-conversion]

  ./folly/Conv.h: In instantiation of "typename std::enable_if<((std::is_integral<_Tp2>::value && std::is_floating_point<_Tp>::value) || (std::is_floating_point<_DInputType>::value && std::is_integral<_Tp>::value)), Tgt>::type folly::to(const Src&) [with Tgt = long int; Src = double; typename std::enable_if<((std::is_integral<_Tp2>::value && std::is_floating_point<_Tp>::value) || (std::is_floating_point<_DInputType>::value && std::is_integral<_Tp>::value)), Tgt>::type = long int]":
  ./folly/dynamic-inl.h:636:51:   required from "T folly::dynamic::asImpl() const [with T = long int]"
  ./folly/dynamic-inl.h:384:64:   required from here
  ./folly/Conv.h:1245:16: error: conversion to "long int" from "double" may alter its value [-Werror=float-conversion]

This change deserves a little more explanation:

  -  return ceil((double(sizeof(IntegerType) * CHAR_BIT) * M_LN2) / M_LN10);
  +  return (unsigned int)(ceil(sizeof(IntegerType) * CHAR_BIT * M_LN2 / M_LN10));

In addition to adding the cast to destination type, I have also removed the unnecessary cast to double of the first product (there is not risk that it will overflow, and no need to promote to double, there), and I have also removed the grouping parentheses, since the standard left-to-right evaluation works just fine here.

Reviewed By: Gownta, yfeldblum

Differential Revision: D2703928

fb-gh-sync-id: 23b49281d9120d106f1fdbd30921e8f39c01367d

6 years agoCodeMod: Use the ExceptionWrapper::with_exception variant that deduces exception...
Yedidya Feldblum [Tue, 1 Dec 2015 06:54:20 +0000 (22:54 -0800)]
CodeMod: Use the ExceptionWrapper::with_exception variant that deduces exception types

Summary: CodeMod: Use the `ExceptionWrapper::with_exception` variant that deduces exception types.

Since we must specify the exception type in the lambda arg, and there is a variant of `ExceptionWrapper::with_exception` that deduces the type of the exception from the type of the lambda arg, we don't need to specify the exception type again as a template parameter anymore.

Reviewed By: meyering

Differential Revision: D2694895

fb-gh-sync-id: 505469f9008973a315e836f356e5db97df4ec921

6 years agofolly/Conv.h: estimateSpaceNeeded: avoid undefined behavior
Jim Meyering [Tue, 1 Dec 2015 05:17:20 +0000 (21:17 -0800)]
folly/Conv.h: estimateSpaceNeeded: avoid undefined behavior

Summary: Do not negate signed numbers like INT_MIN or INTMAX_MIN, since
that would evoke undefined behavior.  Otherwise, the test (below)
would fail with this run-time error:

  [ RUN      ] Conv.Integral2String
  folly/Conv.h:521:47: runtime error: negation of -2147483648 cannot be represented in type 'int'; cast to an unsigned type to negate this value to itself

Reviewed By: markisaa

Differential Revision: D2704195

fb-gh-sync-id: 4036437fb972109672004163880078127e7df797

6 years agoAvoid deprecated Singleton<T>::get() in folly/futures
Yedidya Feldblum [Mon, 30 Nov 2015 23:29:32 +0000 (15:29 -0800)]
Avoid deprecated Singleton<T>::get() in folly/futures

Summary: [Folly] Avoid deprecated `Singleton<T>::get()` in `folly/futures`.

Changes `getTimekeeperSingleton()` to return a `shared_ptr<Timekeeper>`, and patches its call-sites.

Additionally, have the call-sites keep the singleton instance alive for the duration that it is being directly used.

Reviewed By: ddrcoder

Differential Revision: D2702361

fb-gh-sync-id: 82b72ee514dc4f2a7f7522af8b2e92b34df063d6

6 years agoDeprecate get_weak()
Andrii Grynenko [Thu, 26 Nov 2015 20:33:16 +0000 (12:33 -0800)]
Deprecate get_weak()

Summary: get_weak() is always used with .lock(), but try_get() is actually more performant than get_weak().lock().
Using get_weak() to store a weak_ptr and keep locking is not safe in fork scenarios.

Reviewed By: yfeldblum

Differential Revision: D2694223

fb-gh-sync-id: 908d44293ffd9b3782152d43e28d5de172d1654a

6 years agoFix ExceptionWrapper::with_exception to support lvalue ref functors
Yedidya Feldblum [Thu, 26 Nov 2015 05:48:38 +0000 (21:48 -0800)]
Fix ExceptionWrapper::with_exception to support lvalue ref functors

Summary: [Folly] Fix `ExceptionWrapper::with_exception` to support lvalue ref functors.

Ex:

    auto handler = [&](const std::runtime_error& e) { handle_runtime_error(e); };
    exception_wrapper wrap = get_some_exception_wrapper_from_context();
    wrap.with_exception(handler); // broken before this diff, fixed after

Reviewed By: markisaa

Differential Revision: D2698680

fb-gh-sync-id: 4976ba08e3601e22891d00d79a5dae5118887b71

6 years agoNew ReadMostlySharedPtr implementation
Andrii Grynenko [Thu, 26 Nov 2015 04:03:55 +0000 (20:03 -0800)]
New ReadMostlySharedPtr implementation

Summary: This changes ReadMostlySharedPtr API to have 3 types: MainPtr, WeakPtr, SharedPtr. MainPtr and SharedPtr are equivalents of std::shared_ptr, and WeakPtr is an equivalent of std::weak_ptr. The only difference is that it can only be a single MainPtr, and while it's alive copying SharedPtr/WeakPtr or WeakPtr doesn't require atomic operations (and thus can be more performant than std::shared_ptr). Unlike original ReadMostlySharedPtr API, there're no thread-safety guarantees between reset() and getShared() for ReadMostlySharedPtr.

ReadMostlySharedPtr can work with different RefCount implementations. This diff introduces RCURefCount (which is currently using liburcu) and TLRefCount.

Reviewed By: djwatson

Differential Revision: D2683572

fb-gh-sync-id: a7a03af4b1cf5f81a613368c6eebe70b2eaef064

6 years agoFix proxygen OSS build
Mark Isaacson [Wed, 25 Nov 2015 22:25:16 +0000 (14:25 -0800)]
Fix proxygen OSS build

Summary: So far this is a speculative fix.. I am waiting to see the results from the Ubuntu machine, but I suspect this is the problem.

Reviewed By: bugok

Differential Revision: D2697600

fb-gh-sync-id: e33d452a52c5ace2b7401d6495621ce68a5076b5

6 years agoUpdate symlinks
Giuseppe Ottaviano [Wed, 25 Nov 2015 21:49:55 +0000 (13:49 -0800)]
Update symlinks

Summary: See D2696092

Reviewed By: meyering

Differential Revision: D2696360

fb-gh-sync-id: 2cfa9258be899a93a58c59dae70a10fef6f457c8

6 years agoSkip reallocation in shrink_to_fit for empty fbvector
Giuseppe Ottaviano [Wed, 25 Nov 2015 21:39:14 +0000 (13:39 -0800)]
Skip reallocation in shrink_to_fit for empty fbvector

Reviewed By: philippv

Differential Revision: D2696314

fb-gh-sync-id: 105de2d99bb9f5ac82310e37f9302022c103befc

6 years agoMake folly's T_CHECK_TIMEOUT/T_CHECK_TIME_LT use SKIP() on failure
Mark Isaacson [Wed, 25 Nov 2015 19:38:54 +0000 (11:38 -0800)]
Make folly's T_CHECK_TIMEOUT/T_CHECK_TIME_LT use SKIP() on failure

Summary: Make these more spurious failure-aware/tolerant.

Reviewed By: yfeldblum

Differential Revision: D2689775

fb-gh-sync-id: 1a9b247b97cc3529b12f6f7b76a4af2e32822d45

6 years agofolly/detail/ThreadLocalDetail.h: avoid UBSAN-detected memcpy abuse
Jim Meyering [Wed, 25 Nov 2015 19:06:24 +0000 (11:06 -0800)]
folly/detail/ThreadLocalDetail.h: avoid UBSAN-detected memcpy abuse

Summary: [technically, the existing code is probably a no-op on
all systems we care about, but since it is officially UB, switching
to a more strict platform could cause trouble, so it's worth fixing]

Calling memcpy with "nullptr" as 2nd argument is undefined, even when
the third argument is zero, and causes a failure when testing with an
UBSAN-enabled binary (-fsanitize=undefined).
Before this change, the buck-run test below would evoke this failure:

  [ RUN      ] ThreadLocalPtr.BasicDestructor
  folly/detail/ThreadLocalDetail.h:533:29: runtime error: null pointer passed as argument 2, which is declared to never be null
  third-party-buck/build/glibc/include/string.h:47:45: note: nonnull attribute specified here

Ironically, the failure of the target-determinator-buck_push_blocking test (due to an unrelated proxygen dep problem) would block me from landing this, so I am adding this line to override it.

Reviewed By: luciang, alexshap

Differential Revision: D2692625

fb-gh-sync-id: 8bdc5cd2899705f39c9565d640921de1f363807d

6 years agoRemove undefined behavior in goodMallocSize()
Adrien Conrath [Wed, 25 Nov 2015 16:34:09 +0000 (08:34 -0800)]
Remove undefined behavior in goodMallocSize()

Reviewed By: ot

Differential Revision: D2695741

fb-gh-sync-id: d33263c1ffa5651d66f37992ce365dae157ed449

6 years agoMake goodMallocSize always use nallocx
Giuseppe Ottaviano [Wed, 25 Nov 2015 08:26:31 +0000 (00:26 -0800)]
Make goodMallocSize always use nallocx

Summary: `goodMallocSize` is used extensively in `folly` data structures,
especially for containers optimized for small contents, such as
`fbstring` and `small_vector`.

However, it makes the design decision to align the allocation size to
a x86 cache line, forcing a minimum allocation size of `64` bytes,
despite jemalloc can provide smaller size classes (8, 16, 32,
48). This causes a large discontinuity between small contents that can
be inlined and heap-allocated contents:

- For `fbstring`, a string of 23 bytes (including terminator) occupies
  24 bytes (`sizeof(fbstring)`), a string of 24 bytes occupies 24 + 64
  + allocation overhead when it could be 24 + 32 + allocation
  overhead. The waste is more than 50%.

- For `small_vector<uint32_t, 1, uint32_t>`, for instance, a vector
  with 1 element occupies 12 bytes, a vector with 2 elements occupies
  12 + 64 + allocation overhead when it could be 12 + 8 + allocation
  overhead. The waste is more than 250%.

With this diff we just trust jemalloc and always use `nallocx`. If a
data structure need cache-line alignment it should be implemented at
its level.

Reviewed By: elsteveogrande

Differential Revision: D2688156

fb-gh-sync-id: 46548d4a91952e7c673d4f0997c4c067e03c190d

6 years agoA well-known instance of folly::Unit
Yedidya Feldblum [Wed, 25 Nov 2015 04:26:15 +0000 (20:26 -0800)]
A well-known instance of folly::Unit

Summary: [Folly] A well-known instance of `folly::Unit`.

Reviewed By: jsedgwick

Differential Revision: D2693426

fb-gh-sync-id: fe595abc852500275a827b4f400cfda7091a55f7

6 years agoExceptionWrapper::with_exception variant, deducing exception type from functor type
Yedidya Feldblum [Wed, 25 Nov 2015 02:27:46 +0000 (18:27 -0800)]
ExceptionWrapper::with_exception variant, deducing exception type from functor type

Summary: [Folly] `ExceptionWrapper::with_exception` variant deducing exception type from functor type.

From the `README.md`:

    // TODO(jsedgwick) infer exception type from the type of the function

Reviewed By: Gownta

Differential Revision: D2685579

fb-gh-sync-id: 158bcd030019f28b4f392d17167ab0bd67cc1edb

6 years agoAdd sdallocx for jemalloc
Qinfan Wu [Tue, 24 Nov 2015 18:02:25 +0000 (10:02 -0800)]
Add sdallocx for jemalloc

Summary: Make jemalloc's sdallocx function available in folly and allow future code to
use sized deallocation.

Reviewed By: elsteveogrande, ot

Differential Revision: D2687746

fb-gh-sync-id: 9ef8a16d2c63c4eb8cecab7639add423ef093c2d

6 years agoFix Build: folly/test/ThreadLocalTest
Yedidya Feldblum [Tue, 24 Nov 2015 02:45:15 +0000 (18:45 -0800)]
Fix Build: folly/test/ThreadLocalTest

Summary: [Folly] Fix Build: `folly/test/ThreadLocalTest`.

The problem is that the to the shared lib used in the test is hardcoded. It's a relative path, so that's nice, but it has too many assumptions about where the build system deposits test binaries.

Instead, let the code simply look for the shared lib in the same directory as the test binary. This makes fewer assumptions on the build system.

Reviewed By: andriigrynenko

Differential Revision: D2689047

fb-gh-sync-id: 648da079b7af5bc9893709d009e577347dd4a463

6 years agoFix HHWheelTimerTest.AtMostEveryN
Haijun Zhu [Mon, 23 Nov 2015 22:25:47 +0000 (14:25 -0800)]
Fix HHWheelTimerTest.AtMostEveryN

Summary: If it takes too long to finish it is probably heavily loaded. Skip if
so.

Reviewed By: mmcduff

Differential Revision: D2687346

fb-gh-sync-id: 282cac7e4df361f714de1089f11b098af79b1512

6 years agoSimplify an exception handler
Chad Parry [Mon, 23 Nov 2015 20:00:54 +0000 (12:00 -0800)]
Simplify an exception handler

Summary: It looks like there is no reason to catch a `std::exception` and then `dynamic_cast` to a derived exception. We can just catch the derived exception directly.

whatcouldgowrong4

Reviewed By: yfeldblum

Differential Revision: D2677284

fb-gh-sync-id: 0149e4d4aecc96257376d410f592620205441f66

6 years agoBenchmark std::to_string vs. folly::to<std::string>
Michael Oliver [Mon, 23 Nov 2015 19:50:20 +0000 (11:50 -0800)]
Benchmark std::to_string vs. folly::to<std::string>

Summary: We wanted to compare the benefits of using folly::to<std::string> directly, and decided this benchmarking might as well be contributed back to folly.  Here are the results:

```
  ============================================================================
  folly/test/ConvTest.cpp                         relative  time/iter  iters/s
  ============================================================================
  ...
  ----------------------------------------------------------------------------
  u64ToStringClibMeasure(1)                                  157.88ns    6.33M
  u64ToStringFollyMeasure(1)                       212.52%    74.29ns   13.46M
  ----------------------------------------------------------------------------
  u64ToStringClibMeasure(12)                                 158.14ns    6.32M
  u64ToStringFollyMeasure(12)                      212.55%    74.40ns   13.44M
  ----------------------------------------------------------------------------
  u64ToStringClibMeasure(123)                                152.26ns    6.57M
  u64ToStringFollyMeasure(123)                     210.95%    72.18ns   13.85M
  ----------------------------------------------------------------------------
  u64ToStringClibMeasure(1234)                               152.40ns    6.56M
  u64ToStringFollyMeasure(1234)                    204.82%    74.41ns   13.44M
  ----------------------------------------------------------------------------
  u64ToStringClibMeasure(12345)                              156.01ns    6.41M
  u64ToStringFollyMeasure(12345)                   208.90%    74.68ns   13.39M
  ----------------------------------------------------------------------------
  u64ToStringClibMeasure(123456)                             178.21ns    5.61M
  u64ToStringFollyMeasure(123456)                  238.77%    74.63ns   13.40M
  ----------------------------------------------------------------------------
  u64ToStringClibMeasure(1234567)                            161.38ns    6.20M
  u64ToStringFollyMeasure(1234567)                 203.09%    79.46ns   12.58M
  ----------------------------------------------------------------------------
  u64ToStringClibMeasure(12345678)                           159.01ns    6.29M
  u64ToStringFollyMeasure(12345678)                192.42%    82.64ns   12.10M
  ----------------------------------------------------------------------------
  u64ToStringClibMeasure(123456789)                          169.07ns    5.91M
  u64ToStringFollyMeasure(123456789)               203.70%    83.00ns   12.05M
  ----------------------------------------------------------------------------
  u64ToStringClibMeasure(1234567890)                         166.56ns    6.00M
  u64ToStringFollyMeasure(1234567890)              187.21%    88.97ns   11.24M
  ----------------------------------------------------------------------------
  u64ToStringClibMeasure(12345678901)                        176.22ns    5.67M
  u64ToStringFollyMeasure(12345678901)             197.57%    89.20ns   11.21M
  ----------------------------------------------------------------------------
  u64ToStringClibMeasure(123456789012)                       174.74ns    5.72M
  u64ToStringFollyMeasure(123456789012)            182.52%    95.74ns   10.45M
  ----------------------------------------------------------------------------
  u64ToStringClibMeasure(1234567890123)                      178.47ns    5.60M
  u64ToStringFollyMeasure(1234567890123)           186.81%    95.54ns   10.47M
  ----------------------------------------------------------------------------
  u64ToStringClibMeasure(12345678901234)                     187.33ns    5.34M
  u64ToStringFollyMeasure(12345678901234)          184.05%   101.78ns    9.82M
  ----------------------------------------------------------------------------
  u64ToStringClibMeasure(123456789012345)                    198.16ns    5.05M
  u64ToStringFollyMeasure(123456789012345)         182.48%   108.59ns    9.21M
  ----------------------------------------------------------------------------
  u64ToStringClibMeasure(1234567890123456)                   182.56ns    5.48M
  u64ToStringFollyMeasure(1234567890123456)        164.19%   111.19ns    8.99M
  ----------------------------------------------------------------------------
  u64ToStringClibMeasure(12345678901234567)                  186.80ns    5.35M
  u64ToStringFollyMeasure(12345678901234567)       162.33%   115.07ns    8.69M
  ----------------------------------------------------------------------------
  u64ToStringClibMeasure(123456789012345678)                 195.08ns    5.13M
  u64ToStringFollyMeasure(123456789012345678)      165.51%   117.87ns    8.48M
  ----------------------------------------------------------------------------
  u64ToStringClibMeasure(1234567890123456789)                200.27ns    4.99M
  u64ToStringFollyMeasure(1234567890123456789)     164.62%   121.65ns    8.22M
  ----------------------------------------------------------------------------
  u64ToStringClibMeasure(12345678901234567890U)              203.18ns    4.92M
  u64ToStringFollyMeasure(12345678901234567890U)   162.38%   125.13ns    7.99M
  ----------------------------------------------------------------------------
  ...
  ============================================================================
```

Reviewed By: yfeldblum

Differential Revision: D2680998

fb-gh-sync-id: 179fbd756c714c92b7230d3750af024865ecbb0a

6 years agoAdd missing header files to folly-automake
Sara Golemon [Mon, 23 Nov 2015 19:40:18 +0000 (11:40 -0800)]
Add missing header files to folly-automake

Summary: Closes https://github.com/facebook/folly/issues/304

Reviewed By: yfeldblum

Differential Revision: D2685289

fb-gh-sync-id: 563697fb01ee358472b9080058848d2b557ba466

6 years agoFix minor typo in folly/Conv.h
Sara Golemon [Mon, 23 Nov 2015 19:39:26 +0000 (11:39 -0800)]
Fix minor typo in folly/Conv.h

Summary: 'flating point' => 'floating point'

Closes https://github.com/facebook/folly/issues/241

Reviewed By: yfeldblum

Differential Revision: D2685280

fb-gh-sync-id: 2e9c2b5419d0c631af4a32c5ab6b7366033ca34c

6 years agoFix folly/ThreadLocal with clang after PthreadKeyUnregister change
Andrii Grynenko [Sat, 21 Nov 2015 05:25:11 +0000 (21:25 -0800)]
Fix folly/ThreadLocal with clang after PthreadKeyUnregister change

Summary: Make PthreadKeyUnregister a global singleton for all StaticMeta instances.

Reviewed By: luciang

Differential Revision: D2678401

fb-gh-sync-id: c0d58b483fa6f096b29aeb7df71897a75ea8c892

6 years agoRevert "Fix folly::ThreadLocal to work in a shared library"
Lucian Grijincu [Fri, 20 Nov 2015 20:03:52 +0000 (12:03 -0800)]
Revert "Fix folly::ThreadLocal to work in a shared library"

Summary: This reverts commit 9d355f96c4fe073c889f28070d5952fa4eb007a0.

build-break

Reviewed By: sctaylor

Differential Revision: D2680784

fb-gh-sync-id: a2df233bcd3721ffbfcd6fd71ea3cabbebebd4c4

6 years agoget back AsyncServerSocket's setMaxNumMessagesInQueue
Haijun Zhu [Fri, 20 Nov 2015 18:20:54 +0000 (10:20 -0800)]
get back AsyncServerSocket's setMaxNumMessagesInQueue

Summary: D2613681 codemod'ed a thrift server api but it also renamed that api
in AsyncServerSocket, which is a mistake. Fix that in AsyncServerSocket, and
all other places that calls this api.

Reviewed By: alandau, JoelMarcey

Differential Revision: D2677837

fb-gh-sync-id: 0d91f1a623229e99be59ca9dcd27f1330a9a1b64

6 years agoRemove unnecessary resetClientHelloParsing callback in AsyncSSLSocket
Anirudh Ramachandran [Fri, 20 Nov 2015 16:35:40 +0000 (08:35 -0800)]
Remove unnecessary resetClientHelloParsing callback in AsyncSSLSocket

Summary: Remove unnecessary resetClientHelloParsing callback which causes problems wiht
Openssl 1.0.2

Reviewed By: knekritz

Differential Revision: D2664730

fb-gh-sync-id: d1b55ae493b4c92627ad41e7bf85f1e1a777bd2b

6 years agoRemove busy wait and support multiple wait
Alexander Shaposhnikov [Fri, 20 Nov 2015 04:12:41 +0000 (20:12 -0800)]
Remove busy wait and support multiple wait

Summary: Remove busy wait from Future::wait.
If future.wait(timeout) has succeded we should
return a ready future, otherwise we should return a future
for the final result (not necessarily ready).

Reviewed By: yfeldblum

Differential Revision: D2646860

fb-gh-sync-id: 62671d09073ad86e84df8c9257e961d2a8c2a339

6 years agoExpose move result from LockFreeRingBuffer::Cursor
Delyan Kratunov [Fri, 20 Nov 2015 00:25:09 +0000 (16:25 -0800)]
Expose move result from LockFreeRingBuffer::Cursor

Summary: Without feedback that a `moveForward` or `moveBackward` did nothing, a user
cannot implement this idiom safely:

```
while(rb.tryRead(entry, cursor)) {
  doSomething(entry);
  cursor.moveBackward();
}
```

In the case where the ring buffer has not overflowed and slot 0 is still
available, the reader will get stuck at it (read it continuously) until
the buffer overflows.

This diff allows the above example to become:

```
while(rb.tryRead(entry, cursor)) {
  doSomething(etnry);
  if (!cursor.moveBackward()) {
    break;
  }
}
```

Reviewed By: bryceredd

Differential Revision: D2674975

fb-gh-sync-id: a0c5857daf186ef19e203f90acc2145590f85c3b

6 years agoFix usingJEMalloc with Clang
Giuseppe Ottaviano [Thu, 19 Nov 2015 19:39:27 +0000 (11:39 -0800)]
Fix usingJEMalloc with Clang

Summary: Clang is too clever and in some contexts optimizes away the `malloc`, but we rely on a side-effect. Declaring the variable as static forces it to call `malloc`. We could free the pointer relying on the fact that the lambda is guaranteed to be called only once, but I feel more comfortable just leaking it (LSan won't complain).

Reviewed By: philippv

Differential Revision: D2674769

fb-gh-sync-id: 1153a3ca226c6b7aa64c453bd61b036dcbf3ffcc

6 years agoFix invalid DCHECK
Anton Likhtarov [Thu, 19 Nov 2015 19:32:26 +0000 (11:32 -0800)]
Fix invalid DCHECK

Summary: There's a race between insert() and erase(): as soon as insert()
releases the lock (swaps kLockedKey_ with the actual key), an erase() might
jump in and invalidate the key.

As far as I can tell, this bug existed since the beginning.

Reviewed By: nbronson

Differential Revision: D2673099

fb-gh-sync-id: 4721893d2ad4836e11acc0fb4ecb0dd7b2b69be1

6 years agoFix folly::ThreadLocal to work in a shared library
Andrii Grynenko [Thu, 19 Nov 2015 19:30:20 +0000 (11:30 -0800)]
Fix folly::ThreadLocal to work in a shared library

Reviewed By: bmaurer

Differential Revision: D2667499

fb-gh-sync-id: 463f86752240bd88761910de934ba25d6e62fafe

6 years agoAdd getUnderlyingTransport to AsyncTransportWrapper.
Kyle Nekritz [Wed, 18 Nov 2015 18:20:14 +0000 (10:20 -0800)]
Add getUnderlyingTransport to AsyncTransportWrapper.

Summary: Inspired by getSocketFromTransport from proxygen.

Reviewed By: siyengar

Differential Revision: D2663937

fb-gh-sync-id: f076215907cd06d6da3de033c57eec8a6a6ce320

6 years agoUTF8StringPiece, wrapping boost::u8_to_u32
Tom Jackson [Tue, 17 Nov 2015 19:01:28 +0000 (11:01 -0800)]
UTF8StringPiece, wrapping boost::u8_to_u32

Summary: For handling UTF8 strings better.

Reviewed By: yfeldblum

Differential Revision: D1956771

fb-gh-sync-id: e074f9f2c9b472f5e619fef25d8e17296847773c

6 years agoSimplify some checks by using __CLANG_PREREQ
Yedidya Feldblum [Mon, 16 Nov 2015 10:07:19 +0000 (02:07 -0800)]
Simplify some checks by using __CLANG_PREREQ

Summary: [Folly] Simplify some checks by using `__CLANG_PREREQ`.

Reviewed By: meyering

Differential Revision: D2657979

fb-gh-sync-id: 80ff56bbab0e78465d71315b729ad14a09706ed5

6 years agoSimplify some checks by using __CLANG_PREREQ
Yedidya Feldblum [Sun, 15 Nov 2015 19:52:43 +0000 (11:52 -0800)]
Simplify some checks by using __CLANG_PREREQ

Summary: [Folly] Simplify some checks by using `__CLANG_PREREQ`.

Reviewed By: meyering

Differential Revision: D2656842

fb-gh-sync-id: e762fba423fb7cc08907d10dc10f8f46d93a9fe4

6 years agoMake collect work for types with no default constructors
Andrii Grynenko [Fri, 13 Nov 2015 21:47:27 +0000 (13:47 -0800)]
Make collect work for types with no default constructors

Summary: This doesn't make the code less efficient, because RVO can't be used for CollectVariadicContext. Thus moving existing tuple vs constructing new tuple by moving all values from other tuple (where each value is wrapped in folly::Optional) should be pretty much the same.

Reviewed By: hannesr

Differential Revision: D2650293

fb-gh-sync-id: 648a358bf093a0bb9d058a997af9bf59014ad77c

6 years agoAllow a AsyncSocket to be corked the whole time
Yang Chi [Thu, 12 Nov 2015 23:34:45 +0000 (15:34 -0800)]
Allow a AsyncSocket to be corked the whole time

Summary: Add a new method to cork a socket in a persistent manner, instead of the current on-off manner. This is default to false. The liger part of turning this on will be in a separate diff.

I thought about whether I need to turn cork off based on some criteria to alleviate the perf degradation. The obvious things I can think off is just amount of data written as a threshold, or a timeout. But TCP is doing this already for us, unless we want the data threshold to be less than MSS, or we want the timeout to be less than 200ms. THoughts?

Reviewed By: shikong

Differential Revision: D2639260

fb-gh-sync-id: 2821f669c9f72d5ac4c33195bb192fc4110ffe9d

6 years agoMake Malloc.h self-contained
Giuseppe Ottaviano [Thu, 12 Nov 2015 16:37:48 +0000 (08:37 -0800)]
Make Malloc.h self-contained

Reviewed By: philippv

Differential Revision: D2643313

fb-gh-sync-id: 10b9f735725ce47fab4bbfaa5972b3863357365f

6 years agoDisable assertions in FBString when used as std::string
Giuseppe Ottaviano [Thu, 12 Nov 2015 16:37:14 +0000 (08:37 -0800)]
Disable assertions in FBString when used as std::string

Reviewed By: Gownta

Differential Revision: D2643850

fb-gh-sync-id: 2c4bb844ea2006215b0637cb1ba08c636faefe05

6 years agoRestore the definition of HHWheelTimer::UniquePtr
Chad Parry [Thu, 12 Nov 2015 00:02:35 +0000 (16:02 -0800)]
Restore the definition of HHWheelTimer::UniquePtr

Summary: Changing the definition of `HHWheelTimer::UniquePtr` wasn't safe, because some clients were using that type outside of the `HHWheelTimer::newTimer` helper. I'm changing that part back. We'll still be able to proceed with my other codemod to `HHWheelTimer`, but we'll always have two different smart pointer types to manage: `UniquePtr` and `IntrusivePtr`.

Reviewed By: djwatson

Differential Revision: D2644721

fb-gh-sync-id: 14685be62355f09d39c4139ef7186d60b5f48dcd

6 years agoMake Optional nothrow_default_constructible
Stepan Palamarchuk [Wed, 11 Nov 2015 22:22:01 +0000 (14:22 -0800)]
Make Optional nothrow_default_constructible

Summary: It never throws.

Reviewed By: pavlo-fb

Differential Revision: D2640886

fb-gh-sync-id: cd643f8847f4bf5619415731484f91fb07116784

6 years agoAdd a buffer callback to AsyncSocket
Yang Chi [Wed, 11 Nov 2015 21:32:04 +0000 (13:32 -0800)]
Add a buffer callback to AsyncSocket

Summary: This is probably easier than D2612490. The idea is just to add a callback to write, writev and writeChain in AsyncSocket, so upper layer can know when data starts to buffer up

Reviewed By: mzlee

Differential Revision: D2623385

fb-gh-sync-id: 98d32ca83871aaa4f6c75a769b5f1bf0b5d62c3e

6 years agoImprove the DelayedDestruction smart pointer
Chad Parry [Wed, 11 Nov 2015 19:29:28 +0000 (11:29 -0800)]
Improve the DelayedDestruction smart pointer

Summary: There have been several ASAN bugs cropping up because the lifetime of an `HHWheelTimer` is not being manager properly. I think that people are too comfortable passing around a raw `HHWheelTimer*` even when it is difficult to prove correctness. The recommended solution used to be to create a `DestructorGuard` every time it was needed. There is enough friction there that not everyone is doing that like they should. We should make resource management easier---as easy as using raw pointers, in fact.

I've fixed the broken copy semantics of `DestructorGuard` and added the operators that allow it to be used as a smart pointer. I added the `IntrusivePtr` helper that can manage an arbitrary derived class of `DelayedDestructionBase`. Now, clients can all safely pass around an `IntrusivePtr` everywhere they used to use a raw pointer. They will get automatic resource management for free.

If you are not convinced that `DestructorGuard` should be changed, then note that the existing behavior is dangerously buggy. Consider the following innocent code that accidentally uses the implicitly-defined copy constructor:
  auto d = DestructorGuard(p);
This results in undefined behavior, because `p` can be accessed after it is destroyed! The bug happens because the default copy constructor copies the raw pointer but doesn't increment the count.

In a separate diff, I'll change all clients who pass around a raw `HHWheelTimer*` to use an `IntrusivePtr<HHWheelTimer>` instead. Then we can even entertain a long-term plan of switching from intrusive pointers to the standard `shared_ptr`.

Reviewed By: djwatson

Differential Revision: D2627941

fb-gh-sync-id: 58a934d64540d0bbab334adc4f23d31d507692da

6 years agoUNSYNCHRONIZED does NOT unlock the mutex
Shaft Wu [Wed, 11 Nov 2015 04:40:17 +0000 (20:40 -0800)]
UNSYNCHRONIZED does NOT unlock the mutex

Summary: My colleague tuomaspelkonen discovered a weird UNSYNCHRONIZED issue a few weeks ago and we ever since stopped using it. Now I finally have some time to root cause it. It turns out UNSYNCHRONIZED unlock the mutex then lock the mutex again, because it copy constructs LockedPtr for overriding the name within the scope, and copy construct locks the mutex again. A one character fix here is to take a reference of LockedPtr instead of copy construct it. However since this is my first time look at the code here, please advise if this is horribly wrong or propose better fix. Also added a test to reproduce the issue without the fix as well as verify the fix.

Reviewed By: yfeldblum

Differential Revision: D2633028

fb-gh-sync-id: a9e8d39b08d4d1265979f8bdaae83619566d10a0

6 years agoRevert my change (which broke down the cont build)
Alexander Shaposhnikov [Wed, 11 Nov 2015 03:11:56 +0000 (19:11 -0800)]
Revert my change (which broke down the cont build)

Summary: hg backout -r c9f7b5f3185a
Revert my change (which broke down the cont build t9048692)

Reviewed By: djwatson

Differential Revision: D2640797

fb-gh-sync-id: 51f196ac5a3560fde4dc8fe7bb6ef278d74136e5

6 years agoRemove busy wait
Alexander Shaposhnikov [Tue, 10 Nov 2015 23:42:18 +0000 (15:42 -0800)]
Remove busy wait

Summary: Wait uses baton & callback running baton.post
when the original future is ready. However wrapping baton.post
with a funciton call (preparing a new value) adds the following race: baton.wait wakes up
before that function has call actually finished.
The explanation is the following: to prepare the value of the new future it's necessary
1. baton.post() 2. set the value (move constructor, memory operations, ...)
and this code is executed in a separate thread.
The main idea of this fix is to avoid creating a new future
(whose value is calculated using that 2-step procedure)
and set a callback instead. This callback will be executed when the future is ready and actually
it either will be the last step of promise.setValue or it will run immediately if the future
we are waiting for already contains a value.

Reviewed By: fugalh

Differential Revision: D2636409

fb-gh-sync-id: df3e9bbcc56a5fac5834ffecc63f1bcb94ace02c

6 years agochange log error back to check when pipe read fail in notification queue
Shijin Kong [Tue, 10 Nov 2015 23:06:52 +0000 (15:06 -0800)]
change log error back to check when pipe read fail in notification queue

Summary: Revert D2632752

Reviewed By: afrind

Differential Revision: D2638974

fb-gh-sync-id: 43d8135421510db7840a99f3c197119a0fd26c09

6 years agoFBString: fix constructors so it compiles with newer Clang's
Steve O'Brien [Tue, 10 Nov 2015 14:28:14 +0000 (06:28 -0800)]
FBString: fix constructors so it compiles with newer Clang's

Summary: Using Clang 3.7, this minimal test program:

  #include <folly/FBString.h>
  struct S { folly::basic_fbstring<char> x; };
  int main(int argc, char *argv[]) {
    S s {};
    return 0;
  }

... breaks with the following error output:

  FBStringTest.cpp:5:8: error: chosen constructor is explicit in copy-initialization
    S s {};
         ^
  ./folly/FBString.h:1009:12: note: constructor declared here
    explicit basic_fbstring(const A& = A()) noexcept {
             ^
  FBStringTest.cpp:3:40: note: in implicit initialization of field 'x' with omitted initializer
  struct S { folly::basic_fbstring<char> x; };

... because this `basic_fbstring` is used in a struct and the struct is being default-constructed.

This patch splits the (nevertheless-correct) one-default-arg constructor into two, to deconfuse clang and have it select the correct constructor to avoid this issue.

Reviewed By: matbd

Differential Revision: D2632953

fb-gh-sync-id: 2c75ae85732678c31543f5cccc73d58317982f07

6 years agochange CHECK to LOG(ERROR) when pipe read error on NotificationQueue
Shijin Kong [Mon, 9 Nov 2015 21:09:58 +0000 (13:09 -0800)]
change CHECK to LOG(ERROR) when pipe read error on NotificationQueue

Summary: The check was originally added to track down cpu pinning issue. In reality the check did get triggered, so change to soft error to calm the crazy nag bot. what's wrong with you.
We need to see the soft error log to find out what the error is.

Reviewed By: mzlee

Differential Revision: D2632752

fb-gh-sync-id: 87c11b186f97f0eb4a6c5ac13a1117b280198673

6 years agoAdd a tag type for in-place construction
Alexey Spiridonov [Mon, 9 Nov 2015 19:16:11 +0000 (11:16 -0800)]
Add a tag type for in-place construction

Summary: Without this tag type, it's impossible to use `Synchronized` with types like:

```
struct A {
  A(int, const char*);
  A(const A&) = delete;
  A& operator=(const A&) = delete;
  A(A&&) = delete;
  A& operator=(A&&) = delete;
};
```

In-place construction solves this problem. Usage:

```
Synchronized a(construct_in_place, 5, "c");
```

Reviewed By: nbronson

Differential Revision: D2610071

fb-gh-sync-id: 251fe8f8f6a2d7484dda64cf04dcacb998145230

6 years agoMake sure there are no outstanding timers in the destructor
Chad Parry [Mon, 9 Nov 2015 18:39:22 +0000 (10:39 -0800)]
Make sure there are no outstanding timers in the destructor

Summary: This is the solution for our `HHWheelTimer` crashes, as suggested in D2617966#25.

djwatson also mentioned that maybe there should be some logging in the destructor if there are outstanding callbacks. I couldn't think of anything that would add to the `assert` that already exists in `destroy`. I'm open to suggestions though.

Reviewed By: djwatson

Differential Revision: D2628154

fb-gh-sync-id: f3db6e9384517c9bf3cbb60af8c1e711703a07fa

6 years agofolly/portability: add clang version check
Igor Sugak [Sun, 8 Nov 2015 08:32:52 +0000 (00:32 -0800)]
folly/portability: add clang version check

Summary: Define `__CLANG_PREREQ` macro to check version of clang.

Reviewed By: yfeldblum

Differential Revision: D2630325

fb-gh-sync-id: 3d666e554e8ddfc2c1fecd439aaf93f015829025

6 years agoCallbacks should ref the HHWheelTimer
Chad Parry [Fri, 6 Nov 2015 19:52:10 +0000 (11:52 -0800)]
Callbacks should ref the HHWheelTimer

Summary: Callbacks sometimes outlive the `HHWheelTimer` that they reference. Then the `Callback` tries to reference the dead `HHWheelTimer` and it could either misbehave or crash. This was caught reliably by ASAN tests.

Since `HHWheelTimer` already supports intrusive ref counting, the solution is to acquire a reference within the `Callback`.

Reviewed By: djwatson

Differential Revision: D2617966

fb-gh-sync-id: 02be9ffc5851c269d5933288a17ad394b33ac2dd

6 years agoCancel timeout only if not run
Jon Maltiel Swenson [Fri, 6 Nov 2015 02:54:38 +0000 (18:54 -0800)]
Cancel timeout only if not run

Summary: Do not cancel TimeoutHandler timeout if the timeout has already run.

Reviewed By: spalamarchuk

Differential Revision: D2622280

fb-gh-sync-id: 27d83b44ab225e2859695f4e5f21cef934824a35

6 years agocodemod setMaxNumMessagesInQueue to setMaxNumPendingConnectionsPerWorker
Haijun Zhu [Thu, 5 Nov 2015 18:36:46 +0000 (10:36 -0800)]
codemod setMaxNumMessagesInQueue to setMaxNumPendingConnectionsPerWorker

Summary: This name's meaning is very obscure, this method actually sets the
queue size of each acceptor and if all acceptor's queues are full the
connection will be dropped. Change it to a more meaningful name.

Reviewed By: alandau

Differential Revision: D2613681

fb-gh-sync-id: baa374cdf0a87c460df3dd5687e3d755b55f4b4f

6 years agoActivate server timeout after write success
Jon Maltiel Swenson [Thu, 5 Nov 2015 15:24:16 +0000 (07:24 -0800)]
Activate server timeout after write success

Summary: Start server timeout after socket write succeeds in mcrouter.  Add neceessary Fibers logic to enable this behavior.

Reviewed By: pavlo-fb

Differential Revision: D2613344

fb-gh-sync-id: 1bc0fbe8b325a3e91cd010f89104b83ebf183679

6 years agoSet interrupt handler correctly on SharedPromise
Viswanath Sivakumar [Thu, 5 Nov 2015 11:48:26 +0000 (03:48 -0800)]
Set interrupt handler correctly on SharedPromise

Summary: If SharedPromise::getFuture() is invoked after a call to setInterruptHandler,
then the interrupt handler isn't set on the newly created promise. This diff
fixes that.

Reviewed By: yfeldblum

Differential Revision: D2610289

fb-gh-sync-id: bf8fce9e881b83ccac17d13c6788ec2afd0b0153

6 years agoAdd ability to get application protocol from AsyncTransportWrapper
Subodh Iyengar [Wed, 4 Nov 2015 05:38:07 +0000 (21:38 -0800)]
Add ability to get application protocol from AsyncTransportWrapper

Summary: Allows AsyncTransportWrapper's to supply the underlying application
protocol being used, for example using NPN for SSL or some other
generic protocol indication mechanism.

Reviewed By: ranjeeth

Differential Revision: D2614179

fb-gh-sync-id: 2079782bb7d44f898fb14c7df15077209b022424

6 years agoswitch the IndexType and Allocator in AtomicUnorderedInsertMap template
Zhen (Growth) Li [Wed, 4 Nov 2015 01:31:55 +0000 (17:31 -0800)]
switch the IndexType and Allocator in AtomicUnorderedInsertMap template

Summary: Our use case need to have the IndexType Configurable, but in this diff D2583752, lint error shows
  Code from folly::detail is logically private, please avoid use outside of folly.
In order to fix this lint error, I switch the IndexType and Allocator in the AtomicUnorderedInsertMap template.

Reviewed By: nbronson

Differential Revision: D2610921

fb-gh-sync-id: ae81b41e7c8c971f26c61b8c67dabeadff379584

6 years agoAllow underlying transport to be accessible from AsyncTransportWrapper
Subodh Iyengar [Wed, 4 Nov 2015 01:19:40 +0000 (17:19 -0800)]
Allow underlying transport to be accessible from AsyncTransportWrapper

Summary: Allow underlying transport to be accessible from AsyncTransportWrapper.
There are some code paths where we need access to the real transport from
the AsyncTransportWrapper. This allows us to retrieve the underlying transport
and have clients like HTTPSession use it.

Reviewed By: afrind

Differential Revision: D2609200

fb-gh-sync-id: 2b317d1825a005bb64468f83c64bc3f1c9bdfe2c

6 years agomake folly slightly more c++-modules friendly
Louis Brandy [Wed, 4 Nov 2015 01:13:54 +0000 (17:13 -0800)]
make folly slightly more c++-modules friendly

Summary: .. by including what you use.

These are a handful of headers that are depended on via viral inclusion. Include them directly.

Reviewed By: yfeldblum

Differential Revision: D2601172

fb-gh-sync-id: 215e87263325d085fbcb651f83f429f47d14fc1b

6 years agoqualify std::move in StringKeyedTest
Louis Brandy [Tue, 3 Nov 2015 20:30:27 +0000 (12:30 -0800)]
qualify std::move in StringKeyedTest

Summary: Prefer `std::move` to `move` for clarity's sake.

Reviewed By: yfeldblum

Differential Revision: D2601138

fb-gh-sync-id: 64a9e58a83da9c8416a0be5c45a292c6b1342664

6 years agoDisallow nullptr literal in StringPiece constructor
Anton Likhtarov [Tue, 3 Nov 2015 18:45:21 +0000 (10:45 -0800)]
Disallow nullptr literal in StringPiece constructor

Summary: This disallows implicitly constructing a StringPiece from a literal nullptr at compile time
(without this change, nullptr would cause a segfault in strlen()).

Reviewed By: meyering, andriigrynenko

Differential Revision: D2603597

fb-gh-sync-id: cafbc45945bacc72a7c89310b99aa62d19a3ff9f

6 years agoFix multi-line comment warnings
Andrew Gallagher [Sat, 31 Oct 2015 21:42:53 +0000 (14:42 -0700)]
Fix multi-line comment warnings

Summary: Fix warnings triggered by `-Wcomment`.

Reviewed By: ajtulloch

Differential Revision: D2603992

fb-gh-sync-id: aae721f7c236d7d8b8bff2c077a481fc2affcf71

6 years agoAdd writeAndGetCursor to LockFreeRingBuffer
Delyan Kratunov [Fri, 30 Oct 2015 21:59:54 +0000 (14:59 -0700)]
Add writeAndGetCursor to LockFreeRingBuffer

Summary: New API - write an entry and advance a cursor to the just-written slot.

Reviewed By: ritzau, yfeldblum

Differential Revision: D2599388

fb-gh-sync-id: f3ebfae97de0341f01ffc80ab10221313d02087c