folly.git
7 years agocapture exception information when creating exception_wrapper
Eric Niebler [Fri, 13 Jan 2017 20:16:51 +0000 (12:16 -0800)]
capture exception information when creating exception_wrapper

Summary:
prefer creating an exception_wrapper with a reference to the active exception

Depends on D4410421

Reviewed By: spacedentist

Differential Revision: D4410455

fbshipit-source-id: d6b6aeb5fa72782e31d754a0b853514af5fdb8cd

7 years agoAdd AsyncSSLSocket option to turn off transparent tls
Dave Watson [Fri, 13 Jan 2017 18:32:13 +0000 (10:32 -0800)]
Add AsyncSSLSocket option to turn off transparent tls

Summary: Folly parts of D4383906.

Reviewed By: plapukhov

Differential Revision: D4387254

fbshipit-source-id: 3c039720c88c91b7292d60a85272dd1978510296

7 years agoSlight simplification of exception_wrapper constructor
Yedidya Feldblum [Wed, 11 Jan 2017 04:53:38 +0000 (20:53 -0800)]
Slight simplification of exception_wrapper constructor

Summary: [Folly] Slight simplification of `exception_wrapper` constructor.

Reviewed By: ericniebler

Differential Revision: D4391899

fbshipit-source-id: ddb066723bcd10abb0dbbaeab12b1e9be4f39acc

7 years agoadd an option to clear error before calling ssl methods
Subodh Iyengar [Tue, 10 Jan 2017 23:51:33 +0000 (15:51 -0800)]
add an option to clear error before calling ssl methods

Summary:
Normally clearing out the error before calling into an openssl method
is a bad idea. However there might be other code outside AsyncSSLSocket
calling into openssl in the same thread that doesn't use openssl
correctly. This allows users of AsyncSSLSocket to safeguard themselves
from such code.

Reviewed By: anirudhvr

Differential Revision: D4389970

fbshipit-source-id: 12da254d6b281c2b9d522ba19999b2489c0083a2

7 years agoFix an inefficiency in json-serializing a dynamic with sorted keys
Yedidya Feldblum [Tue, 10 Jan 2017 19:57:31 +0000 (11:57 -0800)]
Fix an inefficiency in json-serializing a dynamic with sorted keys

Summary:
[Folly] Fix an inefficiency in json-serializing a `dynamic` with sorted keys.

The inefficiency is that, at each level of the `dynamic` which is map-typed, we effectively make a full copy of the `dynamic` object. This can be expensive.

Reviewed By: Gownta

Differential Revision: D4389671

fbshipit-source-id: 223739397f913d3e65a421a9a4dcb089ec757ec6

7 years agofix typo in comments. s/unitialized/uninitialized/g
kumagi [Tue, 10 Jan 2017 19:55:42 +0000 (11:55 -0800)]
fix typo in comments. s/unitialized/uninitialized/g

Summary:
`unitialized` seems to be typo.
Closes https://github.com/facebook/folly/pull/538

Reviewed By: igorsugak

Differential Revision: D4397318

Pulled By: yfeldblum

fbshipit-source-id: a2e6c665b4dfe95d769430405886b0725423aac2

7 years agoRename exception_wrapper::getExceptionPtr to to_exception_ptr
Yedidya Feldblum [Tue, 10 Jan 2017 04:30:42 +0000 (20:30 -0800)]
Rename exception_wrapper::getExceptionPtr to to_exception_ptr

Summary:
[Folly] Rename `exception_wrapper::getExceptionPtr` to `to_exception_ptr`.

Make it clear that this will sometimes be an expensive operation. Using the word `to` evokes the possibility of an expensive conversion, while using the word `get` implies cheap access.

Reviewed By: djwatson

Differential Revision: D4391621

fbshipit-source-id: 33ca051d9be5a6050ba9f30b20faee35b7e58afb

7 years agoFix dynamic MPMCQueue tryObtainPromisedPushTicket() to prevent tryWriteUntil() and...
Maged Michael [Tue, 10 Jan 2017 02:31:20 +0000 (18:31 -0800)]
Fix dynamic MPMCQueue tryObtainPromisedPushTicket() to prevent tryWriteUntil() and writeIfNotFull() from blocking indefinitely for a matching read.

Summary:
The bug was reported by Alexander Pronchenkov in https://fb.facebook.com/groups/560979627394613/permalink/837052843120622/

Under certain conditions a `tryWriteUntil()`--and also `writeIfNotFull()`--operation may block indefinitely awaiting a matching read. This could happen because in each dynamic MPMCQueue expansion, typically one or two tickets are associated with the closed array not the new one. In the incorrect code, a `tryWriteUntil()` operation that induced expansion but gets a ticket associated with the closed array, incorrectly assumes that because the expansion succeeded then there is space for it. However because the ticket is associated with the closed array, the operation needs to wait (possibly indefinitely) for space to open in the closed array.

The fix: Changed the code in tryObtainPromisedPushTicket() such that the operation tries to acquire a ticket only if there is promised space in the array associated with that ticket. If there is no space, an expansion is attempted if the ticket is not associated with a closed array. If not or if expansion fails because of reaching maximum capacity or for being out-of-memory, then the operation returns false without attempting to acquire the ticket.

Other changes:
- Added a note about this difference in semantic between the dynamic and non-dynamic version to the main comment about the dynamic version.
- Changed `oldCap` to `curCap` because the value is actually current not old.
- Added two tests for checking that tryWriteUntil() never blocks indefinitely for both dynamic and non-dynamic versions.
- Removed all the `never_fail` tests for the dynamic version, because such operations may fails as described above.
- Added `asm_volatile_pause` when spinning on the seqlock.

Reviewed By: djwatson

Differential Revision: D4389347

fbshipit-source-id: c46dbefc9fe08e146250d2ad8ba68b0887f97436

7 years agoUse 2 second loop smoothing period rather than 20 seconds
Christopher Dykes [Mon, 9 Jan 2017 21:04:47 +0000 (13:04 -0800)]
Use 2 second loop smoothing period rather than 20 seconds

Summary: This was accidentally changed in D4375539.

Reviewed By: philippv

Differential Revision: D4393574

fbshipit-source-id: 0420223575bad0c6b0e89f8cf5ed138e063462b9

7 years agoAdd pre received data API to AsyncSSLSocket.
Kyle Nekritz [Mon, 9 Jan 2017 19:51:34 +0000 (11:51 -0800)]
Add pre received data API to AsyncSSLSocket.

Summary: This allows something else (ie fizz) to read data from a socket, and then later decide to to accept an SSL connection with OpenSSL by inserting the data it read in front of future reads on the socket.

Reviewed By: anirudhvr

Differential Revision: D4325634

fbshipit-source-id: 05076d2d911fda681b9c4e5d9d3375559293ea35

7 years agoSet custom ssl bio read method.
Kyle Nekritz [Mon, 9 Jan 2017 19:51:33 +0000 (11:51 -0800)]
Set custom ssl bio read method.

Summary: Similar to the bio write method. This should be essentially a no-op, bioRead has the same functionality as the openssl version. This is needed in the next diff.

Reviewed By: siyengar

Differential Revision: D4325622

fbshipit-source-id: a90b9ec06bee920a1a73551a3ea4c77e1ee0ab08

7 years agoInclude <errno.h> for using "errno" and ENOMEM.
Zonr Chang [Mon, 9 Jan 2017 18:28:20 +0000 (10:28 -0800)]
Include <errno.h> for using "errno" and ENOMEM.

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

Reviewed By: yfeldblum

Differential Revision: D4391871

Pulled By: Orvid

fbshipit-source-id: 80401dd65ae376f9ebbb0b079577d0a1fd15475b

7 years agoRemove folly::Future conversion constructor
Nick Wolchko [Mon, 9 Jan 2017 17:20:48 +0000 (09:20 -0800)]
Remove folly::Future conversion constructor

Summary:
This constructor is unsafe. The check it uses before doing the comparison isn't a safe enough check to see if the cast is valid. For example, this is broken in the presence of multiple inheritance because it doesn't adjust the pointer offset to the correct vtable.

