Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r86192 - in trunk/boost: detail/winapi sync/detail sync/detail/condition_variables sync/detail/mutexes sync/support
From: andrey.semashev_at_[hidden]
Date: 2013-10-07 13:41:43


Author: andysem
Date: 2013-10-07 13:41:43 EDT (Mon, 07 Oct 2013)
New Revision: 86192
URL: http://svn.boost.org/trac/boost/changeset/86192

Log:
Fixed WinAPI compilation. Ported timed mutex to waitable timer. Windows time points are now aligned with FILETIME to reduce conversion overhead when used with waitable timers.

Added:
   trunk/boost/sync/detail/mutexes/basic_mutex_windows.hpp (contents, props changed)
Text files modified:
   trunk/boost/detail/winapi/basic_types.hpp | 4
   trunk/boost/detail/winapi/dll.hpp | 39 ++++++--
   trunk/boost/detail/winapi/synchronization.hpp | 2
   trunk/boost/sync/detail/condition_variables/basic_condition_variable_windows.hpp | 5
   trunk/boost/sync/detail/mutexes/basic_mutex_windows.hpp | 184 ++++++++++++++++++++++++++++++++++++++++
   trunk/boost/sync/detail/mutexes/mutex_windows.hpp | 128 +--------------------------
   trunk/boost/sync/detail/mutexes/timed_mutex_windows.hpp | 77 ++++++++++++++--
   trunk/boost/sync/detail/time_units.hpp | 43 ++++-----
   trunk/boost/sync/detail/waitable_timer.hpp | 17 +-
   trunk/boost/sync/support/posix_time.hpp | 10 +
   10 files changed, 328 insertions(+), 181 deletions(-)

Modified: trunk/boost/detail/winapi/basic_types.hpp
==============================================================================
--- trunk/boost/detail/winapi/basic_types.hpp Mon Oct 7 13:27:42 2013 (r86191)
+++ trunk/boost/detail/winapi/basic_types.hpp 2013-10-07 13:41:43 EDT (Mon, 07 Oct 2013) (r86192)
@@ -101,7 +101,7 @@
 # ifdef _WIN64
 #if defined(__CYGWIN__)
     typedef long INT_PTR_;
- typedef unsigned long INT_PTR_;
+ typedef unsigned long UINT_PTR_;
     typedef long LONG_PTR_;
     typedef unsigned long ULONG_PTR_;
 #else
@@ -112,7 +112,7 @@
 #endif
 # else
     typedef int INT_PTR_;
- typedef unsigned int INT_PTR_;
+ typedef unsigned int UINT_PTR_;
     typedef long LONG_PTR_;
     typedef unsigned long ULONG_PTR_;
 # endif

Modified: trunk/boost/detail/winapi/dll.hpp
==============================================================================
--- trunk/boost/detail/winapi/dll.hpp Mon Oct 7 13:27:42 2013 (r86191)
+++ trunk/boost/detail/winapi/dll.hpp 2013-10-07 13:41:43 EDT (Mon, 07 Oct 2013) (r86192)
@@ -27,26 +27,47 @@
     typedef ::NEARPROC NEARPROC_;
     typedef ::PROC PROC_;
 
- using ::LoadLibrary;
+# ifdef BOOST_NO_ANSI_APIS
+ using ::LoadLibraryW;
+ using ::GetModuleHandleW;
+# else
+ using ::LoadLibraryA;
+ using ::GetModuleHandleA;
+# endif
     using ::FreeLibrary;
     using ::GetProcAddress;
- using ::GetModuleHandleA;
 #else
 extern "C" {
-#ifdef _WIN64
+# ifdef _WIN64
     typedef INT_PTR (WINAPI *FARPROC_)();
     typedef INT_PTR (WINAPI *NEARPROC_)();
     typedef INT_PTR (WINAPI *PROC_)();
-#else
+# else
     typedef int (WINAPI *FARPROC_)();
     typedef int (WINAPI *NEARPROC_)();
     typedef int (WINAPI *PROC_)();
-#endif // _WIN64
+# endif // _WIN64
 
+# ifdef BOOST_NO_ANSI_APIS
+ __declspec(dllimport) HMODULE_ WINAPI
+ LoadLibraryW(
+ LPCWSTR_ lpFileName
+ );
     __declspec(dllimport) HMODULE_ WINAPI
- LoadLibrary(
- LPCTSTR_ lpFileName
+ GetModuleHandleW(
+ LPCWSTR_ lpFileName
     );
+# else
+ __declspec(dllimport) HMODULE_ WINAPI
+ LoadLibraryA(
+ LPCSTR_ lpFileName
+ );
+ __declspec(dllimport) HMODULE_ WINAPI
+ GetModuleHandleA(
+ LPCSTR_ lpFileName
+ );
+# endif
+
     __declspec(dllimport) BOOL_ WINAPI
         FreeLibrary(
             HMODULE_ hModule
@@ -56,10 +77,6 @@
             HMODULE_ hModule,
             LPCSTR_ lpProcName
     );
- __declspec(dllimport) HMODULE_ WINAPI
- GetModuleHandleA(
- LPCSTR_ lpFileName
- );
 }
 #endif
 }

Modified: trunk/boost/detail/winapi/synchronization.hpp
==============================================================================
--- trunk/boost/detail/winapi/synchronization.hpp Mon Oct 7 13:27:42 2013 (r86191)
+++ trunk/boost/detail/winapi/synchronization.hpp 2013-10-07 13:41:43 EDT (Mon, 07 Oct 2013) (r86192)
@@ -147,6 +147,8 @@
 
 #endif // defined( BOOST_USE_WINDOWS_H )
 
+const DWORD_ max_non_infinite_wait = (DWORD_)0xFFFFFFFE;
+
 BOOST_FORCEINLINE HANDLE_ create_anonymous_mutex(_SECURITY_ATTRIBUTES* lpAttributes, BOOL_ bInitialOwner)
 {
 #ifdef BOOST_NO_ANSI_APIS

Modified: trunk/boost/sync/detail/condition_variables/basic_condition_variable_windows.hpp
==============================================================================
--- trunk/boost/sync/detail/condition_variables/basic_condition_variable_windows.hpp Mon Oct 7 13:27:42 2013 (r86191)
+++ trunk/boost/sync/detail/condition_variables/basic_condition_variable_windows.hpp 2013-10-07 13:41:43 EDT (Mon, 07 Oct 2013) (r86192)
@@ -34,6 +34,7 @@
 #include <boost/sync/detail/time_units.hpp>
 #include <boost/sync/detail/interlocked.hpp>
 #include <boost/sync/detail/auto_handle.hpp>
+#include <boost/sync/detail/waitable_timer.hpp>
 #include <boost/sync/locks/lock_guard.hpp>
 #include <boost/sync/mutexes/mutex.hpp>
 #include <boost/sync/condition_variables/cv_status.hpp>
@@ -114,8 +115,8 @@
         sync::detail::system_duration::native_type time_left = t.get();
         while (time_left > 0)
         {
- const unsigned int dur = time_left > INT_MAX ?
- static_cast< unsigned int >(INT_MAX) : static_cast< unsigned int >(time_left);
+ const unsigned int dur = time_left > static_cast< unsigned int >(boost::detail::winapi::max_non_infinite_wait) ?
+ static_cast< unsigned int >(boost::detail::winapi::max_non_infinite_wait) : static_cast< unsigned int >(time_left);
             const boost::detail::winapi::DWORD_ res = boost::detail::winapi::WaitForSingleObject(m_semaphore, dur);
             switch (res)
             {

Added: trunk/boost/sync/detail/mutexes/basic_mutex_windows.hpp
==============================================================================
--- /dev/null 00:00:00 1970 (empty, because file is newly added)
+++ trunk/boost/sync/detail/mutexes/basic_mutex_windows.hpp 2013-10-07 13:41:43 EDT (Mon, 07 Oct 2013) (r86192)
@@ -0,0 +1,184 @@
+/*
+ * Distributed under the Boost Software License, Version 1.0.
+ * (See accompanying file LICENSE_1_0.txt or copy at
+ * http://www.boost.org/LICENSE_1_0.txt)
+ *
+ * (C) Copyright 2007-2008 Anthony Williams
+ * (C) Copyright 2012-2013 Vicente J. Botet Escriba
+ * (C) Copyright 2013 Andrey Semashev
+ */
+/*!
+ * \file detail/mutexes/basic_mutex_windows.hpp
+ *
+ * \brief This header is the Boost.Sync library implementation, see the library documentation
+ * at http://www.boost.org/doc/libs/release/libs/sync/doc/html/index.html.
+ */
+
+#ifndef BOOST_SYNC_DETAIL_MUTEXES_BASIC_MUTEX_WINDOWS_HPP_INCLUDED_
+#define BOOST_SYNC_DETAIL_MUTEXES_BASIC_MUTEX_WINDOWS_HPP_INCLUDED_
+
+#include <cstddef>
+#include <boost/assert.hpp>
+#include <boost/throw_exception.hpp>
+#include <boost/detail/winapi/handles.hpp>
+#include <boost/detail/winapi/synchronization.hpp>
+#include <boost/sync/exceptions/lock_error.hpp>
+#include <boost/sync/exceptions/resource_error.hpp>
+#include <boost/sync/detail/config.hpp>
+#include <boost/sync/detail/interlocked.hpp>
+
+#include <boost/sync/detail/header.hpp>
+
+#ifdef BOOST_HAS_PRAGMA_ONCE
+#pragma once
+#endif
+
+namespace boost {
+
+namespace sync {
+
+namespace detail {
+
+namespace windows {
+
+class basic_mutex
+{
+public:
+ enum
+ {
+ lock_flag_bit = 31u,
+ event_set_flag_bit = 30u,
+ lock_flag_value = 1u << lock_flag_bit,
+ event_set_flag_value = 1u << event_set_flag_bit
+ };
+
+public:
+ long m_active_count;
+ boost::detail::winapi::HANDLE_ m_event;
+
+public:
+ BOOST_CONSTEXPR basic_mutex() BOOST_NOEXCEPT : m_event(NULL), m_active_count(0)
+ {
+ }
+
+ ~basic_mutex()
+ {
+ if (m_event)
+ BOOST_VERIFY(boost::detail::winapi::CloseHandle(m_event) != 0);
+ }
+
+ void lock()
+ {
+ if (try_lock())
+ return;
+
+ long old_count = m_active_count;
+ mark_waiting_and_try_lock(old_count);
+
+ if (old_count & lock_flag_value)
+ {
+ bool lock_acquired = false;
+ boost::detail::winapi::HANDLE_ const sem = get_event();
+
+ do
+ {
+ const boost::detail::winapi::DWORD_ retval = boost::detail::winapi::WaitForSingleObject(sem, boost::detail::winapi::infinite);
+ BOOST_ASSERT(0 == retval || boost::detail::winapi::wait_abandoned == retval);
+ clear_waiting_and_try_lock(old_count);
+ lock_acquired = (old_count & lock_flag_value) == 0;
+ }
+ while (!lock_acquired);
+ }
+ }
+
+ void unlock() BOOST_NOEXCEPT
+ {
+ long const offset = lock_flag_value;
+ long const old_count = BOOST_ATOMIC_INTERLOCKED_EXCHANGE_ADD(&m_active_count, lock_flag_value);
+ if ((old_count & event_set_flag_value) == 0 && (old_count > offset))
+ {
+ if (!sync::detail::windows::interlocked_bit_test_and_set(&m_active_count, event_set_flag_bit))
+ {
+ boost::detail::winapi::SetEvent(get_event());
+ }
+ }
+ }
+
+ bool try_lock()
+ {
+ return !sync::detail::windows::interlocked_bit_test_and_set(&m_active_count, lock_flag_bit);
+ }
+
+ boost::detail::winapi::HANDLE_ get_event()
+ {
+ boost::detail::winapi::HANDLE_ event = sync::detail::windows::interlocked_read_acquire(&m_event);
+
+ if (!event)
+ {
+ event = boost::detail::winapi::CreateEventA(NULL, false, false, NULL);
+#ifdef BOOST_MSVC
+#pragma warning(push)
+#pragma warning(disable:4311)
+#pragma warning(disable:4312)
+#endif
+ boost::detail::winapi::HANDLE_ const old_event = BOOST_ATOMIC_INTERLOCKED_COMPARE_EXCHANGE_POINTER(&m_event, event, NULL);
+#ifdef BOOST_MSVC
+#pragma warning(pop)
+#endif
+ if (old_event != NULL)
+ {
+ boost::detail::winapi::CloseHandle(event);
+ return old_event;
+ }
+ }
+
+ return event;
+ }
+
+ void mark_waiting_and_try_lock(long& old_count)
+ {
+ while (true)
+ {
+ bool const was_locked = (old_count & lock_flag_value) ? true : false;
+ long const new_count = was_locked ? (old_count + 1) : (old_count | lock_flag_value);
+ long const current = BOOST_ATOMIC_INTERLOCKED_COMPARE_EXCHANGE(&m_active_count, new_count, old_count);
+ if (current == old_count)
+ {
+ if (was_locked)
+ old_count = new_count;
+ break;
+ }
+ old_count = current;
+ }
+ }
+
+ void clear_waiting_and_try_lock(long& old_count)
+ {
+ old_count &= ~lock_flag_value;
+ old_count |= event_set_flag_value;
+ while (true)
+ {
+ long const new_count = ((old_count & lock_flag_value) ? old_count : ((old_count - 1) | lock_flag_value)) & ~static_cast< long >(event_set_flag_value);
+ long const current = BOOST_ATOMIC_INTERLOCKED_COMPARE_EXCHANGE(&m_active_count, new_count, old_count);
+ if (current == old_count)
+ break;
+
+ old_count = current;
+ }
+ }
+
+ BOOST_DELETED_FUNCTION(basic_mutex(basic_mutex const&))
+ BOOST_DELETED_FUNCTION(basic_mutex& operator= (basic_mutex const&))
+};
+
+} // namespace windows
+
+} // namespace detail
+
+} // namespace sync
+
+} // namespace boost
+
+#include <boost/sync/detail/footer.hpp>
+
+#endif // BOOST_SYNC_DETAIL_MUTEXES_MUTEX_WINDOWS_HPP_INCLUDED_

Modified: trunk/boost/sync/detail/mutexes/mutex_windows.hpp
==============================================================================
--- trunk/boost/sync/detail/mutexes/mutex_windows.hpp Mon Oct 7 13:27:42 2013 (r86191)
+++ trunk/boost/sync/detail/mutexes/mutex_windows.hpp 2013-10-07 13:41:43 EDT (Mon, 07 Oct 2013) (r86192)
@@ -17,16 +17,8 @@
 #ifndef BOOST_SYNC_DETAIL_MUTEXES_MUTEX_WINDOWS_HPP_INCLUDED_
 #define BOOST_SYNC_DETAIL_MUTEXES_MUTEX_WINDOWS_HPP_INCLUDED_
 
-#include <cstddef>
-#include <boost/assert.hpp>
-#include <boost/throw_exception.hpp>
-#include <boost/detail/winapi/handles.hpp>
-#include <boost/detail/winapi/synchronization.hpp>
-#include <boost/sync/exceptions/lock_error.hpp>
-#include <boost/sync/exceptions/resource_error.hpp>
 #include <boost/sync/detail/config.hpp>
-#include <boost/sync/detail/interlocked.hpp>
-
+#include <boost/sync/detail/mutexes/basic_mutex_windows.hpp>
 #include <boost/sync/detail/header.hpp>
 
 #ifdef BOOST_HAS_PRAGMA_ONCE
@@ -39,146 +31,38 @@
 
 BOOST_SYNC_DETAIL_OPEN_ABI_NAMESPACE {
 
-class timed_mutex;
-
 class mutex
 {
- friend class timed_mutex;
-
 public:
     typedef void _is_condition_variable_compatible;
 
 private:
- enum
- {
- lock_flag_bit = 31u,
- event_set_flag_bit = 30u,
- lock_flag_value = 1u << lock_flag_bit,
- event_set_flag_value = 1u << event_set_flag_bit
- };
-
-private:
- long m_active_count;
- boost::detail::winapi::HANDLE_ m_event;
+ sync::detail::windows::basic_mutex m_mutex;
 
 public:
 #if !defined(BOOST_NO_CXX11_CONSTEXPR)
 #define BOOST_SYNC_DEFINES_MUTEX_CONSTEXPR_CONSTRUCTOR
 #endif
 
- BOOST_CONSTEXPR mutex() BOOST_NOEXCEPT : m_event(NULL), m_active_count(0)
- {
- }
-
- ~mutex()
- {
- if (m_event)
- BOOST_VERIFY(boost::detail::winapi::CloseHandle(m_event) != 0);
- }
+ BOOST_CONSTEXPR mutex() BOOST_NOEXCEPT : m_mutex() {}
 
     void lock()
     {
- if (try_lock())
- return;
-
- long old_count = m_active_count;
- mark_waiting_and_try_lock(old_count);
-
- if (old_count & lock_flag_value)
- {
- bool lock_acquired = false;
- boost::detail::winapi::HANDLE_ const sem = get_event();
-
- do
- {
- const boost::detail::winapi::DWORD_ retval = boost::detail::winapi::WaitForSingleObject(sem, boost::detail::winapi::infinite);
- BOOST_ASSERT(0 == retval || boost::detail::winapi::wait_abandoned == retval);
- clear_waiting_and_try_lock(old_count);
- lock_acquired = (old_count & lock_flag_value) == 0;
- }
- while (!lock_acquired);
- }
+ m_mutex.lock();
     }
 
     void unlock() BOOST_NOEXCEPT
     {
- long const offset = lock_flag_value;
- long const old_count = BOOST_ATOMIC_INTERLOCKED_EXCHANGE_ADD(&m_active_count, lock_flag_value);
- if ((old_count & event_set_flag_value) == 0 && (old_count > offset))
- {
- if (!sync::detail::windows::interlocked_bit_test_and_set(&m_active_count, event_set_flag_bit))
- {
- boost::detail::winapi::SetEvent(get_event());
- }
- }
+ m_mutex.unlock();
     }
 
     bool try_lock()
     {
- return !sync::detail::windows::interlocked_bit_test_and_set(&m_active_count, lock_flag_bit);
+ return m_mutex.try_lock();
     }
 
     BOOST_DELETED_FUNCTION(mutex(mutex const&))
     BOOST_DELETED_FUNCTION(mutex& operator= (mutex const&))
-
-private:
- boost::detail::winapi::HANDLE_ get_event()
- {
- boost::detail::winapi::HANDLE_ event = sync::detail::windows::interlocked_read_acquire(&m_event);
-
- if (!event)
- {
- event = boost::detail::winapi::CreateEventA(NULL, false, false, NULL);
-#ifdef BOOST_MSVC
-#pragma warning(push)
-#pragma warning(disable:4311)
-#pragma warning(disable:4312)
-#endif
- boost::detail::winapi::HANDLE_ const old_event = BOOST_ATOMIC_INTERLOCKED_COMPARE_EXCHANGE_POINTER(&m_event, event, NULL);
-#ifdef BOOST_MSVC
-#pragma warning(pop)
-#endif
- if (old_event != NULL)
- {
- boost::detail::winapi::CloseHandle(event);
- return old_event;
- }
- }
-
- return event;
- }
-
- void mark_waiting_and_try_lock(long& old_count)
- {
- while (true)
- {
- bool const was_locked = (old_count & lock_flag_value) ? true : false;
- long const new_count = was_locked ? (old_count + 1) : (old_count | lock_flag_value);
- long const current = BOOST_ATOMIC_INTERLOCKED_COMPARE_EXCHANGE(&m_active_count, new_count, old_count);
- if (current == old_count)
- {
- if (was_locked)
- old_count = new_count;
- break;
- }
- old_count = current;
- }
- }
-
- void clear_waiting_and_try_lock(long& old_count)
- {
- old_count &= ~lock_flag_value;
- old_count |= event_set_flag_value;
- while (true)
- {
- long const new_count = ((old_count & lock_flag_value) ? old_count : ((old_count - 1) | lock_flag_value)) & ~static_cast< long >(event_set_flag_value);
- long const current = BOOST_ATOMIC_INTERLOCKED_COMPARE_EXCHANGE(&m_active_count, new_count, old_count);
- if (current == old_count)
- break;
-
- old_count = current;
- }
- }
 };
 
 } // namespace winnt

Modified: trunk/boost/sync/detail/mutexes/timed_mutex_windows.hpp
==============================================================================
--- trunk/boost/sync/detail/mutexes/timed_mutex_windows.hpp Mon Oct 7 13:27:42 2013 (r86191)
+++ trunk/boost/sync/detail/mutexes/timed_mutex_windows.hpp 2013-10-07 13:41:43 EDT (Mon, 07 Oct 2013) (r86192)
@@ -18,7 +18,6 @@
 #define BOOST_SYNC_DETAIL_MUTEXES_TIMED_MUTEX_WINDOWS_HPP_INCLUDED_
 
 #include <cstddef>
-#include <limits>
 #include <boost/cstdint.hpp>
 #include <boost/assert.hpp>
 #include <boost/throw_exception.hpp>
@@ -29,7 +28,8 @@
 #include <boost/sync/detail/interlocked.hpp>
 #include <boost/sync/detail/time_traits.hpp>
 #include <boost/sync/detail/time_units.hpp>
-#include <boost/sync/detail/mutexes/mutex_windows.hpp>
+#include <boost/sync/detail/waitable_timer.hpp>
+#include <boost/sync/detail/mutexes/basic_mutex_windows.hpp>
 
 #include <boost/sync/detail/header.hpp>
 
@@ -49,7 +49,7 @@
     typedef void _is_condition_variable_compatible;
 
 private:
- mutex m_mutex;
+ sync::detail::windows::basic_mutex m_mutex;
 
 public:
 #if !defined(BOOST_NO_CXX11_CONSTEXPR)
@@ -99,7 +99,43 @@
 private:
     bool priv_timed_lock(sync::detail::system_time_point const& t)
     {
- return priv_timed_lock(t - sync::detail::system_time_point::now());
+ if (m_mutex.try_lock())
+ return true;
+
+ long old_count = m_mutex.m_active_count;
+ m_mutex.mark_waiting_and_try_lock(old_count);
+ if ((old_count & sync::detail::windows::basic_mutex::lock_flag_value) == 0)
+ return true;
+
+ boost::detail::winapi::HANDLE_ handles[2];
+ handles[0] = m_mutex.get_event();
+ handles[1] = sync::detail::windows::get_waitable_timer();
+
+ if (!boost::detail::winapi::SetWaitableTimer(handles[1], reinterpret_cast< const boost::detail::winapi::LARGE_INTEGER_* >(&t.get()), 0, NULL, NULL, false))
+ abort_lock_and_throw("boost: timed_mutex timedlock failed to set a timeout", __LINE__);
+
+ while (true)
+ {
+ const boost::detail::winapi::DWORD_ res = boost::detail::winapi::WaitForMultipleObjects(sizeof(handles) / sizeof(*handles), handles, false, boost::detail::winapi::infinite);
+ if (res == boost::detail::winapi::wait_failed)
+ abort_lock_and_throw("boost: timed_mutex timedlock failed in WaitForMultipleObjects", __LINE__);
+
+ switch (res)
+ {
+ case boost::detail::winapi::wait_object_0:
+ m_mutex.clear_waiting_and_try_lock(old_count);
+ if ((old_count & sync::detail::windows::basic_mutex::lock_flag_value) == 0)
+ return true;
+ break;
+
+ case boost::detail::winapi::wait_object_0 + 1:
+ BOOST_ATOMIC_INTERLOCKED_EXCHANGE_ADD(&m_mutex.m_active_count, -1);
+ return false;
+
+ default:
+ BOOST_ASSERT(false);
+ }
+ }
     }
 
     bool priv_timed_lock(sync::detail::system_duration const& t)
@@ -109,21 +145,21 @@
 
         long old_count = m_mutex.m_active_count;
         m_mutex.mark_waiting_and_try_lock(old_count);
- if ((old_count & mutex::lock_flag_value) == 0)
+ if ((old_count & sync::detail::windows::basic_mutex::lock_flag_value) == 0)
             return true;
 
         const boost::detail::winapi::HANDLE_ evt = m_mutex.get_event();
         sync::detail::system_duration::native_type time_left = t.get();
         while (time_left > 0)
         {
- const unsigned int dur = time_left > (std::numeric_limits< int >::max)() ?
- static_cast< unsigned int >((std::numeric_limits< int >::max)()) : static_cast< unsigned int >(time_left);
+ const unsigned int dur = time_left > static_cast< unsigned int >(boost::detail::winapi::max_non_infinite_wait) ?
+ static_cast< unsigned int >(boost::detail::winapi::max_non_infinite_wait) : static_cast< unsigned int >(time_left);
             const boost::detail::winapi::DWORD_ res = boost::detail::winapi::WaitForSingleObject(evt, dur);
             switch (res)
             {
             case boost::detail::winapi::wait_object_0:
                 m_mutex.clear_waiting_and_try_lock(old_count);
- if ((old_count & mutex::lock_flag_value) == 0)
+ if ((old_count & sync::detail::windows::basic_mutex::lock_flag_value) == 0)
                     return true;
                 break;
 
@@ -132,8 +168,7 @@
                 break;
 
             default:
- BOOST_ATOMIC_INTERLOCKED_EXCHANGE_ADD(&m_mutex.m_active_count, -1);
- BOOST_THROW_EXCEPTION(lock_error(res, "boost: timed_mutex timedlock failed in WaitForSingleObject"));
+ abort_lock_and_throw("boost: timed_mutex timedlock failed in WaitForSingleObject", __LINE__);
             }
         }
 
@@ -160,6 +195,28 @@
         }
         return false;
     }
+
+ BOOST_NOINLINE BOOST_ATTRIBUTE_NORETURN void abort_lock_and_throw(const char* descr, int line)
+ {
+ const boost::detail::winapi::DWORD_ err = boost::detail::winapi::GetLastError();
+ BOOST_ATOMIC_INTERLOCKED_EXCHANGE_ADD(&m_mutex.m_active_count, -1);
+#if !defined(BOOST_EXCEPTION_DISABLE)
+ boost::throw_exception
+ (
+ set_info
+ (
+ set_info
+ (
+ enable_error_info(lock_error(err, descr)),
+ throw_file(__FILE__)
+ ),
+ throw_line(line)
+ )
+ );
+#else
+ boost::throw_exception(lock_error(err, descr));
+#endif
+ }
 };
 
 } // namespace winnt

Modified: trunk/boost/sync/detail/time_units.hpp
==============================================================================
--- trunk/boost/sync/detail/time_units.hpp Mon Oct 7 13:27:42 2013 (r86191)
+++ trunk/boost/sync/detail/time_units.hpp 2013-10-07 13:41:43 EDT (Mon, 07 Oct 2013) (r86192)
@@ -93,6 +93,7 @@
     static BOOST_CONSTEXPR_OR_CONST uint64_t subsecond_fraction = system_duration::subsecond_fraction;
 
 private:
+ //! Time, since 1970-Jan-01
     native_type m_value;
 
 public:
@@ -102,6 +103,8 @@
         m_value.tv_sec = t;
         m_value.tv_nsec = subsecond;
     }
+
+ //! Creates time point from duration since 1970-Jan-01
     explicit system_time_point(system_duration dur) BOOST_NOEXCEPT
     {
         m_value.tv_sec = dur.get() / subsecond_fraction;
@@ -165,18 +168,23 @@
 {
 public:
     typedef uint64_t native_type;
- // The native subsecond precision is milliseconds
- static BOOST_CONSTEXPR_OR_CONST uint64_t subsecond_fraction = system_duration::subsecond_fraction;
+ // The native subsecond precision is 100 nanoseconds
+ static BOOST_CONSTEXPR_OR_CONST uint64_t subsecond_fraction = 10000000u;
 
 private:
- native_type m_value;
+ //! 100 nanosecond units since 1601-Jan-01 (i.e. equivalent to FILETIME)
+ uint64_t m_value;
 
 public:
     BOOST_CONSTEXPR system_time_point() BOOST_NOEXCEPT : m_value(0) {}
- explicit system_time_point(time_t t, unsigned int subsecond = 0) BOOST_NOEXCEPT : m_value(static_cast< uint64_t >(t) * subsecond_fraction + subsecond)
+ explicit system_time_point(time_t t, unsigned int subsecond = 0) BOOST_NOEXCEPT :
+ m_value(static_cast< uint64_t >(t) * subsecond_fraction + subsecond + 116444736000000000ull)
     {
     }
- explicit system_time_point(system_duration dur) BOOST_NOEXCEPT : m_value(dur.get())
+
+ //! Creates time point from duration since 1970-Jan-01
+ explicit system_time_point(system_duration dur) BOOST_NOEXCEPT :
+ m_value(dur.get() * 10000u + 116444736000000000ull)
     {
     }
 
@@ -184,32 +192,19 @@
 
     static system_time_point now() BOOST_NOEXCEPT
     {
- union
- {
- uint64_t as_uint64;
- boost::detail::winapi::FILETIME_ as_filetime;
- }
- caster;
- boost::detail::winapi::GetSystemTimeAsFileTime(&caster.as_filetime);
-
- // Compensate the difference between 1970-Jan-01 & 1601-Jan-01
- // in 100-nanosecond intervals
- caster.as_uint64 -= 116444736000000000ull; // (27111902 << 32) + 3577643008
-
- // Convert to milliseconds
- caster.as_uint64 /= 10000u;
-
- return system_time_point(system_duration(caster.as_uint64));
+ system_time_point res;
+ boost::detail::winapi::GetSystemTimeAsFileTime(reinterpret_cast< boost::detail::winapi::FILETIME_* >(&res.m_value));
+ return res;
     }
 
     system_time_point& operator+= (system_duration const& dur) BOOST_NOEXCEPT
     {
- m_value += dur.get();
+ m_value += dur.get() * 10000u;
         return *this;
     }
     system_time_point& operator-= (system_duration const& dur) BOOST_NOEXCEPT
     {
- m_value -= dur.get();
+ m_value -= dur.get() * 10000u;
         return *this;
     }
 
@@ -226,7 +221,7 @@
 
     friend system_duration operator- (system_time_point const& left, system_time_point const& right) BOOST_NOEXCEPT
     {
- return system_duration(static_cast< system_duration::native_type >(left.m_value - right.m_value));
+ return system_duration(static_cast< system_duration::native_type >(left.m_value - right.m_value) / 10000u);
     }
 };
 

Modified: trunk/boost/sync/detail/waitable_timer.hpp
==============================================================================
--- trunk/boost/sync/detail/waitable_timer.hpp Mon Oct 7 13:27:42 2013 (r86191)
+++ trunk/boost/sync/detail/waitable_timer.hpp 2013-10-07 13:41:43 EDT (Mon, 07 Oct 2013) (r86192)
@@ -102,7 +102,7 @@
                 boost::detail::winapi::CloseHandle(waitable_timer);
         }
 
