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.
}
}
-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.
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));
}
/// \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
/// \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
mutex.lock();
mutex_ = &mutex;
}
+
/// \brief Destructor.
///
/// Unlocks the mutex.
/// \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();
}
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;
}
}
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;
}
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
}
}
-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) {
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));
}
Exception(file, line, what)
{}
};
+
/// \brief Create and start a thread.
///
/// Create a new thread and run body inside it.
///
/// \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
/// \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.