e.g.
  struct A {
    virtual ~A() {};
    virtual void doSomething() = 0;
  };
  struct B {
    virtual ~B() {}
    virtual void doSomethingElse() = 0;
  };
  struct C : public B, public A {
    virtual ~C() {}
    void doSomething() override {
      std::cout << "Something!" << std::endl;
    }
    void doSomethingElse() override {
      std::cout << "Something Else!" << std::endl;
    }
  };
  int main (int argc, char **argv) {
    auto c = folly::makeFuture<std::shared_ptr<C>>(std::make_shared<C>());
    folly::Future<std::shared_ptr<A>> a = std::move(c);
    a.get()->doSomething();
    return 0;
  }
This code will print "Something else!" when run.

Reviewed By: siyengar

Differential Revision: D3679673

fbshipit-source-id: dcbf40ca82d458f17ee11191591f8b8daf58c919

7 years agoFix folly json to only sort properties of objects based on their key not value.
Michael O'Farrell [Mon, 9 Jan 2017 15:22:20 +0000 (07:22 -0800)]
Fix folly json to only sort properties of objects based on their key not value.

Reviewed By: yfeldblum

Differential Revision: D4386258

fbshipit-source-id: 23499267eb4390f0f40b3643760514cae1ca7838

7 years agoremove always true if-predicate (gcc-5 -Wlogical-op)
Igor Sugak [Sun, 8 Jan 2017 05:58:13 +0000 (21:58 -0800)]
remove always true if-predicate (gcc-5 -Wlogical-op)

Summary:
gcc-5 -Wlogical-op reports:
```lang=bash
folly/io/async/AsyncSSLSocket.cpp: In member function 'void folly::AsyncSSLSocket::invalidState(folly::AsyncSSLSocket::HandshakeCB*)':
folly/io/async/AsyncSSLSocket.cpp:418:35: error: logical 'or' of collectively exhaustive tests is always true [-Werror=logical-op]
```

Reviewed By: yfeldblum

Differential Revision: D4391317

fbshipit-source-id: c0f5ce748f1fc21678e4080c7f6351e7fada1e2b

7 years agoadd folly::as_const, like C++17's std::as_const
Eric Niebler [Sun, 8 Jan 2017 05:44:02 +0000 (21:44 -0800)]
add folly::as_const, like C++17's std::as_const

Summary: A one-liner from C++17, but a useful one. Const-qualifies an lvalue reference, avoiding the need for an awkward const_cast in some cases.

Reviewed By: yfeldblum

Differential Revision: D4389929

fbshipit-source-id: 1650c4901489eb0dd62fd9fa633b4a0da9f01954

7 years agoProperly std::chrono'ize HHWheelTimer
Christopher Dykes [Sat, 7 Jan 2017 20:51:23 +0000 (12:51 -0800)]
Properly std::chrono'ize HHWheelTimer

Summary: It was using `std::chrono::milliseconds` to represent a point in time, so use a time point instead.

Reviewed By: yfeldblum

Differential Revision: D4378116

fbshipit-source-id: f0b10bb7894dda44d78b31672d2b6735f3e1cbf4

7 years agostd::chrono'ize EventBase::loopBody
Christopher Dykes [Sat, 7 Jan 2017 20:51:21 +0000 (12:51 -0800)]
std::chrono'ize EventBase::loopBody

Summary: Because modern is good, and this allows for cleaner interaction with the other APIs being `std::chrono`ized.

Reviewed By: yfeldblum

Differential Revision: D4377773

fbshipit-source-id: 42ad5ad8476a3678e02e9314f9592f5449102f00

7 years agostd::chrono'ize EventBase::setMaxLatency
Christopher Dykes [Sat, 7 Jan 2017 20:51:20 +0000 (12:51 -0800)]
std::chrono'ize EventBase::setMaxLatency

Summary: Onward towards more modern code!

Reviewed By: yfeldblum

Differential Revision: D4377678

fbshipit-source-id: 6ca5ecd902be9028fb55f139374f7919fa522899

7 years agoRemove <glog/logging.h> from folly/Indestructible.h
Yedidya Feldblum [Sat, 7 Jan 2017 06:40:09 +0000 (22:40 -0800)]
Remove <glog/logging.h> from folly/Indestructible.h

Summary:
[Folly] Remove `<glog/logging.h>` from `folly/Indestructible.h`.

This makes the header much lighter. And abort on invalid access (the use-case for `glog`) only in `!defined(NDEBUG)` mode.

Anti-Pattern Combined Mega-Diff Description:
* Add non-`explicit` default ctor.
* Disable ctor explicitly when underlying ctor would fail to compile. Not sure how useful it would be, but it makes type-traits work correctly for whatever that is worth.
* Switch boolean flag to `erased_{false}` so that it is zero-initializable, because that's better.

Reviewed By: ericniebler

Differential Revision: D4380469

fbshipit-source-id: a39cb7470f7ee678fa722778da587409f468d985

7 years agoadd folly::void_t like C++17's std::void_t
Eric Niebler [Sat, 7 Jan 2017 02:32:01 +0000 (18:32 -0800)]
add folly::void_t like C++17's std::void_t

Summary:
C++17 is adding std::void_t. It's handy for controlling class template partial specialization. Folly should have it too.

See http://en.cppreference.com/w/cpp/types/void_t

Reviewed By: yfeldblum

Differential Revision: D4387302

fbshipit-source-id: 85f955f3d8cfacbd6c9e61fb3f5cf53c056459bb

7 years agoSkip defined checks in folly/Memory.h
Yedidya Feldblum [Sat, 7 Jan 2017 00:43:48 +0000 (16:43 -0800)]
Skip defined checks in folly/Memory.h

Summary:
[Folly] Skip `defined` checks in `folly/Memory.h`.

Undefined symbols, when evaluated by the preprocessor in preprocessor integer contexts, evaluate to `0`. That works for these checks.

Reviewed By: ericniebler

Differential Revision: D4385026

fbshipit-source-id: 3162f8b83431ebab2de53f9ba202e7ea8c339210

7 years ago__throw* functions seem to be available in LLVM 4.0 or above.
Zonr Chang [Fri, 6 Jan 2017 23:07:40 +0000 (15:07 -0800)]
__throw* functions seem to be available in LLVM 4.0 or above.

Summary:
__throw* functions exist in master branch but are not included in both
libc++ 3.9.0 and 3.9.1. Expect them to appear in next LLVM release
(which is 4.0).
Closes https://github.com/facebook/folly/pull/536

Reviewed By: yfeldblum, Orvid

Differential Revision: D4377002

Pulled By: smeenai

fbshipit-source-id: 5dd311ca3ec43955f29dd1197fd8fbeb9564a7f6

7 years agostd::chrono'ize EventBase::setLoadAvgMsec
Christopher Dykes [Fri, 6 Jan 2017 22:00:46 +0000 (14:00 -0800)]
std::chrono'ize EventBase::setLoadAvgMsec

Summary: Modernization is good.

Reviewed By: yfeldblum

Differential Revision: D4377612

fbshipit-source-id: adb03d8a92f25c8a792c8e7240a93ab20180b038

7 years agostd::chrono'ize EventBase::SmoothLoopTime
Christopher Dykes [Fri, 6 Jan 2017 20:36:54 +0000 (12:36 -0800)]
std::chrono'ize EventBase::SmoothLoopTime

Summary: Modernizing for the future.

Reviewed By: yfeldblum

Differential Revision: D4375539

fbshipit-source-id: e3edf8dd6b77d3338f8cf58ed128bc5bce62da32

7 years agofix gcc-5 build
Igor Sugak [Fri, 6 Jan 2017 17:53:00 +0000 (09:53 -0800)]
fix gcc-5 build

Summary:
```lang=bash
folly/test/SmallLocksBenchmark.cpp:212:18: error: use of undeclared identifier 'sqrt'
  double stdev = sqrt(accum / (results.size() - 1));
                 ^
folly/test/SmallLocksBenchmark.cpp:221:20: error: use of undeclared identifier 'sqrt'
  double stddev2 = sqrt(variance);
                   ^
2 errors generated.
```
Add the missing header.

Reviewed By: yfeldblum

Differential Revision: D4386343

fbshipit-source-id: 79c3dcae32c62f9b6d162862deabccf5fea7eaef

7 years agofix gcc-5 build
Igor Sugak [Fri, 6 Jan 2017 04:11:17 +0000 (20:11 -0800)]
fix gcc-5 build

Summary:
Clang with libgcc-5 reported:
```lang=bash
folly/experimental/test/BitsTest.cpp:249:16: error: use of undeclared identifier 'pow'
  auto value = pow(2, bits) * (negate ? -2.0 : 2.0) / 3.0;
               ^
```
Add missing include statement, and elaborate with the namespace.

Reviewed By: yfeldblum

Differential Revision: D4385807

fbshipit-source-id: a847dd439cd4c9f28ea8f222aa4ab60876949d13

7 years agomake_unique is ambiguous with c++14 for some platforms
Michael Lee [Thu, 5 Jan 2017 23:49:03 +0000 (15:49 -0800)]
make_unique is ambiguous with c++14 for some platforms

Summary: Missing compatibility case for Android

Reviewed By: yfeldblum

Differential Revision: D4384944

fbshipit-source-id: 0b3e43fb284b6a71c6ae2a2162d1ea7cb99bc817

7 years agodon't use assert in benchmarks. it gets stripped out in release mode.
Eric Niebler [Thu, 5 Jan 2017 21:48:28 +0000 (13:48 -0800)]
don't use assert in benchmarks. it gets stripped out in release mode.

Summary: Critical tests in benchmarks should not be in asserts since they will be stripped out in release mode, which is the only mode interesting for benchmarks.

Reviewed By: yfeldblum

Differential Revision: D4384411

fbshipit-source-id: 5237ab48f99ddcd2bce7a159fcf82900303efec2

7 years agoNamespace the UBSAN_DISABLE symbol
Yedidya Feldblum [Thu, 5 Jan 2017 17:57:26 +0000 (09:57 -0800)]
Namespace the UBSAN_DISABLE symbol

Summary:
[Folly] Namespace the `UBSAN_DISABLE` symbol.

Symbols defined by Folly should typically be namespaced to Folly.

Reviewed By: igorsugak

Differential Revision: D4378401

fbshipit-source-id: ff72f5f44e7c9d1bbf08efcec24bd3dc05d10ee7

7 years agoBackport ceil, floor, round from C++17 std::chrono
Yedidya Feldblum [Thu, 5 Jan 2017 17:54:44 +0000 (09:54 -0800)]
Backport ceil, floor, round from C++17 std::chrono

Summary:
[Folly] Backport `ceil`, `floor`, `round` from C++17 `std::chrono`.

These functions for operating on `std::chrono::duration` and `std::chrono::time_point` values are useful

Note: The implementations are derived from cppreference.com, which has a notice listing all content there as licensed under CC-BY-SA.

Reviewed By: ericniebler

Differential Revision: D4375603

fbshipit-source-id: 36a098bf5f75db071c1670518fc42bbc6df2817d

7 years agoKeep the std::exception* or std::type_info* in folly::exception_wrapper
Yedidya Feldblum [Wed, 4 Jan 2017 21:24:48 +0000 (13:24 -0800)]
Keep the std::exception* or std::type_info* in folly::exception_wrapper

Summary:
[Folly] Keep the `std::exception*` or `std::type_info*` in `folly::exception_wrapper`.

In the case of construction with a `std::exception_ptr`. Instead of keeping a pair of `std::string`s, strictly losing information (not that we use any more information, but theoretically we could).

Of note:
* Shrinks the size of `folly::exception_wrapper`, in all cases, to 48 bytes. Down from 32 bytes + 2 S bytes on 64-bit architectures, where S is the size in bytes of `std::string`. In particular, `libstdc++`'s implementation is 32 bytes, while `libc++`'s and Folly's implementations are 24 bytes on 64-bit architectures, for total original sizes of 96 bytes or 80 bytes.
* Allows to avoid rethrowing in `with_exception` in the case of an instance constructed with an `std::exception_ptr` plus an `std::exception&`.

Reviewed By: ericniebler

Differential Revision: D4369935

fbshipit-source-id: 35155e0b271959a4878fe077fc911b17767a2358

7 years agoAdd preprocessor macro to disable throw* definitions
Shoaib Meenai [Wed, 4 Jan 2017 18:53:21 +0000 (10:53 -0800)]
Add preprocessor macro to disable throw* definitions

Summary:
libc++ 4.0 and above define the throw* functions in `stdexcept`, so
Folly doesn't need to define them itself. In case those definitions are
backported to 3.9, add a Folly macro to skip the definitions regardless
of the libc++ version number.

Reviewed By: yfeldblum, Orvid

Differential Revision: D4378102

fbshipit-source-id: 674d98d7c448aa7e75bdd8b6e9fe965cd34a83d7

7 years agofolly::_t and use it in folly::exception_wrapper
Yedidya Feldblum [Wed, 4 Jan 2017 03:37:13 +0000 (19:37 -0800)]
folly::_t and use it in folly::exception_wrapper

Summary:
[Folly] `folly::_t` and use it in `folly::exception_wrapper`.

Intended in part for use in Folly code, but can be used outside.

Instead of:

```lang=c++
namespace folly {
using original = //...
using decayed = typename std::decay<original>::type;
}
```

In C++14:

```lang=c++
namespace folly {
using original = //...
using decayed = std::decay_t<original>;
}
```

And in C++11:

```lang=c++
namespace folly {
using original = //...
using decayed = _t<std::decay<original>>:
}
```

Not perfect, but better.

HT ericniebler and https://github.com/eniebler/meta.

Reviewed By: ericniebler

Differential Revision: D4371539

fbshipit-source-id: 9046d9caab73141b95f4bce4fb1af26e0c1ac739

7 years ago2017
Yedidya Feldblum [Wed, 4 Jan 2017 02:49:30 +0000 (18:49 -0800)]
2017

Summary: [Folly] 2017.

Reviewed By: Orvid

Differential Revision: D4378593

fbshipit-source-id: 4a448228bb1ddbe191b6e8562483867a399be846

7 years agofolly: ReadMostlySharedPtr fix for `getStdShared()` method
Steve O'Brien [Tue, 3 Jan 2017 21:45:15 +0000 (13:45 -0800)]
folly: ReadMostlySharedPtr fix for `getStdShared()` method

Summary:
Fix this error:

  In file included from FooUtils.cpp:2:
  folly/experimental/ReadMostlySharedPtr.h:323:21: error: 'ptr_' is a private member of 'folly::detail::ReadMostlySharedPtrCore<Foo, folly::TLRefCount>'
        return impl_->ptr_;
                      ^
  ... in instantiation of member function 'folly::ReadMostlySharedPtr<Foo, folly::TLRefCount>::getStdShared' requested here:
          getSomeReadMostlySharedPtr().getStdShared();
                                       ^
  buck-out/dev/gen/folly/__default_headers__#default,headers/folly/experimental/ReadMostlySharedPtr.h:94:22: note: declared private here
    std::shared_ptr<T> ptr_;
                       ^
  1 error generated.

The added test case repro's the above error (and the changes to the class fixes it).

Alternatives include just making `ReadMostlySharedPtr` a friend class of `ReadMostlySharedPtrCore`, but that seems uglier than this fix, which was to simply use the public `getShared` method which already exists.

