folly.git
7 years agoMove detail/FunctionalExcept to portability/BitsFunctexcept
Christopher Dykes [Fri, 19 Aug 2016 20:45:00 +0000 (13:45 -0700)]
Move detail/FunctionalExcept to portability/BitsFunctexcept

Summary: Because it is a portability header, but was created before portability headers were cool.

Reviewed By: mzlee

Differential Revision: D3743475

fbshipit-source-id: 5d2fe23ce08f0425ce48b4871fa660e69f57cc39

7 years agoUpdating documentation for folly::Synchronized to include upgradable
Aaryaman Sagar [Fri, 19 Aug 2016 19:57:30 +0000 (12:57 -0700)]
Updating documentation for folly::Synchronized to include upgradable

Summary:
This diff updates the documentation for folly::Synchronized to include
upgradable locking

Reviewed By: yfeldblum

Differential Revision: D3740983

fbshipit-source-id: d201fcfa6f62ce168125d2a9caa096e079446efa

7 years agoDon't allow getting singleton after shutdown in strict mode
Andrii Grynenko [Fri, 19 Aug 2016 18:55:15 +0000 (11:55 -0700)]
Don't allow getting singleton after shutdown in strict mode

Summary: Make strict mode stricter, by not allowing singleton to be fetched after shutdown (even with try_get).

Reviewed By: yfeldblum

Differential Revision: D3737925

fbshipit-source-id: 8d5536c4f27e13feee722b5abeb15db6fe3d77bf

7 years agoAdding upgradable locks to Synchronized
Aaryaman Sagar [Fri, 19 Aug 2016 02:32:18 +0000 (19:32 -0700)]
Adding upgradable locks to Synchronized

Summary: This diff adds support for upgradeable locks to folly::Synchronized

Reviewed By: yfeldblum

Differential Revision: D3683205

fbshipit-source-id: 1b91ab07076566b4e5b535f2a2dbe1c8d9f3d1c2

7 years agoSupport for EPOLLPRI
Aravind Anbudurai [Thu, 18 Aug 2016 20:48:15 +0000 (13:48 -0700)]
Support for EPOLLPRI

Summary:
Depends on D3718573 and the other diff to sync tp2 on fbcode after D3718573
lands.

This adds the PRI flag for using on EPOLLPRI events. The test makes a
server/client and client a MSG_OOB message to make it appear as a priority
event.

Masked under a preprocessor directive until libevent upstreams the patch that I
will send for 2.0

Reviewed By: djwatson

Differential Revision: D3722009

fbshipit-source-id: c15233d4739a38092d11c3026c483c7a9c8e5dac

7 years agoFix folly::Expected under MSVC
Christopher Dykes [Thu, 18 Aug 2016 19:50:37 +0000 (12:50 -0700)]
Fix folly::Expected under MSVC

Summary:
There are 3 separate issues that this addresses to get Expected working correctly under MSVC.
The first is simply that it doesn't like `StrictConjunction`. Switching that to `std::conjunction`, which is part of C++17 and already implemented in MSVC, solves that issue.
The second is that MSVC was complaining that all members must be initialized in a `constexpr` constructor, but only one of the base classes of `ExpectedStorage` was being initialized, and, as there were no default `constexpr` constructors for the other bases, they couldn't be initialized. This was solved by making the base class's default constructors `constexpr`.
The last was the most fun, as the simple solutions all resulted in internal compiler errors. MSVC doesn't like SFINAE evaluation in the context of a template type parameter on a static operator defined inline in the class via `friend`. The solution is to simply move the definitions after the class and only include the declarations to be able to mark them as `friend`.

Reviewed By: ericniebler

Differential Revision: D3731833

fbshipit-source-id: ea9244b247b69046866f27cee9fdbd1b2405cafb

7 years agobitmap search
Dave Watson [Thu, 18 Aug 2016 15:38:22 +0000 (08:38 -0700)]
bitmap search

Summary:
Use a bitmap search to find the next wheel timer tick instead of a linear scan.
Need to store the current bitmap bit in the individual timeout to support cancellation.

Given the WHEEL_SIZE of 256, this will reduce to 4 ffsl/cmov instructions.

Reviewed By: yfeldblum

Differential Revision: D3637116

fbshipit-source-id: 1a37e19a5342604ec81735eaf85b72b6f673ea1e

7 years agoremove constant tick
Dave Watson [Thu, 18 Aug 2016 15:38:21 +0000 (08:38 -0700)]
remove constant tick

Summary:
Preciesly calculate the next tick.  Currently only calculates the tick based on the lowest level of wheel timer, so it will still tick at least every WHEEL_SIZE intervals.

Currently the tick calculation is a linear scan over all the buckets, the next diff will optimize this.

Reviewed By: yfeldblum

Differential Revision: D3637096

fbshipit-source-id: 53dd596a2085c05c657cccbc7efba267bbd086a6

7 years agoAdd a pair of util functions for getting and setting the BIO fd
Christopher Dykes [Wed, 17 Aug 2016 21:42:29 +0000 (14:42 -0700)]
Add a pair of util functions for getting and setting the BIO fd

Summary: Because we need to translate them between sockets and file descriptors when we're on Windows.

Reviewed By: yfeldblum

Differential Revision: D3724802

fbshipit-source-id: 07fff6e1bec7b9b90e0d39fd98441466a746b7f7

7 years agoFix folly contbuild
Phil Willoughby [Wed, 17 Aug 2016 18:38:06 +0000 (11:38 -0700)]
Fix folly contbuild

Summary: Started failing when we turned on unused-result errors.

Reviewed By: yfeldblum

Differential Revision: D3728565

fbshipit-source-id: b12aee8d99f725f1a1cfaf30e9afa66bd05f7989

7 years agoMove the type_traits portability header into its own portability header
Michael Lee [Wed, 17 Aug 2016 15:33:12 +0000 (08:33 -0700)]
Move the type_traits portability header into its own portability header

Summary: Split up the big, all-inclusive Portability header a bit

Reviewed By: yfeldblum

Differential Revision: D3723253

fbshipit-source-id: a91c38775825626f3c13853ac8168daa078a169a

7 years agoAllow accept callbacks to be short-circuited in primary event-base
Petr Lapukhov [Wed, 17 Aug 2016 12:39:00 +0000 (05:39 -0700)]
Allow accept callbacks to be short-circuited in primary event-base

Summary:
It looks like we were effectively avoiding short-circuiting callbacks submitted for execution in primary event-base (evb == nulptr). The check was there, but it was never effective, since on `addAcceptCallback` we would mask the `nullptr` with our event base pointer.

I see two ways to fix that: either modify the check

    if (info->eventBase == nullptr) { ...} on line 834

to compare to the presently attached event base, or store `eventBase = nullptr` into callbacks_ list (CallbackInfo struct). The second approach requires more changes (implemented here) but allows the caller to still submit callbacks for execution via notification queue event in primary event base by supplying eventBase parameter != nullptr in addAcceptCallback. I therefore chose the second approach.

The existing unit-tests needed modification to avoid using the "broken" nullptr semantics (most cases were assuming it would be using notification queue signaling). I quickly looked at fbcode, and it looks like we only have a few cases of addAcceptCallback() with nullptr, the unit-tests for those are passing.

NOTE: The removeAcceptCallback() semantics is different with regards to eventBase; nullptr here means "scan all callbacks regardless of event-base they belong to".

Reviewed By: djwatson

Differential Revision: D3714697

fbshipit-source-id: 2362bcff86a7e0604914b1cb7f1471fe4d03e78e

7 years agoInvoking correct callback during TFO fallback
Subodh Iyengar [Wed, 17 Aug 2016 04:52:13 +0000 (21:52 -0700)]
Invoking correct callback during TFO fallback

Summary:
If we fallback from SSL to TFO and the connection times
out, invokeConnectSuccess tries to deliver the connectError,
however we've already delivered the connect callback to the user.

This is bad because we have no way of reporting an error back.
This changes it so that when using SSL and we're scheduling a timeout
when we're falling back, we will schedule a timeout of our own which
will invoke AsyncSSLSocket's timeoutExpired. This will return a handshakeError
instead to the client.

Reviewed By: yfeldblum

Differential Revision: D3708699

