$include_dir="/home/hyper-archives/boost-commit/include"; include("$include_dir/msg-header.inc") ?>
Subject: [Boost-commit] svn:boost r86511 - in trunk: boost/interprocess/detail boost/interprocess/sync boost/interprocess/sync/detail boost/interprocess/sync/posix boost/interprocess/sync/shm boost/interprocess/sync/spin boost/interprocess/sync/windows libs/interprocess/doc libs/interprocess/test
From: igaztanaga_at_[hidden]
Date: 2013-10-29 03:39:22
Author: igaztanaga
Date: 2013-10-29 03:39:21 EDT (Tue, 29 Oct 2013)
New Revision: 86511
URL: http://svn.boost.org/trac/boost/changeset/86511
Log:
Simplified, refactored and unified (timed_)lock code based on try_lock(). There were several bugs in when handling timeout expirations.
Added:
   trunk/boost/interprocess/sync/detail/common_algorithms.hpp   (contents, props changed)
   trunk/boost/interprocess/sync/windows/winapi_wrapper_common.hpp   (contents, props changed)
Text files modified: 
   trunk/boost/interprocess/detail/os_file_functions.hpp              |     2                                         
   trunk/boost/interprocess/detail/robust_emulation.hpp               |    63 +---------------------------            
   trunk/boost/interprocess/sync/detail/common_algorithms.hpp         |    73 +++++++++++++++++++++++++++++++++       
   trunk/boost/interprocess/sync/detail/condition_algorithm_8a.hpp    |     8 ---                                     
   trunk/boost/interprocess/sync/detail/condition_any_algorithm.hpp   |     8 ---                                     
   trunk/boost/interprocess/sync/detail/locks.hpp                     |    27 ++++++++++++                            
   trunk/boost/interprocess/sync/file_lock.hpp                        |    84 ++------------------------------------  
   trunk/boost/interprocess/sync/interprocess_sharable_mutex.hpp      |    14 ++---                                   
   trunk/boost/interprocess/sync/interprocess_upgradable_mutex.hpp    |    24 +++-------                              
   trunk/boost/interprocess/sync/named_recursive_mutex.hpp            |     8 ---                                     
   trunk/boost/interprocess/sync/named_semaphore.hpp                  |     8 ---                                     
   trunk/boost/interprocess/sync/posix/condition.hpp                  |    11 ++--                                    
   trunk/boost/interprocess/sync/posix/mutex.hpp                      |    24 +---------                              
   trunk/boost/interprocess/sync/posix/named_mutex.hpp                |     8 ---                                     
   trunk/boost/interprocess/sync/posix/ptime_to_timespec.hpp          |     4 +                                       
   trunk/boost/interprocess/sync/posix/recursive_mutex.hpp            |    22 +--------                               
   trunk/boost/interprocess/sync/posix/semaphore_wrapper.hpp          |    16 +++----                                 
   trunk/boost/interprocess/sync/shm/named_mutex.hpp                  |     8 ---                                     
   trunk/boost/interprocess/sync/shm/named_recursive_mutex.hpp        |     8 ---                                     
   trunk/boost/interprocess/sync/shm/named_semaphore.hpp              |     8 ---                                     
   trunk/boost/interprocess/sync/shm/named_upgradable_mutex.hpp       |    24 +---------                              
   trunk/boost/interprocess/sync/spin/condition.hpp                   |    10 ++-                                     
   trunk/boost/interprocess/sync/spin/mutex.hpp                       |    40 +-----------------                      
   trunk/boost/interprocess/sync/spin/recursive_mutex.hpp             |     5 -                                       
   trunk/boost/interprocess/sync/spin/semaphore.hpp                   |    33 ++------------                          
   trunk/boost/interprocess/sync/windows/named_condition_any.hpp      |     2                                         
   trunk/boost/interprocess/sync/windows/winapi_mutex_wrapper.hpp     |    51 ++---------------------                 
   trunk/boost/interprocess/sync/windows/winapi_semaphore_wrapper.hpp |    48 +--------------------                   
   trunk/boost/interprocess/sync/windows/winapi_wrapper_common.hpp    |    86 ++++++++++++++++++++++++++++++++++++++++
   trunk/libs/interprocess/doc/interprocess.qbk                       |     5 ++                                      
   trunk/libs/interprocess/test/file_lock_test.cpp                    |     4                                         
   trunk/libs/interprocess/test/util.hpp                              |     2                                         
   32 files changed, 276 insertions(+), 462 deletions(-)
Modified: trunk/boost/interprocess/detail/os_file_functions.hpp
==============================================================================
--- trunk/boost/interprocess/detail/os_file_functions.hpp	Mon Oct 28 18:35:44 2013	(r86510)
+++ trunk/boost/interprocess/detail/os_file_functions.hpp	2013-10-29 03:39:21 EDT (Tue, 29 Oct 2013)	(r86511)
@@ -136,7 +136,7 @@
    typedef boost::make_unsigned<offset_t>::type uoffset_t;
    const uoffset_t max_filesize = uoffset_t((std::numeric_limits<offset_t>::max)());
    //Avoid unused variable warnings in 32 bit systems
-   if(size > max_filesize){
+   if(uoffset_t(size) > max_filesize){
       winapi::set_last_error(winapi::error_file_too_large);
       return false;
    }
Modified: trunk/boost/interprocess/detail/robust_emulation.hpp
==============================================================================
--- trunk/boost/interprocess/detail/robust_emulation.hpp	Mon Oct 28 18:35:44 2013	(r86510)
+++ trunk/boost/interprocess/detail/robust_emulation.hpp	2013-10-29 03:39:21 EDT (Tue, 29 Oct 2013)	(r86511)
@@ -25,6 +25,7 @@
 #include <boost/interprocess/detail/intermodule_singleton.hpp>
 #include <boost/interprocess/exceptions.hpp>
 #include <boost/interprocess/sync/spin/wait.hpp>
+#include <boost/interprocess/sync/detail/common_algorithms.hpp>
 #include <string>
 
 namespace boost{
@@ -215,38 +216,7 @@
 
 template<class Mutex>
 inline void robust_spin_mutex<Mutex>::lock()
-{
-   //If the mutex is broken (recovery didn't call consistent()),
-   //then throw an exception
-   if(atomic_read32(&this->state) == broken_state){
-      throw interprocess_exception(lock_error, "Broken id");
-   }
-
-   //This function provokes intermodule_singleton instantiation
-   if(!this->lock_own_unique_file()){
-      throw interprocess_exception(lock_error, "Broken id");
-   }
-
-   //Now the logic. Try to lock, if successful mark the owner
-   //if it fails, start recovery logic
-   spin_wait swait;
-   while(1){
-      if (mtx.try_lock()){
-         atomic_write32(&this->owner, get_current_process_id());
-         break;
-      }
-      else{
-         //Do the dead owner checking each spin_threshold lock tries
-         swait.yield();
-         if(0 == (swait.count() & 255u)){
-            //Check if owner dead and take ownership if possible
-            if(this->robust_check()){
-               break;
-            }
-         }
-      }
-   }
-}
+{  try_based_lock(*this);  }
 
 template<class Mutex>
 inline bool robust_spin_mutex<Mutex>::try_lock()
@@ -277,34 +247,7 @@
 template<class Mutex>
 inline bool robust_spin_mutex<Mutex>::timed_lock
    (const boost::posix_time::ptime &abs_time)
-{
-   //Same as lock() but with an additional timeout
-   if(abs_time == boost::posix_time::pos_infin){
-      this->lock();
-      return true;
-   }
-   //Obtain current count and target time
-   boost::posix_time::ptime now = microsec_clock::universal_time();
-
-   if(now >= abs_time)
-      return this->try_lock();
-
-   spin_wait swait;
-   do{
-      if(this->try_lock()){
-         break;
-      }
-      now = microsec_clock::universal_time();
-
-      if(now >= abs_time){
-         return this->try_lock();
-      }
-      // relinquish current time slice
-      swait.yield();
-   }while (true);
-
-   return true;
-}
+{  return try_based_timed_lock(*this, abs_time);   }
 
 template<class Mutex>
 inline void robust_spin_mutex<Mutex>::owner_to_filename(boost::uint32_t own, std::string &s)
Added: trunk/boost/interprocess/sync/detail/common_algorithms.hpp
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ trunk/boost/interprocess/sync/detail/common_algorithms.hpp	2013-10-29 03:39:21 EDT (Tue, 29 Oct 2013)	(r86511)
@@ -0,0 +1,73 @@
+//////////////////////////////////////////////////////////////////////////////
+//
+// (C) Copyright Ion Gaztanaga 2012-2013. 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)
+//
+// See http://www.boost.org/libs/interprocess for documentation.
+//
+//////////////////////////////////////////////////////////////////////////////
+
+#ifndef BOOST_INTERPROCESS_SYNC_DETAIL_COMMON_ALGORITHMS_HPP
+#define BOOST_INTERPROCESS_SYNC_DETAIL_COMMON_ALGORITHMS_HPP
+
+#include <boost/interprocess/detail/config_begin.hpp>
+#include <boost/interprocess/detail/workaround.hpp>
+
+#include <boost/interprocess/sync/spin/wait.hpp>
+
+namespace boost {
+namespace interprocess {
+namespace ipcdetail {
+
+template<class MutexType>
+bool try_based_timed_lock(MutexType &m, const boost::posix_time::ptime &abs_time)
+{
+   //Same as lock()
+   if(abs_time == boost::posix_time::pos_infin){
+      m.lock();
+      return true;
+   }
+   //Always try to lock to achieve POSIX guarantees:
+   // "Under no circumstance shall the function fail with a timeout if the mutex
+   //  can be locked immediately. The validity of the abs_timeout parameter need not
+   //  be checked if the mutex can be locked immediately."
+   else if(m.try_lock()){
+      return true;
+   }
+   else{
+      spin_wait swait;
+      while(microsec_clock::universal_time() < abs_time){
+         if(m.try_lock()){
+            return true;
+         }
+         swait.yield();
+      }
+      return false;
+   }
+}
+
+template<class MutexType>
+void try_based_lock(MutexType &m)
+{
+   if(!m.try_lock()){
+      spin_wait swait;
+      do{
+         if(m.try_lock()){
+            break;
+         }
+         else{
+            swait.yield();
+         }
+      }
+      while(1);
+   }
+}
+
+}  //namespace ipcdetail
+}  //namespace interprocess
+}  //namespace boost
+
+#include <boost/interprocess/detail/config_end.hpp>
+
+#endif   //BOOST_INTERPROCESS_SYNC_DETAIL_COMMON_ALGORITHMS_HPP
Modified: trunk/boost/interprocess/sync/detail/condition_algorithm_8a.hpp
==============================================================================
--- trunk/boost/interprocess/sync/detail/condition_algorithm_8a.hpp	Mon Oct 28 18:35:44 2013	(r86510)
+++ trunk/boost/interprocess/sync/detail/condition_algorithm_8a.hpp	2013-10-29 03:39:21 EDT (Tue, 29 Oct 2013)	(r86511)
@@ -360,10 +360,6 @@
    template <typename L>
    bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time)
    {
-      if(abs_time == boost::posix_time::pos_infin){
-         this->wait(lock);
-         return true;
-      }
       if (!lock)
          throw lock_exception();
       return algo_type::wait(m_data, lock, true, abs_time);
@@ -372,10 +368,6 @@
    template <typename L, typename Pr>
    bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time, Pr pred)
    {
-      if(abs_time == boost::posix_time::pos_infin){
-         this->wait(lock, pred);
-         return true;
-      }
       if (!lock)
             throw lock_exception();
       while (!pred()){
Modified: trunk/boost/interprocess/sync/detail/condition_any_algorithm.hpp
==============================================================================
--- trunk/boost/interprocess/sync/detail/condition_any_algorithm.hpp	Mon Oct 28 18:35:44 2013	(r86510)
+++ trunk/boost/interprocess/sync/detail/condition_any_algorithm.hpp	2013-10-29 03:39:21 EDT (Tue, 29 Oct 2013)	(r86511)
@@ -189,10 +189,6 @@
    template <typename L>
    bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time)
    {
-      if(abs_time == boost::posix_time::pos_infin){
-         this->wait(lock);
-         return true;
-      }
       if (!lock)
          throw lock_exception();
       return algo_type::wait(m_data, lock, true, abs_time);
@@ -201,10 +197,6 @@
    template <typename L, typename Pr>
    bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time, Pr pred)
    {
-      if(abs_time == boost::posix_time::pos_infin){
-         this->wait(lock, pred);
-         return true;
-      }
       if (!lock)
             throw lock_exception();
       while (!pred()){
Modified: trunk/boost/interprocess/sync/detail/locks.hpp
==============================================================================
--- trunk/boost/interprocess/sync/detail/locks.hpp	Mon Oct 28 18:35:44 2013	(r86510)
+++ trunk/boost/interprocess/sync/detail/locks.hpp	2013-10-29 03:39:21 EDT (Tue, 29 Oct 2013)	(r86511)
@@ -57,6 +57,33 @@
    void unlock()  {   l_.lock();     }
 };
 
+template <class Lock>
+class lock_to_sharable
+{
+   Lock &l_;
+
+   public:
+   explicit lock_to_sharable(Lock &l)
+      :  l_(l)
+   {}
+   void lock()    {  l_.lock_sharable();     }
+   bool try_lock(){  return l_.try_lock_sharable(); }
+   void unlock()  {  l_.unlock_sharable();   }
+};
+
+template <class Lock>
+class lock_to_wait
+{
+   Lock &l_;
+
+   public:
+   explicit lock_to_wait(Lock &l)
+      :  l_(l)
+   {}
+   void lock()    {  l_.wait();     }
+   bool try_lock(){  return l_.try_wait(); }
+};
+
 }  //namespace ipcdetail
 }  //namespace interprocess
 }  //namespace boost
Modified: trunk/boost/interprocess/sync/file_lock.hpp
==============================================================================
--- trunk/boost/interprocess/sync/file_lock.hpp	Mon Oct 28 18:35:44 2013	(r86510)
+++ trunk/boost/interprocess/sync/file_lock.hpp	2013-10-29 03:39:21 EDT (Tue, 29 Oct 2013)	(r86511)
@@ -21,7 +21,8 @@
 #include <boost/interprocess/detail/os_file_functions.hpp>
 #include <boost/interprocess/detail/os_thread_functions.hpp>
 #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
-#include <boost/interprocess/sync/spin/wait.hpp>
+#include <boost/interprocess/sync/detail/common_algorithms.hpp>
+#include <boost/interprocess/sync/detail/locks.hpp>
 #include <boost/move/move.hpp>
 
 //!\file
@@ -142,62 +143,6 @@
    private:
    file_handle_t m_file_hnd;
 
-   bool timed_acquire_file_lock
-      (file_handle_t hnd, bool &acquired, const boost::posix_time::ptime &abs_time)
-   {
-      //Obtain current count and target time
-      boost::posix_time::ptime now = microsec_clock::universal_time();
-      using namespace boost::detail;
-
-      if(now >= abs_time) return false;
-      spin_wait swait;
-      do{
-         if(!ipcdetail::try_acquire_file_lock(hnd, acquired))
-            return false;
-
-         if(acquired)
-            return true;
-         else{
-            now = microsec_clock::universal_time();
-
-            if(now >= abs_time){
-               acquired = false;
-               return true;
-            }
-            // relinquish current time slice
-            swait.yield();
-         }
-      }while (true);
-   }
-
-   bool timed_acquire_file_lock_sharable
-      (file_handle_t hnd, bool &acquired, const boost::posix_time::ptime &abs_time)
-   {
-      //Obtain current count and target time
-      boost::posix_time::ptime now = microsec_clock::universal_time();
-      using namespace boost::detail;
-
-      if(now >= abs_time) return false;
-
-      spin_wait swait;
-      do{
-         if(!ipcdetail::try_acquire_file_lock_sharable(hnd, acquired))
-            return false;
-
-         if(acquired)
-            return true;
-         else{
-            now = microsec_clock::universal_time();
-
-            if(now >= abs_time){
-               acquired = false;
-               return true;
-            }
-            // relinquish current time slice
-            swait.yield();
-         }
-      }while (true);
-   }
    /// @endcond
 };
 
@@ -238,18 +183,7 @@
 }
 
 inline bool file_lock::timed_lock(const boost::posix_time::ptime &abs_time)
-{
-   if(abs_time == boost::posix_time::pos_infin){
-      this->lock();
-      return true;
-   }
-   bool result;
-   if(!this->timed_acquire_file_lock(m_file_hnd, result, abs_time)){
-      error_info err(system_error_code());
-      throw interprocess_exception(err);
-   }
-   return result;
-}
+{  return ipcdetail::try_based_timed_lock(*this, abs_time);   }
 
 inline void file_lock::unlock()
 {
@@ -279,16 +213,8 @@
 
 inline bool file_lock::timed_lock_sharable(const boost::posix_time::ptime &abs_time)
 {
-   if(abs_time == boost::posix_time::pos_infin){
-      this->lock_sharable();
-      return true;
-   }
-   bool result;
-   if(!this->timed_acquire_file_lock_sharable(m_file_hnd, result, abs_time)){
-      error_info err(system_error_code());
-      throw interprocess_exception(err);
-   }
-   return result;
+   ipcdetail::lock_to_sharable<file_lock> lsh(*this);
+   return ipcdetail::try_based_timed_lock(lsh, abs_time);
 }
 
 inline void file_lock::unlock_sharable()
Modified: trunk/boost/interprocess/sync/interprocess_sharable_mutex.hpp
==============================================================================
--- trunk/boost/interprocess/sync/interprocess_sharable_mutex.hpp	Mon Oct 28 18:35:44 2013	(r86510)
+++ trunk/boost/interprocess/sync/interprocess_sharable_mutex.hpp	2013-10-29 03:39:21 EDT (Tue, 29 Oct 2013)	(r86511)
@@ -213,16 +213,14 @@
 inline bool interprocess_sharable_mutex::timed_lock
    (const boost::posix_time::ptime &abs_time)
 {
-   if(abs_time == boost::posix_time::pos_infin){
-      this->lock();
-      return true;
-   }
    scoped_lock_t lck(m_mut, abs_time);
    if(!lck.owns())   return false;
 
    //The exclusive lock must block in the first gate
    //if an exclusive lock has been acquired
    while (this->m_ctrl.exclusive_in){
+      //Mutexes and condvars handle just fine infinite abs_times
+      //so avoid checking it here
       if(!this->m_first_gate.timed_wait(lck, abs_time)){
          if(this->m_ctrl.exclusive_in){
             return false;
@@ -239,6 +237,8 @@
 
    //Now wait until all readers are gone
    while (this->m_ctrl.num_shared){
+      //Mutexes and condvars handle just fine infinite abs_times
+      //so avoid checking it here
       if(!this->m_second_gate.timed_wait(lck, abs_time)){
          if(this->m_ctrl.num_shared){
             return false;
@@ -296,10 +296,6 @@
 inline bool interprocess_sharable_mutex::timed_lock_sharable
    (const boost::posix_time::ptime &abs_time)
 {
-   if(abs_time == boost::posix_time::pos_infin){
-      this->lock_sharable();
-      return true;
-   }
    scoped_lock_t lck(m_mut, abs_time);
    if(!lck.owns())   return false;
 
@@ -308,6 +304,8 @@
    //or there are too many sharable locks
    while (this->m_ctrl.exclusive_in
          || this->m_ctrl.num_shared == constants::max_readers){
+      //Mutexes and condvars handle just fine infinite abs_times
+      //so avoid checking it here
       if(!this->m_first_gate.timed_wait(lck, abs_time)){
          if(this->m_ctrl.exclusive_in
                || this->m_ctrl.num_shared == constants::max_readers){
Modified: trunk/boost/interprocess/sync/interprocess_upgradable_mutex.hpp
==============================================================================
--- trunk/boost/interprocess/sync/interprocess_upgradable_mutex.hpp	Mon Oct 28 18:35:44 2013	(r86510)
+++ trunk/boost/interprocess/sync/interprocess_upgradable_mutex.hpp	2013-10-29 03:39:21 EDT (Tue, 29 Oct 2013)	(r86511)
@@ -325,10 +325,8 @@
 inline bool interprocess_upgradable_mutex::timed_lock
    (const boost::posix_time::ptime &abs_time)
 {
-   if(abs_time == boost::posix_time::pos_infin){
-      this->lock();
-      return true;
-   }
+   //Mutexes and condvars handle just fine infinite abs_times
+   //so avoid checking it here
    scoped_lock_t lck(m_mut, abs_time);
    if(!lck.owns())   return false;
 
@@ -413,10 +411,8 @@
 inline bool interprocess_upgradable_mutex::timed_lock_upgradable
    (const boost::posix_time::ptime &abs_time)
 {
-   if(abs_time == boost::posix_time::pos_infin){
-      this->lock_upgradable();
-      return true;
-   }
+   //Mutexes and condvars handle just fine infinite abs_times
+   //so avoid checking it here
    scoped_lock_t lck(m_mut, abs_time);
    if(!lck.owns())   return false;
 
@@ -492,10 +488,8 @@
 inline bool interprocess_upgradable_mutex::timed_lock_sharable
    (const boost::posix_time::ptime &abs_time)
 {
-   if(abs_time == boost::posix_time::pos_infin){
-      this->lock_sharable();
-      return true;
-   }
+   //Mutexes and condvars handle just fine infinite abs_times
+   //so avoid checking it here
    scoped_lock_t lck(m_mut, abs_time);
    if(!lck.owns())   return false;
 
@@ -607,10 +601,8 @@
 inline bool interprocess_upgradable_mutex::timed_unlock_upgradable_and_lock
    (const boost::posix_time::ptime &abs_time)
 {
-   if(abs_time == boost::posix_time::pos_infin){
-      this->unlock_upgradable_and_lock();
-      return true;
-   }
+   //Mutexes and condvars handle just fine infinite abs_times
+   //so avoid checking it here
    scoped_lock_t lck(m_mut, abs_time);
    if(!lck.owns())   return false;
 
Modified: trunk/boost/interprocess/sync/named_recursive_mutex.hpp
==============================================================================
--- trunk/boost/interprocess/sync/named_recursive_mutex.hpp	Mon Oct 28 18:35:44 2013	(r86510)
+++ trunk/boost/interprocess/sync/named_recursive_mutex.hpp	2013-10-29 03:39:21 EDT (Tue, 29 Oct 2013)	(r86511)
@@ -143,13 +143,7 @@
 {  return m_mut.try_lock();  }
 
 inline bool named_recursive_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
-{
-   if(abs_time == boost::posix_time::pos_infin){
-      this->lock();
-      return true;
-   }
-   return m_mut.timed_lock(abs_time);
-}
+{  return m_mut.timed_lock(abs_time);  }
 
 inline bool named_recursive_mutex::remove(const char *name)
 {  return impl_t::remove(name); }
Modified: trunk/boost/interprocess/sync/named_semaphore.hpp
==============================================================================
--- trunk/boost/interprocess/sync/named_semaphore.hpp	Mon Oct 28 18:35:44 2013	(r86510)
+++ trunk/boost/interprocess/sync/named_semaphore.hpp	2013-10-29 03:39:21 EDT (Tue, 29 Oct 2013)	(r86511)
@@ -153,13 +153,7 @@
 {  return m_sem.try_wait();  }
 
 inline bool named_semaphore::timed_wait(const boost::posix_time::ptime &abs_time)
-{
-   if(abs_time == boost::posix_time::pos_infin){
-      this->wait();
-      return true;
-   }
-   return m_sem.timed_wait(abs_time);
-}
+{  return m_sem.timed_wait(abs_time);  }
 
 inline bool named_semaphore::remove(const char *name)
 {  return impl_t::remove(name);   }
Modified: trunk/boost/interprocess/sync/posix/condition.hpp
==============================================================================
--- trunk/boost/interprocess/sync/posix/condition.hpp	Mon Oct 28 18:35:44 2013	(r86510)
+++ trunk/boost/interprocess/sync/posix/condition.hpp	2013-10-29 03:39:21 EDT (Tue, 29 Oct 2013)	(r86511)
@@ -83,12 +83,13 @@
    template <typename L>
    bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time)
    {
+      if (!lock)
+         throw lock_exception();
+      //Posix does not support infinity absolute time so handle it here
       if(abs_time == boost::posix_time::pos_infin){
          this->wait(lock);
          return true;
       }
-      if (!lock)
-            throw lock_exception();
       return this->do_timed_wait(abs_time, *lock.mutex());
    }
 
@@ -98,17 +99,17 @@
    template <typename L, typename Pr>
    bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time, Pr pred)
    {
+      if (!lock)
+         throw lock_exception();
+      //Posix does not support infinity absolute time so handle it here
       if(abs_time == boost::posix_time::pos_infin){
          this->wait(lock, pred);
          return true;
       }
-      if (!lock)
-            throw lock_exception();
       while (!pred()){
          if (!this->do_timed_wait(abs_time, *lock.mutex()))
             return pred();
       }
-
       return true;
    }
 
Modified: trunk/boost/interprocess/sync/posix/mutex.hpp
==============================================================================
--- trunk/boost/interprocess/sync/posix/mutex.hpp	Mon Oct 28 18:35:44 2013	(r86510)
+++ trunk/boost/interprocess/sync/posix/mutex.hpp	2013-10-29 03:39:21 EDT (Tue, 29 Oct 2013)	(r86511)
@@ -44,7 +44,7 @@
 
 #ifndef BOOST_INTERPROCESS_POSIX_TIMEOUTS
 #  include <boost/interprocess/detail/os_thread_functions.hpp>
-#  include <boost/interprocess/sync/spin/wait.hpp>
+#  include <boost/interprocess/sync/detail/common_algorithms.hpp>
 #endif
 #include <boost/assert.hpp>
 
@@ -103,12 +103,12 @@
 
 inline bool posix_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
 {
+   #ifdef BOOST_INTERPROCESS_POSIX_TIMEOUTS
+   //Posix does not support infinity absolute time so handle it here
    if(abs_time == boost::posix_time::pos_infin){
       this->lock();
       return true;
    }
-   #ifdef BOOST_INTERPROCESS_POSIX_TIMEOUTS
-
    timespec ts = ptime_to_timespec(abs_time);
    int res = pthread_mutex_timedlock(&m_mut, &ts);
    if (res != 0 && res != ETIMEDOUT)
@@ -117,23 +117,7 @@
 
    #else //BOOST_INTERPROCESS_POSIX_TIMEOUTS
 
-   //Obtain current count and target time
-   boost::posix_time::ptime now = microsec_clock::universal_time();
-
-   spin_wait swait;
-   do{
-      if(this->try_lock()){
-         break;
-      }
-      now = microsec_clock::universal_time();
-
-      if(now >= abs_time){
-         return false;
-      }
-      // relinquish current time slice
-      swait.yield();
-   }while (true);
-   return true;
+   return ipcdetail::try_based_timed_lock(*this, abs_time);
 
    #endif   //BOOST_INTERPROCESS_POSIX_TIMEOUTS
 }
Modified: trunk/boost/interprocess/sync/posix/named_mutex.hpp
==============================================================================
--- trunk/boost/interprocess/sync/posix/named_mutex.hpp	Mon Oct 28 18:35:44 2013	(r86510)
+++ trunk/boost/interprocess/sync/posix/named_mutex.hpp	2013-10-29 03:39:21 EDT (Tue, 29 Oct 2013)	(r86511)
@@ -94,13 +94,7 @@
 {  return m_sem.try_wait();  }
 
 inline bool posix_named_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
-{
-   if(abs_time == boost::posix_time::pos_infin){
-      this->lock();
-      return true;
-   }
-   return m_sem.timed_wait(abs_time);
-}
+{  return m_sem.timed_wait(abs_time);  }
 
 inline bool posix_named_mutex::remove(const char *name)
 {  return posix_named_semaphore::remove(name);   }
Modified: trunk/boost/interprocess/sync/posix/ptime_to_timespec.hpp
==============================================================================
--- trunk/boost/interprocess/sync/posix/ptime_to_timespec.hpp	Mon Oct 28 18:35:44 2013	(r86510)
+++ trunk/boost/interprocess/sync/posix/ptime_to_timespec.hpp	2013-10-29 03:39:21 EDT (Tue, 29 Oct 2013)	(r86511)
@@ -22,7 +22,9 @@
 inline timespec ptime_to_timespec (const boost::posix_time::ptime &tm)
 {
    const boost::posix_time::ptime epoch(boost::gregorian::date(1970,1,1));
-   boost::posix_time::time_duration duration (tm - epoch);
+   //Avoid negative absolute times
+   boost::posix_time::time_duration duration  = (tm <= epoch) ? boost::posix_time::time_duration(epoch - epoch)
+                                                              : boost::posix_time::time_duration(tm - epoch);
    timespec ts;
    ts.tv_sec  = duration.total_seconds();
    ts.tv_nsec = duration.total_nanoseconds() % 1000000000;
Modified: trunk/boost/interprocess/sync/posix/recursive_mutex.hpp
==============================================================================
--- trunk/boost/interprocess/sync/posix/recursive_mutex.hpp	Mon Oct 28 18:35:44 2013	(r86510)
+++ trunk/boost/interprocess/sync/posix/recursive_mutex.hpp	2013-10-29 03:39:21 EDT (Tue, 29 Oct 2013)	(r86511)
@@ -38,7 +38,7 @@
 #include <boost/interprocess/exceptions.hpp>
 #ifndef BOOST_INTERPROCESS_POSIX_TIMEOUTS
 #  include <boost/interprocess/detail/os_thread_functions.hpp>
-#  include <boost/interprocess/sync/spin/wait.hpp>
+#  include <boost/interprocess/sync/detail/common_algorithms.hpp>
 #endif
 #include <boost/assert.hpp>
 
@@ -93,11 +93,12 @@
 
 inline bool posix_recursive_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
 {
+   #ifdef BOOST_INTERPROCESS_POSIX_TIMEOUTS
+   //Posix does not support infinity absolute time so handle it here
    if(abs_time == boost::posix_time::pos_infin){
       this->lock();
       return true;
    }
-   #ifdef BOOST_INTERPROCESS_POSIX_TIMEOUTS
 
    timespec ts = ptime_to_timespec(abs_time);
    int res = pthread_mutex_timedlock(&m_mut, &ts);
@@ -107,22 +108,7 @@
 
    #else //BOOST_INTERPROCESS_POSIX_TIMEOUTS
 
-   //Obtain current count and target time
-   boost::posix_time::ptime now = microsec_clock::universal_time();
-   spin_wait swait;
-   do{
-      if(this->try_lock()){
-         break;
-      }
-      now = microsec_clock::universal_time();
-
-      if(now >= abs_time){
-         return false;
-      }
-      // relinquish current time slice
-      swait.yield();
-   }while (true);
-   return true;
+   return ipcdetail::try_based_timed_lock(*this, abs_time);
 
    #endif   //BOOST_INTERPROCESS_POSIX_TIMEOUTS
 }
Modified: trunk/boost/interprocess/sync/posix/semaphore_wrapper.hpp
==============================================================================
--- trunk/boost/interprocess/sync/posix/semaphore_wrapper.hpp	Mon Oct 28 18:35:44 2013	(r86510)
+++ trunk/boost/interprocess/sync/posix/semaphore_wrapper.hpp	2013-10-29 03:39:21 EDT (Tue, 29 Oct 2013)	(r86511)
@@ -174,11 +174,13 @@
 
 inline bool semaphore_timed_wait(sem_t *handle, const boost::posix_time::ptime &abs_time)
 {
+   #ifdef BOOST_INTERPROCESS_POSIX_TIMEOUTS
+   //Posix does not support infinity absolute time so handle it here
    if(abs_time == boost::posix_time::pos_infin){
       semaphore_wait(handle);
       return true;
    }
-   #ifdef BOOST_INTERPROCESS_POSIX_TIMEOUTS
+
    timespec tspec = ptime_to_timespec(abs_time);
    for (;;){
       int res = sem_timedwait(handle, &tspec);
@@ -195,14 +197,10 @@
    }
    return false;
    #else //#ifdef BOOST_INTERPROCESS_POSIX_TIMEOUTS
-   boost::posix_time::ptime now;
-   spin_wait swait;
-   do{
-      if(semaphore_try_wait(handle))
-         return true;
-      swait.yield();
-   }while((now = microsec_clock::universal_time()) < abs_time);
-   return false;
+
+   ipcdetail::lock_to_wait<> lw(*this);
+   return ipcdetail::try_based_timed_lock(lw, abs_time);
+
    #endif   //#ifdef BOOST_INTERPROCESS_POSIX_TIMEOUTS
 }
 
Modified: trunk/boost/interprocess/sync/shm/named_mutex.hpp
==============================================================================
--- trunk/boost/interprocess/sync/shm/named_mutex.hpp	Mon Oct 28 18:35:44 2013	(r86510)
+++ trunk/boost/interprocess/sync/shm/named_mutex.hpp	2013-10-29 03:39:21 EDT (Tue, 29 Oct 2013)	(r86511)
@@ -161,13 +161,7 @@
 {  return this->internal_mutex().try_lock();  }
 
 inline bool shm_named_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
-{
-   if(abs_time == boost::posix_time::pos_infin){
-      this->lock();
-      return true;
-   }
-   return this->internal_mutex().timed_lock(abs_time);
-}
+{  return this->internal_mutex().timed_lock(abs_time);   }
 
 inline bool shm_named_mutex::remove(const char *name)
 {  return shared_memory_object::remove(name); }
Modified: trunk/boost/interprocess/sync/shm/named_recursive_mutex.hpp
==============================================================================
--- trunk/boost/interprocess/sync/shm/named_recursive_mutex.hpp	Mon Oct 28 18:35:44 2013	(r86510)
+++ trunk/boost/interprocess/sync/shm/named_recursive_mutex.hpp	2013-10-29 03:39:21 EDT (Tue, 29 Oct 2013)	(r86511)
@@ -153,13 +153,7 @@
 {  return this->mutex()->try_lock();  }
 
 inline bool shm_named_recursive_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
-{
-   if(abs_time == boost::posix_time::pos_infin){
-      this->lock();
-      return true;
-   }
-   return this->mutex()->timed_lock(abs_time);
-}
+{  return this->mutex()->timed_lock(abs_time);  }
 
 inline bool shm_named_recursive_mutex::remove(const char *name)
 {  return shared_memory_object::remove(name); }
Modified: trunk/boost/interprocess/sync/shm/named_semaphore.hpp
==============================================================================
--- trunk/boost/interprocess/sync/shm/named_semaphore.hpp	Mon Oct 28 18:35:44 2013	(r86510)
+++ trunk/boost/interprocess/sync/shm/named_semaphore.hpp	2013-10-29 03:39:21 EDT (Tue, 29 Oct 2013)	(r86511)
@@ -120,13 +120,7 @@
 {  return semaphore()->try_wait();   }
 
 inline bool shm_named_semaphore::timed_wait(const boost::posix_time::ptime &abs_time)
-{
-   if(abs_time == boost::posix_time::pos_infin){
-      this->wait();
-      return true;
-   }
-   return semaphore()->timed_wait(abs_time);
-}
+{  return semaphore()->timed_wait(abs_time); }
 
 inline bool shm_named_semaphore::remove(const char *name)
 {  return shared_memory_object::remove(name); }
Modified: trunk/boost/interprocess/sync/shm/named_upgradable_mutex.hpp
==============================================================================
--- trunk/boost/interprocess/sync/shm/named_upgradable_mutex.hpp	Mon Oct 28 18:35:44 2013	(r86510)
+++ trunk/boost/interprocess/sync/shm/named_upgradable_mutex.hpp	2013-10-29 03:39:21 EDT (Tue, 29 Oct 2013)	(r86511)
@@ -287,13 +287,7 @@
 
 inline bool named_upgradable_mutex::timed_lock
    (const boost::posix_time::ptime &abs_time)
-{
-   if(abs_time == boost::posix_time::pos_infin){
-      this->lock();
-      return true;
-   }
-   return this->mutex()->timed_lock(abs_time);
-}
+{  return this->mutex()->timed_lock(abs_time);  }
 
 inline void named_upgradable_mutex::lock_upgradable()
 {  this->mutex()->lock_upgradable();  }
@@ -306,13 +300,7 @@
 
 inline bool named_upgradable_mutex::timed_lock_upgradable
    (const boost::posix_time::ptime &abs_time)
-{
-   if(abs_time == boost::posix_time::pos_infin){
-      this->lock_upgradable();
-      return true;
-   }
-   return this->mutex()->timed_lock_upgradable(abs_time);
-}
+{  return this->mutex()->timed_lock_upgradable(abs_time);   }
 
 inline void named_upgradable_mutex::lock_sharable()
 {  this->mutex()->lock_sharable();  }
@@ -325,13 +313,7 @@
 
 inline bool named_upgradable_mutex::timed_lock_sharable
    (const boost::posix_time::ptime &abs_time)
-{
-   if(abs_time == boost::posix_time::pos_infin){
-      this->lock_sharable();
-      return true;
-   }
-   return this->mutex()->timed_lock_sharable(abs_time);
-}
+{  return this->mutex()->timed_lock_sharable(abs_time);  }
 
 inline void named_upgradable_mutex::unlock_and_lock_upgradable()
 {  this->mutex()->unlock_and_lock_upgradable();  }
Modified: trunk/boost/interprocess/sync/spin/condition.hpp
==============================================================================
--- trunk/boost/interprocess/sync/spin/condition.hpp	Mon Oct 28 18:35:44 2013	(r86510)
+++ trunk/boost/interprocess/sync/spin/condition.hpp	2013-10-29 03:39:21 EDT (Tue, 29 Oct 2013)	(r86511)
@@ -41,24 +41,26 @@
    template <typename L>
    bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time)
    {
+      if (!lock)
+         throw lock_exception();
+      //Handle infinity absolute time here to avoid complications in do_timed_wait
       if(abs_time == boost::posix_time::pos_infin){
          this->wait(lock);
          return true;
       }
-      if (!lock)
-         throw lock_exception();
       return this->do_timed_wait(abs_time, *lock.mutex());
    }
 
    template <typename L, typename Pr>
    bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time, Pr pred)
    {
+      if (!lock)
+         throw lock_exception();
+      //Handle infinity absolute time here to avoid complications in do_timed_wait
       if(abs_time == boost::posix_time::pos_infin){
          this->wait(lock, pred);
          return true;
       }
-      if (!lock)
-         throw lock_exception();
       while (!pred()){
          if (!this->do_timed_wait(abs_time, *lock.mutex()))
             return pred();
Modified: trunk/boost/interprocess/sync/spin/mutex.hpp
==============================================================================
--- trunk/boost/interprocess/sync/spin/mutex.hpp	Mon Oct 28 18:35:44 2013	(r86510)
+++ trunk/boost/interprocess/sync/spin/mutex.hpp	2013-10-29 03:39:21 EDT (Tue, 29 Oct 2013)	(r86511)
@@ -22,7 +22,7 @@
 #include <boost/interprocess/detail/atomic.hpp>
 #include <boost/cstdint.hpp>
 #include <boost/interprocess/detail/os_thread_functions.hpp>
-#include <boost/interprocess/sync/spin/wait.hpp>
+#include <boost/interprocess/sync/detail/common_algorithms.hpp>
 
 namespace boost {
 namespace interprocess {
@@ -60,18 +60,7 @@
 }
 
 inline void spin_mutex::lock(void)
-{
-   spin_wait swait;
-   do{
-      boost::uint32_t prev_s = ipcdetail::atomic_cas32(const_cast<boost::uint32_t*>(&m_s), 1, 0);
-
-      if (m_s == 1 && prev_s == 0){
-            break;
-      }
-      // relinquish current timeslice
-      swait.yield();
-   }while (true);
-}
+{  return ipcdetail::try_based_lock(*this); }
 
 inline bool spin_mutex::try_lock(void)
 {
@@ -80,30 +69,7 @@
 }
 
 inline bool spin_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
-{
-   if(abs_time == boost::posix_time::pos_infin){
-      this->lock();
-      return true;
-   }
-   //Obtain current count and target time
-   boost::posix_time::ptime now = microsec_clock::universal_time();
-
-   spin_wait swait;
-   do{
-      if(this->try_lock()){
-         break;
-      }
-      now = microsec_clock::universal_time();
-
-      if(now >= abs_time){
-         return false;
-      }
-      // relinquish current time slice
-      swait.yield();
-   }while (true);
-
-   return true;
-}
+{  return ipcdetail::try_based_timed_lock(*this, abs_time); }
 
 inline void spin_mutex::unlock(void)
 {  ipcdetail::atomic_cas32(const_cast<boost::uint32_t*>(&m_s), 0, 1);   }
Modified: trunk/boost/interprocess/sync/spin/recursive_mutex.hpp
==============================================================================
--- trunk/boost/interprocess/sync/spin/recursive_mutex.hpp	Mon Oct 28 18:35:44 2013	(r86510)
+++ trunk/boost/interprocess/sync/spin/recursive_mutex.hpp	2013-10-29 03:39:21 EDT (Tue, 29 Oct 2013)	(r86511)
@@ -118,10 +118,6 @@
 inline bool spin_recursive_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
 {
    typedef ipcdetail::OS_systemwide_thread_id_t handle_t;
-   if(abs_time == boost::posix_time::pos_infin){
-      this->lock();
-      return true;
-   }
    const handle_t thr_id(ipcdetail::get_current_systemwide_thread_id());
    handle_t old_id;
    ipcdetail::systemwide_thread_id_copy(m_nOwner, old_id);
@@ -133,6 +129,7 @@
       ++m_nLockCount;
       return true;
    }
+   //m_mutex supports abs_time so no need to check it
    if(m_mutex.timed_lock(abs_time)){
       ipcdetail::systemwide_thread_id_copy(thr_id, m_nOwner);
       m_nLockCount = 1;
Modified: trunk/boost/interprocess/sync/spin/semaphore.hpp
==============================================================================
--- trunk/boost/interprocess/sync/spin/semaphore.hpp	Mon Oct 28 18:35:44 2013	(r86510)
+++ trunk/boost/interprocess/sync/spin/semaphore.hpp	2013-10-29 03:39:21 EDT (Tue, 29 Oct 2013)	(r86511)
@@ -20,7 +20,8 @@
 #include <boost/interprocess/detail/atomic.hpp>
 #include <boost/interprocess/detail/os_thread_functions.hpp>
 #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
-#include <boost/interprocess/sync/spin/wait.hpp>
+#include <boost/interprocess/sync/detail/common_algorithms.hpp>
+#include <boost/interprocess/sync/detail/locks.hpp>
 #include <boost/cstdint.hpp>
 
 namespace boost {
@@ -60,10 +61,8 @@
 
 inline void spin_semaphore::wait()
 {
-   spin_wait swait;
-   while(!ipcdetail::atomic_add_unless32(&m_count, boost::uint32_t(-1), boost::uint32_t(0))){
-      swait.yield();
-   }
+   ipcdetail::lock_to_wait<spin_semaphore> lw(*this);
+   return ipcdetail::try_based_lock(lw);
 }
 
 inline bool spin_semaphore::try_wait()
@@ -73,30 +72,10 @@
 
 inline bool spin_semaphore::timed_wait(const boost::posix_time::ptime &abs_time)
 {
-   if(abs_time == boost::posix_time::pos_infin){
-      this->wait();
-      return true;
-   }
-   //Obtain current count and target time
-   boost::posix_time::ptime now(microsec_clock::universal_time());
-
-   spin_wait swait;
-   do{
-      if(this->try_wait()){
-         break;
-      }
-      now = microsec_clock::universal_time();
-
-      if(now >= abs_time){
-         return this->try_wait();
-      }
-      // relinquish current time slice
-      swait.yield();
-   }while (true);
-   return true;
+   ipcdetail::lock_to_wait<spin_semaphore> lw(*this);
+   return ipcdetail::try_based_timed_lock(lw, abs_time);
 }
 
-
 //inline int spin_semaphore::get_count() const
 //{
    //return (int)ipcdetail::atomic_read32(&m_count);
Modified: trunk/boost/interprocess/sync/windows/named_condition_any.hpp
==============================================================================
--- trunk/boost/interprocess/sync/windows/named_condition_any.hpp	Mon Oct 28 18:35:44 2013	(r86510)
+++ trunk/boost/interprocess/sync/windows/named_condition_any.hpp	2013-10-29 03:39:21 EDT (Tue, 29 Oct 2013)	(r86511)
@@ -97,7 +97,7 @@
    /// @cond
    private:
 
-   void windows_named_condition_any::dont_close_on_destruction()
+   void dont_close_on_destruction()
    {}
 
    friend class interprocess_tester;
Modified: trunk/boost/interprocess/sync/windows/winapi_mutex_wrapper.hpp
==============================================================================
--- trunk/boost/interprocess/sync/windows/winapi_mutex_wrapper.hpp	Mon Oct 28 18:35:44 2013	(r86510)
+++ trunk/boost/interprocess/sync/windows/winapi_mutex_wrapper.hpp	2013-10-29 03:39:21 EDT (Tue, 29 Oct 2013)	(r86511)
@@ -21,6 +21,7 @@
 #include <boost/interprocess/permissions.hpp>
 #include <boost/interprocess/detail/win32_api.hpp>
 #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
+#include <boost/interprocess/sync/windows/winapi_wrapper_common.hpp>
 #include <boost/interprocess/errors.hpp>
 #include <boost/interprocess/exceptions.hpp>
 #include <limits>
@@ -44,58 +45,16 @@
    {}
 
    void unlock()
-   {
-      winapi::release_mutex(m_mtx_hnd);
-   }
+   {  winapi::release_mutex(m_mtx_hnd);   }
 
    void lock()
-   {
-      if(winapi::wait_for_single_object(m_mtx_hnd, winapi::infinite_time) != winapi::wait_object_0){
-         error_info err = system_error_code();
-         throw interprocess_exception(err);
-      }
-   }
+   {  return winapi_wrapper_wait_for_single_object(m_mtx_hnd);  }
 
    bool try_lock()
-   {
-      unsigned long ret = winapi::wait_for_single_object(m_mtx_hnd, 0);
-      if(ret == winapi::wait_object_0){
-         return true;
-      }
-      else if(ret == winapi::wait_timeout){
-         return false;
-      }
-      else{
-         error_info err = system_error_code();
-         throw interprocess_exception(err);
-      }
-   }
+   {  return winapi_wrapper_try_wait_for_single_object(m_mtx_hnd);  }
 
    bool timed_lock(const boost::posix_time::ptime &abs_time)
-   {
-      if(abs_time == boost::posix_time::pos_infin){
-         this->lock();
-         return true;
-      }
-      const boost::posix_time::ptime cur_time = microsec_clock::universal_time();
-      if(abs_time < cur_time){
-         return false;
-      }
-      else{
-         unsigned long ret = winapi::wait_for_single_object
-            (m_mtx_hnd, (abs_time - cur_time).total_milliseconds());
-         if(ret == winapi::wait_object_0){
-            return true;
-         }
-         else if(ret == winapi::wait_timeout){
-            return false;
-         }
-         else{
-            error_info err = system_error_code();
-            throw interprocess_exception(err);
-         }
-      }
-   }
+   {  return winapi_wrapper_timed_wait_for_single_object(m_mtx_hnd, abs_time);  }
 
    /// @cond
    protected:
Modified: trunk/boost/interprocess/sync/windows/winapi_semaphore_wrapper.hpp
==============================================================================
--- trunk/boost/interprocess/sync/windows/winapi_semaphore_wrapper.hpp	Mon Oct 28 18:35:44 2013	(r86510)
+++ trunk/boost/interprocess/sync/windows/winapi_semaphore_wrapper.hpp	2013-10-29 03:39:21 EDT (Tue, 29 Oct 2013)	(r86511)
@@ -21,6 +21,7 @@
 #include <boost/interprocess/permissions.hpp>
 #include <boost/interprocess/detail/win32_api.hpp>
 #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
+#include <boost/interprocess/sync/windows/winapi_wrapper_common.hpp>
 #include <boost/interprocess/errors.hpp>
 #include <boost/interprocess/exceptions.hpp>
 #include <limits>
@@ -50,54 +51,13 @@
    }
 
    void wait()
-   {
-      if(winapi::wait_for_single_object(m_sem_hnd, winapi::infinite_time) != winapi::wait_object_0){
-         error_info err = system_error_code();
-         throw interprocess_exception(err);
-      }
-   }
+   {  return winapi_wrapper_wait_for_single_object(m_sem_hnd);  }
 
    bool try_wait()
-   {
-      unsigned long ret = winapi::wait_for_single_object(m_sem_hnd, 0);
-      if(ret == winapi::wait_object_0){
-         return true;
-      }
-      else if(ret == winapi::wait_timeout){
-         return false;
-      }
-      else{
-         error_info err = system_error_code();
-         throw interprocess_exception(err);
-      }
-   }
+   {  return winapi_wrapper_try_wait_for_single_object(m_sem_hnd);  }
 
    bool timed_wait(const boost::posix_time::ptime &abs_time)
-   {
-      if(abs_time == boost::posix_time::pos_infin){
-         this->wait();
-         return true;
-      }
-
-      boost::posix_time::ptime cur_time = microsec_clock::universal_time();
-      if(abs_time < cur_time){
-         return false;
-      }
-      else{
-         unsigned long ret = winapi::wait_for_single_object
-            (m_sem_hnd, (abs_time - cur_time).total_milliseconds());
-         if(ret == winapi::wait_object_0){
-            return true;
-         }
-         else if(ret == winapi::wait_timeout){
-            return false;
-         }
-         else{
-            error_info err = system_error_code();
-            throw interprocess_exception(err);
-         }
-      }
-   }
+   {  return winapi_wrapper_timed_wait_for_single_object(m_sem_hnd, abs_time);  }
 
    long value() const
    {
Added: trunk/boost/interprocess/sync/windows/winapi_wrapper_common.hpp
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ trunk/boost/interprocess/sync/windows/winapi_wrapper_common.hpp	2013-10-29 03:39:21 EDT (Tue, 29 Oct 2013)	(r86511)
@@ -0,0 +1,86 @@
+ //////////////////////////////////////////////////////////////////////////////
+//
+// (C) Copyright Ion Gaztanaga 2011-2012. 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)
+//
+// See http://www.boost.org/libs/interprocess for documentation.
+//
+//////////////////////////////////////////////////////////////////////////////
+
+#ifndef BOOST_INTERPROCESS_DETAIL_WINAPI_WRAPPER_COMMON_HPP
+#define BOOST_INTERPROCESS_DETAIL_WINAPI_WRAPPER_COMMON_HPP
+
+#if defined(_MSC_VER)
+#  pragma once
+#endif
+
+#include <boost/interprocess/detail/config_begin.hpp>
+#include <boost/interprocess/detail/workaround.hpp>
+#include <boost/interprocess/detail/win32_api.hpp>
+#include <boost/interprocess/detail/posix_time_types_wrk.hpp>
+#include <boost/interprocess/errors.hpp>
+#include <boost/interprocess/exceptions.hpp>
+#include <limits>
+
+namespace boost {
+namespace interprocess {
+namespace ipcdetail {
+
+inline void winapi_wrapper_wait_for_single_object(void *handle)
+{
+   if(winapi::wait_for_single_object(handle, winapi::infinite_time) != winapi::wait_object_0){
+      error_info err = system_error_code();
+      throw interprocess_exception(err);
+   }
+}
+
+inline bool winapi_wrapper_try_wait_for_single_object(void *handle)
+{
+   unsigned long ret = winapi::wait_for_single_object(handle, 0);
+   if(ret == winapi::wait_object_0){
+      return true;
+   }
+   else if(ret == winapi::wait_timeout){
+      return false;
+   }
+   else{
+      error_info err = system_error_code();
+      throw interprocess_exception(err);
+   }
+}
+
+inline bool winapi_wrapper_timed_wait_for_single_object(void *handle, const boost::posix_time::ptime &abs_time)
+{
+   //Windows does not support infinity abs_time so check it
+   if(abs_time == boost::posix_time::pos_infin){
+      winapi_wrapper_wait_for_single_object(handle);
+      return true;
+   }
+   const boost::posix_time::ptime cur_time = microsec_clock::universal_time();
+   //Windows uses relative wait times so check for negative waits
+   //and implement as 0 wait to allow try-semantics as POSIX mandates.
+   unsigned long ret = winapi::wait_for_single_object
+      ( handle
+      , (abs_time <= cur_time) ? 0u
+                                 : (abs_time - cur_time).total_milliseconds()
+      );
+   if(ret == winapi::wait_object_0){
+      return true;
+   }
+   else if(ret == winapi::wait_timeout){
+      return false;
+   }
+   else{
+      error_info err = system_error_code();
+      throw interprocess_exception(err);
+   }
+}
+
+}  //namespace ipcdetail {
+}  //namespace interprocess {
+}  //namespace boost {
+
+#include <boost/interprocess/detail/config_end.hpp>
+
+#endif   //BOOST_INTERPROCESS_DETAIL_WINAPI_MUTEX_WRAPPER_HPP
Modified: trunk/libs/interprocess/doc/interprocess.qbk
==============================================================================
--- trunk/libs/interprocess/doc/interprocess.qbk	Mon Oct 28 18:35:44 2013	(r86510)
+++ trunk/libs/interprocess/doc/interprocess.qbk	2013-10-29 03:39:21 EDT (Tue, 29 Oct 2013)	(r86511)
@@ -6717,11 +6717,16 @@
 *  Fixed bugs: 
    * [@https://svn.boost.org/trac/boost/ticket/9221 #9221 (['"message_queue deadlock on linux"])].
    * [@https://svn.boost.org/trac/boost/ticket/9226 #9226 (['"On some computers, Common Appdata is empty in registry, so boost interprocess cannot work"])].
+   * [@https://svn.boost.org/trac/boost/ticket/9285 #9285 (['"CreateMutex returns NULL if fails"])].
+   * [@https://svn.boost.org/trac/boost/ticket/9288 #9288 (['"timed_wait does not check if it has expired"])].
 
 *  [*ABI breaking]: [@https://svn.boost.org/trac/boost/ticket/9221 #9221] showed
    that `BOOST_INTERPROCESS_MSG_QUEUE_CIRCULAR_INDEX` option of message queue,
    was completely broken so an ABI break was necessary to have a working implementation.
 
+*  Simplified, refactored and unified (timed_)lock code based on try_lock().
+   There were several bugs in when handling timeout expirations.
+
 [endsect]
 
 [section:release_notes_boost_1_55_00 Boost 1.55 Release]
Modified: trunk/libs/interprocess/test/file_lock_test.cpp
==============================================================================
--- trunk/libs/interprocess/test/file_lock_test.cpp	Mon Oct 28 18:35:44 2013	(r86510)
+++ trunk/libs/interprocess/test/file_lock_test.cpp	2013-10-29 03:39:21 EDT (Tue, 29 Oct 2013)	(r86511)
@@ -71,8 +71,8 @@
    }
 
    //test::test_all_lock<file_lock_lock_test_wrapper>();
-   //test::test_all_mutex<false, file_lock_lock_test_wrapper>();
-   //test::test_all_sharable_mutex<false, file_lock_lock_test_wrapper>();
+   //test::test_all_mutex<file_lock_lock_test_wrapper>();
+   //test::test_all_sharable_mutex<file_lock_lock_test_wrapper>();
    std::remove(get_filename().c_str());
 
    return 0;
Modified: trunk/libs/interprocess/test/util.hpp
==============================================================================
--- trunk/libs/interprocess/test/util.hpp	Mon Oct 28 18:35:44 2013	(r86510)
+++ trunk/libs/interprocess/test/util.hpp	2013-10-29 03:39:21 EDT (Tue, 29 Oct 2013)	(r86511)
@@ -76,7 +76,7 @@
 template <typename P>
 struct data
 {
-   data(int id, int secs=0)
+   explicit data(int id, int secs=0)
       : m_id(id), m_value(-1), m_secs(secs), m_error(no_error)
    {}
    int            m_id;