As luck would have it, I had tried that, and also found that a `const ReadMostlySharedPtr` would still give some trouble because `getStdShared` was not marked `const`.  Fixed that too.  (I assume if a copy of a const `shared_ptr` member / such a member of a `const` instance is permissible, then the method should be const as well.  Plus it's const in the other `ReadMostlySharedPtr` class.)

Reviewed By: djwatson

Differential Revision: D4377690

fbshipit-source-id: 8e9e778ca991fd04b0eb1e5762795d871ce0ee8d

7 years agoDrop the boost dependency from Traits.h and FBVector.h
Yedidya Feldblum [Tue, 3 Jan 2017 21:44:19 +0000 (13:44 -0800)]
Drop the boost dependency from Traits.h and FBVector.h

Summary:
[Folly] Drop the `boost` dependency from `Traits.h` and `FBVector.h`.

Should not need it anymore with the currently supported compilers.

Reviewed By: ericniebler

Differential Revision: D4375572

fbshipit-source-id: df890c07f49b0499d2d2d08aa21c226b2893281e

7 years agoKill a dead constant in EventBase
Christopher Dykes [Tue, 3 Jan 2017 17:39:23 +0000 (09:39 -0800)]
Kill a dead constant in EventBase

Summary: It's only accessible within EventBase, and is never used.

Reviewed By: yfeldblum

Differential Revision: D4375541

fbshipit-source-id: a0c6d86a2b1968c37c67303c8411a23ed936d9d5

7 years agofbcode: remove unused includes from .cpp files without #if (but possibly #define)
Mathieu Baudet [Mon, 2 Jan 2017 13:19:07 +0000 (05:19 -0800)]
fbcode: remove unused includes from .cpp files without #if (but possibly #define)

Summary: Same as D4312617 but this time not excluding source files with `#define`.

Reviewed By: soumith

Differential Revision: D4344811

fbshipit-source-id: 5a314960c319f029c6737c8c8ac8224ec2f20218

7 years agoDrop <boost/operators.hpp> as a dependency of FBVector.h
Yedidya Feldblum [Mon, 2 Jan 2017 06:25:38 +0000 (22:25 -0800)]
Drop <boost/operators.hpp> as a dependency of FBVector.h

Summary:
[Folly] Drop `<boost/operators.hpp>` as a dependency of `FBVector.h`.

We can rough it.

Reviewed By: wqfish

Differential Revision: D4375986

fbshipit-source-id: 6b8c16ca9a55341fee396a253f7ad0c4d0201b0e

7 years agoMove the traits factories up, and remove <boost/mpl/has_xxx.hpp>
Yedidya Feldblum [Mon, 2 Jan 2017 06:25:37 +0000 (22:25 -0800)]
Move the traits factories up, and remove <boost/mpl/has_xxx.hpp>

Summary:
[Folly] Move the traits factories up, and remove `<boost/mpl/has_xxx.hpp>`.

Also, they now generate aliases to the types aliased by `std::true_type` and `std::false_type`. So now the API is entirely compatible with the requirements of `std::integral_constant` (because it is `std::integral_constant`).

Reviewed By: wqfish

Differential Revision: D4375944

fbshipit-source-id: dfd41283f13d793f7fc1f173590cd93cdee39a10

7 years agoAdd FOLLY_OPENSSL_HAS_ALPN and FOLLY_OPENSSL_HAS_SNI.
Zonr Chang [Mon, 2 Jan 2017 00:24:34 +0000 (16:24 -0800)]
Add FOLLY_OPENSSL_HAS_ALPN and FOLLY_OPENSSL_HAS_SNI.

Summary:
This expresses the intention of long predicate on OpenSSL version and
OPENSSL_NO_TLSEXT more clearly.

This also enables ALPN and SNI support when BoringSSL is in use.

ALPN is an essential function to make HTTP/2 work when building Proxygen
with BoringSSL.
Closes https://github.com/facebook/folly/pull/534

Reviewed By: Orvid

Differential Revision: D4375391

Pulled By: yfeldblum

fbshipit-source-id: 009f311bceb0ee911d904d96a3e678a5f7241575

7 years agoFix assorted missing includes
Yedidya Feldblum [Sun, 1 Jan 2017 23:03:34 +0000 (15:03 -0800)]
Fix assorted missing includes

Summary: Fix assorted missing includes.

Reviewed By: wqfish

Differential Revision: D4375902

fbshipit-source-id: 2b32631857c8f94e505153d32932cdf97d8d0b33

7 years agoAdd more MSG_OOB tests for EventHandler
Petr Lapukhov [Sat, 31 Dec 2016 00:11:41 +0000 (16:11 -0800)]
Add more MSG_OOB tests for EventHandler

Summary:
Sending OOB messages allows for single byte to be sent truly out of band and placed in an OOB buffer. This byte can then be read (by default) independent from normal data.

The two test-cases validate that

(1) We can receive OOB and normal data on same socket
(2) Sending OOB byte does not affect regular data

NOTE: sending more than one byte usually means the OOB data interleaved with regular data.

Reviewed By: alandau

Differential Revision: D4370652

fbshipit-source-id: a573dae7e5afba5a4a410be5371faf4f065c74e9

7 years agoMove internal `decodeUtf8` method from json.cpp to public util Unicode.h
Zbigniew Szymanski [Fri, 30 Dec 2016 13:19:20 +0000 (05:19 -0800)]
Move internal `decodeUtf8` method from json.cpp to public util Unicode.h

Summary:
Moved decodeUtf8 -> folly::utf8ToCodePoint.
Implementation was not changed to make sure no bugs are introduced.

Reviewed By: yfeldblum

Differential Revision: D4372739

fbshipit-source-id: a015a9c47ece825e09e7c243fae454f21f99db80

7 years agoLet Future::then call callbacks outside of the catch handler
Yedidya Feldblum [Fri, 30 Dec 2016 01:25:26 +0000 (17:25 -0800)]
Let Future::then call callbacks outside of the catch handler

Summary:
[Folly] Let `Future::then` call callbacks outside of the catch handler.

And `Future::onError`.

This makes the behavior of calls to `Future` callbacks with respect to currently-handled ("active") exceptions consistent - there will not be an active exception by the time the `Future` callback is called. (Unless `Future::then` or `Future::onError`, etc., is itself called with an active exception. Or unless the `Promise` is fulfilled, outside of the `Future` implementation code, with an active exception.)

This will affect any code which tries to call `std::current_exception()` or `throw;` from within a `Future` callback, such as an `onError` handler. That code will crash. (It was incorrect anyway, and relied on misusing Folly Futures.)

Reviewed By: ericniebler

Differential Revision: D4372173

fbshipit-source-id: 600b22e4db63c98358de29a6abcee807fbc53b0f

7 years agoConst-correctness for folly::exception_wrapper::with_exception with non-std::exception
Yedidya Feldblum [Thu, 29 Dec 2016 23:47:55 +0000 (15:47 -0800)]
Const-correctness for folly::exception_wrapper::with_exception with non-std::exception

Summary:
[Folly] Const-correctness for `folly::exception_wrapper::with_exception` with non-`std::exception`.

This also lets us unify the various flavors of `with_exception` and `is_compatible_with`. And fix `is_compatible_with` for non-`exception` types.

Reviewed By: ericniebler

Differential Revision: D4364474

fbshipit-source-id: 417edfd45f7cfba952ce961559da67769b7c41bc

7 years agofolly::TryException inherits from std::logic_error, like std::bad_optional_access...
Eric Niebler [Thu, 29 Dec 2016 19:51:18 +0000 (11:51 -0800)]
folly::TryException inherits from std::logic_error, like std::bad_optional_access; becomes no-throw copyable

Summary:
folly::TryException was inheriting from std::exception and managing its own message in a std::string data member.
That is suboptimal for the following reasons:

1) The analagous std:: type, std::bad_optional_access, inherits from std::logic_error. According to the Principle of Least
   Astonishment, folly::TryException should follow suit.
2) std::logic_error has a nothrow copy constructor. (This is typically implemented with a ref-counted string.)
   std::string does not. By explicitly managing its own message, folly::TryException picks up a throwing copy
   constructor. Exception classes should try to be nothrow copyable.
3) With most stdlib implementations, std::string is larger by a lot than the std:: exceptions. By changing
   folly::TryException as suggested, its size drops from 40 bytes to 16 on clang and gcc >=5.0.

Reviewed By: yfeldblum

Differential Revision: D4368760

fbshipit-source-id: 327f78f428419ccf9c7af3e0f57cc478d147fb37

