BoolmarkMgr: rationalize a bit the usage of recursive_mutexes

This commit is contained in:
vsonnier 2018-01-16 20:21:16 +01:00
parent 7fb66b6998
commit d8ac9559fe
4 changed files with 67 additions and 68 deletions

View File

@ -15,12 +15,11 @@ BookmarkEntry::~BookmarkEntry() {
BookmarkMgr::BookmarkMgr() { BookmarkMgr::BookmarkMgr() {
rangesSorted = false; rangesSorted = false;
} }
//
//represents an empty BookMarkList that is returned by reference by some functions.
const BookmarkList BookmarkMgr::emptyResults;
void BookmarkMgr::saveToFile(std::string bookmarkFn, bool backup, bool useFullpath) { void BookmarkMgr::saveToFile(std::string bookmarkFn, bool backup, bool useFullpath) {
std::lock_guard < std::recursive_mutex > lock(busy_lock);
DataTree s("cubicsdr_bookmarks"); DataTree s("cubicsdr_bookmarks");
DataNode *header = s.rootNode()->newChild("header"); DataNode *header = s.rootNode()->newChild("header");
header->newChild("version")->element()->set(wxString(CUBICSDR_VERSION).ToStdWstring()); header->newChild("version")->element()->set(wxString(CUBICSDR_VERSION).ToStdWstring());
@ -101,6 +100,8 @@ void BookmarkMgr::saveToFile(std::string bookmarkFn, bool backup, bool useFullpa
bool BookmarkMgr::loadFromFile(std::string bookmarkFn, bool backup, bool useFullpath) { bool BookmarkMgr::loadFromFile(std::string bookmarkFn, bool backup, bool useFullpath) {
std::lock_guard < std::recursive_mutex > lock(busy_lock);
wxFileName loadFile; wxFileName loadFile;
wxFileName failFile; wxFileName failFile;
wxFileName lastLoaded; wxFileName lastLoaded;
@ -129,7 +130,7 @@ bool BookmarkMgr::loadFromFile(std::string bookmarkFn, bool backup, bool useFull
// New instance of bookmark savefiles // New instance of bookmark savefiles
if (backup && !loadFile.FileExists() && !lastLoaded.FileExists() && !backupFile.FileExists()) { if (backup && !loadFile.FileExists() && !lastLoaded.FileExists() && !backupFile.FileExists()) {
wxGetApp().getAppFrame()->getBookmarkView()->loadDefaultRanges(); loadDefaultRanges();
return true; return true;
} }
@ -145,8 +146,8 @@ bool BookmarkMgr::loadFromFile(std::string bookmarkFn, bool backup, bool useFull
// Clear any active data // Clear any active data
bmData.clear(); bmData.clear();
clearRecents(); recents.clear();
clearRanges(); ranges.clear();
bmDataSorted.clear(); bmDataSorted.clear();
if (s.rootNode()->hasAnother("branches")) { if (s.rootNode()->hasAnother("branches")) {
@ -238,15 +239,31 @@ bool BookmarkMgr::loadFromFile(std::string bookmarkFn, bool backup, bool useFull
return loadStatusOk; return loadStatusOk;
} }
void BookmarkMgr::loadDefaultRanges() {
addRange(std::make_shared<BookmarkRangeEntry>(L"160 Meters", 1900000, 1800000, 2000000));
addRange(std::make_shared<BookmarkRangeEntry>(L"80 Meters", 3750000, 3500000, 4000000));
addRange(std::make_shared<BookmarkRangeEntry>(L"60 Meters", 5368500, 5332000, 5405000));
addRange(std::make_shared<BookmarkRangeEntry>(L"40 Meters", 7150000, 7000000, 7300000));
addRange(std::make_shared<BookmarkRangeEntry>(L"30 Meters", 10125000, 10100000, 10150000));
addRange(std::make_shared<BookmarkRangeEntry>(L"20 Meters", 14175000, 14000000, 14350000));
addRange(std::make_shared<BookmarkRangeEntry>(L"17 Meters", 18068180, 17044180, 19092180));
addRange(std::make_shared<BookmarkRangeEntry>(L"15 Meters", 21225000, 21000000, 21450000));
addRange(std::make_shared<BookmarkRangeEntry>(L"12 Meters", 24940000, 24890000, 24990000));
addRange(std::make_shared<BookmarkRangeEntry>(L"10 Meters", 28850000, 28000000, 29700000));
}
void BookmarkMgr::resetBookmarks() { void BookmarkMgr::resetBookmarks() {
std::lock_guard < std::recursive_mutex > lock(busy_lock);
// Clear any active data // Clear any active data
bmData.clear(); bmData.clear();
clearRecents(); recents.clear();
clearRanges(); ranges.clear();
bmDataSorted.clear(); bmDataSorted.clear();
wxGetApp().getAppFrame()->getBookmarkView()->loadDefaultRanges(); loadDefaultRanges();
} }
@ -280,8 +297,7 @@ void BookmarkMgr::addBookmark(std::string group, BookmarkEntryPtr be) {
void BookmarkMgr::removeBookmark(std::string group, BookmarkEntryPtr be) { void BookmarkMgr::removeBookmark(std::string group, BookmarkEntryPtr be) {
std::lock_guard < std::recursive_mutex > lockData(busy_lock); std::lock_guard < std::recursive_mutex > lock(busy_lock);
std::lock_guard < std::mutex > lockEnt(be->busy_lock);
if (bmData.find(group) == bmData.end()) { if (bmData.find(group) == bmData.end()) {
return; return;
@ -296,8 +312,7 @@ void BookmarkMgr::removeBookmark(std::string group, BookmarkEntryPtr be) {
} }
void BookmarkMgr::removeBookmark(BookmarkEntryPtr be) { void BookmarkMgr::removeBookmark(BookmarkEntryPtr be) {
std::lock_guard < std::recursive_mutex > lockData(busy_lock); std::lock_guard < std::recursive_mutex > lock(busy_lock);
std::lock_guard < std::mutex > lockEnt(be->busy_lock);
for (auto &bmd_i : bmData) { for (auto &bmd_i : bmData) {
BookmarkList::iterator i = std::find(bmd_i.second.begin(), bmd_i.second.end(), be); BookmarkList::iterator i = std::find(bmd_i.second.begin(), bmd_i.second.end(), be);
@ -308,8 +323,7 @@ void BookmarkMgr::removeBookmark(BookmarkEntryPtr be) {
} }
void BookmarkMgr::moveBookmark(BookmarkEntryPtr be, std::string group) { void BookmarkMgr::moveBookmark(BookmarkEntryPtr be, std::string group) {
std::lock_guard < std::recursive_mutex > lockData(busy_lock); std::lock_guard < std::recursive_mutex > lock(busy_lock);
std::lock_guard < std::mutex > lockEnt(be->busy_lock);
for (auto &bmd_i : bmData) { for (auto &bmd_i : bmData) {
BookmarkList::iterator i = std::find(bmd_i.second.begin(), bmd_i.second.end(), be); BookmarkList::iterator i = std::find(bmd_i.second.begin(), bmd_i.second.end(), be);
@ -367,11 +381,11 @@ void BookmarkMgr::renameGroup(std::string group, std::string ngroup) {
} }
} }
const BookmarkList& BookmarkMgr::getBookmarks(std::string group) { BookmarkList BookmarkMgr::getBookmarks(std::string group) {
std::lock_guard < std::recursive_mutex > lock(busy_lock); std::lock_guard < std::recursive_mutex > lock(busy_lock);
if (bmData.find(group) == bmData.end()) { if (bmData.find(group) == bmData.end()) {
return emptyResults; return BookmarkList();
} }
if (!bmDataSorted[group]) { if (!bmDataSorted[group]) {
@ -384,7 +398,7 @@ const BookmarkList& BookmarkMgr::getBookmarks(std::string group) {
void BookmarkMgr::getGroups(BookmarkNames &arr) { void BookmarkMgr::getGroups(BookmarkNames &arr) {
std::lock_guard < std::recursive_mutex > lockData(busy_lock); std::lock_guard < std::recursive_mutex > lock(busy_lock);
for (BookmarkMap::iterator i = bmData.begin(); i!= bmData.end(); ++i) { for (BookmarkMap::iterator i = bmData.begin(); i!= bmData.end(); ++i) {
arr.push_back(i->first); arr.push_back(i->first);
@ -392,7 +406,7 @@ void BookmarkMgr::getGroups(BookmarkNames &arr) {
} }
void BookmarkMgr::getGroups(wxArrayString &arr) { void BookmarkMgr::getGroups(wxArrayString &arr) {
std::lock_guard < std::recursive_mutex > lockData(busy_lock); std::lock_guard < std::recursive_mutex > lock(busy_lock);
for (BookmarkMap::iterator i = bmData.begin(); i!= bmData.end(); ++i) { for (BookmarkMap::iterator i = bmData.begin(); i!= bmData.end(); ++i) {
arr.push_back(i->first); arr.push_back(i->first);
@ -401,11 +415,16 @@ void BookmarkMgr::getGroups(wxArrayString &arr) {
void BookmarkMgr::setExpandState(std::string groupName, bool state) { void BookmarkMgr::setExpandState(std::string groupName, bool state) {
std::lock_guard < std::recursive_mutex > lock(busy_lock);
expandState[groupName] = state; expandState[groupName] = state;
} }
bool BookmarkMgr::getExpandState(std::string groupName) { bool BookmarkMgr::getExpandState(std::string groupName) {
std::lock_guard < std::recursive_mutex > lock(busy_lock);
if (expandState.find(groupName) == expandState.end()) { if (expandState.find(groupName) == expandState.end()) {
return true; return true;
} }
@ -415,8 +434,6 @@ bool BookmarkMgr::getExpandState(std::string groupName) {
void BookmarkMgr::updateActiveList() { void BookmarkMgr::updateActiveList() {
std::lock_guard < std::recursive_mutex > lockData(busy_lock);
if (wxGetApp().isShuttingDown()) { if (wxGetApp().isShuttingDown()) {
return; return;
} }
@ -430,8 +447,6 @@ void BookmarkMgr::updateActiveList() {
void BookmarkMgr::updateBookmarks() { void BookmarkMgr::updateBookmarks() {
std::lock_guard < std::recursive_mutex > lockData(busy_lock);
BookmarkView *bmv = wxGetApp().getAppFrame()->getBookmarkView(); BookmarkView *bmv = wxGetApp().getAppFrame()->getBookmarkView();
if (bmv) { if (bmv) {
@ -441,8 +456,6 @@ void BookmarkMgr::updateBookmarks() {
void BookmarkMgr::updateBookmarks(std::string group) { void BookmarkMgr::updateBookmarks(std::string group) {
std::lock_guard < std::recursive_mutex > lockData(busy_lock);
BookmarkView *bmv = wxGetApp().getAppFrame()->getBookmarkView(); BookmarkView *bmv = wxGetApp().getAppFrame()->getBookmarkView();
if (bmv) { if (bmv) {
@ -480,8 +493,9 @@ void BookmarkMgr::removeRecent(BookmarkEntryPtr be) {
} }
const BookmarkList& BookmarkMgr::getRecents() { BookmarkList BookmarkMgr::getRecents() {
std::lock_guard < std::recursive_mutex > lockData(busy_lock); std::lock_guard < std::recursive_mutex > lock(busy_lock);
//return a copy
return recents; return recents;
} }
@ -508,8 +522,6 @@ void BookmarkMgr::addRange(BookmarkRangeEntryPtr re) {
rangesSorted = false; rangesSorted = false;
} }
void BookmarkMgr::removeRange(BookmarkRangeEntryPtr re) { void BookmarkMgr::removeRange(BookmarkRangeEntryPtr re) {
std::lock_guard < std::recursive_mutex > lock(busy_lock); std::lock_guard < std::recursive_mutex > lock(busy_lock);
@ -522,7 +534,7 @@ void BookmarkMgr::removeRange(BookmarkRangeEntryPtr re) {
} }
const BookmarkRangeList& BookmarkMgr::getRanges() { BookmarkRangeList BookmarkMgr::getRanges() {
std::lock_guard < std::recursive_mutex > lock(busy_lock); std::lock_guard < std::recursive_mutex > lock(busy_lock);
if (!rangesSorted) { if (!rangesSorted) {
@ -610,8 +622,6 @@ std::wstring BookmarkMgr::getActiveDisplayName(DemodulatorInstancePtr demod) {
void BookmarkMgr::removeActive(DemodulatorInstancePtr demod) { void BookmarkMgr::removeActive(DemodulatorInstancePtr demod) {
std::lock_guard < std::recursive_mutex > lock(busy_lock);
if (demod == nullptr) { if (demod == nullptr) {
return; return;
} }

View File

@ -16,7 +16,6 @@ class DataNode;
class BookmarkEntry { class BookmarkEntry {
public: public:
std::mutex busy_lock;
std::string type; std::string type;
//maps on the Demod user label. //maps on the Demod user label.
@ -39,8 +38,6 @@ public:
BookmarkRangeEntry(std::wstring label, long long freq, long long startFreq, long long endFreq) : label(label), freq(freq), startFreq(startFreq), endFreq(endFreq) { BookmarkRangeEntry(std::wstring label, long long freq, long long startFreq, long long endFreq) : label(label), freq(freq), startFreq(startFreq), endFreq(endFreq) {
} }
std::mutex busy_lock;
std::wstring label; std::wstring label;
long long freq; long long freq;
@ -97,7 +94,9 @@ public:
void addGroup(std::string group); void addGroup(std::string group);
void removeGroup(std::string group); void removeGroup(std::string group);
void renameGroup(std::string group, std::string ngroup); void renameGroup(std::string group, std::string ngroup);
const BookmarkList& getBookmarks(std::string group); //return an independent copy on purpose
BookmarkList getBookmarks(std::string group);
void getGroups(BookmarkNames &arr); void getGroups(BookmarkNames &arr);
void getGroups(wxArrayString &arr); void getGroups(wxArrayString &arr);
@ -111,14 +110,20 @@ public:
void addRecent(DemodulatorInstancePtr demod); void addRecent(DemodulatorInstancePtr demod);
void addRecent(BookmarkEntryPtr be); void addRecent(BookmarkEntryPtr be);
void removeRecent(BookmarkEntryPtr be); void removeRecent(BookmarkEntryPtr be);
const BookmarkList& getRecents();
//return an independent copy on purpose
BookmarkList getRecents();
void clearRecents(); void clearRecents();
void removeActive(DemodulatorInstancePtr demod); void removeActive(DemodulatorInstancePtr demod);
void addRange(BookmarkRangeEntryPtr re); void addRange(BookmarkRangeEntryPtr re);
void removeRange(BookmarkRangeEntryPtr re); void removeRange(BookmarkRangeEntryPtr re);
const BookmarkRangeList& getRanges();
//return an independent copy on purpose
BookmarkRangeList getRanges();
void clearRanges(); void clearRanges();
static std::wstring getBookmarkEntryDisplayName(BookmarkEntryPtr bmEnt); static std::wstring getBookmarkEntryDisplayName(BookmarkEntryPtr bmEnt);
@ -127,6 +132,7 @@ public:
protected: protected:
void trimRecents(); void trimRecents();
void loadDefaultRanges();
BookmarkEntryPtr demodToBookmarkEntry(DemodulatorInstancePtr demod); BookmarkEntryPtr demodToBookmarkEntry(DemodulatorInstancePtr demod);
BookmarkEntryPtr nodeToBookmark(DataNode *node); BookmarkEntryPtr nodeToBookmark(DataNode *node);
@ -136,10 +142,8 @@ protected:
BookmarkList recents; BookmarkList recents;
BookmarkRangeList ranges; BookmarkRangeList ranges;
bool rangesSorted; bool rangesSorted;
std::recursive_mutex busy_lock; std::recursive_mutex busy_lock;
BookmarkExpandState expandState; BookmarkExpandState expandState;
//represents an empty BookMarkList that is returned by reference by some functions.
static const BookmarkList emptyResults;
}; };

View File

@ -301,7 +301,7 @@ wxTreeItemId BookmarkView::refreshBookmarks() {
bool groupExpanded = searchState || wxGetApp().getBookmarkMgr().getExpandState(gn_i); bool groupExpanded = searchState || wxGetApp().getBookmarkMgr().getExpandState(gn_i);
const BookmarkList& bmList = wxGetApp().getBookmarkMgr().getBookmarks(gn_i); BookmarkList bmList = wxGetApp().getBookmarkMgr().getBookmarks(gn_i);
for (auto &bmEnt : bmList) { for (auto &bmEnt : bmList) {
std::wstring labelVal = BookmarkMgr::getBookmarkEntryDisplayName(bmEnt); std::wstring labelVal = BookmarkMgr::getBookmarkEntryDisplayName(bmEnt);
@ -409,7 +409,7 @@ void BookmarkView::doUpdateActiveList() {
bool rangeExpandState = searchState?false:expandState["range"]; bool rangeExpandState = searchState?false:expandState["range"];
//Ranges //Ranges
const BookmarkRangeList& bmRanges = wxGetApp().getBookmarkMgr().getRanges(); BookmarkRangeList bmRanges = wxGetApp().getBookmarkMgr().getRanges();
m_treeView->DeleteChildren(rangeBranch); m_treeView->DeleteChildren(rangeBranch);
@ -440,7 +440,8 @@ void BookmarkView::doUpdateActiveList() {
bool recentExpandState = searchState || expandState["recent"]; bool recentExpandState = searchState || expandState["recent"];
// Recents // Recents
const BookmarkList& bmRecents = wxGetApp().getBookmarkMgr().getRecents(); BookmarkList bmRecents = wxGetApp().getBookmarkMgr().getRecents();
m_treeView->DeleteChildren(recentBranch); m_treeView->DeleteChildren(recentBranch);
for (auto &bmr_i: bmRecents) { for (auto &bmr_i: bmRecents) {
@ -1555,21 +1556,6 @@ void BookmarkView::onClearSearch( wxCommandEvent& /* event */ ) {
refreshLayout(); refreshLayout();
} }
void BookmarkView::loadDefaultRanges() {
wxGetApp().getBookmarkMgr().addRange(std::make_shared<BookmarkRangeEntry>(L"160 Meters", 1900000, 1800000, 2000000));
wxGetApp().getBookmarkMgr().addRange(std::make_shared<BookmarkRangeEntry>(L"80 Meters", 3750000, 3500000, 4000000));
wxGetApp().getBookmarkMgr().addRange(std::make_shared<BookmarkRangeEntry>(L"60 Meters", 5368500, 5332000, 5405000));
wxGetApp().getBookmarkMgr().addRange(std::make_shared<BookmarkRangeEntry>(L"40 Meters", 7150000, 7000000, 7300000));
wxGetApp().getBookmarkMgr().addRange(std::make_shared<BookmarkRangeEntry>(L"30 Meters", 10125000, 10100000, 10150000));
wxGetApp().getBookmarkMgr().addRange(std::make_shared<BookmarkRangeEntry>(L"20 Meters", 14175000, 14000000, 14350000));
wxGetApp().getBookmarkMgr().addRange(std::make_shared<BookmarkRangeEntry>(L"17 Meters", 18068180, 17044180, 19092180));
wxGetApp().getBookmarkMgr().addRange(std::make_shared<BookmarkRangeEntry>(L"15 Meters", 21225000, 21000000, 21450000));
wxGetApp().getBookmarkMgr().addRange(std::make_shared<BookmarkRangeEntry>(L"12 Meters", 24940000, 24890000, 24990000));
wxGetApp().getBookmarkMgr().addRange(std::make_shared<BookmarkRangeEntry>(L"10 Meters", 28850000, 28000000, 29700000));
}
BookmarkRangeEntryPtr BookmarkView::makeActiveRangeEntry() { BookmarkRangeEntryPtr BookmarkView::makeActiveRangeEntry() {
BookmarkRangeEntryPtr re(new BookmarkRangeEntry); BookmarkRangeEntryPtr re(new BookmarkRangeEntry);

View File

@ -80,7 +80,6 @@ public:
bool getExpandState(std::string branchName); bool getExpandState(std::string branchName);
void setExpandState(std::string branchName, bool state); void setExpandState(std::string branchName, bool state);
void loadDefaultRanges();
static BookmarkRangeEntryPtr makeActiveRangeEntry(); static BookmarkRangeEntryPtr makeActiveRangeEntry();
protected: protected: