folly.git
7 years agoWrappers for some of OpenSSL's crypto hash functions
Yedidya Feldblum [Fri, 17 Jun 2016 01:30:25 +0000 (18:30 -0700)]
Wrappers for some of OpenSSL's crypto hash functions

Summary:
[Folly] Wrappers for some of OpenSSL's crypto hash functions.

Wraps some of the OpenSSL crypto hash functions with variants that take `ByteRange` for input and `MutableByteRange` for output, and also variants that take `const IOBuf&` for input as well.

These are a bit nicer to use than passing pointers and lengths separately.

Reviewed By: ivmaykov

Differential Revision: D3434562

fbshipit-source-id: 3688ef11680a029b7664ac417a7781e70f9c6926

7 years agoFix infinite recursion in sorted_vector_{set|map}::insert(const value_type&)
Christopher Dykes [Fri, 17 Jun 2016 01:12:47 +0000 (18:12 -0700)]
Fix infinite recursion in sorted_vector_{set|map}::insert(const value_type&)

Summary:
We were calling ourself, which MSVC correctly identified:
```
warning C4717: 'folly::sorted_vector_set<int,`anonymous namespace'::less_invert<int>,std::allocator<int>,void>::insert': recursive on all control paths, function will cause runtime stack overflow
```

Just add an explicit `std::move` in to solve the problem.

Reviewed By: yfeldblum

Differential Revision: D3447922

fbshipit-source-id: 803f79510b3dbfeea32a9629238448f69f5b78dc

7 years agoReverted commit D3270439
Jinlong Zhou [Thu, 16 Jun 2016 20:26:54 +0000 (13:26 -0700)]
Reverted commit D3270439

Summary:
[temp] (14)/6:
> A function template, member function of a class template, variable template, or static data member of a class template shall be defined in every translation unit in which it is implicitly instantiated (14.7.1) unless the corresponding specialization is explicitly instantiated (14.7.2) in some translation unit; no diagnostic is required.

`-Wundefined-var-template` warns on any implicit instantiations that are needed but could not be performed because the definition is not available. In particular, for valid code, this warns on templates/temploids which have their definition and all relevant explicit instantiations tucked away in some source file (but for which no explicit instantiation declarations are provided in the relevant header file) - used a few times in folly. This seems a bad style, the static data member template should either be defined in the header file or should be explicitly instantiated in each .cpp file.

Reviewed By: igorsugak

Differential Revision: D3270439

fbshipit-source-id: e8cdb0452c2f6240a348b93cdbf9975813f27bfe

7 years agoUse the standard intrinsics for crc32c
Christopher Dykes [Thu, 16 Jun 2016 19:42:33 +0000 (12:42 -0700)]
Use the standard intrinsics for crc32c

Summary: This mass of conditions is completely pointless, because GCC has supported the standard intrinsics since at least 4.4.7, which is as far back as godbolt lets me test.

Reviewed By: yfeldblum

Differential Revision: D3373640

fbshipit-source-id: 619e971034db9249a9b312d18870bc7e4d579d50

7 years agofolly: fix clang's -Wundefined-var-template
Eric Niebler [Thu, 16 Jun 2016 17:08:54 +0000 (10:08 -0700)]
folly: fix clang's -Wundefined-var-template

Summary:
[temp] (14)/6:
> A function template, member function of a class template, variable template, or static data member of a class template shall be defined in every translation unit in which it is implicitly instantiated (14.7.1) unless the corresponding specialization is explicitly instantiated (14.7.2) in some translation unit; no diagnostic is required.

`-Wundefined-var-template` warns on any implicit instantiations that are needed but could not be performed because the definition is not available. In particular, for valid code, this warns on templates/temploids which have their definition and all relevant explicit instantiations tucked away in some source file (but for which no explicit instantiation declarations are provided in the relevant header file) - used a few times in folly. This seems a bad style, the static data member template should either be defined in the header file or should be explicitly instantiated in each .cpp file.

Reviewed By: igorsugak

Differential Revision: D3270439

fbshipit-source-id: bf305fde3af575a265d65a0ae0bf95db5597587f

7 years agoAdded limited list of supported ciphers
Blake Lawson [Thu, 16 Jun 2016 01:21:44 +0000 (18:21 -0700)]
Added limited list of supported ciphers

Summary: Added method to enable server support for a specific elliptic curve encryption algorithm.

Reviewed By: siyengar

Differential Revision: D3432860

fbshipit-source-id: 078531eead48ea156a68a109f8a62dc4907ac1ec

7 years agofix bug for nullptr in qfind
Haocheng Zhang [Wed, 15 Jun 2016 03:25:09 +0000 (20:25 -0700)]
fix bug for nullptr in qfind

Summary: Fix bug for passing null pointer to memchr function, which requires the first argument to never be null.

Reviewed By: luciang

Differential Revision: D3432130

fbshipit-source-id: 419924dd214d9f641d3d46335dae6abbe44ca751

7 years agoAdd constructor for MultiLevelTimeSeries class that uses initializer_list and add...
Caren Thomas [Wed, 15 Jun 2016 00:52:36 +0000 (17:52 -0700)]
Add constructor for MultiLevelTimeSeries class that uses initializer_list and add stat methods that uses duration as a parameter.

Summary: Introduce a constructor that takes in an initializer list of 'TimeType' objects for the durations. New methods for sum/avg/rate/count/countrate are created with a 'TimeType' parameter - this makes it possible to call these methods using the duration that specifies the level. Previously, these methods could only be called using the index of the level in the levels_ vector. These methods first do a linear scan through levels_ using the method getLevelsByDuration to find the corresponding index, and then perform the function they specify on the level.

Differential Revision: D3414343

fbshipit-source-id: 8e1fcc16fd013d0b8b855a1eebbeff417e945b07

7 years agoAdd conv_benchmark target to Makefile.am
Marcus Holland-Moritz [Tue, 14 Jun 2016 23:47:40 +0000 (16:47 -0700)]
Add conv_benchmark target to Makefile.am

Summary: It's useful to be able to build & run the benchmark.

Differential Revision: D3433731

fbshipit-source-id: c37bd5be3da0f89d4792747857d2e7548aea08ba

7 years agoReverted commit D3427621
Michael Lee [Tue, 14 Jun 2016 00:50:11 +0000 (17:50 -0700)]
Reverted commit D3427621

Summary: The iOS sdk adds `clock_gettime` support.

Reviewed By: clementgenzmer

Differential Revision: D3427621

fbshipit-source-id: 534276ba1e0ade185e3af0665f419f3c51d3f980

7 years agoEventBase keepAlive counter is not atomic
Joseph Griego [Tue, 14 Jun 2016 00:49:38 +0000 (17:49 -0700)]
EventBase keepAlive counter is not atomic

Summary: Since loopKeepAlive() is always used from the EventBase thread, there's no need for the overhead of an shared_ptr (and therefore, an atomic ref counter); we can get away without thread safety. This also allows us to discard the (sometimes incorrect) optimization of not returning a handle when it appears the loop will continue running anyways

Reviewed By: andriigrynenko

Differential Revision: D3375503

fbshipit-source-id: 474e4fcf992bdc4fcca9370d3c57bdcc4e042386

7 years agoTime.h portability for iOS 10.
Michael Lee [Tue, 14 Jun 2016 00:05:24 +0000 (17:05 -0700)]
Time.h portability for iOS 10.

Summary: The iOS sdk adds `clock_gettime` support.

Reviewed By: clementgenzmer

Differential Revision: D3427621

fbshipit-source-id: fcd3022c5ea05ceb8e289fbeb1ae845155d2dea9

7 years agoDCHECK on reentrant invocations of loop()
Alexey Spiridonov [Sat, 11 Jun 2016 02:12:36 +0000 (19:12 -0700)]
DCHECK on reentrant invocations of loop()

Summary:
I had a crash bug where an `EventBase` handler called a function, which tried to use the thread's EventBase as if it were its own:

```
auto evb = folly::EventBaseManager::get()->getEventBase();
// schedule work on evb, which calls evb->terminateLoopSoon() on completion
evb->loopForever();
```

This ended up invoking the `event_base_loop()` reentrantly, and corrupting the heap in a hard-to-debug way.

Reviewed By: djwatson

Differential Revision: D3408155

fbshipit-source-id: b8855aa3b390fa33e032ab295d386122d2cb872a

7 years agoMake Fibers library conditional on working Boost.Context fcontext_t
Ben Hamilton [Fri, 10 Jun 2016 23:01:34 +0000 (16:01 -0700)]
Make Fibers library conditional on working Boost.Context fcontext_t

Summary:
Boost 1.61 removes the `boost::context::fcontext_t` type
which Folly Fibers depends on.

This makes the Fibers library conditional on a working `fcontext_t`,
so Folly will build (just without Fibers) if it's not present.

Depends On D3419638

Fixes https://github.com/facebook/folly/issues/413

Reviewed By: Orvid

Differential Revision: D3420097

fbshipit-source-id: 876f8f76a2d0dda4ce5085c27875ab6aaa7b86c0

7 years agoFix folly::fibers on OSX with boost 1.60
Andrii Grynenko [Fri, 10 Jun 2016 22:25:44 +0000 (15:25 -0700)]
Fix folly::fibers on OSX with boost 1.60

Summary: We only need fcontext.hpp, so let's include it directly. all.hpp includes other headers, which use thread-locals (doesn't work with our OSX build).

Reviewed By: igorsugak, bhamiltoncx

Differential Revision: D3419638

fbshipit-source-id: b4fa1bd14454bcd75eaece52c398ac6510273773

7 years agoAdded support for std::mutex in folly Deterministic Schedule
Maged Michael [Fri, 10 Jun 2016 15:53:32 +0000 (08:53 -0700)]
Added support for std::mutex in folly Deterministic Schedule

Summary:
Added the class `DeterministicMutex`, a wrapper for `std::mutex`, in `folly/test/DeterministicSchedule.h`. Added a test to `folly/test/DeterministicScheduleTest.cpp` to test the correct behavior of `DeterministicMutex` functions and that deterministic schedule is able to detect race conditions that involve `DeterministicMutex`.

Note: Bootcamp task

Reviewed By: djwatson

Differential Revision: D3412579

fbshipit-source-id: c12861c4ec1cfeadcef027be4513e8e4cb7e0176

7 years agoGet rid of circular dependency when including ExceptionWrapper.h from Conv.h
Marcus Holland-Moritz [Fri, 10 Jun 2016 06:40:54 +0000 (23:40 -0700)]
Get rid of circular dependency when including ExceptionWrapper.h from Conv.h

Summary:
This is needed in order to be able to use folly::Try<> with folly::to<> for
non-throwing conversions.

Reviewed By: meyering

Differential Revision: D3411003

fbshipit-source-id: 1f20e7871b9cedda2b35527dac70381579abd708

7 years agoUse intrinsics rather than inline assembly where possible
Christopher Dykes [Thu, 9 Jun 2016 23:01:25 +0000 (16:01 -0700)]
Use intrinsics rather than inline assembly where possible

Summary:
I would switch these to just use the intrinsic functions, but GCC 4.8 doesn't support them.
MSVC supports the intrinsics, which is the primary reason for the switch.

Reviewed By: philippv

Differential Revision: D3278901

fbshipit-source-id: 60103ac7cf7ddfb529f65f4aadc687dbdaf423a1

7 years agoAllow a timer with pending callbacks to be destroyed
Chad Parry [Thu, 9 Jun 2016 22:07:58 +0000 (15:07 -0700)]
Allow a timer with pending callbacks to be destroyed

Summary:
This diff is part of a string of changes that is getting `HHWheelTimer` to use standard smart pointers.

D2617966 is being reverted. The `DestructorGuard` that was added for safety is not possible without intrusive reference counting.
That means that the `count_` (of pending callbacks) will still be correct, but there will be nothing to check it against. Because of that, I removed the assertions to make sure that the count of pending callbacks matches the count of active guards. Anyway, there were already ways to defeat that test, such as by creating a `DestructorGuard` from any client code.
As long as the assertions are gone, the `cancelAll` can be moved to the destructor. It only ever got run when `destroy` was called and `count_` was `0`, which means that `~HHWheelTimer` was also going to get called at the same time.

Reviewed By: djwatson

Differential Revision: D3349303

fbshipit-source-id: 433fa6605ee9921833328e2f82bd07b2e17e42c5

7 years agoFix typo in docs/Function.md
pravic [Thu, 9 Jun 2016 22:01:51 +0000 (15:01 -0700)]
Fix typo in docs/Function.md

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

Reviewed By: yfeldblum

Differential Revision: D3413832

Pulled By: elliottneilclark

fbshipit-source-id: 7af1462c250c8d6b5d6f5dfcb3f3117dff7877c9

7 years agoFix a data race found by TSAN in folly::fibers::Baton
Martin Martin [Thu, 9 Jun 2016 18:53:14 +0000 (11:53 -0700)]
Fix a data race found by TSAN in folly::fibers::Baton

Summary: Fix a data race found by TSAN in folly::fibers::Baton by changing a memory_order_relaxed to memory_order_acquire.  Nathan Bronson says that TSAN is correct and the C++ memory model requires _acquire here.

Reviewed By: andriigrynenko

Differential Revision: D3412519

fbshipit-source-id: bd0043b41d145e689a97fc7ef47aa6e116ea9194

7 years agofix command line parsing
David Callahan [Thu, 9 Jun 2016 01:31:19 +0000 (18:31 -0700)]
fix command line parsing

Summary: parse command line flags to pick up -json flag

Reviewed By: mzlee

Differential Revision: D3405794

fbshipit-source-id: e2d216c74f04c15d7470c70abfc2bd25c2ceda98

7 years agoAllow const mapped types in folly::AtomicHash(Array|Map)
Brett Simmers [Thu, 9 Jun 2016 00:19:20 +0000 (17:19 -0700)]
Allow const mapped types in folly::AtomicHash(Array|Map)

Summary:
Without this const_cast, it's impossible to insert anything into an
AHA with a const mapped_type, making it effectively useless.

Reviewed By: yfeldblum

Differential Revision: D3405687

fbshipit-source-id: 3ecba19e0e92661c2c537c747b4927176104939f

7 years agomake Range::size() constexpr
Adam Simpkins [Wed, 8 Jun 2016 23:15:18 +0000 (16:15 -0700)]
make Range::size() constexpr

Summary:
Declare size() to return a constexpr value, so it can be called on constexpr
Range objects.

This unfortunately does drop the existing assert() check, due to a gcc bug:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71448

Reviewed By: lbrandy

Differential Revision: D3394612

fbshipit-source-id: 77ea3b961dc323a39dea6f0e5850f9a311210d09

7 years agoPack PackedSyncPtr
Christopher Dykes [Wed, 8 Jun 2016 19:51:28 +0000 (12:51 -0700)]
Pack PackedSyncPtr

Summary: The comment was right about the packed attribute not liking private members, so pack `PackedSyncPtr` to get GCC to pack structs containing it correctly.

Reviewed By: yfeldblum

Differential Revision: D3402936

fbshipit-source-id: 11d9def132d5dedf04b0e263c379cac339a52fa3

7 years agoadd hexlify() and unhexlify() convenience methods
Adam Simpkins [Wed, 8 Jun 2016 19:49:45 +0000 (12:49 -0700)]
add hexlify() and unhexlify() convenience methods

Summary:
Add new versions of hexlify() and unhexlify() that directly return a
std::string, similar to the helper functions that already exist for humanify()
and backslashify().

The function signatures for these helpers are different than the humanify() and
backslashify() helpers--these functions always accept StringPiece or ByteRange
arguments.  This allows them to easily accept "const char*" arguments, without
having to convert the arguments to a std::string.  (I think we probably should
fix the humanify() and backslashify() helpers to be similar in a separate
diff.)

Reviewed By: yfeldblum

Differential Revision: D3403863

fbshipit-source-id: a2df49d5857e1d34caac3d78283dc293f4ef1ab6

7 years agoDisable SIGSEGV signal handler if run in JVM
Andrii Grynenko [Wed, 8 Jun 2016 01:30:18 +0000 (18:30 -0700)]
Disable SIGSEGV signal handler if run in JVM

Reviewed By: ldemailly

Differential Revision: D3402550

fbshipit-source-id: 3de47c569868545c3861dab9a7b244ed8fb1fadf

7 years agoExpose current observer from FiberManager
Adam Radziwonczyk-Syta [Tue, 7 Jun 2016 19:59:48 +0000 (12:59 -0700)]
Expose current observer from FiberManager

Summary:
Inteded use case is to write tracing code in common components that can adjust its behavior to the current observer. E.g.
  auto* observer = FiberManager::getUnsafe()->getObserver();
  if (auto* specific_observer = dynamic_cast<SpecificObserver*>(observer)) {
    specific_observer->set_fiber_name(...);
  }

Reviewed By: andriigrynenko

Differential Revision: D3392890

fbshipit-source-id: 284b4964bbb17e9a081779b870af68e8f1a81634

7 years agoAdd TFO support to AsyncSSLSocket
Subodh Iyengar [Tue, 7 Jun 2016 14:43:37 +0000 (07:43 -0700)]
Add TFO support to AsyncSSLSocket

Summary:
This adds TFO support to AsyncSSLSocket which
uses the support for TFO from AsyncSocket.

Because of the way AsyncSSLSocket inherits from
AsyncSocket it is tricky.

The following changes were made:
1. Openssl internally will treat only errors with return
code -1 as READ_REQUIRED or WRITE_REQUIRED errors. So this
diff changes the return value of the errors in the TFO fallback
cases to -1.

2. In case we fallback after SSL_connect() to a normal connect,
we would have to restart the connection process after connect
succeeds. To do this this overrides the connection success callback
and restarts the connection before sending the callback to AsyncSocket
because sometimes callbacks might synchronously call sslConn() in the
normal connect cases.

3. Delegated bioWrite to call sendSocketMessage instead of sendmsg directly.

Reviewed By: djwatson

Differential Revision: D3391735

fbshipit-source-id: 61434f6de4a9c3d03973c9ab9e51eb49e751e5cf

7 years agofolly: define FOLLY_SANITIZE_THREAD convenience macro for TSAN
Kenny Yu [Mon, 6 Jun 2016 23:20:39 +0000 (16:20 -0700)]
folly: define FOLLY_SANITIZE_THREAD convenience macro for TSAN

Summary:
This defines the macro `FOLLY_SANITIZE_THREAD`, similar to `FOLLY_SANITIZE_ADDRESS`.

Note that gcc *just* landed support for the `__SANITIZE_THREAD__` macro two weeks
ago: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64354

Reviewed By: igorsugak

Differential Revision: D3389021

fbshipit-source-id: df6497a7741657c297ee812ed07cbae102b6810d

7 years agoAllow constexpr construction of AtomicStruct under MSVC
Christopher Dykes [Sat, 4 Jun 2016 00:43:42 +0000 (17:43 -0700)]
Allow constexpr construction of AtomicStruct under MSVC

Summary: Attempting to use this under MSVC, as `MemoryIdler::defaultIdleTimeout` does, generated warnings about the symbol being dynamically initialized due to an implementation limation. Solve this by defining a union instead of calling `encode` in the constructor.

Reviewed By: yfeldblum

Differential Revision: D3387916

fbshipit-source-id: b0d628b6d086960e94121b6183a31348fb151aac

7 years agoDeal with a couple of warnings from MSVC
Christopher Dykes [Sat, 4 Jun 2016 00:43:13 +0000 (17:43 -0700)]
Deal with a couple of warnings from MSVC

Summary: MSVC is warning that `client` may be uninitialized, which is possible if the callback doesn't actually set them for some reason. Solve that by explicitly initializing it to `nullptr`. Also deal with the warning about the initialization of `dynamic::array` using braced syntax by not using that syntax.

Reviewed By: yfeldblum

Differential Revision: D3387782

fbshipit-source-id: e4d25ad32e99a73d7d062be9a2f2b5bde5d17396

7 years agoRemove a couple of MSVC portability defines
Christopher Dykes [Sat, 4 Jun 2016 00:42:35 +0000 (17:42 -0700)]
Remove a couple of MSVC portability defines

Summary: The first is both wrong (due to the difference in semantics) and unneeded, as MSVC 2015 supports `snprintf` natively and with the correct semantics. The second is simply never used.

Reviewed By: yfeldblum

Differential Revision: D3387551

fbshipit-source-id: ea666a60ce859a59ad3d3ff961b4d7d642fd86eb

7 years agoAdd a SIGSEGV signal handler which detects stack overflow
Andrii Grynenko [Fri, 3 Jun 2016 22:01:28 +0000 (15:01 -0700)]
Add a SIGSEGV signal handler which detects stack overflow

Summary: GuardPageAllocator now keeps a track of all protected pages. If SIGSEGV occurs, signal handler can check address at fault against protected pages and if it matches - we got a fiber stack overflow.

Reviewed By: yfeldblum

Differential Revision: D3386960

fbshipit-source-id: 775b36ee08fecbbd87da0025f794717c222f5cce

7 years agoAlways override write bio method
Subodh Iyengar [Fri, 3 Jun 2016 19:40:37 +0000 (12:40 -0700)]
Always override write bio method

Summary:
Always overriding write bio method
allows us to more cleanly implement
features like eor tracking, support
multiple ssl libraries, and also TFO

Reviewed By: anirudhvr

Differential Revision: D3350482

fbshipit-source-id: ddd2333431f9d636d69c8325b2c18d7cc043b848

7 years agoUpdate folly OSS build to use 1y
Elliott Clark [Thu, 2 Jun 2016 22:37:35 +0000 (15:37 -0700)]
Update folly OSS build to use 1y

Summary: use std=c++1y for compiling since we use c+=14 feautures but don't want to require a recent gcc.

Reviewed By: bhamiltoncx

Differential Revision: D3380287

fbshipit-source-id: 8ece6007cf613d307bafe692f1af70f5359df95a

7 years agoAssert that MicroSpinLock is a POD type
Christopher Dykes [Wed, 1 Jun 2016 22:36:26 +0000 (15:36 -0700)]
Assert that MicroSpinLock is a POD type

Summary: Because if it's not, all hell will break lose (apparently).

Reviewed By: meyering

Differential Revision: D3373061

fbshipit-source-id: f08a863d1bacadeeff9d9cea041f399f136cfc74

7 years agoPackedSyncPtr.data_ is private
Christopher Dykes [Wed, 1 Jun 2016 22:34:02 +0000 (15:34 -0700)]
PackedSyncPtr.data_ is private

Summary: As the (now removed) comment says, GCC 4.6 doesn't treat structs with private members as POD types... The thing is, GCC 4.6 is long dead, so make `data_` private and assert that it's a POD type.

Reviewed By: meyering

Differential Revision: D3373172

fbshipit-source-id: b2f1ee59d544461fe986705164cc6c866af62865

7 years agoFix test/SynchronizedTest.cpp build break on Clang on OS X
Ben Hamilton [Wed, 1 Jun 2016 17:43:50 +0000 (10:43 -0700)]
Fix test/SynchronizedTest.cpp build break on Clang on OS X

Summary:
The Folly `make check` build is broken with Clang on OS X.

This diff fixes the following break in `test/SynchronizedTest.cpp`:

  SynchronizedTest.cpp:150:10: error: thread-local storage is not supported for the current target
    static thread_local int lockCount_;

The problem is clang on OS X doesn't support the `thread_local` extension:

http://stackoverflow.com/a/29929949

so we now use the `FOLLY_TLS` compatibility macro.

When I fixed this, I found the test also failed to compile if `FOLLY_SYNCHRONIZED_HAVE_TIMED_MUTEXES` was `#define`d to `0` (as it is on OS X), so I fixed that.