7 years agofolly::FutureException inherits from std::logic_error, like std::future_error; become...
Eric Niebler [Thu, 29 Dec 2016 19:50:57 +0000 (11:50 -0800)]
folly::FutureException inherits from std::logic_error, like std::future_error; becomes smaller and nothrow-copy

Summary:
folly::FutureException was inheriting from std::exception and managing its own message in a std::string data member.
That is suboptimal for the following reasons:

1) The analagous std:: type, std::future_error, inherits from std::logic_error. According to the Principle of Least
   Astonishment, folly::FutureExpception should follow suit.
2) std::logic_error has a nothrow copy constructor. (This is typically implemented with a ref-counted string.)
   std::string does not. By explicitly managing its own message, folly::FutureException picks up a throwing copy
   constructor. Exception classes should try to be nothrow copyable.
3) With most stdlib implementations, std::string is larger by a lot than the std:: exceptions. By changing
   folly::FutureException as suggested, its size drops from 40 bytes to 16 on clang and gcc >=5.0.

Also, I took the liberty of fixing some copy-pastas that gave some of these exception types explicit default
constructors.

Reviewed By: yfeldblum

Differential Revision: D4367909

fbshipit-source-id: 1639d404237493f929db6116a760c2d0e599877d

7 years agoMake FunctionRef support bool operator
Bi Xue [Thu, 29 Dec 2016 06:18:32 +0000 (22:18 -0800)]
Make FunctionRef support bool operator

Summary:
To support following code:
```
void foo(folly::FunctionRef<void(void)> callback = {}) {
  if (callback) {
    callback();
  }
}
```

Reviewed By: yfeldblum

Differential Revision: D4372296

fbshipit-source-id: 7d21e6a44b6f6b046b424f0139465511dbae7b8b

7 years agoAvoid shadowing warnings in SYNCHRONIZED
Giuseppe Ottaviano [Thu, 29 Dec 2016 04:18:43 +0000 (20:18 -0800)]
Avoid shadowing warnings in SYNCHRONIZED

Summary: If two `SYNCHRONIZED` blocks are nested the internal `SYNCHRONIZED_*` variables can be compatible shadows (`SYNCHRONIZED_state` always is), so GCC will issue a warning with `-Wshadow-compatible-local`. This diff disambiguates the variable names for each `SYNCHRONIZED` block (as long as they appear on different lines).

Reviewed By: yfeldblum, philippv

Differential Revision: D4371263

fbshipit-source-id: b467a1a2651667c679382a1cc1eaa28f7ee4e6b3

7 years agoAdd OpenSSL portability layer
Anirudh Ramachandran [Thu, 29 Dec 2016 02:54:04 +0000 (18:54 -0800)]
Add OpenSSL portability layer

Summary:
Enable folly to build with OpenSSL alternatives (e.g., BoringSSL).

Some SSL-related tests are disabled:

 - Async session cache ones (BoringSSL has an async cache impl but with a different API)
 - TFO tests
 - Some changes to error tests which expect specific error strings. This is
   flaky as a test because it will break everytime even within a version, so we
   should fix that first.

This should be a noop for OpenSSL 1.0.2.

BoringSSL commit used is 35c8afd3143289c99aa3820e01950c564d7aced8 (10/26/2016)

Closes: https://github.com/facebook/folly/issues/198
Reviewed By: siyengar

Differential Revision: D3280382

fbshipit-source-id: 4141d992e0d8dd797ac4af479cfe90844a23278f

7 years agofolly::copy
Yedidya Feldblum [Thu, 29 Dec 2016 02:03:39 +0000 (18:03 -0800)]
folly::copy

Summary:
[Folly] `folly::copy`.

Usable when you have a function with two overloads:

    class MyData;
    void something(MyData&&);
    void something(const MyData&);

Where the purpose is to make copies and moves explicit without having to spell out the full type names - in this case, for copies, to invoke copy constructors.

When the caller wants to pass a copy of an lvalue, the caller may:

    void foo() {
      MyData data;
      something(folly::copy(data)); // explicit copy
      something(std::move(data)); // explicit move
      something(data); // const& - neither move nor copy
    }

Reviewed By: markisaa, ericniebler

Differential Revision: D3462023

fbshipit-source-id: 6c777be288f2a7012c1b4b46dc988890b8662595

7 years agonuke UNSYNCHRONIZED
Philip Pronin [Thu, 29 Dec 2016 00:28:30 +0000 (16:28 -0800)]
nuke UNSYNCHRONIZED

Summary:
API of `UNSYNCHRONIZED` is confusing as if you have two nested
`SYNCHRONIZED` blocks, `UNSYNCHRONIZED` always unlocks the inner-most,
even if you pass in the variable name used in the outer `SYNCHRONIZED`
block.

The macro was marked as "deprecated" in D3526489, remove it here.

Reviewed By: yfeldblum

Differential Revision: D4371297

fbshipit-source-id: 13ddc1ff77cb3d5045844c5ade0e95dbe2bccf6d

7 years agoA smaller implementation of try_and_catch
Yedidya Feldblum [Wed, 28 Dec 2016 17:57:15 +0000 (09:57 -0800)]
A smaller implementation of try_and_catch

Summary:
[Folly] A smaller implementation of `try_and_catch`.

No longer done as a derived class. This lets us make the protected members of `exception_wrapper` into private members.

Reviewed By: ericniebler

Differential Revision: D4366065

fbshipit-source-id: ae4763b55e431ac08260f74f6a23a257581247c9

7 years agoMake UncaughtExceptionCounter public
Adrian Hamza [Tue, 27 Dec 2016 22:18:20 +0000 (14:18 -0800)]
Make UncaughtExceptionCounter public

Summary: Make UncaughtExceptionCounter public by moving it from folly/detail to folly. I am adding a scope performance counter that works in a similar way to SCOPE_EXIT/SCOPE_ERROR and I need the UncaughtExceptionCounter functionality.

Reviewed By: yfeldblum, ericniebler

Differential Revision: D4342783

fbshipit-source-id: a1848e89cbb6340e2ac48adabf7bf76cece1b86d

7 years agoAdd proper Doxygen comment markings around exception_wrapper example.
Eric Niebler [Tue, 27 Dec 2016 18:45:34 +0000 (10:45 -0800)]
Add proper Doxygen comment markings around exception_wrapper example.

Summary: The Doxegen-generated documentation for exception_wrapper is unreadable. The code needs to be denoted with \code...\endcode tags; otherwise, it is formatted like a paragraph and newlines are ignored.

Reviewed By: igorsugak

Differential Revision: D4367636

fbshipit-source-id: 680bedfb152300d8a6287bf441dd9b81b9b94c80

7 years agopass RNG by reference so state is updated on each call
Christopher Small [Tue, 27 Dec 2016 00:45:14 +0000 (16:45 -0800)]
pass RNG by reference so state is updated on each call

Summary: folly::Random was taking the RNG by value (not reference) so it was not updating the RNG's state on each invocation -- so the RNG would not advance to the next value in the sequence.

Reviewed By: yfeldblum, nbronson

Differential Revision: D4362999

fbshipit-source-id: f93fc11911b92e230ac0cc2406151474d15f85af

7 years agofix include dependencies in folly/stats
Sven Over [Sun, 25 Dec 2016 08:40:42 +0000 (00:40 -0800)]
fix include dependencies in folly/stats

Summary:
Histogram-defs.h depends on Histogram.h, so it should include it.

This missing dependency is especially annoying because users who
include both Histogram.h and Histogram-defs.h will find that
clang-format reorders the includes such that Histogram-defs.h will
be included before Histogram.h, which breaks the build.

Same for BucketedTimeSeries-defs.h and TimeseriesHistogram-defs.h.

Reviewed By: pixelb

Differential Revision: D4366282

fbshipit-source-id: 518ef315154c28beb091e5c097db2bec6eea3ba0

7 years agoFix a wrong filename in a comment in ExceptionWrapper.h
Yedidya Feldblum [Sat, 24 Dec 2016 19:07:39 +0000 (11:07 -0800)]
Fix a wrong filename in a comment in ExceptionWrapper.h

Summary: [Folly] Fix a wrong filename in a comment in `ExceptionWrapper.h`.

Reviewed By: pixelb

Differential Revision: D4366224

fbshipit-source-id: cab31dc0a934ae9e9714436e28c8a1a295f7d3de

7 years agoUse std::chrono for timeouts to sslAccept and sslConn in AsyncSSLSocket
Christopher Dykes [Fri, 23 Dec 2016 21:09:25 +0000 (13:09 -0800)]
Use std::chrono for timeouts to sslAccept and sslConn in AsyncSSLSocket

Summary: Because `std::chrono` makes it clear exactly what unit of time is in use.

Reviewed By: yfeldblum

Differential Revision: D4363560

fbshipit-source-id: 47aeef21f842f39d8e886bec441897ecf1f3761b

7 years agoEnable making libfollyinit even when libdwarf is not available
Jacob Bower [Fri, 23 Dec 2016 08:09:51 +0000 (00:09 -0800)]
Enable making libfollyinit even when libdwarf is not available

Summary:
The hard requirement for libdwarf when building libfollyinit does not appear to be needed.

Without libfollyinit, code with calls to folly::init() won't build. Notably this is breaking build/execution of tests + examples for the Watchman C++ client in Watchman.

Reviewed By: yfeldblum

Differential Revision: D4361498

fbshipit-source-id: f3586d8cdca36eda3634663e95a4a61d879de6cf

7 years agoMove Thrower into exception_wrapper
Yedidya Feldblum [Thu, 22 Dec 2016 20:13:29 +0000 (12:13 -0800)]
Move Thrower into exception_wrapper

Summary:
[Folly] Move `Thrower` into `exception_wrapper`.

It is only used by `exception_wrapper`, so it might as well be a member type.

Also, make it private. That means we need to tweak `try_and_catch`.

Since we are tweaking `try_and_catch`, tweak all similar cases.

Reviewed By: luciang

Differential Revision: D4361815

fbshipit-source-id: c5025894465a2c7760bd79dbbd272079fd34dd79

7 years agoMove the guts of try_and_catch into a detail namespace
Yedidya Feldblum [Thu, 22 Dec 2016 08:17:20 +0000 (00:17 -0800)]
Move the guts of try_and_catch into a detail namespace

Summary:
[Folly] Move the guts of `try_and_catch` into a `detail` namespace.

Just a bit cleaner this way. Also, now we always slice before returning the derived object, so the derived type never escapes.

Reviewed By: luciang

Differential Revision: D4361471

fbshipit-source-id: 5c9567d3c5480ee9943a85139b8f27ba3b9da2d6

7 years agoUpdate future interrupts example in README
Jason Leng [Thu, 22 Dec 2016 07:32:08 +0000 (23:32 -0800)]
Update future interrupts example in README

Summary: Updated the future interrupts example to get rid of the shared pointer circular reference problem

Reviewed By: djwatson

Differential Revision: D4360037

fbshipit-source-id: cb959929a508df4dcf3b81d01012bc55044a0b17

7 years agoMake folly pkg-config more usable on OS X
Jacob Bower [Thu, 22 Dec 2016 06:10:37 +0000 (22:10 -0800)]
Make folly pkg-config more usable on OS X

Summary:
As part of open-sourcing the Watchman C++ client I'm adding an (optional) dependency in Watchman to Folly. When I tried to build this client with Folly on OS X I ran into some problems using the libfolly pkg-config file. This diff addresses these issues:

1. When using `build/bootstrap-osx-homebrew.sh`, the compile config for gflags and OpenSSL are overriden. If present, these custom flags will now be propagated to `libfolly.pc`.
2. I changed the the package requirements in `libfolly.pc` from `Requires.private` to just `Requires`.

Both of these changes are needed as much of Folly's implementation is in header files. As such, consumers of libfolly need to use the same library/include paths when building.

Reviewed By: yfeldblum

Differential Revision: D4332720

fbshipit-source-id: 6a051ae318ac10bdeb4dc383a16de81f1c0327bb

7 years agoto_weak_ptr
Yedidya Feldblum [Thu, 22 Dec 2016 04:29:00 +0000 (20:29 -0800)]
to_weak_ptr

Summary:
[Folly] `to_weak_ptr`.

Make a `weak_ptr` and return it from a `shared_ptr` without specifying the template type parameter and letting the compiler deduce it.

So you can write this:

    auto wptr = to_weak_ptr(getSomethingShared<T>());

Instead of this:

    auto wptr = weak_ptr<T>(getSomethingShared<T>());

Useful when `T` is long, such as:

    using T = foobar::FooBarAsyncClient;

Reviewed By: nbronson

Differential Revision: D4361276

fbshipit-source-id: 782a1d2d57b6e5823cb4018e445110098f1ce41f

7 years agoSupport fchmod and include the correct portability headers for FileUtil
Christopher Dykes [Thu, 22 Dec 2016 03:38:12 +0000 (19:38 -0800)]
Support fchmod and include the correct portability headers for FileUtil

Summary: `FileUtil.cpp` uses `fchmod`, which Windows doesn't have, so implement it and also include the correct portability headers for the use of `mkstemp` and now `fchmod` as well.

Reviewed By: yfeldblum

Differential Revision: D4360650

fbshipit-source-id: 300163689c54574548e7bf274a56264714d216ed

7 years agoAdd missing includes in folly/experimental/observer
Yedidya Feldblum [Thu, 22 Dec 2016 03:17:55 +0000 (19:17 -0800)]
Add missing includes in folly/experimental/observer

Summary:
[Folly] Add missing includes in `folly/experimental/observer`.

In particular, the missing includes of `folly/ExceptionString.h`.

Motivation.

Reviewed By: andrewjcg, nbronson

Differential Revision: D4360991

fbshipit-source-id: 4e4407669926b55d6d85df08edea70784a8f348c

7 years agoFBString: remove unnecessary 7-byte padding in large strings
Amir Shalem [Thu, 22 Dec 2016 00:47:55 +0000 (16:47 -0800)]
FBString: remove unnecessary 7-byte padding in large strings

Summary:
RefCounted struct contains a pointer to `Char data_[1]`
This saved us a +1 when calculating sizes for the null terminator,
but the compiler made the struct size to be 16, instead of a 8+1.

Reviewed By: Gownta, ot

Differential Revision: D4356429

fbshipit-source-id: 420694feb4b367b0c73d44f83c21a9559ac5e7a3

7 years agoFix the return type for sysconf in the unistd portability header
Christopher Dykes [Wed, 21 Dec 2016 21:04:24 +0000 (13:04 -0800)]
Fix the return type for sysconf in the unistd portability header

Summary: It was incorrectly typed.

Reviewed By: yfeldblum

Differential Revision: D4351015

fbshipit-source-id: b0114d536db66ff4429fa135e9ed7b2051a42d6e

7 years agoFix bug where capacity is not updated correctly after reserveLarge()
Amir Shalem [Wed, 21 Dec 2016 06:25:34 +0000 (22:25 -0800)]
Fix bug where capacity is not updated correctly after reserveLarge()

Summary:
When reallocating new large string, using reserveLarge(), make sure to update the new capacity for the RefCounted string data
to reflect the amount of data allocated by jemalloc for our block (using goodMallocSize())

Reviewed By: Gownta

Differential Revision: D4355440

fbshipit-source-id: f2d58e8888e973418781220d57ff46f674e20556

7 years agouse folly::FunctionRef for EventBase::run(ImmediatelyOrRun)?InEventBaseThreadAndWait
Eric Niebler [Tue, 20 Dec 2016 22:56:37 +0000 (14:56 -0800)]
use folly::FunctionRef for EventBase::run(ImmediatelyOrRun)?InEventBaseThreadAndWait

Summary: folly::FunctionRef never allocates memory and is cheaper to pass around. Use it in the interface of EventBase where we can.

Reviewed By: yfeldblum

Differential Revision: D4353992

fbshipit-source-id: 259c5214ed48d30981eb8e38b062aad31d80a080

7 years agoFix sysconf return type
Michael Lee [Tue, 20 Dec 2016 14:21:48 +0000 (06:21 -0800)]
Fix sysconf return type

Summary: The sysconf return type is supposed to be long, not unsigned.

Reviewed By: Orvid

Differential Revision: D4350903

fbshipit-source-id: 29d86081bf75d123e76b831a669b0fd3936ce102

7 years agoEnable -Wunreachable-code
Christopher Dykes [Tue, 20 Dec 2016 03:32:26 +0000 (19:32 -0800)]
Enable -Wunreachable-code

Summary:
Because it finds dead code.
This also removes the dead code found.

Reviewed By: yfeldblum

Differential Revision: D4342893

fbshipit-source-id: e500734ff5927320b2356106d10016e298d67393

7 years agoFix search name for gflags's .pc file
Misty De Meo [Tue, 20 Dec 2016 03:31:52 +0000 (19:31 -0800)]
Fix search name for gflags's .pc file

Summary:
gflags's pkg-config file is installed by default under the name gflags.pc; however, folly has been searching for it under the name libgflags.pc.

Several package managers (Homebrew, apt) install the .pc file under its default upstream name; as such it seems appropriate for folly to look for it under that name, too.

refs Homebrew/homebrew-core#7977.
Closes https://github.com/facebook/folly/pull/531

Reviewed By: yfeldblum

Differential Revision: D4348089

Pulled By: Orvid

fbshipit-source-id: 6adec290ec88b5d3f66103cf8e5094e99f2512ad

7 years agoFix library name for thread_local_test in open source builds
Guobao Sun [Tue, 20 Dec 2016 03:19:45 +0000 (19:19 -0800)]
Fix library name for thread_local_test in open source builds

Summary: Test case ThreadLocal.SharedLibrary is failing due to looking for "thread_local_test_lib.so", while the actual .so generated is "lib_thread_local_test.so". Change Makefile.am to conform it.

Reviewed By: Orvid

Differential Revision: D4350758

fbshipit-source-id: 9a2a23325059ed71529821620398ffa6ed84f83e

7 years agorename ExpectedStorage::isThis to isSelfAssign so that the self-assign check in opera...
Eric Niebler [Mon, 19 Dec 2016 23:12:31 +0000 (15:12 -0800)]
rename ExpectedStorage::isThis to isSelfAssign so that the self-assign check in operator= works

Summary: This corrects an oversight in folly::Expected where self-assign was not being detected correctly due to a half-applied edit.

Reviewed By: yfeldblum

Differential Revision: D4348181

fbshipit-source-id: 710b25c4c6d7aeaaea50493ccc5788d875ec4c2e

7 years agofolly/fibers/TimedMutex-inl.h: avoid shadowing warnings
Jim Meyering [Mon, 19 Dec 2016 17:53:32 +0000 (09:53 -0800)]
folly/fibers/TimedMutex-inl.h: avoid shadowing warnings

Summary:
Fix a warning exposed by gcc's -Wshadow-compatible-local.

Rename inner "lockStolen" to "stolen".

Differential Revision: D4346385

fbshipit-source-id: 323cb6c96ecfe18b5540525f4124dbd75faf0b1b

7 years agofolly/io/async/test/EventBaseTest.cpp: doubly-nested for-loop, do not reuse index
Jim Meyering [Mon, 19 Dec 2016 17:30:11 +0000 (09:30 -0800)]
folly/io/async/test/EventBaseTest.cpp: doubly-nested for-loop, do not reuse index

Summary:
Fix a warning exposed by gcc's -Wshadow-compatible-local option.
Rename inner "i" to "j".

Reviewed By: knekritz

Differential Revision: D4346505

fbshipit-source-id: f0a4ac32fefd6614900e58d1c8c7f15e8934fd04

7 years agofbcode: remove unused includes from .cpp files with no #if and #define v2016.12.19.00
Mathieu Baudet [Sun, 18 Dec 2016 02:23:24 +0000 (18:23 -0800)]
fbcode: remove unused includes from .cpp files with no #if and #define

Summary:
This is a first diff to remove the "easiest" unused includes in fbcode.

* For safety, we only touch .cpp files without #if and #define,
* We do not try to remove redundant systems headers (aka. "packing").

The diff was generated as follows:
```
foundation/scripts/ls-cpp-dirs | grep -v '^\(\.\.\|external/\|.*/external\)' | xargs ffmr -o /tmp/ffmr-diff-1 codegraph/scripts/ffmr/analyze_includes_no_headers_no_packing_skipping_ifdefs.sh

cat /tmp/ffmr-diff-1/*.diff | patch -p2
hg commit -m something
arc diff --prepare --nolint --nounit --less-context --excuse refactoring
```

Note: `grep -v` is just an optimization. The actual configuration is in these two files:
diffusion/FBS/browse/master/fbcode/codegraph/analysis/config.py
diffusion/FBS/browse/master/fbcode/codegraph/scripts/ffmr/analyze_includes_no_headers_no_packing_skipping_ifdefs.sh

See the task for more context, and the recent "safety" improvements on the tool.

depends on D4317825 for very few cases where `nolint` had to be manually added.

Reviewed By: igorsugak

Differential Revision: D4312617

fbshipit-source-id: ecc1f0addfd0651fa4770fcc43cd1314661a311a

7 years agoLet ScopedEventBaseThread destruct the EventBase in the IO thread
Yedidya Feldblum [Sat, 17 Dec 2016 08:53:05 +0000 (00:53 -0800)]
Let ScopedEventBaseThread destruct the EventBase in the IO thread

Summary: [Folly] Let `ScopedEventBaseThread` destruct the `EventBase` in the IO thread.

Reviewed By: andriigrynenko

Differential Revision: D4330951

fbshipit-source-id: 5b0e7071800e1d49048b08aa1233d72b820fe55d

7 years agoSplit EventBaseThread from ScopedEventBaseThread
Yedidya Feldblum [Sat, 17 Dec 2016 04:20:35 +0000 (20:20 -0800)]
Split EventBaseThread from ScopedEventBaseThread

Summary:
[Folly] Split `EventBaseThread` from `ScopedEventBaseThread`.

Now `ScopedEventBaseThread` is really scoped and immovable, while `EventBaseThread` is movable and can be started and stopped.

Users which will never move, and will never start or stop, the `ScopedEventBaseThread` can continue using it. Users which need to move, or which need to start and stop, the object will use `EventBaseThread` instead.

Reviewed By: andriigrynenko

Differential Revision: D4338447

fbshipit-source-id: 57c186630bc199a7a7b7223b1fcb077ce3d86743

7 years agoFix some implicit truncation and implicit sign coersion warnings in the Windows porta...
Christopher Dykes [Fri, 16 Dec 2016 23:09:24 +0000 (15:09 -0800)]
Fix some implicit truncation and implicit sign coersion warnings in the Windows portability layer

Summary: More progress towards being able to build Folly cleanly with MSVC's truncation and sign coersion warnings enabled.

Reviewed By: yfeldblum

Differential Revision: D4288060

fbshipit-source-id: ce42d2099163ed5a9c9a9bb736a80bb2407f7595

7 years agoadd cancelFunctionAndWait and cancelAllFunctionsAndWait to FunctionScheduler
Junlin Zhang [Fri, 16 Dec 2016 23:07:47 +0000 (15:07 -0800)]
add cancelFunctionAndWait and cancelAllFunctionsAndWait to FunctionScheduler

Summary: In FunctionScheduler, cancelFunction and cancelAllFunction will not wait for current running function, which will lead to segmentation fault if the function captures resource to be destructed right after calling cancelFunction or cancelAllFunction. This diff introduces cancelFunctionAndWait and cancelAllFunctionsAndWait, making it possible to wait for completion of current running function.

Reviewed By: yfeldblum

Differential Revision: D4324384

fbshipit-source-id: ac4b272894f753ef3bb175173f10d1ca20c17c41

7 years agoFix TimedMutex deadlock when used both from fiber and main context
Andrii Grynenko [Fri, 16 Dec 2016 21:27:33 +0000 (13:27 -0800)]
Fix TimedMutex deadlock when used both from fiber and main context

