folly.git
7 years agodelete const rvalue reference ctor of folly::Function v2016.09.05.00
Philip Pronin [Fri, 2 Sep 2016 16:51:19 +0000 (09:51 -0700)]
delete const rvalue reference ctor of folly::Function

Summary:
This code compiles and is causing stack overflow at runtime:

```
using F = folly::Function<void()>;
void foo(F);

F bar;
auto baz = [bar = std::move(bar)] {
  foo(std::move(bar));
};
baz();
```

The bug there is that `baz` is missing `mutable` keyword, so when
constructing argument for `foo`, `F(const F&&)` is called, which is selecting
`template <typename Fun> F(Fun&&)` (where `Fun = F const`) and we end up in
an infinite `F<Fun>(Fun&&) <-> F<Fun>(Fun&&, SmallTag|HeapTag)` cycle.

This diff transforms this easy-to-make-bug into compile-time error.

Reviewed By: yfeldblum

Differential Revision: D3810269

fbshipit-source-id: f80a18ab02bd0715d692cf67c3c8943f557c2982

7 years agozstd is no longer in beta -- s/ZSTD_BETA/ZSTD/g
Chip Turner [Thu, 1 Sep 2016 23:40:08 +0000 (16:40 -0700)]
zstd is no longer in beta -- s/ZSTD_BETA/ZSTD/g

Summary:
itshappening

Need to override unit test failures after repeated landing failures unrelated
to the change.

Reviewed By: yfeldblum

Differential Revision: D3803301

fbshipit-source-id: b6581e3d2ff6baba57eb0f61b890a61107118bf7

7 years agoFix folly conversions for Clang with GCC5's libstdc++
Phil Willoughby [Thu, 1 Sep 2016 20:09:24 +0000 (13:09 -0700)]
Fix folly conversions for Clang with GCC5's libstdc++

Summary: Now builds correctly

Reviewed By: meyering

Differential Revision: D3763879

fbshipit-source-id: e272fbcc28a74fcf36b63a0135534c5063a7c4ee

7 years agoAdd ScopeGuard.h include in places where it is needed.
Eric Niebler [Thu, 1 Sep 2016 18:06:06 +0000 (11:06 -0700)]
Add ScopeGuard.h include in places where it is needed.

Summary: Fix code that is transitively including ScopeGuard.h via FBString.h.

Reviewed By: yfeldblum

Differential Revision: D3801328

fbshipit-source-id: 7e1dc37cd6dd3aa691dd1baf31385d3a8c51eed0

7 years agoProperly report observable returning nullptr
Andrii Grynenko [Wed, 31 Aug 2016 21:12:57 +0000 (14:12 -0700)]
Properly report observable returning nullptr

Summary: Make sure exception is propagated to caller (especially when Observer is just created).

Reviewed By: yfeldblum

Differential Revision: D3798991

fbshipit-source-id: cee8452cfd40dcfbaf0e2ae2c2ee628af0362c6e

7 years agoUpdate FBVector.md
Colin Ni [Wed, 31 Aug 2016 19:59:54 +0000 (12:59 -0700)]
Update FBVector.md

Summary:
Minor changes. Simplified the memory-handling explanation; also fixed some grammar issues and re-worded some confusing paragraphs. (I suggest reading the edited and unedited versions separately.)
Closes https://github.com/facebook/folly/pull/459

Reviewed By: simpkins

Differential Revision: D3779296

Pulled By: Orvid

fbshipit-source-id: 24b086cbd0b67e4c592731aeec6a7ffc14ff0319

7 years agoupdate TimeseriesHistogram::rate to return a template type
Adam Simpkins [Wed, 31 Aug 2016 19:21:13 +0000 (12:21 -0700)]
update TimeseriesHistogram::rate to return a template type

Summary:
Update TimeseriesHistogram::rate(int level) to have a configurable return type,
similar to the rate(TimeType start, TimeType end) function, as well as the
avg() functions.  I believe it was simply an oversight initially that this
version of rate did not have a configurable return type.

Since rate() and avg() are template methods, their full definitions should
really be available in TimeseriesHistogram.h rather than
TimeseriesHistogram-defs.h.  This also fixes that problem.  Most of the logic
in these functions isn't actually dependent on the return type, so that was
split out into separate non-template helper functions that are still in
TimeseriesHistogram-defs.h

Reviewed By: yfeldblum

Differential Revision: D3776017

fbshipit-source-id: 7deebf5b9ea1be143b5d772a15246706cb0cae80

7 years agoConsolidate namespace for using GFlags.
Zonr Chang [Wed, 31 Aug 2016 18:37:58 +0000 (11:37 -0700)]
Consolidate namespace for using GFlags.