Reviewed By: mzlee

Differential Revision: D3361215

fbshipit-source-id: e93be6872bcab03090448b9421e502e472f149ff

7 years agoSingletonVault_c.cpp is always C++
Christopher Dykes [Wed, 1 Jun 2016 17:14:26 +0000 (10:14 -0700)]
SingletonVault_c.cpp is always C++

Summary: It's simply not possible to compile the C++ side of the C API as C, so don't guard the `extern "C"` as if it is.

Reviewed By: meyering, yfeldblum

Differential Revision: D3369474

fbshipit-source-id: d3316704ce17a471bc9d6a3a235472bf40cedb9d

7 years agoMethods to change cipher suite list
Subodh Iyengar [Wed, 1 Jun 2016 08:45:00 +0000 (01:45 -0700)]
Methods to change cipher suite list

Summary: Add methods to change cipher suite list and sigalg list

Reviewed By: anirudhvr

Differential Revision: D3295935

fbshipit-source-id: eb46311986465e399eafa69e3070b53b36bce820

7 years agoSocketFastOpen should include <cerrno>
Michael Lee [Tue, 31 May 2016 18:24:56 +0000 (11:24 -0700)]
SocketFastOpen should include <cerrno>

Summary: Include cerrno to define `EOPNOTSUPP` and `ENOPROTOOPT`.

