folly.git
6 years agoSet the appropriate AsyncSocketExceptionType from SSLException
Neel Goyal [Mon, 2 May 2016 16:30:18 +0000 (09:30 -0700)]
Set the appropriate AsyncSocketExceptionType from SSLException

Summary:
We should use the appropriate AsyncSocketExceptionType when firing SSLExceptions.  This derives the type from the constructor args, and introduces a `NETWORK_ERROR` exception type.  Open to suggestions on reusing something there.

Additionally, rename getType() to getSSLError() to prevent hiding the AsyncSocketException::getType.

Reviewed By: siyengar

Differential Revision: D3241204

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

6 years agoSimplify CryptoSSLRequestManager using unified futures API
Anirudh Ramachandran [Mon, 2 May 2016 16:12:01 +0000 (09:12 -0700)]
Simplify CryptoSSLRequestManager using unified futures API

Summary:
This diff unifies the nearly-replicated call paths for ECDSA and RSA
sign request/response/fallback/etc. into one generic futures-based sign API.
Once this is tested, we should be able to remove ~500 lines of code from
CryptoSSLRequestManager

Depends on D3094660

Reviewed By: siyengar

Differential Revision: D3116535

fb-gh-sync-id: 11c50d81dab39e5426679b5a172c1ece2039fc10
fbshipit-source-id: 11c50d81dab39e5426679b5a172c1ece2039fc10

6 years agoAvoid glog when warning about Singleton double-registration
Yedidya Feldblum [Mon, 2 May 2016 09:26:07 +0000 (02:26 -0700)]
Avoid glog when warning about Singleton double-registration

Summary:
[Folly] Avoid glog when warning about `Singleton` double-registration.

Because registration happens at static initialization time, long before main, and possibly long before glog is initialized.

This makes a difference because, in some cases of double-registration, we can get a SIGSEGV with no message, because we are attempting to `LOG(FATAL)` when glog is uninitialized. What we would much rather have is a SIGABRT with a message.

Reviewed By: meyering

Differential Revision: D3245047

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

6 years agoDon't require folly::Unit in addTaskFinally
Andrii Grynenko [Sat, 30 Apr 2016 00:50:50 +0000 (17:50 -0700)]
Don't require folly::Unit in addTaskFinally

Summary:
In D3241498 addTaskFuture was fixed to work with function returning void. This however changed addTaskFinally API to not allow finally functor which accepts Try<void>.

folly::fibers generally supports Try<void> so there's no reason to force users to use folly::Unit instead of void for addTaskFinally too.

Reviewed By: yfeldblum

Differential Revision: D3243893

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

6 years agoMake addTask[Remote]Future() work for functions returning void.
Martin Martin [Fri, 29 Apr 2016 18:54:34 +0000 (11:54 -0700)]
Make addTask[Remote]Future() work for functions returning void.

Summary:
There's no Future<void>, you're supposed to use Future<Unit>
instead.  Unit has a "Lift" template to do the conversion.

Reviewed By: andriigrynenko

Differential Revision: D3241498

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

6 years agoClang-format in preparation for other change
Martin Martin [Fri, 29 Apr 2016 17:50:23 +0000 (10:50 -0700)]
Clang-format in preparation for other change

Summary: Clang-format in preparation for other change

Reviewed By: andriigrynenko

Differential Revision: D3241297

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

6 years agodon't destory defaultElfCache in exit handlers
Philip Pronin [Fri, 29 Apr 2016 00:21:06 +0000 (17:21 -0700)]
don't destory defaultElfCache in exit handlers

Summary:
As it might be used during global destruction (for example, to
symbolize unhandled exception happened at that time).

Reviewed By: luciang, ot

Differential Revision: D3238303

fb-gh-sync-id: 39d8a6f99d95e700586349436e961f3624c95718
fbshipit-source-id: 39d8a6f99d95e700586349436e961f3624c95718

6 years agouse std::forward instead of std::move on objects whose types have been deduced; don...
Eric Niebler [Thu, 28 Apr 2016 23:39:30 +0000 (16:39 -0700)]
use std::forward instead of std::move on objects whose types have been deduced; don't take the sizeof incomplete types

Summary: Pretty sure std::forward is needed here instead of std::move. If you trace the call chain to see where the types of the objects come from, you'll see they can be deduced to be lvalues, so std::forward is the right choice. Also, moved some dicey looking code that appeared to be taking the size of some incomkplete types.

Reviewed By: spacedentist

Differential Revision: D3214199

fb-gh-sync-id: 778190ffb25a648b839760a3dddfad8dc6d41c88
fbshipit-source-id: 778190ffb25a648b839760a3dddfad8dc6d41c88

6 years agoCreate ReadHolder::unlock
Andrew Birchall [Thu, 28 Apr 2016 23:39:10 +0000 (16:39 -0700)]
Create ReadHolder::unlock

Summary:
Currently you need to depend on the destructor of `ReadHolder` (using closures as in code block #1 below or empty assignment as in code block #2 below) to ensure that a `ReadHolder` goes out of scope (and unlocks) in order to subsequently acquire a write lock via `WriteHolder` without deadlocking.
This diff introduces a way of unlocking a `ReadHolder` while it's still in scope such that a `WriteHolder` can be acquired. This makes the code more straight forward (reducing the risk of deadlock due to a programmer's misunderstanding of how `SharedMutex` / the holder framework works) => see code block # 3 below
Also add some documentation about why `WriteHolder::WriteHolder(ReadHolder&&)` doesn't exist

Code Block #1 : Use of closures
```
class foo {
 public:
  std::string getMemoizedData() {
    {
      folly::SharedMutex::ReadHolder readHolder(lock_);
      if (!data_.empty()) {
        // important to return by value, otherwise caller might access
        // data_ after we release the read lock
        return data_;
      }
    }
    {
      // try again with a write lock
      folly::SharedMutex::WriteHolder writeHolder(lock_);
      if (data_.empty()) {
        data_ = "my awesome string";
      }
      return data_;
    }
  }

 private:
  folly::SharedMutex lock_;
  std::string data_;
};
```

Code Block #2 : Use of empty assignment
```
class foo {
 public:
  std::string getMemoizedData() {
    folly::SharedMutex::ReadHolder readHolder(lock_);
    if (!data_.empty()) {
      // important to return by value, otherwise caller might access
      // data_ after we release the read lock
      return data_;
    }
    readHolder = {};

    // try again with a write lock
    folly::SharedMutex::WriteHolder writeHolder(lock_);
    if (data_.empty()) {
      data_ = "my awesome string";
    }
    return data_;
  }

 private:
  folly::SharedMutex lock_;
  std::string data_;
};
```

Code Block #3 : Use of unlock()
```
class foo {
 public:
  std::string getMemoizedData() {
    folly::SharedMutex::ReadHolder readHolder(lock_);
    if (!data_.empty()) {
      // important to return by value, otherwise caller might access
      // data_ after we release the read lock
      return data_;
    }
    readHolder->unlock();

    // try again with a write lock
    folly::SharedMutex::WriteHolder writeHolder(lock_);
    if (data_.empty()) {
      data_ = "my awesome string";
    }
    return data_;
  }

 private:
  folly::SharedMutex lock_;
  std::string data_;
};
```

Reviewed By: yfeldblum

Differential Revision: D3176025

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

6 years agofolly: Function: in-class init, drop (void*) casts
Lucian Grijincu [Thu, 28 Apr 2016 21:34:27 +0000 (14:34 -0700)]
folly: Function: in-class init, drop (void*) casts

Summary: A few code cleanups, no functionality changes.

Reviewed By: ericniebler

Differential Revision: D3231369

fb-gh-sync-id: 1a1c8508aab6dce7279e9dfc3f3da6add5496c67
fbshipit-source-id: 1a1c8508aab6dce7279e9dfc3f3da6add5496c67

6 years agoBetter exception types to SSLexception
Subodh Iyengar [Thu, 28 Apr 2016 19:10:12 +0000 (12:10 -0700)]
Better exception types to SSLexception

Summary:
Better enum exception types for SSLException.
These enum types are fairly high level, and more
info can be gleaned from the error.what()

Reviewed By: knekritz

Differential Revision: D3234501

fb-gh-sync-id: 7cd4fbccd7f4367354dc3bd1fe4cd480d58d6173
fbshipit-source-id: 7cd4fbccd7f4367354dc3bd1fe4cd480d58d6173

6 years agoStop abusing errno
Subodh Iyengar [Thu, 28 Apr 2016 19:10:01 +0000 (12:10 -0700)]
Stop abusing errno

Summary:
We abuse errno to propagate exceptions from AsyncSSLSocket.
Stop doing this and propagate exceptions correctly.

This also formats the exception messages better.

Reviewed By: anirudhvr

Differential Revision: D3226808

fb-gh-sync-id: 15a5e67b0332136857e5fb85b1765757e548e040
fbshipit-source-id: 15a5e67b0332136857e5fb85b1765757e548e040

6 years agoStrict validation for certs
Subodh Iyengar [Thu, 28 Apr 2016 18:12:54 +0000 (11:12 -0700)]
Strict validation for certs

Summary: Add X509 param types

Reviewed By: knekritz

Differential Revision: D3230008

fb-gh-sync-id: 660a2f5bdc9355f5b42c2ccfb1d7cab83df34fc2
fbshipit-source-id: 660a2f5bdc9355f5b42c2ccfb1d7cab83df34fc2

6 years agoFix asan build
Nicholas Ormrod [Thu, 28 Apr 2016 17:41:19 +0000 (10:41 -0700)]
Fix asan build

Summary:
The array-bounds checker in gcc complains that r could be -1 after leaving CHECK_ERR in ASAN mode. Switch to DCHECK to avoid this possibility. The code was not initially compiling; now it does.

Error from gcc:
  array subscript is below array bounds [-Werror=array-bounds]

Reviewed By: yfeldblum

Differential Revision: D3232694

fb-gh-sync-id: 17f579eb152b661a8e5e4ed29ef91eec405fb90a
fbshipit-source-id: 17f579eb152b661a8e5e4ed29ef91eec405fb90a

6 years agoUse the portability header for malloc_usable_size in small_vector.h
Christopher Dykes [Thu, 28 Apr 2016 17:17:19 +0000 (10:17 -0700)]
Use the portability header for malloc_usable_size in small_vector.h

Summary: `small_vector.h` had it's own thing being defined inline, and it was breaking the OSX build of HHVM, so have it use the portability header instead.

Reviewed By: yfeldblum

Differential Revision: D3232885

fb-gh-sync-id: 2078a54d1ec9700e81e0a9f4baed8e2d623fa5f3
fbshipit-source-id: 2078a54d1ec9700e81e0a9f4baed8e2d623fa5f3

6 years agoFix races in TLRefCount
Andrii Grynenko [Thu, 28 Apr 2016 04:00:57 +0000 (21:00 -0700)]
Fix races in TLRefCount

Summary:
This fixes 2 races in TLRefCount:
1. Thread-local constructor race, exposed by the stress test. It was possible for LocalRefCount to be created (grabbing collectGuard), but not be added to the thread-local list, so that accessAllThreads wasn't collecting it. collectAll() was then blocking waiting on baton to be posted, causing a dead-lock.
2. LocalRefCount::count_ has to be made atomic, because otherwise += operation may be not flushed (nbronson explained the race in D3133443).

Reviewed By: djwatson

Differential Revision: D3166956

fb-gh-sync-id: 17d58a215ebfc572f8316ed46bafaa5e6a9e2368
fbshipit-source-id: 17d58a215ebfc572f8316ed46bafaa5e6a9e2368

6 years agoFurther Function simplification
Giuseppe Ottaviano [Thu, 28 Apr 2016 00:47:43 +0000 (17:47 -0700)]
Further Function simplification

Summary:
Use tag dispatching instead of `enable_if`: it is clearer, it
sidesteps the GCC mangling bug, and more importantly the conditional
doesn't leak into the symbol, making stack traces and profiles more
readable.

Testing on a compilation unit with 1000 `Function`s from simple lambdas.
Before:
```
folly::impl::Function<int (), false>::Function<main::{lambda()#1}, {lambda()#1}>(main::{lambda()#1}&&, std::enable_if<std::integral_constant<bool, ((sizeof (std::decay<main::{lambda()#1}>::type))<=(sizeof folly::detail::function::Data::small))&&std::is_nothrow_move_constructible<std::decay<main::{lambda()#1}> >::value>::value, folly::detail::Tag>::type)::Ops::call(folly::detail::function&)
```
After:
```
folly::impl::Function<int (), false>::OpsSmall<main::{lambda()#1}>::call(folly::detail::function::Data&)
```
Note that the function type is repeated 5 times before, and only once after. This makes a large difference with long namespaces.

Binary size is almost unaffected, compile times slightly better:
Before:
GCC opt: 22.3 seconds, 4435232 bytes
Clang dev: 7.7 seconds, 5257344 bytes

After:
GCC opt: 18.6 seconds, 4493920 bytes
Clang dev: 7.2 seconds, 5469136 bytes

Reviewed By: ericniebler

Differential Revision: D3231530

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

6 years agoFaster onDestroy
Pavlo Kushnir [Thu, 28 Apr 2016 00:45:07 +0000 (17:45 -0700)]
Faster onDestroy

Summary: there is no need to call `std::function::invoke` for every DestructorGuard.

Reviewed By: yfeldblum

Differential Revision: D3229345

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

6 years agoHave the GFlags portability header stick things in namespaces
Christopher Dykes [Wed, 27 Apr 2016 21:34:33 +0000 (14:34 -0700)]
Have the GFlags portability header stick things in namespaces

Summary:
Just like the real thing does.
This was causing some issues when trying to link against glog, which expects the flags to be namespaced like this.

Reviewed By: mzlee

Differential Revision: D3230630

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

6 years agoReimplement folly::Function to improve compile times.
Eric Niebler [Wed, 27 Apr 2016 17:02:00 +0000 (10:02 -0700)]
Reimplement folly::Function to improve compile times.

Summary:
folly::Function is causing significant compile time regressions. Reimplement it in a simpler way.

These are the times for a file containing 1000 instantiations of folly::Fuction (old), folly::Function (new), and std::function with **g++ 4.8 -O3** on my CentOS7 server.

|        | Old `folly::Function` | `std::function` | New `folly::Function` |
|--------|-----------------------|-----------------|-----------------------|
| Time   | 10m37s                | 0m16.81s        | 0m14.75s              |

And for the executable size:

|        | Old `folly::Function` | `std::function` | New `folly::Function` |
|--------|-----------------------|-----------------|-----------------------|
| Size   | 10,409,504            | 732,150         | 562,781               |

That's a **43X** improvement in compile times and an **18X** reduction in executable bloat over the old implementation.

The times for **clang (trunk)** are very different:

|       | Old `folly::Function` | `std::function` | New `folly::Function` |
|-------|-----------------------|-----------------|-----------------------|
| Time  | 4m6s                  | 0m45.27s        | 0m11.78s              |

That's a **20X** improvement over the old implementation and almost a **4X** improvement over `std::function`.

For **gcc-5.3.0**, compile times are again different:

|       | Old `folly::Function` | `std::function` | New `folly::Function` |
|-------|-----------------------|-----------------|-----------------------|
| Time  | 2m49s                 | 0m18.99s        | 0m20.70s              |