Summary:
Default namespace for GFlags has been changed from "google" to "gflags".

See: https://github.com/gflags/gflags/commit/d9d06b9.
Closes https://github.com/facebook/folly/pull/466

Reviewed By: yfeldblum

Differential Revision: D3787144

Pulled By: Orvid

fbshipit-source-id: db7bcaf4e2bcd46b022a48b17b50ef155570f296

7 years agoReverted commit D3755446
Philip Pronin [Wed, 31 Aug 2016 12:10:43 +0000 (05:10 -0700)]
Reverted commit D3755446

Summary:
Move ThreadLocal object destruction to occur under the lock to avoid races.
This causes a few cascading changes - the Tag lock needs to be a recursive_mutex so
constructing a new object while destroying another st. Also, forking requires
a new mutex to avoid deadlocking on accessing a recursive_mutex across a fork()

Reviewed By: andriigrynenko

Differential Revision: D3755446

fbshipit-source-id: f1f1f92175eb39e77aaa2add6915e5c9bb68d0fb

7 years agoFix folly::SocketAddress::reset
Naizhi Li [Wed, 31 Aug 2016 05:56:57 +0000 (22:56 -0700)]
Fix folly::SocketAddress::reset

Summary:
Currently reset does not actually reset it. This change
make sure it does the job.

Reviewed By: yfeldblum

Differential Revision: D3788869

fbshipit-source-id: 771a641e31bb3cf307e5bebb979b81a3d3feea55

7 years agoExpose SSL key materials to debug SSL
Gabriel Grise [Wed, 31 Aug 2016 02:55:12 +0000 (19:55 -0700)]
Expose SSL key materials to debug SSL

Summary: Adding two methods to export the parameters used to generate the key material   (key_block). These parameter can be used to decrypt a TLS session from a packet capture.

Reviewed By: anirudhvr

Differential Revision: D3687099

fbshipit-source-id: 04137f34dd32c387a1b7aec04b3ed6066f123a8e

7 years agoUse a normal variable if thread local is not available.
Michael Lee [Wed, 31 Aug 2016 02:29:59 +0000 (19:29 -0700)]
Use a normal variable if thread local is not available.

Summary: This should not affect correctness, but it means SharedMutex would need to share the variable across threads.

Reviewed By: yfeldblum

Differential Revision: D3621227

fbshipit-source-id: dc1baa7c47cd2d459cd0ef89451bcd7fd4d553aa

7 years agoFix joining threads twice when exiting EventBaseTest.RunInThread normally
Zonr Chang [Tue, 30 Aug 2016 22:46:50 +0000 (15:46 -0700)]
Fix joining threads twice when exiting EventBaseTest.RunInThread normally

Summary:
10e9cd3 set up SCOPE_EXIT to join threads (wait for them to exit) on
exceptions. However, it is also called on normal exit. Remove the
original code at the end for joining threads.

This fixes EventBaseTest regression.
Closes https://github.com/facebook/folly/pull/468

Reviewed By: yfeldblum

Differential Revision: D3792977

Pulled By: Orvid

fbshipit-source-id: e4b12202ae3e35fd942ad2ca1d8d405cf1a28f68

7 years agoDefine "__STDC_FORMAT_MACROS" for using PRI{u,x}64 in C++.
Zonr Chang [Tue, 30 Aug 2016 20:41:51 +0000 (13:41 -0700)]
Define "__STDC_FORMAT_MACROS" for using PRI{u,x}64 in C++.

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

Reviewed By: yfeldblum

Differential Revision: D3787151

Pulled By: Orvid

fbshipit-source-id: e255c9daea9917082f684e52e27984770c9f08b1

7 years agoMake type conversion from Expected<> to bool explicitly.
Zonr Chang [Tue, 30 Aug 2016 20:37:29 +0000 (13:37 -0700)]
Make type conversion from Expected<> to bool explicitly.

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

Reviewed By: yfeldblum

Differential Revision: D3787146

Pulled By: Orvid

fbshipit-source-id: 17433a256338bd107eec41e69af3eef58de3339b

7 years agoAdded a better check for openssl
Bruno Goncalves [Tue, 30 Aug 2016 20:37:13 +0000 (13:37 -0700)]
Added a better check for openssl

Summary:
For some systems like my Fedora 23 is missing -lcrypto library to be together with -lssl

The following error ocurr running 'make check':
avoid undefined reference to symbol 'EVP_DigestInit_ex
Closes https://github.com/facebook/folly/pull/428

Reviewed By: yfeldblum

Differential Revision: D3787160

Pulled By: Orvid

fbshipit-source-id: 8ff69d00c9c752d0a005d2a5e8d68e65347cc43b

7 years agoModified ref-qualifiers return type for Optional::value() and Optional::operator*
Anand Mazumdar [Tue, 30 Aug 2016 06:21:40 +0000 (23:21 -0700)]
Modified ref-qualifiers return type for Optional::value() and Optional::operator*

Summary:
Optional::value() returns a temporary object when the object is an rvalue. This is different in semantics then what boost::optional/std::optional do.

The decision to make the copy or not should be up to the user and not the library. Consider an example:

```
void F(Optional<T> &&opt) {
  T&& t = std::move(opt).get();
  // I know `opt` is alive in this scope, I should be able to keep a rvalue ref to the internals
}
// if we were to return a `T`, that would actually return a new temporary.
```

```
void G(T&& t);
G(std::move(opt).get()); // This could have surprising behavior too !
```

This change modified the return type to be `T&&` and also introduces an extra overload for `const T&&`. Also, deleted two test-cases that assume the lifetime to be extended. This is a breaking change but this brings folly::Optional on parity with other siblings.
Closes https://github.com/facebook/folly/pull/353

Reviewed By: ddrcoder

Differential Revision: D3714962

Pulled By: yfeldblum

fbshipit-source-id: 1794d51590062db4ad02fc8688cb28a06712c076

7 years agoFix ThreadCachedInt race condition
Dan Schatzberg [Mon, 29 Aug 2016 23:42:44 +0000 (16:42 -0700)]
Fix ThreadCachedInt race condition

Summary:
Move ThreadLocal object destruction to occur under the lock to avoid races.
This causes a few cascading changes - the Tag lock needs to be a recursive_mutex so
constructing a new object while destroying another st. Also, forking requires
a new mutex to avoid deadlocking on accessing a recursive_mutex across a fork()

Reviewed By: andriigrynenko

Differential Revision: D3755446

fbshipit-source-id: bb4c4f29bab98d763490df29b460066f124303e0

7 years agoResolve fibers-futures dependency
Andrii Grynenko [Mon, 29 Aug 2016 23:02:38 +0000 (16:02 -0700)]
Resolve fibers-futures dependency

Reviewed By: mzlee

Differential Revision: D3780312

fbshipit-source-id: c42c8f0a06b82520ee1b46f105a2a85ad524c442

7 years agogen::dereference should perfectly-forward unwrapped values
Yedidya Feldblum [Mon, 29 Aug 2016 21:57:05 +0000 (14:57 -0700)]
gen::dereference should perfectly-forward unwrapped values

Summary:
[Folly] `gen::dereference` should perfectly-forward unwrapped values.

The problem comes in when the wrapped value is not actually a pointer, but is actually an rvalue-ref to some other kind of wrapper type with `Inner&& operator*() &&`. In such cases, the compiler emits a type mismatch error that it cannot cast `Inner` to `Inner&&`, with the errors originating in `Dereference::foreach` and `Dereference::apply`.

Fixes a couple other missing-forwarding and extra-forwarding bugs.

Reviewed By: ddrcoder

Differential Revision: D3776617

fbshipit-source-id: 6926fc18244a572846b22d428bd407d37fb20aa1

7 years agoUse Synchronized in RequestContext
Yedidya Feldblum [Mon, 29 Aug 2016 21:01:15 +0000 (14:01 -0700)]
Use Synchronized in RequestContext

Summary:
[Folly] Use `Synchronized` in `RequestContext`.

Because we can. And it makes the code a tad simpler and also enforces access correctness a tad.

Also use `folly::make_unique` in `RequestContextTest` to keep balance between the explicit `new` and `delete` ops.

Reviewed By: markisaa

Differential Revision: D3781115

fbshipit-source-id: 63b41ddd8009e9546e3be5f89bdd23a4d791105c

7 years agoAdd default constructor to folly::IOBuf::Iterator.
Nick Terrell [Mon, 29 Aug 2016 16:50:36 +0000 (09:50 -0700)]
Add default constructor to folly::IOBuf::Iterator.

Summary:
Iterators must be default constructible.
folly::IOBuf can now be used as a range in ranges-v3.

Reviewed By: yfeldblum

Differential Revision: D3782536

fbshipit-source-id: 854813b4e3336aba50048649e6ae7b375d49e382

7 years agoFix up TokenBucket.h in Makefile headers
Jon Maltiel Swenson [Mon, 29 Aug 2016 16:30:52 +0000 (09:30 -0700)]
Fix up TokenBucket.h in Makefile headers

Summary: Title.

Reviewed By: andreazevedo

Differential Revision: D3785212

fbshipit-source-id: c551e0367196cbc68cf39a5d40f2b324883fcdf4

7 years agoGeneralized and polished folly::TokenBucket v2016.08.29.00
Philipp Unterbrunner [Mon, 29 Aug 2016 13:27:23 +0000 (06:27 -0700)]
Generalized and polished folly::TokenBucket

Summary: Added support for user-defined clock classes, improved comments, and removed part of the std::atomics use that had no effect on thread-safety.

Reviewed By: yfeldblum

Differential Revision: D3708378

fbshipit-source-id: 1a933c3707c12311584a3b33afd773ee91577167

7 years agoAdded fiber-compatible semaphore.
Lee Howes [Fri, 26 Aug 2016 22:58:45 +0000 (15:58 -0700)]
Added fiber-compatible semaphore.

Summary: Adds a standard semaphore type with signal and wait methods that is safe to use in both multi-threaded contexts and from fibers.

Reviewed By: andriigrynenko

Differential Revision: D3778943

fbshipit-source-id: 6997f1fb870739e07f982399dbebfd8b3e45daa2

7 years agoUse folly::Random and ensure M_PI is defined
Christopher Dykes [Fri, 26 Aug 2016 20:11:03 +0000 (13:11 -0700)]
Use folly::Random and ensure M_PI is defined

Summary: Because MSVC doesn't have `random()` and `M_PI` is guarded by a pre-processor macro.

Reviewed By: yfeldblum

Differential Revision: D3767159

fbshipit-source-id: fcb1c41cd75925455c76efad38423cd52bd98aeb

7 years agocollectOne
Yunqiao Zhang [Fri, 26 Aug 2016 19:53:24 +0000 (12:53 -0700)]
collectOne

Summary:
The resultant future of collectOne will be fulfilled when the first one of
the future in the list completes without exception. If all input futures throws
exception, the resultant future will get the last exception that was thrown.

Reviewed By: andriigrynenko

Differential Revision: D3764760

fbshipit-source-id: 76484254e35182eddc8266865853d65c28170f82

7 years agoAdd CachelinePadded<T> to folly.
David Goldblatt [Fri, 26 Aug 2016 19:40:58 +0000 (12:40 -0700)]
Add CachelinePadded<T> to folly.

Summary:
This class allows easy insertion of padding bytes after an object to ensure
that two cacheline-padded elements of an array don't engage in false sharing.

Reviewed By: yfeldblum

Differential Revision: D3485144

fbshipit-source-id: a3ece1e34566b20f94ff9f66532b2386ab19a9b1

7 years agoRemove now-useless FOLLY_MALLOC_H_
Nicholas Ormrod [Fri, 26 Aug 2016 18:41:38 +0000 (11:41 -0700)]
Remove now-useless FOLLY_MALLOC_H_

Summary:
Malloc.h is intentionally included multiple times, once from folly and once from libstdc++. The current ###pragma once## implicitly allows this to happen. FBString.h has an undef for Malloc.h's include guard, which originally accomplished this goal.

The undefing code is presently moot, since its functionality has been replaced by ###pragma once##. Remove it.

I noticed this when ott was copying FBString over to libstdc++ in D3757853.
The diff that switched the include guards to pragmas was a codemod, and was not specific to fbstring. D3054492

Reviewed By: ot

Differential Revision: D3758119

fbshipit-source-id: e796d039a031d5f842ed39bf55a6b1aeb2686bc4

7 years agoUse intrusive base hook rather than a member hook
Christopher Dykes [Fri, 26 Aug 2016 17:51:13 +0000 (10:51 -0700)]
Use intrusive base hook rather than a member hook

Summary: Because MSVC is not happy about the member hook when used in complex inheritence scenarios.

Reviewed By: yfeldblum

Differential Revision: D3774513

fbshipit-source-id: 9e1ef8dd76d966de339f8486ff1a1d0ab1571849

7 years agoSuppress an abort in FileUtilTest under MSVC
Christopher Dykes [Thu, 25 Aug 2016 21:53:29 +0000 (14:53 -0700)]
Suppress an abort in FileUtilTest under MSVC

Summary: Because MSVC is right, this shouldn't be happening, but we test weird things.

Reviewed By: yfeldblum

Differential Revision: D3773486

fbshipit-source-id: 50f28bf0ac8ff53f63231b4f6f9ce050b509b464

7 years agolseek returns the new offset, not 0
Christopher Dykes [Thu, 25 Aug 2016 21:52:59 +0000 (14:52 -0700)]
lseek returns the new offset, not 0

Summary:
With how this was written it would fail if the call to `lseek` failed, or if it succeeded, unless it was setting the length to 0, in which case it would succeed.
This makes it work right.

Reviewed By: yfeldblum

Differential Revision: D3773311

fbshipit-source-id: 3dc94502d0c4259f6f2766b4c0903c081d7c36ab

7 years agoAllow locking nullptr if the length is 0
Christopher Dykes [Thu, 25 Aug 2016 21:52:05 +0000 (14:52 -0700)]
Allow locking nullptr if the length is 0

Summary:
Because apparently this is valid (and we test it).
This also adds a check in mmap to make sure we aren't passing an invalid handle to `MapViewOfFileEx`.

Reviewed By: yfeldblum

Differential Revision: D3772853

fbshipit-source-id: 11593997a3fb12b7b391c5e52661060b71341aef

7 years agoFix SimplerObservable build with -Werror=unused-local-typedefs
Andrii Grynenko [Thu, 25 Aug 2016 18:23:20 +0000 (11:23 -0700)]
Fix SimplerObservable build with -Werror=unused-local-typedefs

Reviewed By: yfeldblum

Differential Revision: D3765642

fbshipit-source-id: 9b1cc4007c553da5082799fa4ed0af8f7850ec8d

7 years agoDon't pass the single quote format parameter when formatting things
Christopher Dykes [Thu, 25 Aug 2016 17:18:25 +0000 (10:18 -0700)]
Don't pass the single quote format parameter when formatting things

Summary:
We are formatting an integer value, so it is doing absolutely nothing at all, as confirmed by the version I implemented for MSVC, which does output the value with digit separators, which fails the unit tests because the unit test expects the digit separators to not be present. What's more, whe specifically assert that the user has not requested digit separators in the format string.
This also kills the MSVC special case entirely because it's not needed.

Reviewed By: yfeldblum

Differential Revision: D3768405

fbshipit-source-id: 388cd0ca9699e257c71798b9bf355aa651748e60

7 years agoEasy: Mark folly::static_function_deleter::operator() const
Felix Handte [Thu, 25 Aug 2016 00:42:37 +0000 (17:42 -0700)]
Easy: Mark folly::static_function_deleter::operator() const

Summary:
First, folly::static_function_deleter::operator() is in fact a const
operation, so this should be a reasonable change. The motivation for this is
to make folly::ThreadLocalPtr and folly::static_function_deleter play nice with
each other.

Minimal example:
```lang=c++

void deleter(int* ptr) {
  free(ptr);
}

int main(int argc, char* argv[]) {
  folly::ThreadLocalPtr<int> tl;
  tl.reset(std::unique_ptr<int, folly::static_function_deleter<int, deleter>>(
      new int(0)));
  return 0;
}
```

Currently produces:
```
folly/ThreadLocal.h:207:7: error: no matching function for call to object of type 'const folly::static_function_deleter<int, &deleter>'
      delegate(ptr);
      ^~~~~~~~
Test.cpp:10:6: note: in instantiation of function template specialization 'folly::ThreadLocalPtr<int, void>::reset<int, folly::static_function_deleter<int, &deleter>, void>' requested here
  tl.reset(std::unique_ptr<int, folly::static_function_deleter<int, deleter>>(new int(0)));
     ^
folly/Memory.h:91:8: note: candidate function not viable: 'this' argument has type 'const folly::static_function_deleter<int, &deleter>', but method is not marked const
  void operator()(T* t) { f(t); }
       ^
1 error generated.
```

With the fix, the build succeeds.

Reviewed By: yfeldblum

Differential Revision: D3764624

fbshipit-source-id: c28c791b79f1415704c205c36bfda2d888d6c010

7 years agoFix a typo in ScopeGuard.h
Stephane Sezer [Wed, 24 Aug 2016 23:26:18 +0000 (16:26 -0700)]
Fix a typo in ScopeGuard.h

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

Reviewed By: yfeldblum

Differential Revision: D3765911

Pulled By: Orvid

fbshipit-source-id: 30de0b99ec622471e52a775835b5a0564437febc

7 years agoDon't assume unsigned long is 64-bit
Christopher Dykes [Wed, 24 Aug 2016 23:25:11 +0000 (16:25 -0700)]
Don't assume unsigned long is 64-bit

Summary: Because it isn't. Not on MSVC anyways.

Reviewed By: yfeldblum

Differential Revision: D3767326

fbshipit-source-id: 0f9421773819999f99be38ae0646ae3e81bde4e4