Reviewed By: elindsey

Differential Revision: D3366479

fbshipit-source-id: 3a93a1c247ffa4a9d4ea03577713362a2a35c8b7

7 years agoSubprocessTest does not use dirent.h
Michael Lee [Tue, 31 May 2016 16:31:41 +0000 (09:31 -0700)]
SubprocessTest does not use dirent.h

Summary: Remove it as we do not need it here.

Reviewed By: yfeldblum

Differential Revision: D3365991

fbshipit-source-id: bb36caca0319df4c8d6e3d978045b7c28369f043

7 years agoAdd support for TFO connections
Subodh Iyengar [Tue, 31 May 2016 04:08:33 +0000 (21:08 -0700)]
Add support for TFO connections

Summary:
This adds support to establish connections
over TFO.

The API introduced here retains the same
connect() + write() api that clients currently
use.

If enableTFO() is called then the connect will
be deferred to the first write. This only works
with request response protocols since a write
must trigger the connect. There is a tradeoff here
for the simpler API, and we can address this with
other signals such as a short timeout in the future.

Even though the client might enable TFO, the program
might run on machines without TFO support.
There were 2 choices for supporting machines where
TFO might not be enabled:
1. Fallback to normal connect if tfo sendmsg fails
2. Check tfo supported on the machine before using it

