diff --git a/src/history.cpp b/src/history.cpp index 1f64d1fa9..a967653b9 100644 --- a/src/history.cpp +++ b/src/history.cpp @@ -177,20 +177,13 @@ class history_lru_cache_t : public lru_cache_t m_histories; + owning_lock>> histories; - public: - history_collection_t() { VOMIT_ON_FAILURE_NO_ERRNO(pthread_mutex_init(&m_lock, NULL)); } - ~history_collection_t() { - for (std::map::const_iterator i = m_histories.begin(); - i != m_histories.end(); ++i) { - delete i->second; - } - pthread_mutex_destroy(&m_lock); - } - history_t &alloc(const wcstring &name); + public: + history_t &get_creating(const wcstring &name); void save(); }; @@ -698,16 +691,21 @@ static size_t offset_of_next_item(const char *begin, size_t mmap_length, return result; } -history_t &history_collection_t::alloc(const wcstring &name) { +history_t &history_collection_t::get_creating(const wcstring &name) { + // Return a history for the given name, creating it if necessary // Note that histories are currently never deleted, so we can return a reference to them without - // using something like shared_ptr. - scoped_lock locker(m_lock); - history_t *¤t = m_histories[name]; - if (current == NULL) current = new history_t(name); - return *current; + // using something like shared_ptr + auto hs = histories.acquire(); + std::unique_ptr &hist = hs.value[name]; + if (! hist) { + hist = make_unique(name); + } + return *hist; } -history_t &history_t::history_with_name(const wcstring &name) { return histories.alloc(name); } +history_t &history_t::history_with_name(const wcstring &name) { + return histories.get_creating(name); +} history_t::history_t(const wcstring &pname) : name(pname), @@ -1647,11 +1645,10 @@ void history_t::incorporate_external_changes() { void history_init() {} void history_collection_t::save() { - // Save all histories. - for (std::map::iterator iter = m_histories.begin(); - iter != m_histories.end(); ++iter) { - history_t *hist = iter->second; - hist->save(); + // Save all histories + auto h = histories.acquire(); + for (auto &p : h.value) { + p.second->save(); } }