7 years agoUse cbegin and cend in static reflection container traits
Yedidya Feldblum [Wed, 24 Aug 2016 21:25:31 +0000 (14:25 -0700)]
Use cbegin and cend in static reflection container traits

Summary:
[Thrift] Use `cbegin` and `cend` in static reflection container traits.

These are functions that all the C++ standard library containers have.

And add the two methods to `folly::sorted_vector_set` and `folly::sorted_vector_map`.

Reviewed By: juchem

Differential Revision: D3763187

fbshipit-source-id: 9d467a1cf391206eba671f454da428323c4a566a

7 years agoAllow building the NotificationQueue tests without event FDs
Christopher Dykes [Wed, 24 Aug 2016 20:59:55 +0000 (13:59 -0700)]
Allow building the NotificationQueue tests without event FDs

Summary:
Because event file descriptors don't exist on Windows.
This also disables the fork() test on Windows, as we don't have fork().

Reviewed By: yfeldblum

Differential Revision: D3766266

fbshipit-source-id: af8d81615957a3d7bdfcb204afbf648723921f76

7 years agoExplicit typecast of unused constant to make 32 bit compiler happy.
Michael Callahan [Wed, 24 Aug 2016 20:48:02 +0000 (13:48 -0700)]
Explicit typecast of unused constant to make 32 bit compiler happy.

Summary:
GCC 5.3 is confused that we might be trying to assign a 64
bit value to a 32 bit size_t although we are clearly not.  Just
typecast the confusion away.

Reviewed By: yfeldblum

Differential Revision: D3762839

fbshipit-source-id: 1b705c4e70b782fdb106844fee52ab25aa038acd

7 years agoCycle detection
Andrii Grynenko [Wed, 24 Aug 2016 18:24:33 +0000 (11:24 -0700)]
Cycle detection

Summary:
1. This implements a GraphCycleDetector which can check if newly added edge belongs to a cycle in a directed graph. GraphCycleDetector is used to detect cycles between Observers when creator function is run.
2. This also fixes a bug where new dependencies could be saved even if Observer creator failed.

Reviewed By: yfeldblum

Differential Revision: D3746743

fbshipit-source-id: 99d10446c56fa4d8f7485f38309e8a282cd21bdf

7 years agofolly: fix enumeral vs non-enumeral type in conditional expression
Huapeng Zhou [Wed, 24 Aug 2016 17:07:45 +0000 (10:07 -0700)]
folly: fix enumeral vs non-enumeral type in conditional expression

Summary:
Titled.
```
mcrouter/lib/carbon/CarbonProtocolReader.h:143:49:   required from here
folly/small_vector.h:441:38: error: enumeral and non-enumeral type in conditional expression [-Werror]
cc1plus: all warnings being treated as errors
```

Reviewed By: yfeldblum

Differential Revision: D3763073

fbshipit-source-id: da490e91cdb12dacf37d71c1796239bdc361a31b

7 years agoPrevent compiler warning about mixing enumeral and non-enumeral types in ternary...
Jon Maltiel Swenson [Wed, 24 Aug 2016 16:35:48 +0000 (09:35 -0700)]
Prevent compiler warning about mixing enumeral and non-enumeral types in ternary expression

Summary:
The GCC warning "warning: enumeral and non-enumeral type in conditional expression"
is logged a lot in many builds since lots of code depends on small_vector.  This diff
should prevent those warnings.

Reviewed By: yfeldblum

Differential Revision: D3762835

fbshipit-source-id: 49831e4364e716592287c05d1dbf1912326324f6

7 years agoFix a folly build failure with GCC5
Phil Willoughby [Wed, 24 Aug 2016 08:05:41 +0000 (01:05 -0700)]
Fix a folly build failure with GCC5

Summary:
I tested it doesn't break the gcc 4.9 and clang builds - I didn't try any other versions of anything.

build[all]

Reviewed By: meyering

Differential Revision: D3757152

fbshipit-source-id: a126cd31cb89057dececfed1b265f452ee29fbff

7 years agoAsyncSSLSocket getRawBytes fix
Heng Hong Lee [Tue, 23 Aug 2016 23:31:17 +0000 (16:31 -0700)]
AsyncSSLSocket getRawBytes fix

Summary:
While adding logging around our socket and looking into `AsyncSSLSocket` it seems like the data that is actually written into the socket is not correctly attributed.  I
added logs and printed out what happens on the socket layer, P56563098 in this paste you can see that the `[fishhook]` logs are actually those added in D3698728.

Those are the actual bytes written onto the socket, in the paste, the bytes that are written by the AsyncSocket::bioWrite method are
the ones that actually get written onto the socket. some of the bytes written into the bio comes from bf_buff.c and bss_mem.c which are not eventually attributed to a
socket message and will be incorrectly counted when getRawBytesWritten/Read invoked on AsyncSSLSocket.cpp

Unfortunately/Fortunately this issue is not symmetrical and does not manifest in the getRawBytesReceived in AsyncSSLSocket, reading the bio for read bytes
correctly attributes the actual number of bytes written on the socket. moreover, pulling the asyncsocket data for getRawBytesRead doesnt give the full read bytes on
wire because SSL_connect and SSL_read dont return the number of bytes they read but return the number of bytes without the TLS bytes used.

siyengar seems like a right person to look at this. so adding him here.

Would love to discuss more about this and am open to iterating more on this solution

Reviewed By: knekritz

Differential Revision: D3698744

fbshipit-source-id: 541aa478778b9607f51db194fcbfe28bd23c737f

7 years agoMake it build on Ubuntu 16.04
Meng Zhang [Tue, 23 Aug 2016 22:17:52 +0000 (15:17 -0700)]
Make it build on Ubuntu 16.04

Summary:
include linux/membarrier.h if it is available.
Closes https://github.com/facebook/folly/pull/455

Reviewed By: lukenels

Differential Revision: D3714952

Pulled By: Orvid

fbshipit-source-id: 8c85756af2cb132152b2182816becfea138f0149

7 years agoMake fbstring::assign() tight, and simplify operator=() and append()
Giuseppe Ottaviano [Tue, 23 Aug 2016 20:00:48 +0000 (13:00 -0700)]
Make fbstring::assign() tight, and simplify operator=() and append()

Summary:
`fbstring::assign()` uses `append()`, which triggers exponential growth, but it's preferable that `assign()` behaves like the `fbstring(const char*, size_t)` constructor, which is tight.

Also rewrite `operator=()` in terms of `assign()`, to avoid duplicating the logic, and refactor the logic of `append()` for the aliasing case so that it uses the same expansion operation as the non-aliasing case.

Reviewed By: luciang

Differential Revision: D3754932

fbshipit-source-id: 5423f2a360b4268b6a05dd0ae9d2fe5bd1eb855d

7 years agoMake Malloc.h self-contained again
Giuseppe Ottaviano [Tue, 23 Aug 2016 17:14:40 +0000 (10:14 -0700)]
Make Malloc.h self-contained again

Summary: D3743475 adds to `Malloc.h` a dependency on another folly header, which breaks it when used in stand-alone mode. This diff moves the include to the right `#ifdef` section.

Reviewed By: Gownta

Differential Revision: D3757819

fbshipit-source-id: 71664ca6a3a47b6e4449a4ef603fedf052c5df3b

7 years agoFixup a reference to detail/FunctionalExcept.cpp
Christopher Dykes [Mon, 22 Aug 2016 22:55:49 +0000 (15:55 -0700)]
Fixup a reference to detail/FunctionalExcept.cpp

Summary: I missed this reference when I moved it to the portability folder. It is already being compiled at its new location.

Reviewed By: yfeldblum

Differential Revision: D3754227

fbshipit-source-id: 357b0c26ddbcefdc7640f6a334150abba90ed711

7 years agoUse a constexpr char* under MSVC for the constexpr StringPiece test
Christopher Dykes [Mon, 22 Aug 2016 21:15:39 +0000 (14:15 -0700)]
Use a constexpr char* under MSVC for the constexpr StringPiece test

Summary: MSVC can't handle the array as a constexpr value, but works fine with the char*.

Reviewed By: yfeldblum

Differential Revision: D3705189

fbshipit-source-id: e8208b3f2831a720095641f0e1e72ac63ed845a0

7 years agoMore fixup of the build on Mac for HHVM
Christopher Dykes [Mon, 22 Aug 2016 16:52:34 +0000 (09:52 -0700)]
More fixup of the build on Mac for HHVM

Summary: `std::__throw_bad_alloc()` is defined in `<new>` on OSX, so bring back the `#ifdef` guards that were there previously.

Reviewed By: markw65

Differential Revision: D3749714

fbshipit-source-id: 0338a4cece928fce0b9d33d41c17cfa99a319abe

7 years agofix(FBString): fix bugs v2016.08.22.00
pp__qq [Mon, 22 Aug 2016 04:50:45 +0000 (21:50 -0700)]
fix(FBString): fix bugs

Summary:
fix(FBString): compile error on instantiate `basic_fbstring` with a `Storage` that is not `fbstring_core<E>`
Closes https://github.com/facebook/folly/pull/398

Reviewed By: ot

Differential Revision: D3714957

Pulled By: yfeldblum

fbshipit-source-id: 1c5d2538b674049f7e1872a0b623ec330dc8d7b2

7 years agoGet ObserverManager compiling under MSVC
Christopher Dykes [Sun, 21 Aug 2016 17:02:55 +0000 (10:02 -0700)]
Get ObserverManager compiling under MSVC

Summary: It only needed a minor tweak, but the change still had to be made :(

Reviewed By: andriigrynenko

Differential Revision: D3745021

fbshipit-source-id: 286c6db706c3571842006537c6b17f506609e51d

7 years agoFix folly::to<> under MSVC
Christopher Dykes [Sun, 21 Aug 2016 17:02:29 +0000 (10:02 -0700)]
Fix folly::to<> under MSVC

Summary: MSVC didn't like the conversion to using `Expected`, and, after `Expected` was fixed to work under MSVC, conv still needs more changes. Specifically MSVC doesn't understand the single-instantiation form of getting the last element, so we have to switch to a tuple-backed version instead, which requires more template instantions, but produces the same performance at runtime.

Reviewed By: ericniebler

Differential Revision: D3744063

fbshipit-source-id: affcab1574c721d8b9529784d7ca2a46233d9935

7 years agoSuggestion of fixing folly::to<std::string>(double/float) performance issue
Bi Xue [Sun, 21 Aug 2016 04:10:36 +0000 (21:10 -0700)]
Suggestion of fixing folly::to<std::string>(double/float) performance issue

Summary:
When calling folly::to<SomeString>(double), generic implementation will
firstly reserve 24 (or 25 when negative value) bytes. This will introduce
a malloc call for most of mainstream string implementation.

But for most cases, a floating point doesn't need 24 (or 25) bytes to
be converted as a string.

This diff try to introduce a special version which does not do string reserve.

Reviewed By: ericniebler

Differential Revision: D3728171

fbshipit-source-id: d70ead396ad6c8d0df1f542c5516f7534e82cb97

7 years agoAn intro to the upgrade mutex in the Synchronized docs
Yedidya Feldblum [Sat, 20 Aug 2016 06:17:28 +0000 (23:17 -0700)]
An intro to the upgrade mutex in the Synchronized docs

Summary:
[Folly] An intro to the upgrade mutex in the `Synchronized` docs.

Describes what the upgrade mutex is. Extends the existing docs which describe `Synchronized` and `LockPtr` interface and behavior in the presence of an upgrade mutex.

Reviewed By: snarkmaster, simpkins

Differential Revision: D3746177

fbshipit-source-id: 68b0570a36cc1f4393d5ccca535efa02752ca11d

7 years agofix flaky EventHandlerTest
Aravind Anbudurai [Fri, 19 Aug 2016 23:33:50 +0000 (16:33 -0700)]
fix flaky EventHandlerTest

Summary: Lets the kernel choose the port instead of hardcoding it.

Reviewed By: djwatson

Differential Revision: D3745911

fbshipit-source-id: d9680ec286e8015abb9274c30b572ff1d91548ce

7 years agoRemove a dead comment in folly/test/SynchronizedTest.cpp
Yedidya Feldblum [Fri, 19 Aug 2016 23:27:36 +0000 (16:27 -0700)]
Remove a dead comment in folly/test/SynchronizedTest.cpp

Summary: [Folly] Remove a dead comment in `folly/test/SynchronizedTest.cpp`.

Reviewed By: simpkins

Differential Revision: D3745609

fbshipit-source-id: acdbd3eaa6d947213b72fe13cec0291545a60c87

7 years agoInclude <unordered_set> in experimental/observer/detail/Core.h
Christopher Dykes [Fri, 19 Aug 2016 21:53:37 +0000 (14:53 -0700)]
Include <unordered_set> in experimental/observer/detail/Core.h

Summary: Because it, and a whole bunch of other things, are used in the header but not properly included.

Reviewed By: yfeldblum

Differential Revision: D3744653

fbshipit-source-id: c10dbb83109200b6186b8ed5ef0d2447d4200f69

7 years agoMark the long overload of digits_to as noexcept
Christopher Dykes [Fri, 19 Aug 2016 20:51:26 +0000 (13:51 -0700)]
Mark the long overload of digits_to as noexcept

Summary: Well, the explicit instantion for it anyways. The extern template declaration for it declares it as `noexcept`, so MSVC generates an error because the explicit instantiation is not also marked as `noexcept`

Reviewed By: yfeldblum, ericniebler

Differential Revision: D3744090

fbshipit-source-id: 4e756b2761c23a436097a6131b9b2ecd59faf4f9

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