]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[2202] Don't throw from destructors
authorMichal 'vorner' Vaner <michal.vaner@nic.cz>
Wed, 26 Sep 2012 09:29:07 +0000 (11:29 +0200)
committerMichal 'vorner' Vaner <michal.vaner@nic.cz>
Wed, 26 Sep 2012 09:29:07 +0000 (11:29 +0200)
The situations are very bad anyway, and that should not happen during
normal operation. So we assert instead.

src/lib/util/threads/lock.cc
src/lib/util/threads/thread.cc

index 7c34baa56dd7724fc348a5fd480e5e47108400c1..d5c32d417966cd2598e81cb84a5e9f018d022e7f 100644 (file)
@@ -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);
     }
 }
 
index d5154039454d4f170b57ea3978db07176647d04d..bbf7f513e25db7317762677011d6b386551e1e06 100644 (file)
@@ -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);
     }
 }