Both these have their tradeoffs, however option 1 does
not require us to read from procfs in the common code
path.

Reviewed By: Orvid

Differential Revision: D3327480

fbshipit-source-id: 9ac3a0c7ad2d206b158fdc305641fedbd93aa44d

7 years agoSwitch uses of networking headers to <folly/portability/Sockets.h>
Christopher Dykes [Mon, 30 May 2016 23:05:21 +0000 (16:05 -0700)]
Switch uses of networking headers to <folly/portability/Sockets.h>

Summary:
Diff #14 of 14.

This just switches the includes of the headers, it does not make the adjustments actually required to work for MSVC.

Reviewed By: mzlee

Differential Revision: D3249009

fbshipit-source-id: 434f58a7b8f4d0b7d142bc6a4abed8d95c953a7f

7 years agoSwitch uses of <sys/uio.h> to <folly/portability/SysUio.h>
Christopher Dykes [Mon, 30 May 2016 23:05:20 +0000 (16:05 -0700)]
Switch uses of <sys/uio.h> to <folly/portability/SysUio.h>

Summary: Diff #13 of 14.

Reviewed By: mzlee

Differential Revision: D3116264

fbshipit-source-id: 80e498b7062f40d723e156901bf62a2101a6e6ba

7 years agoSwitch uses of <unistd.h> to <folly/portability/Unistd.h>
Christopher Dykes [Mon, 30 May 2016 23:05:19 +0000 (16:05 -0700)]
Switch uses of <unistd.h> to <folly/portability/Unistd.h>

Summary: Diff #12 of 14.

Reviewed By: mzlee

Differential Revision: D3187000

fbshipit-source-id: ad56ca8ff20fa88949554b61545ee1bf982b0569

7 years agoInclude <folly/portability/PThread.h> where needed
Christopher Dykes [Mon, 30 May 2016 23:05:18 +0000 (16:05 -0700)]
Include <folly/portability/PThread.h> where needed

Summary: Diff #11 of 14.

Reviewed By: mzlee

Differential Revision: D3262478

fbshipit-source-id: d06eee44d1ccefc78a381e44c400c0242b5e0e5a

7 years agoInclude <folly/portability/Time.h> where needed
Christopher Dykes [Mon, 30 May 2016 23:05:17 +0000 (16:05 -0700)]
Include <folly/portability/Time.h> where needed

Summary: Diff #10 of 14

Reviewed By: mzlee

Differential Revision: D3270909

fbshipit-source-id: 2c644ca5db4ecf0387b167d2f6ecc62ddf687a56

7 years agoInclude <folly/portability/Fcntl.h> where needed
Christopher Dykes [Mon, 30 May 2016 23:05:16 +0000 (16:05 -0700)]
Include <folly/portability/Fcntl.h> where needed

Summary: Diff #9 of 14.

Reviewed By: mzlee

Differential Revision: D3262436

fbshipit-source-id: 60c5df03e91163c651bc6e5e0d38ba8b46b8c723

7 years agoSwitch uses of <sys/syscall.h> to <folly/portability/SysSyscall.h>
Christopher Dykes [Mon, 30 May 2016 23:05:15 +0000 (16:05 -0700)]
Switch uses of <sys/syscall.h> to <folly/portability/SysSyscall.h>

Summary: Diff #8 of 14.

Reviewed By: mzlee

Differential Revision: D3186943

fbshipit-source-id: 3b631faac36f3ec6e7fce031f07d8a98c76d7908

7 years agoSwitch uses of <dirent.h> to <folly/portability/Dirent.h>
Christopher Dykes [Mon, 30 May 2016 23:05:13 +0000 (16:05 -0700)]
Switch uses of <dirent.h> to <folly/portability/Dirent.h>

Summary: Diff #5 of 14.

Reviewed By: mzlee

Differential Revision: D3186885

fbshipit-source-id: 1947656137cad62a019cdf687cff9913bcecde2a

7 years agoSwitch uses of <sys/resource.h> to <folly/portability/SysResource.h>
Christopher Dykes [Mon, 30 May 2016 23:05:12 +0000 (16:05 -0700)]
Switch uses of <sys/resource.h> to <folly/portability/SysResource.h>

Summary: Diff #4 of 14.

Reviewed By: mzlee

Differential Revision: D3181101

fbshipit-source-id: d0db67ae19ee71e6a251c299ef672baead3ab9d9

7 years agoSwitch uses of <sys/mman.h> to <folly/portability/SysMman.h>
Christopher Dykes [Mon, 30 May 2016 23:05:11 +0000 (16:05 -0700)]
Switch uses of <sys/mman.h> to <folly/portability/SysMman.h>

Summary: Diff #3 of 14.

Reviewed By: mzlee

Differential Revision: D3110299

fbshipit-source-id: 39e4980df14b0b108b20dbbe57f2c5c6b361ea2e

7 years agoSwitch uses of <sys/file.h> to <folly/portability/SysFile.h>
Christopher Dykes [Mon, 30 May 2016 23:05:10 +0000 (16:05 -0700)]
Switch uses of <sys/file.h> to <folly/portability/SysFile.h>

Summary: Diff #2 of 14.

Reviewed By: mzlee

Differential Revision: D3115972

fbshipit-source-id: c75cbc95f37e8f2c2bf81311a8ca08ee766fc107

7 years agoInclude <folly/portability/SysTime.h> rather than <sys/time.h>
Christopher Dykes [Mon, 30 May 2016 23:05:07 +0000 (16:05 -0700)]
Include <folly/portability/SysTime.h> rather than <sys/time.h>

Summary:
This changes includes of `<sys/time.h>` to `<folly/portability/SysTime.h>`.

The ultimate goal of all of this is to get HHVM compiling on Windows with MSVC 2015. At the moment there are no plans to support MCRouter, Squangle, or AFDT under Windows. There are also no current plans to support MinGW or Cygwin.

Now, on to the headers themselves. There are three primary kinds of portability headers.

  - Replacement headers. These are headers such as `<sys/time.h>` that MSVC simply does not have. These headers should always be included instead of the headers they are replacing.
  - Additive headers. These are headers, such as `<time.h>` that MSVC has, but are either missing functions, or has the functions but under a different name. The portability headers for these only needs to be included instead of the original header if you are using a function that MSVC does not have. The reason for this is that forcing the use of the portability header for `<stdlib.h>` just because MSVC doesn't have `mkdtemp` and `realpath` would just be messy.
  - Utility headers. These are portability headers, such as `Memory.h` that exist to provide a portable interface to various things, in this case the allocation and freeing of aligned memory. They are not implementing any existing API, and all contents are in the `folly` namespace.

The API's implemented in the first and second types of headers are intended as drop-in replacements for the Linux versions, however they only implement the functions and features that are actually needed, and are not intended as feature-complete replacements. They are not intended for use outside of Facebook projects. Any API's expecting text input expect them to be in the same encoding as the Posix API's define, in most cases that means UTF-8. Anywhere in the portability headers that fails to account for this is a bug, and should be pointed out in the diff (if it's still in review), or else by filing a task detailing the issue and assigning it to me.

For all headers, if there is a mechanism in C++11, or else in Folly itself, that allows the same functionality, that mechanism should be used instead of adding a function to the portability headers.

Reviewed By: swtaarrs, yfeldblum

Differential Revision: D3095643

fbshipit-source-id: 3a5c0029f95b03ea320725df88e14a67ca38a445

7 years agoadd Cursor::readWhile() and skipWhile()
Adam Simpkins [Sat, 28 May 2016 00:48:14 +0000 (17:48 -0700)]
add Cursor::readWhile() and skipWhile()

Summary:
Add generic functions for reading or skipping until a predicate check fails.
This will allow us to simplify a few different call sites that have their own
logic similar to this.  Also change readTerminatedString() to use
readWhile().

Reviewed By: alandau

Differential Revision: D3337581

fbshipit-source-id: 9f50914c83adfc882219046862972661bed0e72a

7 years agoFix test/ProducerConsumerQueueTest.cpp build break
Ben Hamilton [Sat, 28 May 2016 00:01:53 +0000 (17:01 -0700)]
Fix test/ProducerConsumerQueueTest.cpp build break

Summary:
The Folly `make check` build is broken with Clang on OS X.

This diff fixes the following break in `test/ProducerConsumerQueueTest.cpp`:

  In file included from ProducerConsumerQueueTest.cpp:17:
  ../../folly/ProducerConsumerQueue.h:55:18: error: no member named 'bad_alloc' in namespace 'std'
        throw std::bad_alloc();

The problem is a missing `#include <memory>` in `folly/ProducerConsumerQueue.h`.

Reviewed By: djwatson

Differential Revision: D3361252

fbshipit-source-id: d740914f54ede5617abc16437ab2e20a7376f6e5

7 years agoFix test/FunctionTest.cpp build breaks
Ben Hamilton [Sat, 28 May 2016 00:00:50 +0000 (17:00 -0700)]
Fix test/FunctionTest.cpp build breaks

Summary:
The Folly `make check` build is broken with Clang on OS X.

This diff fixes two breaks in `test/FunctionTest.cpp`:

1. `FunctionTest.cpp:39:20: error: implicit instantiation of undefined template 'std::__1::array<int, 100>'`
2. `FunctionTest.cpp:103:3: error: no matching conversion for functional-style cast from 'Function<int (int)>' to '::testing::AssertionResult'`

The first was a missing `#include <array>`, and the second is a workaround for this gtest bug:
https://github.com/google/googletest/issues/429

Reviewed By: djwatson

Differential Revision: D3361188

fbshipit-source-id: 8140de978a2cbaf0f4aab45781ce0d656f03202b

7 years agoFix open source `make check` build broken by `ThreadedExecutorTest.cpp` moving to...
Ben Hamilton [Fri, 27 May 2016 23:12:47 +0000 (16:12 -0700)]
Fix open source `make check` build broken by `ThreadedExecutorTest.cpp` moving to `wangle`

Summary:
The Folly `make check` build is broken with Clang on OS X.

This diff fixes the following break in `make check`:

  make[3]: *** No rule to make target `../futures/test/ThreadedExecutorTest.cpp', needed by `../futures/test/ThreadedExecutorTest.o'.  Stop.

The problem is a reference to a file which was moved to `wangle` in D3286988.

Reviewed By: simpkins

Differential Revision: D3361287

fbshipit-source-id: a9ae794374982b4accf806bd9608988430c75cfd

7 years agoSet bufferCallback_ back to nullptr upon HTTPSession shutdown
Yang Chi [Fri, 27 May 2016 22:33:40 +0000 (15:33 -0700)]
Set bufferCallback_ back to nullptr upon HTTPSession shutdown

Summary: AsyncSocket::handleWrite may trigger HTTPSession::onWriteSuccess which may totally delete the HTTPSession. AsyncSocket::handleWrite also calls bufferCallback_->onEgressBufferCleared(). That bufferCallback_ is the HTTPSession. So in this diff resets bufferCallback_ to nullptr upon HTTPSession shutdown. afrind had a suggestion of adding a DestructorGuard in AsyncSocket::handleWrite. But i don't know DestructorGuard that well. I think it will keep AsyncSocket from being deleted but not the HTTPSession? Or maybe I can DestructorGuard dg(bufferCallback_) ?

Reviewed By: afrind

Differential Revision: D3311058

fbshipit-source-id: cdb5fcbd53837a3effbc096eab87fca4e5d17291

7 years agoFix test for corking
Subodh Iyengar [Fri, 27 May 2016 17:36:48 +0000 (10:36 -0700)]
Fix test for corking

Summary:
Min rto is now 50ms, so change the
test accordingly

Reviewed By: w-o-o

Differential Revision: D3357140

fbshipit-source-id: 5a807fabc1a3c216a5120b8eb4930e5c1eb52b7a

7 years agoUse exponential growth in fbstring::append(size_type n, value_type c)
Giuseppe Ottaviano [Fri, 27 May 2016 16:49:40 +0000 (09:49 -0700)]
Use exponential growth in fbstring::append(size_type n, value_type c)

Summary:
Contrary to the other `append` flavors, `fbstring::append(size_type n, value_type c)`
does not use exponential growth, which can trigger quadratic behavior.

Reviewed By: philippv

Differential Revision: D3357315

fbshipit-source-id: 2301ed1a9953544368663107113890a25c6621ae

7 years agoDisable a couple of warnings for MSVC in folly::Function
Christopher Dykes [Fri, 27 May 2016 16:48:20 +0000 (09:48 -0700)]
Disable a couple of warnings for MSVC in folly::Function

Summary:
MSVC generates warnings if you define multiple assignment operators or multiple copy constructors on the same type. This is deliberate in `folly::Function`, so just disable the warnings for the struct instead.
Note that the warnings have to be disabled for the entire struct for them to actually be disabled. Disabling them just around the declarations of the constructors and operators in question does not actually disable the warnings.

Reviewed By: yfeldblum

Differential Revision: D3347746

fbshipit-source-id: abb53a1e62bcfb7ce02759a7ce8637d824a82081

7 years agomove io/Cursor-defs.h to io/Cursor.cpp
Adam Simpkins [Thu, 26 May 2016 22:38:13 +0000 (15:38 -0700)]
move io/Cursor-defs.h to io/Cursor.cpp

Summary:
The two function definitions in this file are both normal function definitions,
not template functions.  Therefore this should be just a normal .cpp file, and
not a -defs.h header file.

Reviewed By: igorsugak

Differential Revision: D3348439

fbshipit-source-id: 3ebe49c84493f5aab06c568d9df15a37c2c48836

7 years agoadd a default Subprocess constructor
Adam Simpkins [Thu, 26 May 2016 19:50:38 +0000 (12:50 -0700)]
add a default Subprocess constructor

Summary:
The default constructor creates the Subprocess in an uninitialized state.  This
makes it possible to default-construct a Subprocess, but only initialize it
later using the move assignment operator.

Even before this diff, it was possible to have Subprocess objects in
uninitialized states after using the move assignment operator to move the data
out of a Subprocess object.

Reviewed By: yfeldblum

Differential Revision: D3348490

fbshipit-source-id: aa6acef9be770de8f0ee118da294cb134f04a466

7 years agoDisable the signal stack on Windows
Christopher Dykes [Thu, 26 May 2016 19:25:43 +0000 (12:25 -0700)]
Disable the signal stack on Windows

Summary: There is no equivalent functionality for Windows, so disable it completely.

Reviewed By: andriigrynenko

Differential Revision: D3348481

fbshipit-source-id: 52a104f5a8013113f54d5b10c2f0f9b720eeb4f6

7 years agoRemove the declaration of TimeseriesHistogram::getBucketIdx
Christopher Dykes [Thu, 26 May 2016 17:10:47 +0000 (10:10 -0700)]
Remove the declaration of TimeseriesHistogram::getBucketIdx

Summary: Because, as MSVC rightly tells us, it's not actually defined anywhere.

Reviewed By: yfeldblum

Differential Revision: D3347654

fbshipit-source-id: 2162bc745136ee0129caa4de78f38e1361083cd1

7 years agoInclude <folly/portability/Windows.h> in folly::format
Christopher Dykes [Thu, 26 May 2016 17:08:57 +0000 (10:08 -0700)]
Include <folly/portability/Windows.h> in folly::format

Summary:
Because apparently I didn't actually add the `Windows.h` include when I added support for MSVC.
This is safe for all platforms because the portability header only includes `Windows.h` when compiling on Windows.

Reviewed By: yfeldblum

Differential Revision: D3347785

fbshipit-source-id: 4f3e4e6c11953e2588b6fed287324a92e3ef8873

7 years agoFix doVecOperation in the SysUio portability header
Christopher Dykes [Thu, 26 May 2016 17:08:13 +0000 (10:08 -0700)]
Fix doVecOperation in the SysUio portability header

Summary: Apparently I failed to accurately transfer this particular change to fbcode, which meant it didn't actually compile on Windows.

Reviewed By: yfeldblum

Differential Revision: D3347976

fbshipit-source-id: 775682f10ab68672f6463ccc69a222ae0a52a097

7 years agoadd Cursor::peekBytes()
Adam Simpkins [Wed, 25 May 2016 19:07:07 +0000 (12:07 -0700)]
add Cursor::peekBytes()

Summary:
The existing Cursor::peek() method pre-dates the existence of folly::ByteRange,
and so it returns a std::pair containing a pointer and length.  This is usually
more awkward to use than ByteRange.

This adds a peekBytes() method that returns a ByteRange object, and updates all
users of peek() in folly to use peekBytes() instead.  Eventually I think we
should add a FOLLY_DEPRECATED attribute to peek().  I will wait to add this tag
until after converting a few other projects to use peekBytes(), though.

Reviewed By: alandau

Differential Revision: D3337302

fbshipit-source-id: 14220a059d915bf5adc66b8b283f7228b796a4dc

7 years agostd::make_unique -> folly::make_unique
Jon Maltiel Swenson [Tue, 24 May 2016 22:41:25 +0000 (15:41 -0700)]
std::make_unique -> folly::make_unique

Summary:
We should use folly::make_unique instead of std::make_unique in order to support
compilers without support for C++14.

Reviewed By: pavlo-fb

Differential Revision: D3343288

fbshipit-source-id: 9150af752e57988962a7580851ffa32086c669d7

7 years agoUpdate generate_varint_tables to support MSVC
Christopher Dykes [Tue, 24 May 2016 20:57:25 +0000 (13:57 -0700)]
Update generate_varint_tables to support MSVC

Summary: MSVC is a massive pain in this respect, and, after testing many workarounds, and only ending up with it just emitting a dynamic initializer for them, it's easier to just generate the table as a `uint64_t` and load it explicitly.

Reviewed By: yfeldblum

Differential Revision: D3270226

fbshipit-source-id: 77bc84e58d393373de05a28a30dcb80b66c09c9f

7 years agoClear old fs::path when moving a TemporaryDirectory
Joseph Griego [Tue, 24 May 2016 20:14:54 +0000 (13:14 -0700)]
Clear old fs::path when moving a TemporaryDirectory

Summary:
If a TemporaryDirectory with scope DELETE_ON_DESTRUCTION gets moved
and then goes out of scope, the directory will be deleted while the moved
object still holds that path; we just clear the path from the old object to
prevent this

Reviewed By: yfeldblum

Differential Revision: D3331079

fbshipit-source-id: 9c99413661e2436ccec937d3fa652da810133b34

7 years agoSwitch HHWheelTimer::SharedPtr to a standard shared pointer
Chad Parry [Tue, 24 May 2016 18:05:14 +0000 (11:05 -0700)]
Switch HHWheelTimer::SharedPtr to a standard shared pointer

Summary:
As part of my campaign to get `HHWheelTimer` away from intrusive ref-counting semantics, (cf. D3237530), I am redefining `HHWheelTimer::SharedPtr`. It is now a true `std::shared_ptr`. This will break clients that attempt the following:

  HHWheelTimer::UniquePtr timer1{HHWheelTimer::newTimer()};
  HHWheelTimer::SharedPtr timer2{timer1};

In the past, that code would compile, because `timer2` could use the intrusive ref-counting, and `timer1` would still be valid. After this change, the second timer would need to be initialized with `std::move(timer1)` instead. The `UniquePtr` is starting to actually be unique.

There is only one place in the code that actually tries to do such a copy. It's in a test, which I have changed. Any other instances of that behavior would create a compilation error, so it's impossible for one to be overlooked.

Reviewed By: djwatson

Differential Revision: D3337038

fbshipit-source-id: 085e4da41c9a142d253a1ac0b1dd0fc508dff704

7 years agoSupport folly::assume under MSVC
Christopher Dykes [Tue, 24 May 2016 00:30:29 +0000 (17:30 -0700)]
Support folly::assume under MSVC

Summary: Actually __assume when we assume.

Reviewed By: meyering

Differential Revision: D3336033

fbshipit-source-id: 6c931e9f832dd1d861d03e3ad8e308f612522401

7 years agoAvoid std::initializer_list ctor in folly::dynamic::array
Yedidya Feldblum [Mon, 23 May 2016 06:35:19 +0000 (23:35 -0700)]
Avoid std::initializer_list ctor in folly::dynamic::array

Summary:
[Folly] Avoid `std::initializer_list` ctor in `folly::dynamic::array`.

This will help deprecate the dangerous initializer-list constructor. The init-list construction is dangerous because its semantics vary by compiler and by compiler version.

Reviewed By: ot

Differential Revision: D3333148

fbshipit-source-id: 031e7593b7e28b299d5eeeb5ce7aed9216dd3a5c

7 years agoMake outstanding LoopKeepAlive hold EventBase destructor
Andrii Grynenko [Fri, 20 May 2016 22:55:24 +0000 (15:55 -0700)]
Make outstanding LoopKeepAlive hold EventBase destructor

Summary:
LoopKeepAlive handle can be grabbed to signal that some external event will later be scheduled on the EventBase via runInEventBaseThread. Usually the code which will be calling runInEventBaseThread only has a raw pointer to an EventBase, so it doesn't have any way to know it was destroyed.

This change ensures that EventBase destructor will keep running the event loop until all such LoopKeepAlive handles are released.

Reviewed By: yfeldblum

Differential Revision: D3323835

fbshipit-source-id: 4071dae691a61dfebe2f1759cf99f661b161fa4a

7 years agoFix the detection of preadv and pwritev on OSX in OSS
Christopher Dykes [Fri, 20 May 2016 21:26:38 +0000 (14:26 -0700)]
Fix the detection of preadv and pwritev on OSX in OSS

Summary:
The configure script doesn't define `FOLLY_HAVE_PREADV` if `preadv` isn't supported, so these checks were wrong. Undefined defines expand to `0`, so this is safe.
Fixes https://github.com/facebook/folly/issues/412

Reviewed By: mzlee, yfeldblum

Differential Revision: D3329405

fbshipit-source-id: faee8951c68d4c67e06e7720dfc142e63a9dbd9f

7 years agotemplating folly::fibers::await by Baton
Shubhanshu Agrawal [Fri, 20 May 2016 05:03:16 +0000 (22:03 -0700)]
templating folly::fibers::await by Baton

Summary:
This diff templates folly::fibers::await by Baton.
This would be helpful in passing in custom BatonType's, which is needed for D3007734

Depends on: D3314891

Reviewed By: andriigrynenko

Differential Revision: D3314925

fbshipit-source-id: 9052dc503b9509f16cd41b5e3ede0479a98067aa

7 years agoPromote memory_order_consume to memory_order_acquire
Alejandro [Thu, 19 May 2016 16:19:54 +0000 (09:19 -0700)]
Promote memory_order_consume to memory_order_acquire

Summary:
The current use case of `std::memory_order_consume` in this project was intended to provide the appropriate synchronization in cases where a consumer spins on `while( spsc_queue.empty() ) {} `, and then attempts to use an element of the queue since the loop was broken out of, according to comments [here](https://reviews.facebook.net/D48141). Consume semantics do not provide this guarantee according to the standard since there is no data dependency from the producer that can be carried to the consumer by doing a load-consume from the corresponding functions. What is needed is a load-acquire. Current compilers promote `memory_order_consume` to `memory_order_acquire`. Thus, this example appears to work simply due to the promotion from consume to acquire, but would fail to generate the right synchronization instructions on weaker architectures once `memory_order_consume` is implemented as intended.  Therefore, the `memory_order` should be tightened to `memory_order_acquire` to guarantee visibility to the consumer
Closes https://github.com/facebook/folly/pull/381

Reviewed By: djwatson

Differential Revision: D3173574

Pulled By: nbronson

fbshipit-source-id: b109be2e74f016750a3fe1f848e3fc66e609b9d2

7 years agoExtract Try to top-level
Yedidya Feldblum [Thu, 19 May 2016 09:20:24 +0000 (02:20 -0700)]
Extract Try to top-level

Summary:
[Folly] Extract `Try` to top-level.

It was in `folly/futures/`, but this diff moves it to `folly/`.

It is needed for futures, but it is are more general than futures and can be used separately.

Use `folly/Try.h` instead of `folly/futures/Try.h`.

Also fixes up all `#include` sites:

    hg grep -lw folly/futures/Try | xargs perl -pi -e 's,\bfolly/futures/Try\b,folly/Try,g'

Reviewed By: markisaa

Differential Revision: D3309908

fbshipit-source-id: cdf13f0ac0b0e36aa07e0f1d04870d06500c5874

7 years agofolly: FBString: avoid ubsan in assert
Lucian Grijincu [Thu, 19 May 2016 08:14:49 +0000 (01:14 -0700)]
folly: FBString: avoid ubsan in assert

Summary:
```
buck-out/gen/folly/__default_headers__#default,headers/folly/FBString.h:365:7: runtime error: null pointer passed as argument 2, which is declared to never be null
third-party-buck/build/glibc/include/string.h:70:33: note: nonnull attribute specified here
    #0 0x433a39 in _ZZN5folly13fbstring_coreIcEC1EPKcmbENKUlvE_clEv ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////buck-out/gen/folly/__default_headers__#default,headers/folly/FBString.h:365:7
    #1 0x4335a9 in _ZN5folly14ScopeGuardImplIZNS_13fbstring_coreIcEC1EPKcmbEUlvE_E7executeEv ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////buck-out/gen/folly/__default_headers__#default,headers/folly/ScopeGuard.h:153:29
    #2 0x4335a9 in _ZN5folly14ScopeGuardImplIZNS_13fbstring_coreIcEC1EPKcmbEUlvE_ED2Ev ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////buck-out/gen/folly/__default_headers__#default,headers/folly/ScopeGuard.h:130
    #3 0x4335a9 in folly::fbstring_core<char>::fbstring_core(char const*, unsigned long, bool) ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////buck-out/gen/folly/__default_headers__#default,headers/folly/FBString.h:427
    #4 0x4353fa in folly::basic_fbstring<char, std::char_traits<char>, std::allocator<char>, folly::fbstring_core<char> >::basic_fbstring(char const*, unsigned long, std::allocator<char> const&) ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////buck-out/gen/folly/__default_headers__#default,headers/folly/FBString.h:1038:9
```

Reviewed By: Gownta

Differential Revision: D3321266

fbshipit-source-id: 28d5aef16e91a98066a1de6bab95403fbc63eaab

7 years agoMove fibers out of experimental
Andrii Grynenko [Thu, 19 May 2016 05:57:40 +0000 (22:57 -0700)]
Move fibers out of experimental

Summary: folly::fibers have been used by mcrouter for more than 2 years, so not really experimental.

Reviewed By: pavlo-fb

Differential Revision: D3320595

fbshipit-source-id: 68188f92b71a4206d57222993848521ca5437ef5

7 years agoadding Promise::await
Shubhanshu Agrawal [Thu, 19 May 2016 03:42:24 +0000 (20:42 -0700)]
adding Promise::await

Summary:
This diff adds a static await method to Promise.
Also after this, folly::fibers::await is just a wrapper around Promise::await.
This also removes the friend relationship between Promise and await.

This would be helpful in making the folly::fibers::await templated by Baton changes easier to make.

Reviewed By: andriigrynenko

Differential Revision: D3314891

fbshipit-source-id: 361546c078caafd067734d2f474c617d7fb888b0

7 years agoMake Try independent of Future
Yedidya Feldblum [Thu, 19 May 2016 00:39:53 +0000 (17:39 -0700)]
Make Try independent of Future

Summary:
[Folly] Make `Try` independent of `Future`.

`Try` is useful for futures, but it can be used independently from futures as well.

Reviewed By: djwatson

Differential Revision: D3319130

fbshipit-source-id: badfcaa482dee6e7bdef14a501b4efc91634fa51

7 years agoExport documentation from dex to folly
Andrii Grynenko [Thu, 19 May 2016 00:11:24 +0000 (17:11 -0700)]
Export documentation from dex to folly

Reviewed By: yfeldblum

Differential Revision: D3317666

fbshipit-source-id: f5558b117c8da74789ee444cdaa00231e75dc31e

7 years agoFixing typo in Readme
iivlev [Wed, 18 May 2016 23:56:35 +0000 (16:56 -0700)]
Fixing typo in Readme

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

Reviewed By: yfeldblum

Differential Revision: D3320043

Pulled By: elliottneilclark

fbshipit-source-id: 5d21421216180fb601015a40a3de79e3dd2a162e

7 years agoFlesh out the comments about Unit
Yedidya Feldblum [Wed, 18 May 2016 21:46:49 +0000 (14:46 -0700)]
Flesh out the comments about Unit

Summary:
[Folly] Flesh out the comments about `Unit`.

* Remove mention of `Future`.
* Compare `unit` to `void`.

Reviewed By: djwatson

Differential Revision: D3316114

fbshipit-source-id: d30cf36cc029025734bb16c5bdbd640a289cad79

7 years agoExtract Unit to top-level
Yedidya Feldblum [Wed, 18 May 2016 03:38:11 +0000 (20:38 -0700)]
Extract Unit to top-level

Summary:
[Folly] Extract `Unit` to top-level.

It was in `folly/futures/`, but this diff moves it to `folly/`. It is needed for futures, but it is more general than futures and can be used separately.

Users must replace `folly/futures/Unit.h` with `folly/Unit.h`.

Also codemods existing usage sites:

```
hg grep -lw folly/futures/Unit.h | xargs perl -pi -e 's,\bfolly/futures/Unit\.h\b,folly/Unit.h,g'
```

Reviewed By: igorsugak

Differential Revision: D3314280

fbshipit-source-id: 16279b76b1d24529bec49077ccb36cd7d39f23b8

7 years agoadded IOBuf::wrapBufferAsValue()
Ilya Maykov [Wed, 18 May 2016 01:53:27 +0000 (18:53 -0700)]
added IOBuf::wrapBufferAsValue()

Summary:
Added a version of IOBuf::wrapBuffer() which returns the new IOBuf by value rather than heap-allocating it.
Motivation: we have a lot of places in the crypto code that do something like this:

  // take a string or vector parameter
  auto buf = folly::IOBuf::wrapBuffer( /* string or vector goes here */);
  // do something with buf
  // return

In these cases, a stack-allocated IOBuf would save us from an unnecessary heap allocation. But calling `folly::IOBuf(folly::IOBuf::WrapBufferOp::WRAP_BUFFER, ...)` is gross and in practice people just call the much more readable `wrapBuffer()` function. Hypothesis: readability trumps performance, but if we had a readable version that returned a stack-allocated IOBuf, it might see usage.

Reviewed By: yfeldblum

Differential Revision: D3314037

fbshipit-source-id: 4d4b5ec1d067762a27de1d7d6f7cac406388bfa3

7 years agoFix one more race in Promise::setTry
Andrii Grynenko [Wed, 18 May 2016 00:21:56 +0000 (17:21 -0700)]
Fix one more race in Promise::setTry

Summary:
Baton::post has to be the last step of Promise::setTry. If Promise is not owned by
the posting thread, it may be destroyed right after Baton::post is called.

Reviewed By: pavlo-fb

Differential Revision: D3312506

fbshipit-source-id: 6d5c3f0925c3bb9cd3f981e7550f888d5ed76189

7 years agoKeep the Unit test suite free of Promise and Future
Yedidya Feldblum [Wed, 18 May 2016 00:04:40 +0000 (17:04 -0700)]
Keep the Unit test suite free of Promise and Future

Summary:
[Folly] Keep the `Unit` test suite free of `Promise` and `Future`.

Move the `Promise`-related tests to the `Promise` test suite and the `Future`-related tests to the `Future` test suite.

Reviewed By: djwatson

Differential Revision: D3313635

fbshipit-source-id: 05c82c8719719d7709063ad58a4806036ca10fb3

7 years agoSimplify Unit
Yedidya Feldblum [Tue, 17 May 2016 22:27:43 +0000 (15:27 -0700)]
Simplify Unit

Summary:
[Folly] Simplify `Unit`.

Specifically:
* Make `Unit::Lift` and `Unit::Drop` simply be aliases to `std::conditional<...>`. No need for anything more complicated.
* Remove `is_void_or_unit`, which is unused and unnecessary.

Reviewed By: djwatson

Differential Revision: D3312481

fbshipit-source-id: 280d40aa8ef91c52f96a51b03e0a109d76c939ec

7 years agofolly: fbvector: ubsan: don't call memcpy() with nullptr args if size == 0
Marcus Holland-Moritz [Tue, 17 May 2016 20:08:17 +0000 (13:08 -0700)]
folly: fbvector: ubsan: don't call memcpy() with nullptr args if size == 0

Reviewed By: meyering, luciang

Differential Revision: D3310683

fbshipit-source-id: c86471d54062b2f3455f15fb73340fac486d9e44

7 years agoIPAddress::validate
Yedidya Feldblum [Tue, 17 May 2016 01:43:22 +0000 (18:43 -0700)]
IPAddress::validate

Summary: [Folly] `IPAddress::validate`.

Reviewed By: igorsugak

Differential Revision: D3308683

fbshipit-source-id: 48af18d6930f16718372021a4cc08062bf17327e

7 years agoFix a race in Promise::setTry
Andrii Grynenko [Mon, 16 May 2016 21:06:54 +0000 (14:06 -0700)]
Fix a race in Promise::setTry

Summary:
This fixes a race which can only be exposed if Promise is owned by the same thread which was calling await and Promise is fulfilled from a different thread. What could happen then was:
1. Thread 2 fulfills the promise and thus saves the value and posts Baton.
2. Thread 1 wakes up, await returns and then Thread 1 destroys the Promise.
3. Promise destructor checks value_, which is still not null, so it tries to fulfil it with exception.

Reviewed By: spalamarchuk

Differential Revision: D3306969

fbshipit-source-id: adf799cfd7b75b0201fa675a9e44ac7c7c42ac95

7 years agoMake await exception safe
Andrii Grynenko [Mon, 16 May 2016 20:13:24 +0000 (13:13 -0700)]
Make await exception safe

Summary: This fixes fibers::await to correctly handle exception thrown by user-passed lambda. Await still always waits for the promise to be fulfilled (if the promise was not moved out, it will be destroyed and thus auto-fulfilled with "promise not fulfilled" exception). However if user-passed lambda throws, promise result is ignored (even if exception) and exception thrown by lambda is re-thrown.

Reviewed By: pavlo-fb

Differential Revision: D3303393

fbshipit-source-id: c4ba01fde0e156cc88e5d07aaf763e3abe121d11

7 years agoPrefer constexpr to preprocessor conditionals when checking endianness
Yedidya Feldblum [Sat, 14 May 2016 19:49:52 +0000 (12:49 -0700)]
Prefer constexpr to preprocessor conditionals when checking endianness

Summary:
[Folly] Prefer `constexpr` to preprocessor conditionals when checking endianness.

Using `if (folly::kIsLittleEndian) {` v.s. `#if __BYTE_ORDER__ == __LITTLE_ENDIAN__`.

Reviewed By: meyering

Differential Revision: D3296770

fbshipit-source-id: b26df83fdd42a50663746fc7c9d5fbe67e6671eb

7 years agoA thread-per-task executor
Yedidya Feldblum [Sat, 14 May 2016 11:29:37 +0000 (04:29 -0700)]
A thread-per-task executor

Summary:
[Wangle] A thread-per-task executor.

Moved from Folly into Wangle and fleshed out.
* Starts task threads from a control thread, rather than starting task threads from the submitter thread. Because starting task threads is likely to be more expensive than moving a functor.
* Joins task threads as they finish, rather than joining all task threads in the executor's destructor.

Suitable for running tasks which spend most of their time sleeping. Such as blocking IO, blocking fork-exec-wait, etc., when it is inconvenient to use the nonblocking variants with an IO executor.

Reviewed By: simpkins

Differential Revision: D3286988

fbshipit-source-id: 4b91133a7d55332ebbae020c1515c60e816906b3