• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdevelop API Reference
  • KDE Home
  • Contact Us
 

kdevplatform/language/backgroundparser

  • sources
  • kfour-appscomplete
  • kdevelop
  • kdevplatform
  • language
  • backgroundparser
urlparselock.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of KDevelop
3  *
4  * Copyright 2007-2009 David Nolden <[email protected]>
5  * Copyright 2016 Milian Wolff <[email protected]>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Library General Public License as
9  * published by the Free Software Foundation; either version 2 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public
18  * License along with this program; if not, write to the
19  * Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  */
22 #include "urlparselock.h"
23 
24 #include <QHash>
25 #include <QMutex>
26 
27 using namespace KDevelop;
28 
29 namespace {
30 struct PerUrlData
31 {
32  PerUrlData()
33  // TODO: make this non-recursive
34  : mutex(QMutex::Recursive)
35  {}
36  QMutex mutex;
37  // how many people are (trying to) parse this url
38  // we use this to delete the entry once no-one needs it anymore
39  uint ref = 0;
40 };
41 
42 // this mutex protects the parsingUrls
43 // NOTE: QBasicMutex is safe to initialize statically
44 QBasicMutex parsingUrlsMutex;
45 
46 // Hash of urls that are currently being parsed and their protection data
47 using ParsingUrls = QHash<IndexedString, PerUrlData*>;
48 ParsingUrls& parsingUrls()
49 {
50  // delay initialization of the hash until it's needed
51  static ParsingUrls parsingUrls;
52  return parsingUrls;
53 }
54 }
55 
56 UrlParseLock::UrlParseLock(const IndexedString& url)
57  : m_url(url)
58 {
59  QMutexLocker lock(&parsingUrlsMutex);
60 
61  // NOTE: operator[] default-initializes the ptr to zero for us when not available
62  auto& perUrlData = parsingUrls()[url];
63  if (!perUrlData) {
64  // if that was the case, we are the first to parse this url, create an entry
65  perUrlData = new PerUrlData;
66  }
67 
68  // always increment the refcount
69  ++perUrlData->ref;
70 
71  // now lock the url, but don't do so while blocking the global mutex
72  auto& mutex = perUrlData->mutex;
73  lock.unlock();
74 
75  mutex.lock();
76 }
77 
78 UrlParseLock::~UrlParseLock()
79 {
80  QMutexLocker lock(&parsingUrlsMutex);
81 
82  // find the entry for this url
83  auto& urls = parsingUrls();
84  auto it = urls.find(m_url);
85  Q_ASSERT(it != urls.end()); // it must exist
86  auto& perUrlData = it.value();
87 
88  // unlock the per-url mutex
89  perUrlData->mutex.unlock();
90 
91  // decrement the refcount
92  --perUrlData->ref;
93  if (perUrlData->ref == 0) {
94  // and cleanup, if possible
95  delete perUrlData;
96  urls.erase(it);
97  }
98 }
QMutex
urlparselock.h
KDevelop::UrlParseLock::~UrlParseLock
~UrlParseLock()
Definition: urlparselock.cpp:78
QMutexLocker::unlock
void unlock()
KDevelop::UrlParseLock::UrlParseLock
UrlParseLock(const IndexedString &url)
Definition: urlparselock.cpp:56
KDevelop
Definition: backgroundparser.h:39
QMutexLocker
QHash
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Thu Jan 14 2021 23:34:40 by doxygen 1.8.16 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kdevplatform/language/backgroundparser

Skip menu "kdevplatform/language/backgroundparser"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdevelop API Reference

Skip menu "kdevelop API Reference"
  • kdevplatform
  •   debugger
  •   documentation
  •   interfaces
  •   language
  •     assistant
  •     backgroundparser
  •     checks
  •     classmodel
  •     codecompletion
  •     codegen
  •     duchain
  •     editor
  •     highlighting
  •     interfaces
  •     util
  •   outputview
  •   project
  •   serialization
  •   shell
  •   sublime
  •   tests
  •   util
  •   vcs

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal