X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2FSynchronized.h;h=4fc47afd5ed7e22cb4afb9a04453df09e2b1b8b8;hb=942ad3f3218f172b81de85c0d0565ee46a93da3e;hp=83e59e80cef189ca7cd20650a92953288375804b;hpb=7de41cd34151873c87d86353e596fbae2ebc74a8;p=folly.git diff --git a/folly/Synchronized.h b/folly/Synchronized.h index 83e59e80..4fc47afd 100644 --- a/folly/Synchronized.h +++ b/folly/Synchronized.h @@ -1,5 +1,5 @@ /* - * Copyright 2014 Facebook, Inc. + * Copyright 2015 Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -27,8 +27,8 @@ #include #include #include -#include "folly/Preprocessor.h" -#include "folly/Traits.h" +#include +#include namespace folly { @@ -39,6 +39,14 @@ enum InternalDoNotUse {}; * Free function adaptors for std:: and boost:: */ +// Android, OSX, and Cygwin don't have timed mutexes +#if defined(ANDROID) || defined(__ANDROID__) || \ + defined(__APPLE__) || defined(__CYGWIN__) +# define FOLLY_SYNCHRONIZED_HAVE_TIMED_MUTEXES 0 +#else +# define FOLLY_SYNCHRONIZED_HAVE_TIMED_MUTEXES 1 +#endif + /** * Yields true iff T has .lock() and .unlock() member functions. This * is done by simply enumerating the mutexes with this interface in @@ -49,7 +57,7 @@ struct HasLockUnlock { enum { value = IsOneOf::value}; + static constexpr bool nxMoveCtor{ + std::is_nothrow_move_constructible::value}; + + /** + * Helper constructors to enable Synchronized for + * non-default constructible types T. + * Guards are created in actual public constructors and are alive + * for the time required to construct the object + */ + template + Synchronized(const Synchronized& rhs, + const Guard& /*guard*/) noexcept(nxCopyCtor) + : datum_(rhs.datum_) {} + + template + Synchronized(Synchronized&& rhs, const Guard& /*guard*/) noexcept(nxMoveCtor) + : datum_(std::move(rhs.datum_)) {} + + public: /** * Copy constructor copies the data (with locking the source and * all) but does NOT copy the mutex. Doing so would result in * deadlocks. */ - Synchronized(const Synchronized& rhs) { - auto guard = rhs.operator->(); - datum_ = rhs.datum_; - } + Synchronized(const Synchronized& rhs) noexcept(nxCopyCtor) + : Synchronized(rhs, rhs.operator->()) {} /** * Move constructor moves the data (with locking the source and all) * but does not move the mutex. */ - Synchronized(Synchronized&& rhs) { - auto guard = rhs.operator->(); - datum_ = std::move(rhs.datum_); - } + Synchronized(Synchronized&& rhs) noexcept(nxMoveCtor) + : Synchronized(std::move(rhs), rhs.operator->()) {} /** * Constructor taking a datum as argument copies it. There is no * need to lock the constructing object. */ - explicit Synchronized(const T& rhs) : datum_(rhs) {} + explicit Synchronized(const T& rhs) noexcept(nxCopyCtor) : datum_(rhs) {} /** * Constructor taking a datum rvalue as argument moves it. Again, * there is no need to lock the constructing object. */ - explicit Synchronized(T&& rhs) : datum_(std::move(rhs)) {} + explicit Synchronized(T&& rhs) noexcept(nxMoveCtor) + : datum_(std::move(rhs)) {} /** * The canonical assignment operator only assigns the data, NOT the