1 /***************************************************************************
2 * Copyright 2006 Hamish Rodda <rodda@kde.org> *
3 * Copyright 2007 Alexander Dymo <adymo@kdevelop.org> *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU Library General Public License as *
7 * published by the Free Software Foundation; either version 2 of the *
8 * License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU Library General Public *
16 * License along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
28 #include <language/interfaces/ilanguagesupport.h>
32 struct LanguagePrivate
{
33 ILanguageSupport
*support
;
35 mutable QHash
<QThread
*, QMutex
*> parseMutexes
;
39 Language::Language(ILanguageSupport
*support
, QObject
*parent
)
40 : ILanguage(support
->name(), parent
)
42 kDebug() << "creating language" << support
->name();
44 d
= new LanguagePrivate
;
53 void Language::deactivate()
55 kDebug() << "deactivating language" << name();
58 void Language::activate()
60 kDebug() << "activating language" << name();
63 ILanguageSupport
*Language::languageSupport()
68 QMutex
*Language::parseMutex(QThread
*thread
) const
72 QMutexLocker
lock(&d
->mutexMutex
);
74 if (!d
->parseMutexes
.contains(thread
)) {
75 connect(thread
, SIGNAL(finished()), SLOT(threadFinished()));
76 d
->parseMutexes
.insert(thread
, new QMutex(QMutex::Recursive
));
79 return d
->parseMutexes
[thread
];
82 void Language::lockAllParseMutexes()
84 QMutexLocker
lock(&d
->mutexMutex
);
86 QList
<QMutex
*> waitForLock
;
88 // Grab the easy pickings first
89 QHashIterator
<QThread
*, QMutex
*> it
= d
->parseMutexes
;
90 while (it
.hasNext()) {
92 if (!it
.value()->tryLock())
93 waitForLock
.append(it
.value());
97 // Work through the stragglers. mutexMutex must be unlocked, else we deadlock.
98 foreach (QMutex
* mutex
, waitForLock
)
102 void Language::unlockAllParseMutexes()
104 QMutexLocker
lock(&d
->mutexMutex
);
106 foreach(QMutex
* mutex
, d
->parseMutexes
)
110 void Language::threadFinished()
114 QMutexLocker
lock(&d
->mutexMutex
);
116 QThread
* thread
= static_cast<QThread
*>(sender());
118 Q_ASSERT(d
->parseMutexes
.contains(thread
));
120 QMutex
* mutex
= d
->parseMutexes
[thread
];
123 d
->parseMutexes
.remove(thread
);
128 #include "language.moc"