Summary: TimedMutex is a fair mutex, which can cause a deadlock if same mutex is requested first in a fiber, and then in main context.

Reviewed By: yfeldblum

Differential Revision: D4209155

fbshipit-source-id: 0623d9a2e6a0b5cc310fb71ad1b1cf33afd6a30e

7 years agoAdd BEXTR instruction support
Yinghai Lu [Fri, 16 Dec 2016 16:46:45 +0000 (08:46 -0800)]
Add BEXTR instruction support

Summary: BEXTR is an instruction in BMI set, which extract given number of bits from a 64 bit register starting from a given position. The position and size of extracted bits are packed into a one 64bit integer.

Reviewed By: Orvid

Differential Revision: D4316516

fbshipit-source-id: 3f752388763b0e26c506d7d49cf6c05cf28271c0

7 years agoRevert D4310312: [Folly] Enable -Wunreachable-code
Christopher Dykes [Fri, 16 Dec 2016 08:01:29 +0000 (00:01 -0800)]
Revert D4310312: [Folly] Enable -Wunreachable-code

Summary: This reverts commit 8178dacc9268e1001efc5f803a35ef49e23d692a

Differential Revision: D4310312

fbshipit-source-id: 7c4b90e834f1a95e51524e1e82ac5294e5ba2dc5

7 years agoAlways use an EventBaseManager with ScopedEventBaseThread
Yedidya Feldblum [Fri, 16 Dec 2016 05:21:21 +0000 (21:21 -0800)]
Always use an EventBaseManager with ScopedEventBaseThread

Summary: [Folly] Always use an `EventBaseManager` with `ScopedEventBaseThread`.

Reviewed By: andriigrynenko

Differential Revision: D4336412

fbshipit-source-id: 459a679f8fda0db1ca0a9fdca2f852bb00c5231e

7 years agoEnable -Wunreachable-code
Christopher Dykes [Fri, 16 Dec 2016 04:11:07 +0000 (20:11 -0800)]
Enable -Wunreachable-code

Summary:
Because it finds dead code.
This also removes the dead code found.

Reviewed By: yfeldblum

Differential Revision: D4310312

fbshipit-source-id: 8178dacc9268e1001efc5f803a35ef49e23d692a

7 years agoAllow folly to compile cleanly with most of the rest of MSVC's sign mismatch warnings
Christopher Dykes [Fri, 16 Dec 2016 04:09:21 +0000 (20:09 -0800)]
Allow folly to compile cleanly with most of the rest of MSVC's sign mismatch warnings

Summary:
This allows folly to compile with warnings 4245, 4287 and 4365 enabled, all of which are sign mismatch warnings.
This is labeled as 'mostly' because I'll probably have to clean up a few more of these.

Reviewed By: yfeldblum

Differential Revision: D4263259

fbshipit-source-id: 0db618f0405817503a63094edd75b24ec1e5c9ad

7 years agoMore implicit truncation warning fixes
Christopher Dykes [Fri, 16 Dec 2016 04:09:19 +0000 (20:09 -0800)]
More implicit truncation warning fixes

Summary:
This makes the changes required to compile Folly cleanly with the rest of MSVC's truncation warnings, 4244 & 4267.
Only another 2800 sign mismatch warnings left to go.

Reviewed By: yfeldblum

Differential Revision: D4257094

fbshipit-source-id: 1651eca875a31f53774d36c682f5e2745ddfcda5

7 years agoRefactor stats to use the same type for indexes
Christopher Dykes [Fri, 16 Dec 2016 03:59:27 +0000 (19:59 -0800)]
Refactor stats to use the same type for indexes

Summary:
This refactors folly/stats/* to use a single type for indexes rather than `size_t`, `int`, `unsigned int`, `uint64_t` and `int64_t` depending on where you looked.
This also has the result of getting MSVC to not complain about implicit sign conversions and implicit truncations.

Reviewed By: simpkins

Differential Revision: D4282174

fbshipit-source-id: 8529be34dce8ad18bc64395330bbdf2cd7305be4

7 years agoInclude <cassert> when using assert
Christopher Dykes [Fri, 16 Dec 2016 01:31:48 +0000 (17:31 -0800)]
Include <cassert> when using assert

Summary:
We were using `assert` without including anything that defines it.

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

Reviewed By: yfeldblum

Differential Revision: D4337047

fbshipit-source-id: 327d19a685e7a4cc1e1eb6e471a6457fe9dee6c7

7 years agoCheck the baton waits in the ScopedEventBaseThread tests
Yedidya Feldblum [Thu, 15 Dec 2016 09:37:21 +0000 (01:37 -0800)]
Check the baton waits in the ScopedEventBaseThread tests

Summary:
[Folly] Check the baton waits in the `ScopedEventBaseThread` tests.

Also:
* `Baton<T>::timed_wait` can take a `duration`, not just a `time_point`.
* Allow a bit more time for waiting, just in case the machine running the tests is under load.

Reviewed By: andriigrynenko

Differential Revision: D4330126

fbshipit-source-id: 55878577b1deeb260686647e5f22a81d6fb9e06d

7 years agoRequire runBeforeLoop callbacks to be canceled prior to EventBase destruction
Andrii Grynenko [Thu, 15 Dec 2016 04:06:38 +0000 (20:06 -0800)]
Require runBeforeLoop callbacks to be canceled prior to EventBase destruction

Summary: This makes it similar to other other events registered with EventBase.

Reviewed By: yfeldblum

Differential Revision: D4331277

fbshipit-source-id: 5d728d4ae5faff1a72ed724b4e2a6c0dd6c3b02d

7 years agoadd folly::init to libfolly for OSS build
Wez Furlong [Thu, 15 Dec 2016 04:03:32 +0000 (20:03 -0800)]
add folly::init to libfolly for OSS build

Summary:
We use this in eden and this avoids needing to patch the homebrew recipe.

I've also made the use of the folly symbolizer specific to the facebook
internal build of folly, because it fails to compile in the Linux OSS build
too.

Reviewed By: yfeldblum

Differential Revision: D4292166

fbshipit-source-id: b69153be8ab9cc461bc7f456248e73972ba7f461

7 years agoMake FunctionLoopCallback available outside of EventBase.cpp
Andrii Grynenko [Thu, 15 Dec 2016 04:03:18 +0000 (20:03 -0800)]
Make FunctionLoopCallback available outside of EventBase.cpp

Reviewed By: yfeldblum

Differential Revision: D4331194

fbshipit-source-id: 1e1579e3b775b1b4e329aa28aae11a2b54294697

7 years agoMake SingletonVault state use ReadPriority mutex
Andrii Grynenko [Thu, 15 Dec 2016 04:02:46 +0000 (20:02 -0800)]
Make SingletonVault state use ReadPriority mutex

Summary: This fixes a deadlock possible when singleton chain is created concurrently with destroyInstances().

Reviewed By: lbrandy, yfeldblum

Differential Revision: D4329028

fbshipit-source-id: a11b3ff42d164ead2f8e3e77e0e17be43a8ad306

7 years agowork around GCC#61971 (spurious -Warray-bounds warnings) in folly::FixedString
Eric Niebler [Wed, 14 Dec 2016 20:21:25 +0000 (12:21 -0800)]
work around GCC#61971 (spurious -Warray-bounds warnings) in folly::FixedString

Summary:
GCC has the temerity to insinuate that my code has out-of-array-bounds access. After cross-checking with clang and ubsan, reviewing the code, and running constexpr tests (for which out-of-range errors are caught at compile time), I can say with pretty high confidence that this is an instance of GCC#61971 (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61971). Basically, the gcc-4 series is known to issue spurious -Warray-bounds warnings. The "fix" is to route internal accesses to some helper functions, for which -Warray-bounds has been suppressed.

User code that accesses elements with operator[] will still warn on out-of-bounds access. If this is a problem in practice, we can suppress the warning there, too. Trying this for now since it is less likely to hide real problems.

Reviewed By: yfeldblum

Differential Revision: D4317305

fbshipit-source-id: 7bf92f993ac1a29631463c582c1b64d76f755181