namespace { // unnamed namespace
typedef std::map<std::string, boost::weak_ptr<Mutex> > SyncMap;
-typedef boost::shared_ptr<Mutex> MutexPtr;
Mutex sync_map_mutex;
SyncMap sync_map;
Mutex::Locker locker(sync_map_mutex);
- // Unref the shared mutex first.
+ // Unref the shared mutex.
+ locker_.reset();
mutex_.reset();
+ // Remove name from the map if it is unused anymore.
SyncMap::iterator it = sync_map.find(task_name_);
assert(it != sync_map.end());
}
// First grab the thread lock...
- mutex_->lock();
+ LockerPtr locker(new Mutex::Locker(*mutex_));
// ... then the file lock.
- try {
- if (do_lock(F_SETLKW, F_WRLCK)) {
- is_locked_ = true;
- return (true);
- }
- } catch (...) {
- mutex_->unlock();
- throw;
+ if (do_lock(F_SETLKW, F_WRLCK)) {
+ is_locked_ = true;
+ locker_ = locker;
+ return (true);
}
- mutex_->unlock();
return (false);
}
}
// First grab the thread lock...
- if (!mutex_->tryLock()) {
+ LockerPtr locker;
+ try {
+ locker.reset(new Mutex::Locker(*mutex_, false));
+ } catch (const Mutex::Locker::AlreadyLocked&) {
return (false);
}
// ... then the file lock.
- try {
- // ... then the file lock.
- if (do_lock(F_SETLK, F_WRLCK)) {
- is_locked_ = true;
- return (true);
- }
- } catch (...) {
- mutex_->unlock();
- throw;
+ if (do_lock(F_SETLK, F_WRLCK)) {
+ is_locked_ = true;
+ locker_ = locker;
+ return (true);
}
- mutex_->unlock();
return (false);
}
return (false);
}
- mutex_->unlock();
+ locker_.reset();
is_locked_ = false;
return (true);
}
int fd_; ///< The descriptor for the open file
typedef boost::shared_ptr<isc::util::thread::Mutex> MutexPtr;
- MutexPtr mutex_; ///< A mutex for mutual exclusion among threads
+ typedef boost::shared_ptr<isc::util::thread::Mutex::Locker> LockerPtr;
+ MutexPtr mutex_; ///< A mutex for mutual exclusion among threads
+ LockerPtr locker_; ///< A locker on mutex_
};
} // namespace util