Make ~mutex and ~condition_variable trivial on Windows.

The implementations of __libcpp_mutex_destroy and __libcpp_condvar_destroy
are already NOPs, so this optimization is safe to perform.

See r365273 and PR27658 for more information.

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@365281 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/__config b/include/__config
index 1ed5169..1ecced9 100644
--- a/include/__config
+++ b/include/__config
@@ -1098,13 +1098,22 @@
 #endif
 
 // The Apple, glibc, and Bionic implementation of pthreads implements
-// pthread_mutex_destroy as nop for regular mutexes.
+// pthread_mutex_destroy as nop for regular mutexes. Additionally, Win32
+// mutexes have no destroy mechanism.
 // TODO(EricWF): Enable this optimization on Apple and Bionic platforms after
 // speaking to their respective stakeholders.
-#if defined(_LIBCPP_HAS_THREAD_API_PTHREAD) && defined(__GLIBC__)
+#if (defined(_LIBCPP_HAS_THREAD_API_PTHREAD) && defined(__GLIBC__)) \
+  || defined(_LIBCPP_HAS_THREAD_API_WIN32)
 # define _LIBCPP_HAS_TRIVIAL_MUTEX_DESTRUCTION
 #endif
 
+// Destroying a condvar is a nop on Windows.
+// TODO(EricWF): This is potentially true for some pthread implementations
+// as well.
+#if defined(_LIBCPP_HAS_THREAD_API_WIN32)
+# define _LIBCPP_HAS_TRIVIAL_CONDVAR_DESTRUCTION
+#endif
+
 // Systems that use capability-based security (FreeBSD with Capsicum,
 // Nuxi CloudABI) may only provide local filesystem access (using *at()).
 // Functions like open(), rename(), unlink() and stat() should not be
diff --git a/include/__mutex_base b/include/__mutex_base
index 61abae9..f828bea 100644
--- a/include/__mutex_base
+++ b/include/__mutex_base
@@ -282,26 +282,20 @@
 
 class _LIBCPP_TYPE_VIS condition_variable
 {
-#ifndef _LIBCPP_CXX03_LANG
     __libcpp_condvar_t __cv_ = _LIBCPP_CONDVAR_INITIALIZER;
-#else
-    __libcpp_condvar_t __cv_;
-#endif
-
 public:
     _LIBCPP_INLINE_VISIBILITY
-#ifndef _LIBCPP_CXX03_LANG
-    constexpr condition_variable() _NOEXCEPT = default;
+    _LIBCPP_CONSTEXPR condition_variable() _NOEXCEPT = default;
+
+#ifdef _LIBCPP_HAS_TRIVIAL_CONDVAR_DESTRUCTION
+    ~condition_variable() = default;
 #else
-    condition_variable() _NOEXCEPT {__cv_ = (__libcpp_condvar_t)_LIBCPP_CONDVAR_INITIALIZER;}
-#endif
     ~condition_variable();
+#endif
 
-private:
-    condition_variable(const condition_variable&); // = delete;
-    condition_variable& operator=(const condition_variable&); // = delete;
+    condition_variable(const condition_variable&) = delete;
+    condition_variable& operator=(const condition_variable&) = delete;
 
-public:
     void notify_one() _NOEXCEPT;
     void notify_all() _NOEXCEPT;
 
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 56d7b98..c13f890 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -8,6 +8,7 @@
   charconv.cpp
   chrono.cpp
   condition_variable.cpp
+  condition_variable_destructor.cpp
   debug.cpp
   exception.cpp
   functional.cpp
diff --git a/src/condition_variable.cpp b/src/condition_variable.cpp
index e9b3739..b8426c6 100644
--- a/src/condition_variable.cpp
+++ b/src/condition_variable.cpp
@@ -21,10 +21,7 @@
 
 _LIBCPP_BEGIN_NAMESPACE_STD
 
-condition_variable::~condition_variable()
-{
-    __libcpp_condvar_destroy(&__cv_);
-}
+// ~condition_variable is defined elsewhere.
 
 void
 condition_variable::notify_one() _NOEXCEPT
diff --git a/src/condition_variable_destructor.cpp b/src/condition_variable_destructor.cpp
new file mode 100644
index 0000000..44fa240
--- /dev/null
+++ b/src/condition_variable_destructor.cpp
@@ -0,0 +1,46 @@
+//===---------------- condition_variable_destructor.cpp ------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// Define ~condition_variable.
+//
+// On some platforms ~condition_variable has been made trivial and the
+// definition is only provided for ABI compatibility.
+
+#include "__config"
+#include "__threading_support"
+
+#if !defined(_LIBCPP_HAS_NO_THREADS)
+# if _LIBCPP_ABI_VERSION == 1 || !defined(_LIBCPP_HAS_TRIVIAL_CONDVAR_DESTRUCTION)
+#   define NEEDS_CONDVAR_DESTRUCTOR
+# endif
+#endif
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+#ifdef NEEDS_CONDVAR_DESTRUCTOR
+
+class _LIBCPP_TYPE_VIS condition_variable
+{
+    __libcpp_condvar_t __cv_ = _LIBCPP_CONDVAR_INITIALIZER;
+public:
+    _LIBCPP_INLINE_VISIBILITY
+    constexpr condition_variable() noexcept = default;
+
+    ~condition_variable();
+
+    condition_variable(const condition_variable&) = delete;
+    condition_variable& operator=(const condition_variable&)  = delete;
+};
+
+condition_variable::~condition_variable()
+{
+    __libcpp_condvar_destroy(&__cv_);
+}
+#endif
+
+_LIBCPP_END_NAMESPACE_STD