With gcc-5.3, the new implementation "only" compiles 8x faster than the old one, and is roughly the same as `std::function`.

Reviewed By: spacedentist, ot, luciang

Differential Revision: D3199985

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

6 years agoAdd recursion limit to folly::parseJson.
Kyle Nekritz [Wed, 27 Apr 2016 16:42:56 +0000 (09:42 -0700)]
Add recursion limit to folly::parseJson.

Summary: Without this, malicious inputs can crash anything using folly::parseJson.

Reviewed By: yfeldblum

Differential Revision: D3219036

fb-gh-sync-id: 3604a060170c0201473c420035b21b018383789c
fbshipit-source-id: 3604a060170c0201473c420035b21b018383789c

6 years agoRemove dead code in AsyncSSLSocket
Subodh Iyengar [Wed, 27 Apr 2016 16:13:48 +0000 (09:13 -0700)]
Remove dead code in AsyncSSLSocket

Summary: Remove dead code in AsyncSSLSocket.

Reviewed By: knekritz

Differential Revision: D3226948

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

6 years agofix access-after-free bug in ProducerConsumerQueueTest
Sven Over [Wed, 27 Apr 2016 13:46:08 +0000 (06:46 -0700)]
fix access-after-free bug in ProducerConsumerQueueTest

Summary: This bug causes failure when the test is run under ASAN.

Reviewed By: meyering

Differential Revision: D3229494

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

6 years agoCheaper bumpHandlingTime
Pavlo Kushnir [Wed, 27 Apr 2016 07:20:04 +0000 (00:20 -0700)]
Cheaper bumpHandlingTime

Summary: no need in virtual call from EventHandler.

Reviewed By: yfeldblum

Differential Revision: D3226960

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

6 years agoOptimize away IOBuf clones on hot path
Jon Maltiel Swenson [Wed, 27 Apr 2016 01:12:20 +0000 (18:12 -0700)]
Optimize away IOBuf clones on hot path

Summary:
'get' is the most frequent memcached operation. On a get reply, we should go to heap
as little as possible.  This diff optimizes for this scenario, where replies
have only one IOBuf field.

Reviewed By: pavlo-fb

Differential Revision: D3226592

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

6 years agoAdding a constructor to UTF8Range that uses std::string
Aditya Muttur [Tue, 26 Apr 2016 22:53:40 +0000 (15:53 -0700)]
Adding a constructor to UTF8Range that uses std::string

Summary:
Currently UTF8Range has a constructor that allows you construct an object of type UTF8Range using only an object of type folly::Range. Adding a constructor so that we can construct an UTF8Range object using a std::string.

Currently,
  void sampleMethod(UTF8StringPiece sp) {...}
  /*
  ...
  */
  std::string str = "example";
  folly::UTF8Range utf8Range(str);
  folly::StringPiece sp(str);
  sampleMethod(utf8Range); // works
  sampleMethod(sp); // works
  sampleMethod(str); // doesn't work

This diff hopes to fix this issue.

Reviewed By: ddrcoder

Differential Revision: D3221144

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

6 years agoAPI to set folly::RequestContext for current scope, try 2
Mirek Klimos [Tue, 26 Apr 2016 16:58:33 +0000 (09:58 -0700)]
API to set folly::RequestContext for current scope, try 2

Summary: same as D3156698, without changes in Cpp2Connection (which was the only real change in the diff)

Reviewed By: haijunz

Differential Revision: D3222792

fb-gh-sync-id: 245c7add837c0fc6d0bc84aa7d80b929ba2ce386
fbshipit-source-id: 245c7add837c0fc6d0bc84aa7d80b929ba2ce386

6 years agoDefine DECLARE_VARIABLE in portability/GFlags.h
Christopher Dykes [Tue, 26 Apr 2016 16:38:18 +0000 (09:38 -0700)]
Define DECLARE_VARIABLE in portability/GFlags.h

Summary: If we don't, then anywhere we include `glog/logging.h` after the portability header will get the `DECLARE_*` and `DEFINE_*` macros undefined -_-....

Reviewed By: mzlee

Differential Revision: D3220797

fb-gh-sync-id: 52907ddcd6b222fb1c6423034ea999eac5ce09ab
fbshipit-source-id: 52907ddcd6b222fb1c6423034ea999eac5ce09ab

6 years agoRemove extra dependency on Portability.h
Michael Lee [Tue, 26 Apr 2016 15:43:27 +0000 (08:43 -0700)]
Remove extra dependency on Portability.h

Summary: This dependency is in the wrong order.

Reviewed By: knekritz

Differential Revision: D3224312

fb-gh-sync-id: 98d2c58b75cf5f16892d462ae1b877d06bce3faa
fbshipit-source-id: 98d2c58b75cf5f16892d462ae1b877d06bce3faa

6 years agoUse std::string in folly::dynamic
Pavlo Kushnir [Tue, 26 Apr 2016 09:27:37 +0000 (02:27 -0700)]
Use std::string in folly::dynamic

Summary: Why not kill fbstring entirely? There are some legitimate use cases. For example, `folly::IOBuf` supports `moveToFbstring()` method which is impossible to implement with `std::string`.

Reviewed By: joeg, snarkmaster

Differential Revision: D3189410

fb-gh-sync-id: 9bb9090ca6012ac32ba9fb79041b26ec4888781f
fbshipit-source-id: 9bb9090ca6012ac32ba9fb79041b26ec4888781f

6 years agoRetire BOOST_STATIC_ASSERT in favor of static_assert
Yedidya Feldblum [Tue, 26 Apr 2016 07:56:27 +0000 (00:56 -0700)]
Retire BOOST_STATIC_ASSERT in favor of static_assert

Summary:
Retire `BOOST_STATIC_ASSERT` in favor of `static_assert`.

`static_assert` is part of C++ now, so we don't need workarounds like `BOOST_STATIC_ASSERT` anymore.

Partially automated:

    hg grep -lw BOOST_STATIC_ASSERT | xargs perl -pi -e 's~\bBOOST_STATIC_ASSERT\(([^;]*)\);~static_assert(\1, "");~g'
    hg grep -lw 'boost/static_assert.hpp' | xargs perl -pi -e 's,^#include <boost/static_assert\.hpp>\n,,gm'

Caught most instances. Formatting and remaining instances addressed manually.

Reviewed By: meyering

Differential Revision: D3215944

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

6 years agoRemove unnecessary construction of IOBuf in Cursor
Stepan Palamarchuk [Mon, 25 Apr 2016 21:12:21 +0000 (14:12 -0700)]
Remove unnecessary construction of IOBuf in Cursor

Summary: This assignment is unnecessary, because in `for` loop we **always** call `cloneOneInto` that would assign another IOBuf to that IOBuf.

Reviewed By: alikhtarov

Differential Revision: D3215861

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

6 years agoUndelayedDestruction virtual=>override
Andrew Cox [Mon, 25 Apr 2016 17:51:47 +0000 (10:51 -0700)]
UndelayedDestruction virtual=>override

Summary:
Had this in another diff, but it caused too much test noise. So I've separated
it out on it's own.

Reviewed By: yfeldblum

Differential Revision: D3213605

fb-gh-sync-id: 9ebfcf8430da7c66a31868032a0cef1e616ffc58
fbshipit-source-id: 9ebfcf8430da7c66a31868032a0cef1e616ffc58

6 years agouse forward instead of move for universal references
Sven Over [Sat, 23 Apr 2016 15:07:25 +0000 (08:07 -0700)]
use forward instead of move for universal references

Summary:When dealing with universal references, std::move will move from
objects that are passed as lvalue references. Instead std::forward
should be used, which only moves from an object if it's passed
as rvalue reference.

Reviewed By: yfeldblum

Differential Revision: D3200402

fb-gh-sync-id: 14be071e8498dd64cb8b2583c0cc2dd383bfebb8
fbshipit-source-id: 14be071e8498dd64cb8b2583c0cc2dd383bfebb8

6 years agoupdate futures README
Hans Fugal [Fri, 22 Apr 2016 21:11:29 +0000 (14:11 -0700)]
update futures README

Summary:It's been awhile.

Still has intern URLs (#10943549) but I'll fix that separately.

Reviewed By: jsedgwick

Differential Revision: D3213245

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

6 years agoSubprocess: allow non-copyable callbacks
Sven Over [Fri, 22 Apr 2016 18:00:39 +0000 (11:00 -0700)]
Subprocess: allow non-copyable callbacks

Summary:Instead of std::function<bool(int, int)>, use folly::Function to
pass callbacks to Subprocess::communicate. This makes it possible
to pass non-copyable callables, which is especially interesting
because Subprocess::readLinesCallback returns a non-copyable
object.

This diff also fixes the forwarding of the callback passed to
readLinesCallback in case you pass an lvalue reference.

Reviewed By: snarkmaster

Differential Revision: D3169956

fb-gh-sync-id: 7a906f9a3ab50502fc04e0d83a23ca5e0201bb3e
fbshipit-source-id: 7a906f9a3ab50502fc04e0d83a23ca5e0201bb3e

6 years agoget_default and get_ref_default variants taking functions
Yedidya Feldblum [Fri, 22 Apr 2016 06:14:27 +0000 (23:14 -0700)]
get_default and get_ref_default variants taking functions

Summary:[Folly] `get_default` and `get_ref_default` variants taking functions.

Useful if the default value is computationally expensive to construct or requires IO.

Reviewed By: andriigrynenko, spacedentist

Differential Revision: D3189247

fb-gh-sync-id: 51c64293f8712d7590348d53cbfd892a5efd9e82
fbshipit-source-id: 51c64293f8712d7590348d53cbfd892a5efd9e82

6 years agoUpdates the internal representation of signals in NotificationQueue
Tobias Ritzau [Fri, 22 Apr 2016 02:04:13 +0000 (19:04 -0700)]
Updates the internal representation of signals in NotificationQueue

Summary: The signals were represented using bytes in a pipe or using a count on an event fd (when available). This count was ever growing and caused the pipe to overflow, and in both cases you would get signals on empty queues. This diff only writes to the fd if it there are no bytes to read. Due to races there can still be multiple bytes in the pipe, but overflowing should not be possible. Instead of blindly signaling when there could be messages in the queue, the signals are now synchronized with the state of the queue so that the signals are drained when the queue is empty. This also made it possible to skip the semaphore behavior of the event fd which should improve perf.

Reviewed By: dcolascione

Differential Revision: D3198252

fb-gh-sync-id: 39e620b10c254ffcacabc4c5ac36950a215d4803
fbshipit-source-id: 39e620b10c254ffcacabc4c5ac36950a215d4803

6 years agofix cancelAllFunctions
Taiyuan Zhang [Thu, 21 Apr 2016 22:42:26 +0000 (15:42 -0700)]
fix cancelAllFunctions

Summary: as titled. When I tried re-adding all functions after cancelAllFunctions, it throws error. This is because it doesn't reset currentFunction to null.

Reviewed By: jjs0

Differential Revision: D3209237

fb-gh-sync-id: 5b6e2d967fc3c0986320d23b8d61eb1bfbff8940
fbshipit-source-id: 5b6e2d967fc3c0986320d23b8d61eb1bfbff8940

6 years agofix: readvNoInt use wrong read function
chenkan [Thu, 21 Apr 2016 22:02:44 +0000 (15:02 -0700)]
fix: readvNoInt use wrong read function

Summary: Closes https://github.com/facebook/folly/pull/401

Reviewed By: yfeldblum

Differential Revision: D3207682

Pulled By: snarkmaster

fb-gh-sync-id: 617aeec0e6fe1f46d2464df4b72a3e91c953dc12
fbshipit-source-id: 617aeec0e6fe1f46d2464df4b72a3e91c953dc12

6 years agoDefine more pieces of portability/GFlags
Michael Lee [Wed, 20 Apr 2016 22:54:54 +0000 (15:54 -0700)]
Define more pieces of portability/GFlags

Summary: Add more compatibility for missing gflags

Reviewed By: yfeldblum

Differential Revision: D3202560

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

6 years agoAdded FOLLY_IGNORE and FOLLY_SEMICOLON helper macros
Marcelo Juchem [Wed, 20 Apr 2016 21:23:31 +0000 (14:23 -0700)]
Added FOLLY_IGNORE and FOLLY_SEMICOLON helper macros

Summary: these macros are generally useful for other higher-order macros

Reviewed By: yfeldblum

Differential Revision: D3194777

fb-gh-sync-id: 667fc51c681786ad422309ee463881dc22c972f7
fbshipit-source-id: 667fc51c681786ad422309ee463881dc22c972f7

6 years agoClean up deprecation warnings
Michael Lee [Wed, 20 Apr 2016 20:24:25 +0000 (13:24 -0700)]
Clean up deprecation warnings

Summary:Clean up a deprecation warning for JSONSchema's use of
Singleton. This is in experimental, so the interface change should be
reasonable..

Reviewed By: agoder

Differential Revision: D3203348

fb-gh-sync-id: 3283c7d9c507a545f3217eb5afc3734eb047b833
fbshipit-source-id: 3283c7d9c507a545f3217eb5afc3734eb047b833

6 years agoFix use of dynamic {} initialization in JSONSchemaTest
Michael Lee [Wed, 20 Apr 2016 20:24:18 +0000 (13:24 -0700)]
Fix use of dynamic {} initialization in JSONSchemaTest

Summary: find replace 's/{([0-9, a-z"-]+)}/dynamic::array(\1)/g'

Reviewed By: agoder

Differential Revision: D3203454

fb-gh-sync-id: 064bb2bdd0e19a80da32e8f4eb730fc4268d7cad
fbshipit-source-id: 064bb2bdd0e19a80da32e8f4eb730fc4268d7cad

6 years agoReverted commit D3156698
Mirek Klimos [Wed, 20 Apr 2016 20:08:48 +0000 (13:08 -0700)]
Reverted commit D3156698

Summary:There're currently two ways to set RequestContext
- RequestContext::create() - creates new context and sets it
- RequestContext::setContext(context) - sets context previously captured by saveContext
In most cases, the RequestContext is set back after the request is processed but sometimes it's not (especially with RequestContext::create). We want to measure cpu time for a request by measuring the total cpu time when a RequestContext is set, so we need to make sure that it's properly reset after the thread is done with the request. Scope guards can help us with that.

Reviewed By: haijunz

Differential Revision: D3156698

fb-gh-sync-id: 7c3eb06c1cc27849071625011bf64c5ad36c0612
fbshipit-source-id: 7c3eb06c1cc27849071625011bf64c5ad36c0612

6 years agoThrow on errors in read callback
Alexey Spiridonov [Wed, 20 Apr 2016 19:08:29 +0000 (12:08 -0700)]
Throw on errors in read callback

Summary: Before this fix, the callback would silently ignore `ret == -1` if the error isn't `EAGAIN`.

Reviewed By: spacedentist

Differential Revision: D3193845

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

6 years agoBackward compatibility for older versions of clang.
Michael Lee [Wed, 20 Apr 2016 02:47:54 +0000 (19:47 -0700)]
Backward compatibility for older versions of clang.

Summary: `__attribute__((no_sanitize("address")))` is not valid in clang 3.6 and earlier

Reviewed By: skotchvail

Differential Revision: D3197412

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

6 years ago`detail::Futex` wants 4 bytes but MicroLock only gives you one
Michael Lee [Tue, 19 Apr 2016 14:52:23 +0000 (07:52 -0700)]
`detail::Futex` wants 4 bytes but MicroLock only gives you one

Summary: ^^^ this is a stack overflow in the test, and a possible stack or heap overflow.

Reviewed By: dcolascione

Differential Revision: D3151717

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

6 years agoAdd make_array to folly
Mark Isaacson [Tue, 19 Apr 2016 07:46:56 +0000 (00:46 -0700)]
Add make_array to folly

Summary: This function is being proposed in WG21, the C++ standards body for inclusion in the STL via the Library Fundamentals v2 TS. Using the normal constructor for a std::array with an initializer list introduces a source of coupling between the # of elements you put in the initializer list and the size of the array you specify as a template argument. Worse still, if you put less things in the initializer list than the template argument specifies, it doesn't warn you that you've probably made a pretty devious and subtle error. With this function your array size will always be the same as the # of things you actually put in it. What's more, in some cases this can deduce the type of the elements as well.

Reviewed By: yfeldblum

Differential Revision: D3164432

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

6 years agoAPI to set folly::RequestContext for current scope
Mirek Klimos [Mon, 18 Apr 2016 21:28:06 +0000 (14:28 -0700)]
API to set folly::RequestContext for current scope

Summary:There're currently two ways to set RequestContext
- RequestContext::create() - creates new context and sets it
- RequestContext::setContext(context) - sets context previously captured by saveContext
In most cases, the RequestContext is set back after the request is processed but sometimes it's not (especially with RequestContext::create). We want to measure cpu time for a request by measuring the total cpu time when a RequestContext is set, so we need to make sure that it's properly reset after the thread is done with the request. Scope guards can help us with that.

Reviewed By: haijunz

Differential Revision: D3156698

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

6 years agoNotificationQueue Logging for Android bug
Brian Walker [Mon, 18 Apr 2016 17:59:14 +0000 (10:59 -0700)]
NotificationQueue Logging for Android bug

Summary: Add some logging to figure out the state of the Qeueu when the write error happens.

Differential Revision: D3190882

fb-gh-sync-id: 6515d77df8aa3086922cb4053f2e437f3e527a36
fbshipit-source-id: 6515d77df8aa3086922cb4053f2e437f3e527a36

6 years agoremove superfluous include directive
Sven Over [Mon, 18 Apr 2016 01:37:01 +0000 (18:37 -0700)]
remove superfluous include directive

Summary:folly::fibers does not use folly::MoveWrapper anymore, so no need to
include that header file.

Reviewed By: yfeldblum

Differential Revision: D3169374

fb-gh-sync-id: 2409b46156843fb0a1454b560c42186f1c412988
fbshipit-source-id: 2409b46156843fb0a1454b560c42186f1c412988

6 years agoremove MoveWrapper from io/async/AsyncUDPServerSocket.h
Sven Over [Sat, 16 Apr 2016 03:26:30 +0000 (20:26 -0700)]
remove MoveWrapper from io/async/AsyncUDPServerSocket.h

Summary:This is the last place in folly that uses MoveWrapper (other than
the MoveWrapper implementation itself and its tests, of course).

Instead of using a MoveWrapper, the object in question is moved
into the lambda using C++14 syntax, and the lambda is moved in a
call to EventBase::runInEventBaseThread which is possible now
that the EventBase methods accept non-copyable callbacks.

Reviewed By: yfeldblum

Differential Revision: D3169316

fb-gh-sync-id: 2dcb1a523ac417f4619c607898e58b572648e3da
fbshipit-source-id: 2dcb1a523ac417f4619c607898e58b572648e3da

6 years agoNon-intrusive AtomicLinkedList
Andrii Grynenko [Sat, 16 Apr 2016 02:50:46 +0000 (19:50 -0700)]
Non-intrusive AtomicLinkedList

Summary:Renamed AtomicLinkedList to AtomicIntrusiveLinkedList.
AtomicLinkedList is a simple AtomicIntrusiveLinkedList wrapper, which handles intrusive list hook.

Reviewed By: yfeldblum

Differential Revision: D3188171

fb-gh-sync-id: 0823b04a48336d65e0a6a8cd412c75f52afe02b9
fbshipit-source-id: 0823b04a48336d65e0a6a8cd412c75f52afe02b9

6 years agoForgot to fix the guard for String.cpp
Michael Lee [Fri, 15 Apr 2016 22:16:46 +0000 (15:16 -0700)]
Forgot to fix the guard for String.cpp

Summary:9ab69bc removed `__APPLE__` around the strndup definition
because it is defined on modern OSX.  But there was another reference
in String.cpp. Fixing that.

Reviewed By: yfeldblum

Differential Revision: D3186360

fb-gh-sync-id: 709c3b93cd22945e2237412637929df1979526c1
fbshipit-source-id: 709c3b93cd22945e2237412637929df1979526c1

6 years agoRemove the strings.h portability header
Christopher Dykes [Fri, 15 Apr 2016 17:57:01 +0000 (10:57 -0700)]
Remove the strings.h portability header

Summary:Although, according to the manpage, these functions are defined in `strings.h`, but they are also defined in `string.h`. We never actually use these functions via `strings.h`, and instead only ever reference them via `string.h`.
To keep things sane, lets just move the functions into `string.h` and kill `strings.h`.

Reviewed By: yfeldblum

Differential Revision: D3181596

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

6 years agoCorrectly deduce RNG type in folly::Random
David Goldblatt [Fri, 15 Apr 2016 17:14:13 +0000 (10:14 -0700)]
Correctly deduce RNG type in folly::Random

Summary:Fix a bug in folly where it can't infer any RNG type except the
default-supplied parameter.

Differential Revision: D3163421

fb-gh-sync-id: 23d3963ba19dac93fa3407f3a2dfd1d9aa39ea44
fbshipit-source-id: 23d3963ba19dac93fa3407f3a2dfd1d9aa39ea44

6 years agoFix build for no FOLLY_TLD_USE_FOLLY_TLS
Michael Lee [Fri, 15 Apr 2016 00:20:26 +0000 (17:20 -0700)]
Fix build for no FOLLY_TLD_USE_FOLLY_TLS

Summary: unique_ptr doesn't cast to void*

Reviewed By: andriigrynenko

Differential Revision: D3181346

fb-gh-sync-id: 5b912d2b5d46f3c9953b44d5ced7a8e1b4320678
fbshipit-source-id: 5b912d2b5d46f3c9953b44d5ced7a8e1b4320678

6 years agoImprove folly::ThreadLocal perf
Andrii Grynenko [Thu, 14 Apr 2016 18:19:59 +0000 (11:19 -0700)]
Improve folly::ThreadLocal perf

Summary: This fixes folly::ThreadLocal perf after several refactorings which touched the code.

Reviewed By: ericniebler

Differential Revision: D3170761

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

6 years agoFix GuardPageAllocator to do mprotect lazily
Andrii Grynenko [Thu, 14 Apr 2016 04:30:45 +0000 (21:30 -0700)]
Fix GuardPageAllocator to do mprotect lazily

Summary:Currently GuargPageAllocator mmap's a memory block enough for 100 fiber stacks and then protects a bottom page for each stack. If FiberManager is used to process a single task, protecting 100 stacks doesn't make much sense, but is costly.

This change makes sure we protect a bottom page of every stack the first time given stack is requested.

Reviewed By: alikhtarov

Differential Revision: D3139944

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

6 years agoremove duplicate licence prolog
Den Raskovalov [Thu, 14 Apr 2016 02:46:08 +0000 (19:46 -0700)]
remove duplicate licence prolog

Reviewed By: yfeldblum

Differential Revision: D3174112

fb-gh-sync-id: 00a01f64fb43d2333818097bb2212e26e4dd5775
fbshipit-source-id: 00a01f64fb43d2333818097bb2212e26e4dd5775

6 years agoFix the includes in portability/Malloc.h
Christopher Dykes [Wed, 13 Apr 2016 17:19:24 +0000 (10:19 -0700)]
Fix the includes in portability/Malloc.h

Summary:We use `size_t` but didn't include `<stdlib.h>`.
Fix #385

Reviewed By: markisaa

Differential Revision: D3171643

fb-gh-sync-id: 1f915f9602c4be59a21cdf77b31fe18816f35f81
fbshipit-source-id: 1f915f9602c4be59a21cdf77b31fe18816f35f81

6 years agoAdd method to get the connect timeout used for an AsyncSocket
Neel Goyal [Wed, 13 Apr 2016 15:04:45 +0000 (08:04 -0700)]
Add method to get the connect timeout used for an AsyncSocket

Summary: Have the AsyncSocket keep track of the timeout used for connecting and add a getter to retrieve it.

Reviewed By: hiteshk

Differential Revision: D3170625

fb-gh-sync-id: 61d0ecd8d975c49978a1cf222671aa16a2160499
fbshipit-source-id: 61d0ecd8d975c49978a1cf222671aa16a2160499

6 years agoSorry, forgot to add portability/Memory.cpp
Michael Lee [Tue, 12 Apr 2016 17:59:21 +0000 (10:59 -0700)]
Sorry, forgot to add portability/Memory.cpp

Summary: Add it to the Makefile.am

Reviewed By: Orvid

Differential Revision: D3168833

fb-gh-sync-id: 7184acc565ffebde0f93bc8bdce4d38ba1a79923
fbshipit-source-id: 7184acc565ffebde0f93bc8bdce4d38ba1a79923

6 years agoFix the order of EXPECT_EQ parameters
Alexander Shaposhnikov [Mon, 11 Apr 2016 20:15:45 +0000 (13:15 -0700)]
Fix the order of EXPECT_EQ parameters

Summary:Change the order of EXPECT_EQ parameters to
EXPECT_EQ(expected, actual). It will make gtest print
the correct error message if a test fails.

Reviewed By: yfeldblum

Differential Revision: D3161878

fb-gh-sync-id: 69e4536c6f396858d189a9d25904ef4df639ad49
fbshipit-source-id: 69e4536c6f396858d189a9d25904ef4df639ad49

6 years agoSome cleanups to folly::EventBase after converting to folly::Function
Yedidya Feldblum [Sun, 10 Apr 2016 21:51:09 +0000 (14:51 -0700)]
Some cleanups to folly::EventBase after converting to folly::Function

Summary:[Folly] Some cleanups to `folly::EventBase` after converting to `folly::Function`.

* Fix up some comments referring to `std::function`.
* Remove the `SmallFunctor` bits - `folly::Function` takes over for that.
* Remove `runFunctionPtr` - it's unused.

Reviewed By: spacedentist

Differential Revision: D3155511

fb-gh-sync-id: 38c75d1254993f59c8eaa7826dc8d9facb50a3a1
fbshipit-source-id: 38c75d1254993f59c8eaa7826dc8d9facb50a3a1

6 years agoAdd <new> header for placement new
Adam Norton [Sat, 9 Apr 2016 02:30:43 +0000 (19:30 -0700)]
Add <new> header for placement new

Summary: This header is necessary for the placement new operator used in the construct method (line 265)

Reviewed By: yfeldblum

Differential Revision: D3156741

fb-gh-sync-id: 84a64821553b42d46fb70b5ee267a401028c94f2
fbshipit-source-id: 84a64821553b42d46fb70b5ee267a401028c94f2

6 years agoExtensibility for folly::to<> through ADL
Tom Jackson [Thu, 7 Apr 2016 21:01:28 +0000 (14:01 -0700)]
Extensibility for folly::to<> through ADL

Summary: Primarily to support slightly more flexible implementations of `split()`;

Reviewed By: ot

Differential Revision: D3116763

fb-gh-sync-id: 69023c0f26058516f25b9c1f9824055efc7021f9
fbshipit-source-id: 69023c0f26058516f25b9c1f9824055efc7021f9

6 years agoDynamicParser to reliably and reversibly convert JSON to structs
Alexey Spiridonov [Thu, 7 Apr 2016 16:53:34 +0000 (09:53 -0700)]
DynamicParser to reliably and reversibly convert JSON to structs

Summary:We have a bunch of code that manually parses `folly::dynamic`s into program structures. I can be quite hard to get this parsing to be good, user-friendly, and concise. This diff was primarily motivated by the mass of JSON-parsing done by Bistro, but this pattern recurs in other pieces of internal code that parse dynamics.

This diff **not** meant to replace using Thrift structs with Thrift's JSON serialization / deserialization. When all you have to deal with is correct, structured plain-old-data objects produced by another program -- **not** manually entered user input -- Thrift + JSON is perfect. Go use that.

However, sometimes you need to parse human-edited configuration. The input JSON might have complex semantics, and require validation beyond type-checking. The UI for editing your configs can easily enforce correct JSON syntax. Perhaps, you can use `folly/experimental/JSONSchema.h` to have your edit UI provide type correctness. Despite all this, people can still make semantic errors, and those can be impossible to detect until you interpret the config at runtime. Also, as your system evolves, sometimes you need to break semantic backwards-compatibility for the sake of moving forward ? thus making previously valid configurations invalid, and requiring them to be fixed up manually.

So, people end up needing to write manual parsers for `dynamic`s. These all have very similar recurring issues:

 - Verbose: to get an int field out of an object, typical code: (i) tests if the field is present, (ii) checks if the field is an integer, (iii) extracts the integer. Sometimes, you also want to handle exceptions, and compose helpful error messages. This makes the code far longer than its intent, and encourages people to write bad parsers.

 - Unsystematic: sometimes, we use `if (const auto* p = dyn_obj.get_ptr("key")) { ... }`, other times we use `dyn_obj.getDefault()` or `if (dyn_obj.count())`, and so on. The patterns differ subtly in meaning. Exceptions sometimes get thrown, leading to error messages that cannot be understood by the user.

 - Imperative parses: a typical parse proceeds step by step, and throws at the earliest error. This is bad because (i) errors have to be fixed one-by-one, instead of getting a full list upfront, (ii) even if 99% of the config is parseable, the imperative code has no way of recording the information it would have parsed after the first error.

 `DynamicParser` fixes all of the above, and makes your parsing so clean that you might not even bother with `JSONSchema` as your first line of defense -- type-coercing, type-enforcing, friendly-error-generating C++ ends up being more concise. Besides all the sweet syntax sugar, `DynamicParser` lets you parse **all** the valid data in your config, while recording  *all* the errors in a way that does not lose the original, buggy config. This means your code can parse a config that has errors, and still be able to meaningfully export it back to JSON. As a result, stateless clients (think REST APIs) can provide a far better user experience than just discarding the user?s input, and returning a cryptic error message.

For the details, read the docs (and see the example) in `DynamicParser.h`. Here are the principles of `DynamicParser::RECORD` mode in a nutshell:
 - Pre-populate your program struct with meaningful defaults **before** you parse.
 - Any config part that fails to parse will keep the default.
 - Any config part that parses successfully will get to update the program struct.
 - Any errors will be recorded with a helpful error message, the portion of the dynamic that caused the error, and the path through the dynamic to that portion.

 I ported Bistro to use this in D3136954. I looked at using this for JSONSchema's parsing of schemas, but it seemed like too much trouble for the gain, since it would require major surgery on the code.

Reviewed By: yfeldblum

Differential Revision: D2906819

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

6 years agoUse folly::Function in folly::EventBase
Yedidya Feldblum [Thu, 7 Apr 2016 10:30:56 +0000 (03:30 -0700)]
Use folly::Function in folly::EventBase

Summary:[Folly] Use `folly::Function` in `folly::EventBase`.

`folly::Function` is moveable but noncopyable and therefore supports wrapping moveable but noncopyable lambdas - like the kind that arises when move-capturing a `std::unique_ptr`.

`std::function` is copyable - therefore it does not support wrapping such noncopyable lambdas.

Switching `folly::EventBase` to use it will allow callers to pass such noncopyable lambdas, allowing, e.g.:

```
auto numptr = folly::make_unique<int>(7); // unique_ptr is noncopyable
folly::EventBase eb;
eb.runInLoop([numptr = std::move(numptr)] { // therefore lambda is noncopyable
  int num = *numptr;
});
eb.loop();
```

This allows us to move away from the `folly::MoveWrapper` hack, which worked like:

```
auto numptr = folly::make_unique<int>(7); // unique_ptr is noncopyable
auto numptrw = folly::makeMoveWrapper(std::move(numptr)); // MoveWrapper is "copyable" - hacky
folly::EventBase eb;
eb.runInLoop([numptrw] { // therefore lambda is "copyable" - hacky
  int num = **numptrw;
});
```

We needed to do that hack while:

But neither condition is true anymore.

Reviewed By: spacedentist

Differential Revision: D3143931

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

6 years agoRemove leftover comment in Portability.h
Giuseppe Ottaviano [Wed, 6 Apr 2016 18:57:19 +0000 (11:57 -0700)]
Remove leftover comment in Portability.h

Reviewed By: luciang

Differential Revision: D3145033

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

6 years agomalloc.h doesn't exist on OSX
Bert Maher [Wed, 6 Apr 2016 18:31:41 +0000 (11:31 -0700)]
malloc.h doesn't exist on OSX

Summary: Use malloc/malloc.h there instead

Reviewed By: Orvid

Differential Revision: D3143768

fb-gh-sync-id: 3258b68df49cf7fbff28a3853663d39c147aee8b
fbshipit-source-id: 3258b68df49cf7fbff28a3853663d39c147aee8b

6 years agoStart compiling a bit of `-Wshadow`
Michael Lee [Wed, 6 Apr 2016 15:00:14 +0000 (08:00 -0700)]
Start compiling a bit of `-Wshadow`

Summary: Sometimes variable shadowing is fine, sometimes it can cause subtle mistakes.

Reviewed By: yfeldblum

Differential Revision: D3140450

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

6 years agofolly::fibers::Fiber: use folly::Function instead of std::function
Sven Over [Wed, 6 Apr 2016 10:55:14 +0000 (03:55 -0700)]
folly::fibers::Fiber: use folly::Function instead of std::function

Summary:We are jumping through some ugly hoops in the implementation of
fibers to allow for non-copyable functors/lambdas. We can get rid of
those by using folly::Function instead of std::function to store
functions in folly::fibers::Fiber.

This involves one observable interface change: the virtual function
folly::fibers::InlineFunctionRunner::run must take a
folly::Function<void()> argument instead of a std::function<void()>.

Reviewed By: andriigrynenko

Differential Revision: D3102711

fb-gh-sync-id: 56705b972dc24cc0da551109ed44732b97cb6e13
fbshipit-source-id: 56705b972dc24cc0da551109ed44732b97cb6e13

6 years agoAllow usage of Symbolizer options for ExceptionStats printing
Dmitry Pleshkov [Wed, 6 Apr 2016 04:39:18 +0000 (21:39 -0700)]
Allow usage of Symbolizer options for ExceptionStats printing

Summary: For exceptions aggregation conveniece

Reviewed By: philippv

Differential Revision: D3128132

fb-gh-sync-id: 48c72df364228c1d2c8f29161c2b85c131b4fea8
fbshipit-source-id: 48c72df364228c1d2c8f29161c2b85c131b4fea8

6 years agoFunction::asStdFunction()
James Sedgwick [Tue, 5 Apr 2016 22:05:33 +0000 (15:05 -0700)]
Function::asStdFunction()

Summary:This is an ugly but occassionally useful hack to accomodate cases
where we want to propagate folly::Function but run into the brick wall that is
std::function

@override-unit-failures

Reviewed By: spacedentist

Differential Revision: D3118432

fb-gh-sync-id: 80ed56494dfcf60e0f266013d880107acabc7dcf
fbshipit-source-id: 80ed56494dfcf60e0f266013d880107acabc7dcf

6 years agoUpdate libgcc and boost symlinks (Disable SSO on ASan builds)
Giuseppe Ottaviano [Tue, 5 Apr 2016 19:30:17 +0000 (12:30 -0700)]
Update libgcc and boost symlinks (Disable SSO on ASan builds)

Summary: Allow ASan to detect access to invalidated strings even when they are small (in-situ). See D3114022 for details.

Reviewed By: luciang

Differential Revision: D3118161

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

6 years agofolly/futures: replace MoveWrappers with generalised lambda capture
Sven Over [Tue, 5 Apr 2016 18:34:21 +0000 (11:34 -0700)]
folly/futures: replace MoveWrappers with generalised lambda capture

Summary:Now that folly::Future uses folly::Function, we can use non-copyable
callbacks. That allows us to get rid of folly::MoveWrapper in the
implementaion.

This diff also enforces perfect forwarding in the implementation of
folly::Future, thereby reducing the number of times that a callable
that is passed to Future::then et al. gets passed by value.

Before folly::Function, Future::then(callback) has invoked the move
constructor of the callback type 5 times for small callback objects
(fitting into the in-place storage inside folly::detail::Core) and
6 times for large callback objects. This has been reduced to 5 times
in all cases with the switch to UniqueFunction. This diff reduces it
to 2 times.

Reviewed By: yfeldblum

Differential Revision: D2976647

fb-gh-sync-id: 9da470d7e9130bd7ad8af762fd238ef9a3ac5892
fbshipit-source-id: 9da470d7e9130bd7ad8af762fd238ef9a3ac5892

6 years agofolly::FunctionScheduler: replace std::function w/ folly::Function
Sven Over [Mon, 4 Apr 2016 09:32:10 +0000 (02:32 -0700)]
folly::FunctionScheduler: replace std::function w/ folly::Function

Summary:By using folly::Function instead of std::function to internally store
functions in FunctionScheduler, this diff allows users to pass
non-copyable lambdas to FunctionScheduler::addFunction.

All exisiting unit tests still pass. Also, passing std::function to
addFunction still works, as the std::function will be implicitly
converted (i.e. wrapped) in a folly::Function. However, this implies
a performance penalty.

Reviewed By: fugalh

Differential Revision: D3092944

fb-gh-sync-id: 1e8081e70d4717025f2eadbb6b6da173460d4373
fbshipit-source-id: 1e8081e70d4717025f2eadbb6b6da173460d4373

6 years agofbstring: Make SSO disabling and insertImplDiscr implementation simpler
Giuseppe Ottaviano [Sat, 2 Apr 2016 22:12:30 +0000 (15:12 -0700)]
fbstring: Make SSO disabling and insertImplDiscr implementation simpler

Summary:Instead of using preprocessor to disable SSO, use a default argument. Also
reimplement `insertImplDiscr` to mirror `insertImpl`.

Reviewed By: philippv

Differential Revision: D3130901

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

6 years agoAdd portability header for libgen.h
Christopher Dykes [Sat, 2 Apr 2016 01:29:47 +0000 (18:29 -0700)]
Add portability header for libgen.h

Summary: The only thing that matters in it is dirname, and Windows doesn't have it.

Reviewed By: yfeldblum

Differential Revision: D2977655

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

6 years agoCreate a portability header for syslog.h
Christopher Dykes [Sat, 2 Apr 2016 01:28:33 +0000 (18:28 -0700)]
Create a portability header for syslog.h

Summary: Windows doesn't have it, so stub it out for now, because not getting output to the system log is a minor inconvience, not a breaking issue.

Reviewed By: yfeldblum

Differential Revision: D2977537

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

6 years agoDon't use the plus operator unecessarily
Christopher Dykes [Sat, 2 Apr 2016 00:56:17 +0000 (17:56 -0700)]
Don't use the plus operator unecessarily

Summary: MSVC doesn't like this and complains about ambigious overloads.

Reviewed By: ericniebler

Differential Revision: D3107438

fb-gh-sync-id: 597ba85fdb7640b0a0fda352bf53ce31cf0e38b7
fbshipit-source-id: 597ba85fdb7640b0a0fda352bf53ce31cf0e38b7

6 years agofolly::Function: fix swap function and put in correct namespace
Sven Over [Sat, 2 Apr 2016 00:55:57 +0000 (17:55 -0700)]
folly::Function: fix swap function and put in correct namespace

Summary:The swap function belongs in the same namespace as Function: folly.
Also, to avoid amibiguities with a generic swap function in
<algorithm>, we need two variants: one for identical types of
folly::Function, and one for folly::Functions with different
configurations.

Reviewed By: ericniebler

Differential Revision: D3106429

fb-gh-sync-id: 11b04e9bc709d52016ac94c078278410f5ee43c6
fbshipit-source-id: 11b04e9bc709d52016ac94c078278410f5ee43c6

6 years agoupdate SocketAddress::setFromPath() to take a StringPiece
Adam Simpkins [Fri, 1 Apr 2016 18:35:38 +0000 (11:35 -0700)]
update SocketAddress::setFromPath() to take a StringPiece

Summary:Update setFromPath() to accept a StringPiece rather than just std::string
or a plain const char*.

Also fix two other minor issues:
- Leave the old address untouched on failure.  Previously it could leave the
  SocketAddress in a partially updated state.
- Don't assume the input is nul terminated.  Previously the input code read
  one past the specified input length, and copied this into the address,
  assuming it was a nul terminator.  The new code explicitly writes a 0 byte.

Reviewed By: yfeldblum

Differential Revision: D3119882

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

6 years agoCreate the Builtins portability header
Christopher Dykes [Fri, 1 Apr 2016 18:19:53 +0000 (11:19 -0700)]
Create the Builtins portability header

Summary: Because we don't have these builtins under MSVC, and not having to deal with the differences in API in the places that use these builtins is good.

Reviewed By: yfeldblum

Differential Revision: D2984842

fb-gh-sync-id: 34db7455debf81e4abffe57c154eb731ae097ff6
fbshipit-source-id: 34db7455debf81e4abffe57c154eb731ae097ff6

6 years agoCreate the dirent.h portability header
Christopher Dykes [Fri, 1 Apr 2016 18:18:42 +0000 (11:18 -0700)]
Create the dirent.h portability header

Summary: We don't have dirent.h on Windows, but we can emulate its behavior.

Reviewed By: yfeldblum

Differential Revision: D2978570

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

6 years agoCreate the pthread.h portability header
Christopher Dykes [Fri, 1 Apr 2016 18:16:57 +0000 (11:16 -0700)]
Create the pthread.h portability header

Summary: The primary issue is that the pthread implementation we use for Windows defines `pthread_t` as a struct (yes, it is allowed to do this), which breaks a lot of things.

Reviewed By: yfeldblum

Differential Revision: D2862671

fb-gh-sync-id: 551569b6a9e2e374cf77e186e148b6b397f8f25f
fbshipit-source-id: 551569b6a9e2e374cf77e186e148b6b397f8f25f

6 years ago`strndup` is defined on modern OSX
Michael Lee [Fri, 1 Apr 2016 16:32:29 +0000 (09:32 -0700)]
`strndup` is defined on modern OSX

Summary: `strndup` is defined on modern versions of OSX

Reviewed By: Orvid, grp

Differential Revision: D3122635

fb-gh-sync-id: 678792765addf2fb226e835d3b7a67e155ed6dc5
fbshipit-source-id: 678792765addf2fb226e835d3b7a67e155ed6dc5

6 years agofolly: replace old-style header guards with "pragma once"
Sven Over [Fri, 1 Apr 2016 15:31:10 +0000 (08:31 -0700)]
folly: replace old-style header guards with "pragma once"

Summary:Most header files in folly already used "#pragma once" to
protect against multiple inclusion. This diff removes old-style
ifndef/define/endif header guards and replaces them with
pragma once.

In some cases the defined symbol is tested in other header
files. In those cases the "#define" is kept.

Reviewed By: igorsugak

Differential Revision: D3054492

fb-gh-sync-id: 20aa0b9b926a30dd021e4b8f5440e8888874681c
fbshipit-source-id: 20aa0b9b926a30dd021e4b8f5440e8888874681c

6 years agofolly/docs: add documentation about folly::Function
Sven Over [Fri, 1 Apr 2016 14:13:33 +0000 (07:13 -0700)]
folly/docs: add documentation about folly::Function

Summary: This diff adds folly/docs/Function.md

Reviewed By: yfeldblum

Differential Revision: D3120617

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

6 years agoImplement GDB pretty-printers for folly::fibers
Andrii Grynenko [Fri, 1 Apr 2016 00:57:14 +0000 (17:57 -0700)]
Implement GDB pretty-printers for folly::fibers

Summary:This adds basic print functions for FiberManager, Fiber and FiberManager map.
It also adds a global list of fibers to FiberManager. Fibers are only removed from that list on Fiber object destruction, so it shouldn't have any perf impact.

Inspired by tao/server/scripts/fiber_bt.gdb

FiberManager map example:
  (gdb) print_folly_fiber_manager_map
    Global FiberManager map has 2 entries.
      (folly::EventBase*)0x7fffffffdb60 -> (folly::fibers::FiberManager*)0x7ffff5b58480
      (folly::EventBase*)0x7fffffffd930 -> (folly::fibers::FiberManager*)0x7ffff5b58300

FiberManager example:
  (gdb) print_folly_fiber_manager &manager
    (folly::fibers::FiberManager*)0x7fffffffdbe0

    Fibers active: 3
    Fibers allocated: 3
    Fibers pool size: 0
    Active fiber: (folly::fibers::Fiber*)(nil)
    Current fiber: (folly::fibers::Fiber*)(nil)

    Active fibers:
      (folly::fibers::Fiber*)0x7ffff5b5b000   State: Awaiting
      (folly::fibers::Fiber*)0x7ffff5b5b300   State: Awaiting
      (folly::fibers::Fiber*)0x7ffff5b5b600   State: Awaiting

Fiber example: P56244621

Reviewed By: yfeldblum

Differential Revision: D3119616

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

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

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

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

Reviewed By: snarkmaster, ot

Differential Revision: D3115580

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

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

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

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

Reviewed By: yfeldblum, Orvid

Differential Revision: D3119813

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

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

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

Reviewed By: siyengar

Differential Revision: D3117395

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

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

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

Reviewed By: yfeldblum

Differential Revision: D2984232

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

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

Summary:Let's break OSX!

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

Reviewed By: yfeldblum

Differential Revision: D3019938

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

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

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

Reviewed By: yfeldblum

Differential Revision: D3106365

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

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

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

Reviewed By: yfeldblum

Differential Revision: D3116467

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

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

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

Reviewed By: yfeldblum

Differential Revision: D3104296

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