- static void NTAPI destroy(PVOID_ p, BOOLEAN_ /*timed_out*/)
+ static void NTAPI destroy(boost::detail::winapi::PVOID_ p, boost::detail::winapi::BOOLEAN_ /*timed_out*/)
         {
             delete static_cast< thread_local_context* >(p);
         }
@@ -225,22 +225,23 @@
         typedef NTSTATUS_ (__stdcall *NtQuerySemaphore_t)(boost::detail::winapi::HANDLE_ h, unsigned int info_class, semaphore_basic_information* pinfo, boost::detail::winapi::ULONG_ info_size, boost::detail::winapi::ULONG_* ret_len);
 
         // Retrieve the TLS key from the semaphore
- NtQuerySemaphore_t nt_query_semaphore = (NtQuerySemaphore_t)boost::detail::winapi::GetProcAddress(boost::detail::winapi::GetModuleHandleA("ntdll.dll"), "NtQuerySemaphore");
+ const boost::detail::winapi::HMODULE_ ntdll =
+#ifndef BOOST_NO_ANSI_APIS
+ boost::detail::winapi::GetModuleHandleA("ntdll.dll");
+#else
+ boost::detail::winapi::GetModuleHandleW(L"ntdll.dll");
+#endif
+ NtQuerySemaphore_t nt_query_semaphore = (NtQuerySemaphore_t)boost::detail::winapi::GetProcAddress(ntdll, "NtQuerySemaphore");
         if (nt_query_semaphore)
         {
             semaphore_basic_information info = {};
             NTSTATUS_ err = nt_query_semaphore(tls_key_holder, 0 /* SemaphoreBasicInformation */, &info, sizeof(info), NULL);
             if (err == 0)
             {
- tls_key = info.current_count;
+ tls_key = static_cast< boost::detail::winapi::DWORD_ >(info.current_count);
                 BOOST_ATOMIC_INTERLOCKED_EXCHANGE(&initialized, st_initialized);
                 return;
             }
- else
- {
- BOOST_ATOMIC_INTERLOCKED_EXCHANGE(&initialized, st_uninitialized);
- BOOST_THROW_EXCEPTION(resource_error("Boost.Sync: unable to create a semaphore for TLS storage"));
- }
         }
 
         // We failed to obtain the TLS key from the semaphore. Just allocate one already.

Modified: trunk/boost/sync/support/posix_time.hpp
==============================================================================
--- trunk/boost/sync/support/posix_time.hpp Mon Oct 7 13:27:42 2013 (r86191)
+++ trunk/boost/sync/support/posix_time.hpp 2013-10-07 13:41:43 EDT (Mon, 07 Oct 2013) (r86192)
@@ -40,8 +40,14 @@
 
     static system_time_point to_sync_unit(struct ::timespec const& point)
     {
- BOOST_STATIC_ASSERT_MSG(system_time_point::subsecond_fraction == 1000000000u, "Boost.Sync: Unexpected system time point resolution");
- return system_time_point(point.tv_sec, point.tv_nsec);
+ enum
+ {
+ nanoseconds_fraction = 1000000000u,
+ conversion_ratio = system_time_point::subsecond_fraction >= nanoseconds_fraction ?
+ system_time_point::subsecond_fraction / nanoseconds_fraction :
+ nanoseconds_fraction / system_time_point::subsecond_fraction
+ };
+ return system_time_point(point.tv_sec, (system_time_point::subsecond_fraction < nanoseconds_fraction ? point.tv_nsec / conversion_ratio : point.tv_nsec * conversion_ratio));
     }
 };
 


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk