Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r85199 - trunk/boost/log/detail
From: andrey.semashev_at_[hidden]
Date: 2013-08-03 09:05:31


Author: andysem
Date: 2013-08-03 09:05:31 EDT (Sat, 03 Aug 2013)
New Revision: 85199
URL: http://svn.boost.org/trac/boost/changeset/85199

Log:
Added exceptions in case of errors. Fixes #8915.

Text files modified:
   trunk/boost/log/detail/spin_mutex.hpp | 71 ++++++++++++++++++++++++++++++++++-----
   1 files changed, 61 insertions(+), 10 deletions(-)

Modified: trunk/boost/log/detail/spin_mutex.hpp
==============================================================================
--- trunk/boost/log/detail/spin_mutex.hpp Sat Aug 3 07:37:08 2013 (r85198)
+++ trunk/boost/log/detail/spin_mutex.hpp 2013-08-03 09:05:31 EDT (Sat, 03 Aug 2013) (r85199)
@@ -24,6 +24,9 @@
 
 #ifndef BOOST_LOG_NO_THREADS
 
+#include <boost/throw_exception.hpp>
+#include <boost/thread/exceptions.hpp>
+
 #if defined(BOOST_THREAD_POSIX) // This one can be defined by users, so it should go first
 #define BOOST_LOG_SPIN_MUTEX_USE_PTHREAD
 #elif defined(BOOST_WINDOWS)
@@ -192,28 +195,52 @@
 public:
     spin_mutex()
     {
- BOOST_VERIFY(pthread_spin_init(&m_State, PTHREAD_PROCESS_PRIVATE) == 0);
+ const int err = pthread_spin_init(&m_State, PTHREAD_PROCESS_PRIVATE);
+ if (err != 0)
+ throw_exception< thread_resource_error >(err, "failed to initialize a spin mutex", "spin_mutex::spin_mutex()", __FILE__, __LINE__);
     }
+
     ~spin_mutex()
     {
- pthread_spin_destroy(&m_State);
+ BOOST_VERIFY(pthread_spin_destroy(&m_State) == 0);
     }
+
     bool try_lock()
     {
- return (pthread_spin_trylock(&m_State) == 0);
+ const int err = pthread_spin_trylock(&m_State);
+ if (err == 0)
+ return true;
+ if (err != EBUSY)
+ throw_exception< lock_error >(err, "failed to lock a spin mutex", "spin_mutex::try_lock()", __FILE__, __LINE__);
+ return false;
     }
+
     void lock()
     {
- BOOST_VERIFY(pthread_spin_lock(&m_State) == 0);
+ const int err = pthread_spin_lock(&m_State);
+ if (err != 0)
+ throw_exception< lock_error >(err, "failed to lock a spin mutex", "spin_mutex::lock()", __FILE__, __LINE__);
     }
+
     void unlock()
     {
- pthread_spin_unlock(&m_State);
+ BOOST_VERIFY(pthread_spin_unlock(&m_State) == 0);
     }
 
     // Non-copyable
     BOOST_DELETED_FUNCTION(spin_mutex(spin_mutex const&))
     BOOST_DELETED_FUNCTION(spin_mutex& operator= (spin_mutex const&))
+
+private:
+ template< typename ExceptionT >
+ static BOOST_NOINLINE BOOST_LOG_NORETURN void throw_exception(int err, const char* descr, const char* func, const char* file, int line)
+ {
+#if !defined(BOOST_EXCEPTION_DISABLE)
+ boost::exception_detail::throw_exception_(ExceptionT(err, descr), func, file, line);
+#else
+ boost::throw_exception(ExceptionT(err, descr));
+#endif
+ }
 };
 
 #else // defined(_POSIX_SPIN_LOCKS)
@@ -227,28 +254,52 @@
 public:
     spin_mutex()
     {
- BOOST_VERIFY(pthread_mutex_init(&m_State, NULL) == 0);
+ const int err = pthread_mutex_init(&m_State, NULL);
+ if (err != 0)
+ throw_exception< thread_resource_error >(err, "failed to initialize a spin mutex", "spin_mutex::spin_mutex()", __FILE__, __LINE__);
     }
+
     ~spin_mutex()
     {
- pthread_mutex_destroy(&m_State);
+ BOOST_VERIFY(pthread_mutex_destroy(&m_State) == 0);
     }
+
     bool try_lock()
     {
- return (pthread_mutex_trylock(&m_State) == 0);
+ const int err = pthread_mutex_trylock(&m_State);
+ if (err == 0)
+ return true;
+ if (err != EBUSY)
+ throw_exception< lock_error >(err, "failed to lock a spin mutex", "spin_mutex::try_lock()", __FILE__, __LINE__);
+ return false;
     }
+
     void lock()
     {
- BOOST_VERIFY(pthread_mutex_lock(&m_State) == 0);
+ const int err = pthread_mutex_lock(&m_State);
+ if (err != 0)
+ throw_exception< lock_error >(err, "failed to lock a spin mutex", "spin_mutex::lock()", __FILE__, __LINE__);
     }
+
     void unlock()
     {
- pthread_mutex_unlock(&m_State);
+ BOOST_VERIFY(pthread_mutex_unlock(&m_State) == 0);
     }
 
     // Non-copyable
     BOOST_DELETED_FUNCTION(spin_mutex(spin_mutex const&))
     BOOST_DELETED_FUNCTION(spin_mutex& operator= (spin_mutex const&))
+
+private:
+ template< typename ExceptionT >
+ static BOOST_NOINLINE BOOST_LOG_NORETURN void throw_exception(int err, const char* descr, const char* func, const char* file, int line)
+ {
+#if !defined(BOOST_EXCEPTION_DISABLE)
+ boost::exception_detail::throw_exception_(ExceptionT(err, descr), func, file, line);
+#else
+ boost::throw_exception(ExceptionT(err, descr));
+#endif
+ }
 };
 
 #endif // defined(_POSIX_SPIN_LOCKS)


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