fbshipit-source-id: 41fe668f00972c0875bb0318c6a6de863d3ab8f9

7 years agoFix ssl timeouts during TFO
Subodh Iyengar [Wed, 17 Aug 2016 04:52:12 +0000 (21:52 -0700)]
Fix ssl timeouts during TFO

Summary:
Handle handshake timestamps and timeouts when TFO
is used.

If we fallback from TFO to connects, we should unset
the handshake timeout.

When we restart the handshake, we should reset the handshake
timeout and also reset the timestamps of the connection.

Reviewed By: yfeldblum, djwatson

Differential Revision: D3708660

fbshipit-source-id: 960030ca14d9f1cc8cb83059491ceffe6ba8f2ed

7 years agoSimplify TFO write path
Subodh Iyengar [Wed, 17 Aug 2016 04:52:08 +0000 (21:52 -0700)]
Simplify TFO write path

Summary:
We currently call handleInitialReadWrite.
The reason for this is that if the read callback
was set before TFO was done with connecting, then
we need to call handleinitialreadwrite to setup the
read callback similar to how connect invokes handleInitialReadWrite
after it's done.

However handleinitalreadwrite may also call handleWrite
if writeReqHead_ is non null.
Practically this will not happen since TFO will happen on
the first write only where writeReqHead_ will be null.

The current code path though is a little bit complicated.
This simplfies the code so that we dont need to potentially
call handleWrite within a write call.

We schedule the initial readwrite call asynchrously.
The reason for this is that handleReadWrite can actually fail if updating
events fails. This might cause weird state issues once it returns and we
have no mechanism of processing it.

Reviewed By: djwatson

Differential Revision: D3695925

fbshipit-source-id: 72e19a9e1802caa14e872e05a5cd9bf4e34c5e7d

7 years agofolly::Observer
Andrii Grynenko [Wed, 17 Aug 2016 02:25:54 +0000 (19:25 -0700)]
folly::Observer

Summary:
This is a basic implementation of a new Observer API. I mostly see this being useful as a replacement for various configuration subscription APIs (Configerator, SMC etc.)

The library provides an Observer primitive. At any time user can take a Snapshot of data in the Observer (which is a view with the most recent version of the data). New Observer can be created by providing a generator functor. Such Observer mays depend on other Observers and its generator functor is re-run automatically every time, when at least one of the dependencies are updated. The implementation may also ignore intermediate updates and will only use the most recent version of other Observers, when re-computing Observer data.

Reviewed By: yfeldblum

Differential Revision: D3481231

fbshipit-source-id: 96c165f8081cff0141d5814ec2bc88adeb2e1e74

7 years agoSplit up FibersTest into test and Benchmark.
Michael Lee [Tue, 16 Aug 2016 23:47:09 +0000 (16:47 -0700)]
Split up FibersTest into test and Benchmark.

Summary: And clean up the extra main.cpp

Reviewed By: yfeldblum

Differential Revision: D3717445

fbshipit-source-id: 2a5d554824c454b5339514d0d4ca7ae9474abdb9

7 years agoUse std::is_nothrow_swappable under MSVC
Christopher Dykes [Tue, 16 Aug 2016 23:27:28 +0000 (16:27 -0700)]
Use std::is_nothrow_swappable under MSVC

Summary:
The current implementation didn't work for MSVC, as it tries to evaluate `noexcept` expressions in base class lists before the template is instantiated.
`std::is_nothrow_swappable` is coming in C++17, and MSVC already has it, so use it instead.

Reviewed By: yfeldblum

Differential Revision: D3724399

fbshipit-source-id: 7927584618a7870824b2e7242fcae562647f4ef4

7 years agoUse folly::Expected to implement folly::tryTo, a non-throwing variant of folly::to
Eric Niebler [Tue, 16 Aug 2016 23:06:57 +0000 (16:06 -0700)]
Use folly::Expected to implement folly::tryTo, a non-throwing variant of folly::to

Summary:
This change adds a non-throwing interface for folly::to<T>: tryTo<T>, which
returns an Expected<T, ConversionCode>.

Here is how the non-throwing interface compares to the regular interface in
terms of performance. On the successful path, there's generally not much
difference between using the throwing and non-throwing interfaces. For the
error path, tryTo<> is about three orders of magnitude faster than to<>.

Reviewed By: mhx

Differential Revision: D3720512

fbshipit-source-id: dadb8db1b7d7ad8d3e80c1cc69c0480169f9217a

7 years agoUse a base hook rather than a member hook for EventBase callbacks
Christopher Dykes [Tue, 16 Aug 2016 22:44:03 +0000 (15:44 -0700)]
Use a base hook rather than a member hook for EventBase callbacks

Summary:
Using boost intrusive member hooks in complex inheritence heirarchies under MSVC is [unsupported](http://www.boost.org/doc/libs/1_61_0/doc/html/intrusive/usage.html#intrusive.usage.usage_member_hook), and, in combination with a [regression in VS 2015 Update 2](https://connect.microsoft.com/VisualStudio/Feedback/Details/2555433) that is not fixed in Update 3, the async socket tests that use this compile under MSVC, but fail at runtime because the hook pointer ends up pointing at the node's vtable rather than the hook.
This just works around the issue by using a base hook instead.

Reviewed By: djwatson

Differential Revision: D3724521

fbshipit-source-id: a55e36a2e79a15d9943b6090f6194fd480e19074

7 years agoDisable SSL socket cache tests if cache isn't available
Christopher Dykes [Tue, 16 Aug 2016 21:22:59 +0000 (14:22 -0700)]
Disable SSL socket cache tests if cache isn't available

Summary: These tests will always fail if we're building against a version of OpenSSL that doesn't have the extension.

Reviewed By: anirudhvr

Differential Revision: D3724893

fbshipit-source-id: a093d62b9b5ea8239b5d52a66da2a833911b4f47

7 years agoexplicit return types in Partial to work around gcc bug
Nathan Bronson [Tue, 16 Aug 2016 20:11:33 +0000 (13:11 -0700)]
explicit return types in Partial to work around gcc bug

Summary: Workaround for https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70983

Reviewed By: mhx, spacedentist

Differential Revision: D3724410

fbshipit-source-id: 692b1c1f49211a4170900505339794f09cec2369

7 years agoFix extension length counter in client hello parsing.
Kyle Nekritz [Tue, 16 Aug 2016 16:53:26 +0000 (09:53 -0700)]
Fix extension length counter in client hello parsing.

Summary: Orvid noticed this was always throwing on a properly formated client hello, since the sig algs extension length isn't subtracted from the counter. This doesn't actually affect any behavior (except possibly a slight perf hit), but is pretty clowny.

Reviewed By: anirudhvr

Differential Revision: D3722887

fbshipit-source-id: 579993caac96da24fb567ab977b09fca519750a0

7 years agoIntroducing folly::partial
Sven Over [Tue, 16 Aug 2016 15:42:54 +0000 (08:42 -0700)]
Introducing folly::partial

Summary:
This diff adds folly::partial, a function to partially apply
a set of zero or more arguments to a callable. It is similar
to Python's `functools.partial`.

`folly::partial` takes a callable object and additional
arguments and returns a callable with those additional
arguments bound to it. When the returned callable is invoked
with additional arguments, those are appended to the set of
arguments that were passed to `folly::partial`.

It is similar to `std::bind`, but more simple as it does not
support reordering of parameters, but also does not
require you to know how many arguments will be
eventually passed to the callable. Also, `std::bind`
does not support move-only types being passed by-value.
`folly::partial` does:

  void someFunc(std::unique_ptr<Foo>, int);
  auto p = folly::partial(&someFunc, std::move(foo_unique_ptr));
  ...
  std::move(p)(42);

Reviewed By: mhx

Differential Revision: D3252539

fbshipit-source-id: ee093771ac732fa70052b9908dcb75e90ba80efe

7 years agoScopedBoundPort
Yedidya Feldblum [Tue, 16 Aug 2016 02:39:44 +0000 (19:39 -0700)]
ScopedBoundPort

Summary:
[Folly] `ScopedBoundPort`.

An RAII class for `bind`ing, but not `listen`ing, an ephemral port.

Useful when there is a need to test server-down cases - in such cases, a guaranteed-not-listening port is required.

Extracted from a unit-test in Thrift; switches the Thrift test to use this extracted implementation instead.

Reviewed By: djwatson

Differential Revision: D3714293

fbshipit-source-id: 649fba1a0b7f0519b8297a3183d03c5dde23ddc6

7 years agoFix applyTuple to work under MSVC again
Christopher Dykes [Mon, 15 Aug 2016 22:56:35 +0000 (15:56 -0700)]
Fix applyTuple to work under MSVC again

Summary:
Because MSVC didn't like the new version used to add support for multiple tuples.
This also switches to std::index_sequence rather than our own home-grown version.

Reviewed By: yfeldblum

Differential Revision: D3706506

fbshipit-source-id: 724c995fe2671d21f78cd7ffa4b19ea1b278c308

7 years agoLet FOLLY_SAFE_DCHECK be erased by the optimizer in release builds
Yedidya Feldblum [Mon, 15 Aug 2016 22:22:00 +0000 (15:22 -0700)]
Let FOLLY_SAFE_DCHECK be erased by the optimizer in release builds

Summary:
[Folly] Let `FOLLY_SAFE_DCHECK` be erased by the optimizer in release builds.

Just like `DCHECK` is erased by the optimizer, not by the preprocessor, in release builds. `assert`, by contrast, is erased by the preprocessor. But the intention here is to mimic `DCHECK`.

This makes a difference if the expression uses a parameter or local variable. This way, the AST still sees a use of that parameter or local variable in release builds (the optimizer later removes that use). Rather than the AST not seeing any uses of that parameter or local variable, and consequently the compiler emitting a warning or error that the parameter or local variable is unused.

Even so, the expression is never evaluated in release builds. We ensure that by actually using `true || (expr)` as the conditional of our expanded expression in release builds. The `true` comes from `!folly::kIsDebug`, which is a `constexpr bool` expression and is `true` in release builds. The `||` short-circuits; the optimizer sees that the whole expanded expression statically evaluates to `static_cast<void>(0)` and removes it the expanded expression entirely.

Reviewed By: simpkins

Differential Revision: D3701227

fbshipit-source-id: e4fa48ee5a88e45dc08757c14d1944de734796ff

7 years agoReverted commit D3557832
Eric Niebler [Mon, 15 Aug 2016 22:08:57 +0000 (15:08 -0700)]
Reverted commit D3557832

Summary:
This change adds a non-throwing interface for folly::to<T>: tryTo<T>, which
returns an Expected<T, ConversionCode>.

Here is how the non-throwing interface compares to the regular interface in
terms of performance. On the successful path, there's generally not much
difference between using the throwing and non-throwing interfaces. For the
error path, tryTo<> is about three orders of magnitude faster than to<>.

Reviewed By: mhx

Differential Revision: D3557832

fbshipit-source-id: 21f06b3c1a72b06dcf867ed3a3410f51e0fdaf45

7 years agoUse folly::Expected to implement folly::tryTo, a non-throwing variant of folly::to
Eric Niebler [Mon, 15 Aug 2016 21:03:58 +0000 (14:03 -0700)]
Use folly::Expected to implement folly::tryTo, a non-throwing variant of folly::to

Summary:
This change adds a non-throwing interface for folly::to<T>: tryTo<T>, which
returns an Expected<T, ConversionCode>.

Here is how the non-throwing interface compares to the regular interface in
terms of performance. On the successful path, there's generally not much
difference between using the throwing and non-throwing interfaces. For the
error path, tryTo<> is about three orders of magnitude faster than to<>.

Reviewed By: mhx

Differential Revision: D3557832

fbshipit-source-id: 25b29834b6fda474a7cdd43a7dea69ff4ee183c8

7 years agoDon't derive name on singleton creation
Alex Orlov [Mon, 15 Aug 2016 20:58:06 +0000 (13:58 -0700)]
Don't derive name on singleton creation

Summary: instead fetch it only on exception

Reviewed By: andriigrynenko

Differential Revision: D3718303

fbshipit-source-id: a021d150f20ab00fe8146f17b19a69fa0da2b0da

7 years agoAdd a pair of overloads to dynamic::setDefault
Christopher Dykes [Mon, 15 Aug 2016 18:17:38 +0000 (11:17 -0700)]
Add a pair of overloads to dynamic::setDefault

Summary:
This is a workaround to an (already reported) bug in MSVC that results in it not considering the constructors of `dynamic` when attempting to construct the default value of a parameter if the type of the parameter is a universal reference.
It works by simply adding a pair of template specializations that cause MSVC to properly construct the default value.
This also removes the default value on the universal reference version, as the only things that should be using that overload now are non-dynamic values.

Reviewed By: yfeldblum

Differential Revision: D3704990

fbshipit-source-id: 7b85c4e48a1a1023bc2fe0a76a9632b11c4e9364

7 years agoAdd folly::Expected, an alternative to exceptions for non-throwing APIs that can...
Eric Niebler [Mon, 15 Aug 2016 17:20:25 +0000 (10:20 -0700)]
Add folly::Expected, an alternative to exceptions for non-throwing APIs that can fail.

Summary:
Expected is like an Optional with extra state for reporting //why// the Expected is empty. Something like it is currently under review for inclusion in the C++ standard [1], and Andrei Alexandrescu has spoken about it [2]. It corresponds to the Either Monad in Haskell, where it is used as a return type of an API that has more than one failure mode.

By adding folly::Expected, we get a way to implement non-throwing APIs with a consistent and composable interface.

[^1]: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4015.pdf
[^2]: https://channel9.msdn.com/Shows/Going+Deep/C-and-Beyond-2012-Andrei-Alexandrescu-Systematic-Error-Handling-in-C

Reviewed By: mhx

Differential Revision: D3522501

fbshipit-source-id: 48b8ea2dfbd0769f26ec84d2d52fd41db75dc05a

7 years agoadd forgotten apply_tuple_test to Makefile.am v2016.08.15.00
Sven Over [Sun, 14 Aug 2016 19:34:11 +0000 (12:34 -0700)]
add forgotten apply_tuple_test to Makefile.am

Summary:
ApplyTupleTest.cpp was never added to Makefile.am. This commit fixes
that.

Reviewed By: yfeldblum

Differential Revision: D3715213

fbshipit-source-id: e1e914438d44affb6fe683614506338c3a3355c6

7 years agoFix typos in FBVector's comments
Guobao Sun [Sat, 13 Aug 2016 04:45:45 +0000 (21:45 -0700)]
Fix typos in FBVector's comments

Summary:
Found this nit while trying to understand how fbvector works. 1) grwoing -> growing 2) remove that unnecessary "and"

Not sure about reviewers' choosing so I included the author and whoever phabricator suggested to me.

Reviewed By: meyering, yfeldblum

Differential Revision: D3714166

fbshipit-source-id: 8bf575c3f2954feb9e92611acd0aa23c5eee9a0a

7 years agoDon't call strlen(nullptr) in mkdtemp and mkstemp
Christopher Dykes [Fri, 12 Aug 2016 21:29:28 +0000 (14:29 -0700)]
Don't call strlen(nullptr) in mkdtemp and mkstemp

Summary: Because it's a terrible idea. It also didn't work.

Reviewed By: yfeldblum

Differential Revision: D3711443

fbshipit-source-id: 48ab77dfff005347c72daeaf0d27390bb86f4bd1

7 years agofix folly::NotificationQueue for move-only value types
Sven Over [Fri, 12 Aug 2016 21:18:46 +0000 (14:18 -0700)]
fix folly::NotificationQueue for move-only value types

Summary:
folly::NotificationQueue::tryConsume did not compile for
move-only types such as folly::Function. This diff fixes that.

Reviewed By: andriigrynenko

Differential Revision: D3711406

fbshipit-source-id: a52eba3eb31743165e4726f830f2a38d241f25a5

7 years agoprepare for folly::Executor taking folly::Function
Sven Over [Fri, 12 Aug 2016 09:01:54 +0000 (02:01 -0700)]
prepare for folly::Executor taking folly::Function

Summary:
A bunch of small changes that are necessary before we change
the definition of folly::Func from std::function<void()> to
folly::Function<void()>.

Reviewed By: yfeldblum, mzlee

Differential Revision: D3706210

fbshipit-source-id: 198d8a391ef6f545ad4411d316ba9e271ea795e3

7 years agoAdditional changes to MicroLock
Giuseppe Ottaviano [Fri, 12 Aug 2016 06:14:07 +0000 (23:14 -0700)]
Additional changes to MicroLock

Summary:
Changes discussed on the initial MicroLock diff, but that were
accidentally lost before commit.

Reviewed By: luciang, yfeldblum

Differential Revision: D3010789

fbshipit-source-id: 9538ebd1383d1561dd2ce210a2d05e342c6863e6

7 years agoDon't attempt to escape % in SparseByteSetBench
Christopher Dykes [Thu, 11 Aug 2016 22:58:04 +0000 (15:58 -0700)]
Don't attempt to escape % in SparseByteSetBench

Summary: Because it doesn't need to be escaped, and MSVC will warn about invalid escape sequences. If being built with HHVM's settings for MSVC, it's actually an error, as the same warning is produced for multi-byte escape sequences in narrow strings.

Reviewed By: yfeldblum

Differential Revision: D3704596

fbshipit-source-id: 603e57a35f8b7433d1f6e47f4c2882244536b14f

7 years agoConstruct TestObject in AtomicLinkedListTest via a const reference
Christopher Dykes [Thu, 11 Aug 2016 22:38:49 +0000 (15:38 -0700)]
Construct TestObject in AtomicLinkedListTest via a const reference

Summary:
MSVC doesn't currently guarantee left-to-right argument evaluation order for values constructed via braced initializers, and produces a warning if you try to pass an argument with a non-trivial copy constructor by-value in a constructor used by the braced initializer.
This prevents the warning by simply accepting a const reference instead.

Reviewed By: yfeldblum

Differential Revision: D3705073

fbshipit-source-id: 917ec1bb776d6ec4bfefe50907d4c5ac2f2379b1

7 years agoInclude intrin.h in CpuId for the cpuid builtins
Christopher Dykes [Thu, 11 Aug 2016 21:35:16 +0000 (14:35 -0700)]
Include intrin.h in CpuId for the cpuid builtins

Summary: Some shuffling and elmination of other includes exposed the fact that this was calling the cpuid builtins, but hadn't included the header they were defined in.

Reviewed By: yfeldblum

Differential Revision: D3704321

fbshipit-source-id: 332c2d22714abaa74090abe7e7a5b28aec6b905a

7 years agoAdjust the AsyncSocket TestServer to work with Winsock
Christopher Dykes [Thu, 11 Aug 2016 21:26:41 +0000 (14:26 -0700)]
Adjust the AsyncSocket TestServer to work with Winsock

Summary:
Winsock doesn't let you listen to a socket if it hasn't been bound to anything, so bind it to the port first.
TestServer was also never closing the file descriptor, causing the socket to leak.

Reviewed By: yfeldblum

Differential Revision: D3698467

fbshipit-source-id: cca415143009fbee99ebf94d7f46aedc9c59191d

7 years agoDisable the constexpr tests for findFirstSet, findLastSet and nextPowTwo under MSVC
Christopher Dykes [Thu, 11 Aug 2016 21:25:18 +0000 (14:25 -0700)]
Disable the constexpr tests for findFirstSet, findLastSet and nextPowTwo under MSVC

Summary: Because they aren't currently constexpr under MSVC.

Reviewed By: yfeldblum

Differential Revision: D3705095

fbshipit-source-id: ea266ebd9677fb2ba232476160f7c10a23954db9

7 years agoUse explicitly sized integers in the Conv test
Christopher Dykes [Thu, 11 Aug 2016 21:17:31 +0000 (14:17 -0700)]
Use explicitly sized integers in the Conv test

Summary: Because the tests are explicitly dependent on the size of these types, and `long` is a different size on Windows.

Reviewed By: yfeldblum

Differential Revision: D3704488

fbshipit-source-id: e15e95242c5e9c84165a50cfd2c3bc6e0d2e0c49

7 years agoEliminate accidental trigraph in FBString
Christopher Dykes [Thu, 11 Aug 2016 19:43:44 +0000 (12:43 -0700)]
Eliminate accidental trigraph in FBString

Summary: `<::` is a trigraph, which, although removed in C++17, MSVC still warns about the sequences (although it doesn't actually intepret them as trigraphs)

Reviewed By: yfeldblum

Differential Revision: D3704382

fbshipit-source-id: b093f854343db01bc166b1cc44763676fdd97a51

7 years agoUse _r_debug instead of /proc/<pid>/maps for folly::symbolizer
Max Wang [Thu, 11 Aug 2016 18:22:46 +0000 (11:22 -0700)]
Use _r_debug instead of /proc/<pid>/maps for folly::symbolizer

Summary:
Using _r_debug offers a number of benefits:
- It allows us to symbolize addresses in data segments like .bss that
  are marked "[heap]" in /proc/<pid>/maps.
- It requires a lot less code.

It also fixes a preexisting bug where we would fail to symbolize
addresses that fell in any section past the first in any position-
independent objects.  Since `elfFile->getBaseAddress()` should always
return 0 for any PIEs, we wouldn't correctly adjust the address to be
ELF-relative when performing symbol lookup.  (For the first section,
we just used the start of the range we found in /perf/<pid>/maps.)

There is a minor downside:
- We have to open an ELF header for each object in order to check if a
  given address is in the range of some segment.  Before, we used
  /proc/<pid>/maps to make the range check, and only opened the header
  once we knew it was the one we wanted.  In the common case, however,
  where the addresses are from our own executable, we don't open any
  more files than before (and, in fact, one fewer).

Reviewed By: luciang

Differential Revision: D3695872

fbshipit-source-id: 9bdcc77da4d658ffad5c671f9b8ea65a471ed64f

7 years agoRemove ConditionallyExistent, because it violates the C++ spec
Yedidya Feldblum [Thu, 11 Aug 2016 18:08:33 +0000 (11:08 -0700)]
Remove ConditionallyExistent, because it violates the C++ spec

Summary:
[Folly] Remove `ConditionallyExistent`, because it violates the C++ spec.

The spec says that struct and class types occupy at least one byte. But the point of this class is to occupy zero bytes when the condition is false. That can't be made to work.

GCC and Clang support an extension allowing, in some cases, structs to occupy no space at all. But it violates the spec, and MSVC does not support the extension.

There is, sort of, the possibility of empty-base-class-optimization. But it will be very painful to use, and it will only work in some cases.

Since this is broken now, and fixing it would violate the C++ spec and break this under MSVC, it's better just to remove it.

Reviewed By: nbronson, Orvid

Differential Revision: D3696371

fbshipit-source-id: c475c6e15d9ff1bc4c44dc7e336ce74e6db640ef

7 years agoLet applyTuple accept any number of tuples
Sven Over [Thu, 11 Aug 2016 08:40:38 +0000 (01:40 -0700)]
Let applyTuple accept any number of tuples

Summary:
Instead of passing exactly one tuple of arguments to applyTuple,
this diff allows to pass any number (zero or more) of tuples.

This can be very handy when piecing together arguments for
a function call.

Reviewed By: yfeldblum

Differential Revision: D3681499

fbshipit-source-id: a75a448636759f71db8d303e9dccada5b559af54

7 years ago__google_stl_debug_vector: erase() at invalid position: folly/experimental/test:futur...
Lucian Grijincu [Thu, 11 Aug 2016 07:50:06 +0000 (00:50 -0700)]
__google_stl_debug_vector: erase() at invalid position: folly/experimental/test:future_dag_test - FutureDAGTest.RemoveNodeComplex

Summary:
```
[ RUN      ] FutureDAGTest.RemoveNodeComplex
terminate called after throwing an instance of 'std::out_of_range'
  what():  erase() at invalid position
*** Aborted at 1470867850 (Unix time, try 'date -d 1470867850') ***
*** Signal 6 (SIGABRT) (0x133c001ed107) received by PID 2019591 (pthread TID 0x7ff1e6df5980) (linux TID 2019591) (maybe from PID 2019591, UID 4924), stack trace: ***
    @ 00007ff1e702020f folly::symbolizer::(anonymous namespace)::innerSignalHandler(int, siginfo_t*, void*)
    @ 00007ff1e701faf1 folly::symbolizer::(anonymous namespace)::signalHandler(int, siginfo_t*, void*)
    @ 00007ff1e87c714f (unknown)
    @ 00007ff1e7cf6d02 gsignal
    @ 00007ff1e7cf8af5 abort
    @ 00007ff1e73880e4 __gnu_cxx::__verbose_terminate_handler()
    @ 00007ff1e7385ee5 __cxxabiv1::__terminate(void (*)())
    @ 00007ff1e7385f30 std::terminate()
    @ 00007ff1e73861c0 __cxa_throw
    @ 00007ff1e740ae00 std::__throw_out_of_range(char const*)
    @ 00000000004483db std::vector<folly::FutureDAG::Node, std::allocator<folly::FutureDAG::Node> >::_M_erase(__gnu_cxx::__normal_iterator<folly::FutureDAG::Node*, std::vector<folly::FutureDAG::Node, std::allocator<folly::FutureDAG::Node> > >)
    @ 00000000004478fa std::vector<folly::FutureDAG::Node, std::allocator<folly::FutureDAG::Node> >::erase(__gnu_cxx::__normal_iterator<folly::FutureDAG::Node const*, std::vector<folly::FutureDAG::Node, std::allocator<folly::FutureDAG::Node> > >)
    @ 000000000044770a folly::FutureDAG::remove(unsigned long)
    @ 0000000000424bc2 FutureDAGTest::remove(unsigned long)
    @ 000000000041f7ed FutureDAGTest_RemoveNodeComplex_Test::TestBody()
    @ 00007ff1e9b19361 void testing::internal::HandleExceptionsInMethodIfSupported<testing::Test, void>(testing::Test*, void (testing::Test::*)(), char const*)
                       /home/engshare/third-party2/gtest/1.7.0/src/gtest-1.7.0/src/gtest.cc:2420
                       -> /home/engshare/third-party2/gtest/1.7.0/src/gtest-1.7.0/src/gtest-all.cc
    @ 00007ff1e9b029de testing::Test::Run()
                       /home/engshare/third-party2/gtest/1.7.0/src/gtest-1.7.0/src/gtest.cc:2437
                       -> /home/engshare/third-party2/gtest/1.7.0/src/gtest-1.7.0/src/gtest-all.cc
    @ 00007ff1e9b02aca testing::TestInfo::Run()
                       /home/engshare/third-party2/gtest/1.7.0/src/gtest-1.7.0/src/gtest.cc:2612
                       -> /home/engshare/third-party2/gtest/1.7.0/src/gtest-1.7.0/src/gtest-all.cc
    @ 00007ff1e9b02cfc testing::TestCase::Run()
                       /home/engshare/third-party2/gtest/1.7.0/src/gtest-1.7.0/src/gtest.cc:2730
                       -> /home/engshare/third-party2/gtest/1.7.0/src/gtest-1.7.0/src/gtest-all.cc
    @ 00007ff1e9b0d2a7 testing::internal::UnitTestImpl::RunAllTests()
                       /home/engshare/third-party2/gtest/1.7.0/src/gtest-1.7.0/src/gtest.cc:4602
                       -> /home/engshare/third-party2/gtest/1.7.0/src/gtest-1.7.0/src/gtest-all.cc
    @ 00007ff1e9b0d60c testing::UnitTest::Run()
                       /home/engshare/third-party2/gtest/1.7.0/src/gtest-1.7.0/src/gtest.cc:2420
                       -> /home/engshare/third-party2/gtest/1.7.0/src/gtest-1.7.0/src/gtest-all.cc
    @ 00007ff1eb64ec70 RUN_ALL_TESTS()
    @ 00007ff1eb64ec3c main
    @ 00007ff1e7ce10f5 __libc_start_main
    @ 000000000041f2d2 (unknown)
Summary (total time 0.07s):
  PASS: 0
  FAIL: 0
  SKIP: 0
  FATAL: 1
    folly/experimental/test:future_dag_test - FutureDAGTest.RemoveNodeComplex
  TIMEOUT: 0
````

Reviewed By: yfeldblum

Differential Revision: D3700956

fbshipit-source-id: dffca9460ccca1d8069218c5b7310e4d6e061697

7 years agoPolymorphic Functor implementation in Folly::FutureDAG
Shayan Mohanty [Thu, 11 Aug 2016 02:15:36 +0000 (19:15 -0700)]
Polymorphic Functor implementation in Folly::FutureDAG

Summary:
Implements a polymorphic functor for FutureDAGs. In order for FutureDAGs to be stateful they must be wrapped by a class of some sort. This is a really common pattern which we've been using in Gossit (and further - across the RedWood stack) in order to maintain state, and we feel it's generalized enough to be useful elsewhere.

`state` is an instance of the type declared in the template, and the exec* methods wrap go().get() so client-side implementations only have to touch the functor after construction in order to drive their DAGs.

Reviewed By: tjkswaine

Differential Revision: D3685651

fbshipit-source-id: 81169aefcff13ac8cc6cbb6bef6d90047732ad8a

7 years agoGate SysMembarrier to not FOLLY_MOBILE platforms
Michael Lee [Wed, 10 Aug 2016 23:45:18 +0000 (16:45 -0700)]
Gate SysMembarrier to not FOLLY_MOBILE platforms

Summary: Gate `kIsLinux` to not include `FOLLY_MOBILE` because "mobile" linux does not really behave like real linux.

Differential Revision: D3697717

fbshipit-source-id: 1b0f4208d2f71c35399c30f20a71bfa4ba4724e8

7 years agoFix scheduling bug
Dave Watson [Wed, 10 Aug 2016 22:41:29 +0000 (15:41 -0700)]
Fix scheduling bug

Summary:
Noticed this while debugging other timerwheel changes.  Scheduling new events in callbacks may result in us *running them right away* if they land in a bucket we are currently processing.

Instead, round up all the timeouts, then run them separately.  This means you can never schedule a timeout that will run immediately (0ticks), but that's probably fine.

Reviewed By: yfeldblum

Differential Revision: D3679413

fbshipit-source-id: 7e18632f23ea33c072c2718e30b378641895b094

7 years agofix bug in FBString::find
Philip Pronin [Wed, 10 Aug 2016 22:03:37 +0000 (15:03 -0700)]
fix bug in FBString::find

Summary:
Standard (21.4.7.2 / 1):

> Determines the lowest position xpos, if possible, such that both of the following conditions obtain:
- pos <= xpos and xpos + str.size() <= size();
- traits::eq(at(xpos+I), str.at(I)) for all elements I of the string controlled
by str.

The existing logic violates the first requirement for `str.size() == 0` by
unconditionally returning `pos`.

Reviewed By: ot, Gownta

Differential Revision: D3698862

fbshipit-source-id: 9622f1b99b259d2d81ae83795dff9cd94619c725

7 years agoMark FormatArg::error as noreturn
Christopher Dykes [Wed, 10 Aug 2016 20:18:10 +0000 (13:18 -0700)]
Mark FormatArg::error as noreturn

Summary: Because, if no inlining is enabled, the absence of this attribute caused compile errors due to functions that were expected to return a value not returning a value.

Reviewed By: meyering

Differential Revision: D3698413

fbshipit-source-id: 8f3505b17a2fa7b9710e3fb56d18c6ca00feacb3

7 years agoDon't attempt to call getsockname before binding an async socket
Christopher Dykes [Wed, 10 Aug 2016 20:15:18 +0000 (13:15 -0700)]
Don't attempt to call getsockname before binding an async socket

Summary: With WinSock, calling `getsockname` on a socket before it's been bound will always result in an error, so make sure to bind the socket first.

Reviewed By: yfeldblum

Differential Revision: D3698112

fbshipit-source-id: e9efe05323b242add3808ee1a6fec2593beb04ac

7 years agoSpecial-case /dev/null in open in the portability header
Christopher Dykes [Wed, 10 Aug 2016 19:35:31 +0000 (12:35 -0700)]
Special-case /dev/null in open in the portability header

Summary: `/dev/null` doesn't exist on Windows, but thankfully, `NUL` does, and has the same semantics.

Reviewed By: meyering

Differential Revision: D3698007

fbshipit-source-id: 5ef31c6576f988dd747ea3c39e296c244bc640b7

7 years agoUse ReadMostlyMainPtrDeleter in folly::Singleton
Andrii Grynenko [Wed, 10 Aug 2016 02:53:52 +0000 (19:53 -0700)]
Use ReadMostlyMainPtrDeleter in folly::Singleton

Reviewed By: yfeldblum

Differential Revision: D3691541

fbshipit-source-id: 9488c1487ebd0500a23300292215456ca3220f03

7 years agoReadMostlyMainPtrDeleter
Andrii Grynenko [Wed, 10 Aug 2016 02:42:57 +0000 (19:42 -0700)]
ReadMostlyMainPtrDeleter

Summary:
Deleter helper object, which may release multiple ReadMostlyMainPtrs at once.
This allows underlying ref count implementation to perform expensive synchronization operations (e.g. asymmetric heavy barrier only once).

Reviewed By: yfeldblum

Differential Revision: D3691524

fbshipit-source-id: 3ac593b0d813345daba3a81ff4e2eb71b4db292e

7 years agoWait for all threads to finish executing before leaving in an EventBase test
Christopher Dykes [Wed, 10 Aug 2016 00:33:05 +0000 (17:33 -0700)]
Wait for all threads to finish executing before leaving in an EventBase test

Summary:
If one of the assertions failed, it would result in any threads that are still running accessing already-disposed data, so wait for the threads to exit, even when an exception is thrown.

This also slightly cleans up the handling of threads a bit further down, because the mess it was doing was completely unnecessary.

Reviewed By: yfeldblum

Differential Revision: D3692438

fbshipit-source-id: 9082ba248b2cf1062e46c97faf0bc062312ee9ae

7 years agoAdd a utility for disabling some CRT assertions in debug mode
Christopher Dykes [Wed, 10 Aug 2016 00:00:14 +0000 (17:00 -0700)]
Add a utility for disabling some CRT assertions in debug mode

Summary:
See the comment on `msvcReturnInvalidParams` for more info on the issue.
This also adds a use of it to TestUtilTest.

Reviewed By: yfeldblum

Differential Revision: D3691526

fbshipit-source-id: f0f596e9b4c830e1d31de01926162636757329e8

7 years agoSupport read and write from blocking and non-blocking pipes and sockets
Christopher Dykes [Tue, 9 Aug 2016 23:58:56 +0000 (16:58 -0700)]
Support read and write from blocking and non-blocking pipes and sockets

Summary: Also switch `pipe` to return a socket pair instead, as libevent can't poll against a pipe on Windows. And lastly, fix the file descriptor for sockets leaking when close is called.

Reviewed By: yfeldblum

Differential Revision: D3691255

fbshipit-source-id: c684242d255ac5158a4c805d432d345dac1b3bd8

7 years agoAllow the time remaining parameter to nanosleep to be null
Christopher Dykes [Tue, 9 Aug 2016 23:47:40 +0000 (16:47 -0700)]
Allow the time remaining parameter to nanosleep to be null

Summary: Pah! This definitely wasn't causing the lock tests to segfault. Nope, not at all <_>.

Reviewed By: yfeldblum

Differential Revision: D3691120

fbshipit-source-id: 6601db637f22c7b0bc326907a2a7976f6df7c159

7 years agoProperly support socketpair and reading and writing non-blocking sockets
Christopher Dykes [Tue, 9 Aug 2016 23:44:11 +0000 (16:44 -0700)]
Properly support socketpair and reading and writing non-blocking sockets

Summary:
Also handle invalid file descriptors correctly and switch away from `WSASendMsg` for the implementation of `sendmsg` due to limitations imposed by WinSock.
This also fixes up a few places that were explicitly referencing the global version of some socket functions, which was bypassing the socket portability layer.

Reviewed By: yfeldblum

Differential Revision: D3692358

fbshipit-source-id: 05f394dcc9bac0591df7581345071ba2501b7695

7 years agoImplement setenv correctly and support setting values to empty strings
Christopher Dykes [Tue, 9 Aug 2016 23:34:41 +0000 (16:34 -0700)]
Implement setenv correctly and support setting values to empty strings

Summary: Just calling `SetEnvironmentVariableA` wasn't updating `_environ`, which meant that calls to `getenv` weren't reflecting the changes made via `setenv`. The correct way to implement it is using `_putenv_s`, but there's one problem with that: passing an empty string as the value to `_putenv_s` results in the environment variable being unset. To make it possible to set the environment variable to an empty string, we shall dive head-first into the implementation details of the CRT and emerge victorious by blatently ignoring the documentation of `getenv` and modifying the string it returns to terminate it early.

Reviewed By: yfeldblum

Differential Revision: D3691007

fbshipit-source-id: 350c2ec72ec90b9178a9a45b2c2ed2659b788e37

7 years agoLook for the PATH environment variable rather than USER
Christopher Dykes [Tue, 9 Aug 2016 23:24:21 +0000 (16:24 -0700)]
Look for the PATH environment variable rather than USER

Summary: Windows is weird and calls it USERNAME instead, so just use one that everything agrees on: PATH

Reviewed By: yfeldblum

Differential Revision: D3691072

fbshipit-source-id: 579c6484736ef47e130049c29bef8b59c66a4482

7 years agoSupport using fcntl to mark pipes as non-blocking
Christopher Dykes [Tue, 9 Aug 2016 21:57:19 +0000 (14:57 -0700)]
Support using fcntl to mark pipes as non-blocking

Summary: Because the comment was a lie; sockets are blocking by default, not non-blocking.

Reviewed By: yfeldblum

Differential Revision: D3691145

fbshipit-source-id: 5d3c62b3573205fe416d77fe4b5b9fbd593ffd93

7 years agoRemove getTFOSucceeded
Subodh Iyengar [Tue, 9 Aug 2016 14:53:04 +0000 (07:53 -0700)]
Remove getTFOSucceeded

Summary:
The getTFOsucceeded doesn't really work right now
as intended. Currently we check right after sendmsg,
however we can only know whether or not the server
has acked the data after 1-RTT.

This removes getTFOSucceeded. Will fix in another diff.

Differential Revision: D3679235

fbshipit-source-id: 6b2bb01d3743ea7e68ad3cc9a26be6806f17ffbe

7 years agoconstexpr_abs
Yedidya Feldblum [Tue, 9 Aug 2016 02:46:27 +0000 (19:46 -0700)]
constexpr_abs

Summary:
[Folly] `constexpr_abs`.

Is `constexpr`.

Works over integral and floating types.

If given an integral type, the return type is the usigned version of that integral type, thereby avoiding the undefined behavior of `std::abs(std::numeric_limits<int>::min())`.

Reviewed By: simpkins

Differential Revision: D3654072

fbshipit-source-id: 24fefc0c3b055f78ba3e07472c38fb9c550e0f31

7 years agoRemoving noexcept specifications in constructors for Synchronized that call contextua...
Aaryaman Sagar [Tue, 9 Aug 2016 01:43:01 +0000 (18:43 -0700)]
Removing noexcept specifications in constructors for Synchronized that call contextualLock() and contextualRLock()

Summary:
Most mutex lock() functions do not have a noexcept guarantee, saying that the
constructor for Synchronized based on whether the underlying constructor for
the type stored is not enough.  Although this will *rarely* cause bugs, it
probably can be fixed

Reviewed By: simpkins

Differential Revision: D3682974

fbshipit-source-id: ec0bb701d0af41ffc79128fe8db7935a5f19bc70

7 years agoAdd MSG_NOSIGNAL to AsyncSSLSocket
Subodh Iyengar [Tue, 9 Aug 2016 00:55:10 +0000 (17:55 -0700)]
Add MSG_NOSIGNAL to AsyncSSLSocket

Summary:
We are definitely not prepared to handle
SIGPIPEs, so add MSG_NOSIGNAL to sendmsg
of AsyncSSLSocket.

This is a problem which exists in openssl
as well which calls send with flags = 0.
We recently made a change to move the send
into our control, so we can now supply the
flag

Reviewed By: yfeldblum

Differential Revision: D3686679

fbshipit-source-id: ff8fe662e62923c25876bdfd516352639505dca6

7 years agoUpdate documentation for Synchronized
Adam Simpkins [Mon, 8 Aug 2016 19:40:50 +0000 (12:40 -0700)]
Update documentation for Synchronized

Summary:
Update the documentation for Synchronized to remove references to the
various SYNCHRONIZED macros, and document the lock() and withLock() APIs
instead.

Reviewed By: yfeldblum

Differential Revision: D3618871

fbshipit-source-id: e970f9a23e45831d62232eea2ba133a55e5a4a49

7 years agoInclude sys/syscall.h to SysMembarrier
Michael Lee [Mon, 8 Aug 2016 17:54:18 +0000 (10:54 -0700)]
Include sys/syscall.h to SysMembarrier

Summary: It calls syscall, but does not include the header.

Reviewed By: knekritz

Differential Revision: D3586636

fbshipit-source-id: 2ef05fc4b88f236a84b79afa708e98fe21529685

7 years agoMethodology for using DeterministicSchedule support for auxiliary data and global...
Maged Michael [Mon, 8 Aug 2016 17:07:59 +0000 (10:07 -0700)]
Methodology for using DeterministicSchedule support for auxiliary data and global invariants

Summary:
Depends on D3648195

This test example is intended to demonstrate the methodology for using DeterministicSchedule support for auxiliary data and global invariants.
The main goal is fine-grained invariant checking, ideally after every shared update.
The secondary goals are:
- Minimize intrusion in the original code. In this proposed methodology, it is adding a friend.
- Minimize duplication of original tested code. Unfortunately, depending on the original code, it seems that significant duplication may be unavoidable if we don't want to change the original code.

This diff is primarily about the methodology for testing already developed code. I plan to apply what we agree on through this diff to the dynamic MPMCQueue code (D3462592).

A future goal to keep in mind is creating a methodology for developing new code with hooks for DSched aux. data and invariant checking integrated in it, in order to minimize or eliminate duplication of tested code.  In past projects, I used non-standard source code (basically algorithm code) that is automatically translatable through scripts and macros to input to a DSched-like tool as well as to compilable code. The main challenge for such a methodology is to allow the original source code to be standard readable C++ code.

Reviewed By: djwatson

Differential Revision: D3675447

fbshipit-source-id: aae2c9f0550af88dc3a5dcbe53318a75a86b6e2b

7 years agoUse the socket portability layer when needed. v2016.08.08.00
Christopher Dykes [Fri, 5 Aug 2016 22:16:29 +0000 (15:16 -0700)]
Use the socket portability layer when needed.

Summary: This switches the places in Folly that need to explicitly reference the socket portability implementation to do exactly that.

Reviewed By: yfeldblum

Differential Revision: D3299984

fbshipit-source-id: 57cd8ebe66c9055aba66581a8c0fcf6c125d96f9

7 years agoUpper level tests
Dave Watson [Fri, 5 Aug 2016 19:45:58 +0000 (12:45 -0700)]
Upper level tests

Summary:
Add some tests to test the other levels of the timer wheel.

They are slow by necessity.

Reviewed By: yfeldblum

Differential Revision: D3637132

fbshipit-source-id: badf8d37d726dbeb5a8220d50c60b8efdaee7989

7 years agoremove catchupEveryN
Dave Watson [Fri, 5 Aug 2016 19:15:19 +0000 (12:15 -0700)]
remove catchupEveryN

Summary: This feature doesn't make sense when wheeltimer doesn't constantly tick - we always have to get the current clock time.  On the plus side, we'll only be grabbing the clock on timer schedule or timeout, never for individual ticks.

Reviewed By: yfeldblum

Differential Revision: D3637088

fbshipit-source-id: ed8fe52419259332a14b6dc1d357979dcf258a20

7 years agoHandle creating the default crypto context if it doesn't already exist
Christopher Dykes [Fri, 5 Aug 2016 16:49:03 +0000 (09:49 -0700)]
Handle creating the default crypto context if it doesn't already exist

Summary: It's perfectly possible that the default crypto context simply hasn't been created yet, so try to create it if the initial acquisition fails.

Reviewed By: yfeldblum

Differential Revision: D3673138

fbshipit-source-id: 122955df04055ff4f99513b182375d4388dd0305

7 years agoDon't throw in the Watchdog destructor
Christopher Dykes [Fri, 5 Aug 2016 16:48:30 +0000 (09:48 -0700)]
Don't throw in the Watchdog destructor

Summary: As-of C++11, destructors are assumed to be `noexcept` unless explicitly marked otherwise. `Watchdog`'s destructor throws, so switch it to a `LOG(FATAL)` instead.

Reviewed By: andriigrynenko

Differential Revision: D3672621

fbshipit-source-id: 5224ecf85d101462e02e12da257e033bab4db1a1

7 years agoAdding policies for all the upgrade and downgrade mutex transitions that are going...
Aaryaman Sagar [Fri, 5 Aug 2016 03:05:35 +0000 (20:05 -0700)]
Adding policies for all the upgrade and downgrade mutex transitions that are going to be supported by Synchronized

Summary:
This diff contains the lock policies that will be used by Synchronized to
implement mutex transitions by means of heterogenous RAII mutex wrappers

Reviewed By: yfeldblum

Differential Revision: D3665020

fbshipit-source-id: a5509dfd58a1dd6cd60a7d3afe929d0da860926d

7 years agoAdding support for upgradable mutexes to LockTraits
Aaryaman Sagar [Fri, 5 Aug 2016 00:14:45 +0000 (17:14 -0700)]
Adding support for upgradable mutexes to LockTraits

Summary:
This diff adds support for upgradable mutexes to the LockTraits
abstraction used by folly::Synchronized

Reviewed By: yfeldblum

Differential Revision: D3645453

fbshipit-source-id: 30f16eb3fbebc687a4136256f1103962c0e4c465

7 years agoUse std::max_align_t rather than __attribute__((__aligned__)) to get max alignment
Christopher Dykes [Thu, 4 Aug 2016 23:32:18 +0000 (16:32 -0700)]
Use std::max_align_t rather than __attribute__((__aligned__)) to get max alignment

Summary: Because the former is standardized, and the latter is a syntax error under MSVC.

Reviewed By: yfeldblum

Differential Revision: D3672667

fbshipit-source-id: c0149b11367e36ba5574625240c41a167348199f

7 years agoCreate trace points for thread events
Melanie Subbiah [Thu, 4 Aug 2016 23:09:23 +0000 (16:09 -0700)]
Create trace points for thread events

Summary: RequestContext update.

Reviewed By: mibpl

Differential Revision: D3604937

fbshipit-source-id: 07b315fe22a6dba40f0bab53c8092f35c172ae85

7 years agoDon't attempt to mmap an anonymous shared piece of memory
Christopher Dykes [Thu, 4 Aug 2016 23:00:55 +0000 (16:00 -0700)]
Don't attempt to mmap an anonymous shared piece of memory

Summary: The portability implementation of `mmap` for Windows doesn't currently support anonymous shared allocations, as they are non-trivial to manage, and the places this is being used doesn't actually need the memory to be allocated as shared, so allocate it as private instead.

Reviewed By: yfeldblum

Differential Revision: D3671495

fbshipit-source-id: e74d4fd925363fef16c1bf8445da1ff32bf8266d

7 years agoUse std::thread rather than pthreads in AtomicHashMapTest
Christopher Dykes [Thu, 4 Aug 2016 23:00:27 +0000 (16:00 -0700)]
Use std::thread rather than pthreads in AtomicHashMapTest

Summary: The tests aren't dependent on it being pthread, so use standardized stuff instead.

Reviewed By: yfeldblum

Differential Revision: D3665698

fbshipit-source-id: ad91facb75a9c5d7a90bfa294cc98bb07629de1b

7 years agoFix compilation errors when attempting to compile Folly without WIN32_LEAN_AND_MEAN...
Christopher Dykes [Thu, 4 Aug 2016 22:49:36 +0000 (15:49 -0700)]
Fix compilation errors when attempting to compile Folly without WIN32_LEAN_AND_MEAN defined

Summary: Without `WIN32_LEAN_AND_MEAN` defined, including `Windows.h` includes a massive number of extra headers. There are situations that need things that are defined in those headers, so keep folly working even when it isn't defined.

Reviewed By: yfeldblum

Differential Revision: D3671350

fbshipit-source-id: 0de986e17cdd4be386be8a9eeb0075b4af44024d

7 years agoAdd a couple more things to the socket portability layer
Christopher Dykes [Thu, 4 Aug 2016 22:44:27 +0000 (15:44 -0700)]
Add a couple more things to the socket portability layer

Summary: To make it possible for things to actually compile, this adds `socketpair` and the `sockaddr_un` struct, and also removes the `FOLLY_HAVE_UNIX_SOCKETS` define, as it is no longer needed.

Reviewed By: yfeldblum

Differential Revision: D3671158

fbshipit-source-id: 7333470f2e85c24edb935be5e1b94b4edc6e7267

7 years agoDisable the use of direct TLS in ThreadLocalDetail under MSVC
Christopher Dykes [Thu, 4 Aug 2016 22:40:29 +0000 (15:40 -0700)]
Disable the use of direct TLS in ThreadLocalDetail under MSVC

Summary: The comment tells the truth: StaticMeta doesn't mix well with MSVC's TLS implementation.

Reviewed By: yfeldblum

Differential Revision: D3671274

fbshipit-source-id: 293eb32699ad5cee59965f24f985ec7099c90136

7 years agoFix a pair of non-ascii quotes that made their way into AsyncSocket
Christopher Dykes [Thu, 4 Aug 2016 22:40:16 +0000 (15:40 -0700)]
Fix a pair of non-ascii quotes that made their way into AsyncSocket

Summary: Because the lint error was annoying me.

Reviewed By: yfeldblum

Differential Revision: D3671252

fbshipit-source-id: 2962db7616077f531e64c6f2b26ad708c7adf6ed

7 years agoEliminate the atomic porability header's dependence on Windows.h
Christopher Dykes [Thu, 4 Aug 2016 22:38:29 +0000 (15:38 -0700)]
Eliminate the atomic porability header's dependence on Windows.h

Summary:
This is done by using the intrinsic directly instead.
This also marks the input parameter as `volatile`, to make it more clear that that's how it is handled by `_InterlockedExchangeAdd64`.

Reviewed By: yfeldblum

Differential Revision: D3671320

fbshipit-source-id: 6010085ec3b6952a3eb1e952965ec6ad87566db2

7 years agoDon't assume boost::path contains narrow characters
Christopher Dykes [Thu, 4 Aug 2016 22:38:00 +0000 (15:38 -0700)]
Don't assume boost::path contains narrow characters

Summary: Because it contains `wchar_t`'s on Windows.

Reviewed By: yfeldblum

Differential Revision: D3672098

fbshipit-source-id: b49db642dedf48bf7379cf339130c93f52a22f9a

7 years agoDon't try to ignore the SIGPIPE signal if it isn't defined
Christopher Dykes [Thu, 4 Aug 2016 22:37:49 +0000 (15:37 -0700)]
Don't try to ignore the SIGPIPE signal if it isn't defined

Summary: Which is the case when compiling under MSVC.

Reviewed By: pixelb

Differential Revision: D3671357

fbshipit-source-id: b7e51479cae44933c3f1caa21d45775a32f294a3

7 years agoAssume p is not nullptr in storeUnaligned
Christopher Dykes [Thu, 4 Aug 2016 22:05:50 +0000 (15:05 -0700)]
Assume p is not nullptr in storeUnaligned

Summary:
Because we're constructing the value with a placement new, which has some of C++'s most unhelpful behavior ever put into the spec: If `p` is `nullptr` and we are not compiling in C++14 mode, where the restriction was changed, then the placement new will do absolutely nothing at all. By adding the assumption that `p` is not `nullptr`, we'll trip a segfault in release mode rather than failing silently.
Note that MSVC would generate the nullptr check regardless of which mode it's in, so this assume forces the removal of the check.

Reviewed By: yfeldblum

Differential Revision: D3651116

fbshipit-source-id: ee15a38f85ce4e3cb3186fda0b7bcca39acda27a

7 years agoRequestContext::create should call onUnset callback
Mirek Klimos [Thu, 4 Aug 2016 19:09:50 +0000 (12:09 -0700)]
RequestContext::create should call onUnset callback

Summary: melaniesubbiah introduced onSet / onUnset callbacks on RequestData in D3604948; we need unset() to be called when an RC is overriden with RequestContext::create() so that things work as expected. Also, change the order of calling onSet / onUnset - from RequestData perspective, it shouldn't look like there are two contexts set at the same time

Reviewed By: palmtenor

Differential Revision: D3667017

fbshipit-source-id: b9bfb858fe65ffb11de8e6d6f13b8f4cf6266bc9

7 years agoInclude the fcntl.h portability header where it's needed
Christopher Dykes [Thu, 4 Aug 2016 17:50:19 +0000 (10:50 -0700)]
Include the fcntl.h portability header where it's needed

Summary: There are a few places that need this, so make it so.

Reviewed By: yfeldblum

Differential Revision: D3667234

fbshipit-source-id: 84c16e1b036d9c2afddf03aa84c43210c5672f0a

7 years agoSupport TimeUtil under MSVC
Christopher Dykes [Thu, 4 Aug 2016 17:49:23 +0000 (10:49 -0700)]
Support TimeUtil under MSVC

Summary: Well, make it compile anyways. There's no simple equivelant for Windows, so just return 0 instead.

Reviewed By: yfeldblum

Differential Revision: D3667474

fbshipit-source-id: 02224c6666dfcfdec237bfbbd4714170407a952a

7 years agoFix the Windows portability implementations of mmap and munmap
Christopher Dykes [Thu, 4 Aug 2016 17:48:08 +0000 (10:48 -0700)]
Fix the Windows portability implementations of mmap and munmap

Summary: Because they simply didn't work before.

Reviewed By: yfeldblum

Differential Revision: D3666894

fbshipit-source-id: 7a34073b2bbc43c489ad963c28fac24dcbb187f2

7 years agoFix Enumerate C++17 Support
Nick Terrell [Thu, 4 Aug 2016 00:53:52 +0000 (17:53 -0700)]
Fix Enumerate C++17 Support

Summary: `RangeEnumerator`s `begin()` and `end()` could return different values, but `Enumerator`s `operator ==` didn't support comparision with different types.

Reviewed By: yfeldblum

Differential Revision: D3662576

fbshipit-source-id: cfd10fffd220c70191ce0ac2ed78edd35daf5538

7 years agoDon't use a macro for no reason
Christopher Dykes [Thu, 4 Aug 2016 00:45:28 +0000 (17:45 -0700)]
Don't use a macro for no reason

Summary: There is no reason at all for this to be a macro, so get rid of the macro.

Reviewed By: igorsugak, yfeldblum

Differential Revision: D3666339

fbshipit-source-id: cf131fd4182fe89c5ce0c981edf9bd7fb46158a9

7 years agoDon't name a member small
Christopher Dykes [Thu, 4 Aug 2016 00:22:57 +0000 (17:22 -0700)]
Don't name a member small

Summary:
Because, if you aren't compiling with `WIN32_LEAN_AND_MEAN`, including `WinSock2.h` will result in `small` being `#define`'d as `char`. The best way to work around the issue is to simply change the name of the member to something else.
It would also have been possible to solve the problem by adding a section to `folly/portability/Windows.h`, but that would have required including that header in `folly/Function.h`, which there really is no reason to do.

Reviewed By: yfeldblum

Differential Revision: D3666417

fbshipit-source-id: 21c0363a4b77bb01e4ec20b2fc625c40729879c3

7 years agoFix the mode being used in the implementation of open in the Fcntl portability header
Christopher Dykes [Wed, 3 Aug 2016 22:09:29 +0000 (15:09 -0700)]
Fix the mode being used in the implementation of open in the Fcntl portability header

Summary: The mode parameter to `open` under MSVC is not the same as the mode parameter everywhere else, so we need to do a bit of translation.

Reviewed By: yfeldblum

Differential Revision: D3651218

fbshipit-source-id: 80df1e15f34b8d66533256107d8c9218f757fde2