Browse Source

[2202] Don't throw from destructors

The situations are very bad anyway, and that should not happen during
normal operation. So we assert instead.
Michal 'vorner' Vaner 12 years ago
parent
commit
6e3d1a58e4
2 changed files with 17 additions and 22 deletions
  1. 14 15
      src/lib/util/threads/lock.cc
  2. 3 7
      src/lib/util/threads/thread.cc

+ 14 - 15
src/lib/util/threads/lock.cc

@@ -47,11 +47,9 @@ struct Deinitializer {
     {}
     ~Deinitializer() {
         const int result = pthread_mutexattr_destroy(&attributes_);
-        if (result != 0) {
-            // This really should not happen. We might as well
-            // try to use assert here.
-            isc_throw(isc::InvalidOperation, strerror(result));
-        }
+        // This should never happen. According to the man page,
+        // if there's error, it's our fault.
+        assert(result == 0);
     }
     pthread_mutexattr_t& attributes_;
 };
@@ -101,16 +99,17 @@ Mutex::~Mutex() {
         const int result = pthread_mutex_destroy(&impl_->mutex);
         const bool locked = impl_->locked_count != 0;
         delete impl_;
-        if (result != 0) {
-            // Yes, really throwing from the destructor.
-            // But the error should not happen during normal
-            // operations, this means something is screwed up
-            // and must be fixed.
-            isc_throw(isc::InvalidOperation, strerror(result));
-        }
-        if (locked) {
-            isc_throw(isc::InvalidOperation, "Destroying locked mutex");
-        }
+        // We don't want to throw from the destructor. Also, if this ever
+        // fails, something is really screwed up a lot.
+        assert(result == 0);
+
+        // We should not try to destroy a locked mutex, bad threaded monsters
+        // could get loose if we ever do and it is also forbidden by pthreads.
+
+        // This should not be possible to happen, since the
+        // pthread_mutex_destroy should check for it already. But it seems
+        // there are systems that don't check it.
+        assert(!locked);
     }
 }
 

+ 3 - 7
src/lib/util/threads/thread.cc

@@ -113,13 +113,9 @@ Thread::~Thread() {
         const int result = pthread_detach(impl_->tid);
         Impl::done(impl_);
         impl_ = NULL;
-        if (result != 0) {
-            // Yes, really throwing from destructor. But this would
-            // mean someone really messed up the internal state, so
-            // we need to do something about it, even if it causes
-            // application to terminate.
-            isc_throw(isc::InvalidOperation, strerror(result));
-        }
+        // If the detach ever fails, something is screwed rather
+        // badly.
+        assert(result == 0);
     }
 }