Browse Source

[2202] some editorial/style fixes

- spacing
- new lines for readability
- const
- brace position for catch
- position of ++/--
JINMEI Tatuya 12 years ago
parent
commit
abd88f16af

+ 9 - 9
src/lib/util/threads/lock.cc

@@ -45,8 +45,8 @@ struct Deinitializer {
     Deinitializer(pthread_mutexattr_t& attributes):
         attributes_(attributes)
     {}
-    ~ Deinitializer() {
-        int result = pthread_mutexattr_destroy(&attributes_);
+    ~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.
@@ -96,10 +96,10 @@ Mutex::Mutex(bool recursive) :
     }
 }
 
-Mutex::~ Mutex() {
+Mutex::~Mutex() {
     if (impl_ != NULL) {
-        int result = pthread_mutex_destroy(&impl_->mutex);
-        bool locked = impl_->locked != 0;
+        const int result = pthread_mutex_destroy(&impl_->mutex);
+        const bool locked = impl_->locked != 0;
         delete impl_;
         if (result != 0) {
             // Yes, really throwing from the destructor.
@@ -117,18 +117,18 @@ Mutex::~ Mutex() {
 void
 Mutex::lock() {
     assert(impl_ != NULL);
-    int result = pthread_mutex_lock(&impl_->mutex);
+    const int result = pthread_mutex_lock(&impl_->mutex);
     if (result != 0) {
         isc_throw(isc::InvalidOperation, strerror(result));
     }
-    impl_->locked ++; // Only in debug mode
+    ++impl_->locked; // Only in debug mode
 }
 
 void
 Mutex::unlock() {
     assert(impl_ != NULL);
-    impl_->locked --; // Only in debug mode
-    int result = pthread_mutex_unlock(&impl_->mutex);
+    --impl_->locked; // Only in debug mode
+    const int result = pthread_mutex_unlock(&impl_->mutex);
     if (result != 0) {
         isc_throw(isc::InvalidOperation, strerror(result));
     }

+ 5 - 2
src/lib/util/threads/lock.h

@@ -61,6 +61,7 @@ public:
     /// \throw isc::InvalidOperation Other unspecified errors around the mutex.
     ///     This should be rare.
     Mutex(bool recursive = false);
+
     /// \brief Destructor.
     ///
     /// Destroyes the mutex. It is not allowed to destroy a mutex which is
@@ -70,7 +71,8 @@ public:
     /// \throw isc::InvalidOperation when the OS reports an error. This should
     ///     generally happen only when the Mutex was used in a wrong way,
     ///     meaning programmer error.
-    ~ Mutex();
+    ~Mutex();
+
     /// \brief This holds a lock on a Mutex.
     ///
     /// To lock a mutex, create a locket. It'll get unlocked when the locker
@@ -100,6 +102,7 @@ public:
             mutex.lock();
             mutex_ = &mutex;
         }
+
         /// \brief Destructor.
         ///
         /// Unlocks the mutex.
@@ -107,7 +110,7 @@ public:
         /// \throw isc::InvalidOperation when OS repotrs error. This usually
         ///     means an attempt to use the mutex in a wrong way (unlocking
         ///     a mutex belonging to a differen thread).
-        ~ Locker() {
+        ~Locker() {
             if (mutex_ != NULL) {
                 mutex_->unlock();
             }

+ 10 - 10
src/lib/util/threads/thread.cc

@@ -49,7 +49,7 @@ public:
         bool should_delete(false);
         { // We need to make sure the mutex is unlocked before it is deleted
             Mutex::Locker locker(impl->mutex);
-            if (-- impl->waiting_ == 0) {
+            if (--impl->waiting_ == 0) {
                 should_delete = true;
             }
         }
@@ -62,13 +62,11 @@ public:
         Impl* impl = reinterpret_cast<Impl*>(impl_raw);
         try {
             impl->main_();
-        }
-        catch (const exception& e) {
+        } catch (const exception& e) {
             Mutex::Locker locker(impl->mutex);
             impl->exception_ = true;
             impl->exception_text_ = e.what();
-        }
-        catch (...) {
+        } catch (...) {
             Mutex::Locker locker(impl->mutex);
             impl->exception_ = true;
         }
@@ -93,7 +91,8 @@ Thread::Thread(const boost::function<void ()>& main) :
     impl_(NULL)
 {
     auto_ptr<Impl> impl(new Impl(main));
-    int result = pthread_create(&impl->tid, NULL, &Impl::run, impl.get());
+    const int result = pthread_create(&impl->tid, NULL, &Impl::run,
+                                      impl.get());
     // Any error here?
     switch (result) {
         case 0: // All 0K
@@ -106,10 +105,10 @@ Thread::Thread(const boost::function<void ()>& main) :
     }
 }
 
-Thread::~ Thread() {
+Thread::~Thread() {
     if (impl_ != NULL) {
         // In case we didn't call wait yet
-        int result = pthread_detach(impl_->tid);
+        const int result = pthread_detach(impl_->tid);
         Impl::done(impl_);
         impl_ = NULL;
         if (result != 0) {
@@ -125,10 +124,11 @@ Thread::~ Thread() {
 void
 Thread::wait() {
     if (impl_ == NULL) {
-        isc_throw(isc::InvalidOperation, "Wait called and no thread to wait for");
+        isc_throw(isc::InvalidOperation,
+                  "Wait called and no thread to wait for");
     }
 
-    int result = pthread_join(impl_->tid, NULL);
+    const int result = pthread_join(impl_->tid, NULL);
     if (result != 0) {
         isc_throw(isc::InvalidOperation, strerror(result));
     }

+ 6 - 3
src/lib/util/threads/thread.h

@@ -49,6 +49,7 @@ public:
             Exception(file, line, what)
         {}
     };
+
     /// \brief Create and start a thread.
     ///
     /// Create a new thread and run body inside it.
@@ -63,10 +64,11 @@ public:
     ///
     /// \param main The code to run inside the thread.
     ///
-    /// \throw std::bad_alloc if allocation of the new thread or other resources
-    ///     fails.
+    /// \throw std::bad_alloc if allocation of the new thread or other
+    /// resources fails.
     /// \throw isc::InvalidOperation for other errors (should not happen).
     Thread(const boost::function<void()>& main);
+
     /// \brief Destructor.
     ///
     /// It is completely legitimate to destroy the thread without calling
@@ -77,7 +79,8 @@ public:
     /// \throw isc::InvalidOperation in the rare case of OS reporting a
     ///     problem. This should not happen unless you messed up with the raw
     ///     thread by the low-level API.
-    ~ Thread();
+    ~Thread();
+
     /// \brief Wait for the thread to terminate.
     ///
     /// Waits until the thread terminates. Must be called at most once.