Fix no newlines warnings. Patch by Peter Oberndorfer
[kdevelopdvcssupport.git] / shell / language.cpp
blob4e3c6584926ebf2d2b8eb8d9dde93cc94be7c4c4
1 /***************************************************************************
2 * Copyright 2006 Hamish Rodda <rodda@kde.org> *
3 * Copyright 2007 Alexander Dymo <adymo@kdevelop.org> *
4 * *
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. *
9 * *
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. *
14 * *
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 ***************************************************************************/
20 #include "language.h"
22 #include <QHash>
23 #include <QMutex>
24 #include <QThread>
26 #include <kdebug.h>
28 #include <language/interfaces/ilanguagesupport.h>
30 namespace KDevelop {
32 struct LanguagePrivate {
33 ILanguageSupport *support;
35 mutable QHash<QThread*, QMutex*> parseMutexes;
36 QMutex mutexMutex;
39 Language::Language(ILanguageSupport *support, QObject *parent)
40 : ILanguage(support->name(), parent)
42 kDebug() << "creating language" << support->name();
44 d = new LanguagePrivate;
45 d->support = support;
48 Language::~Language()
50 delete d;
53 void Language::deactivate()
55 kDebug() << "deactivating language" << name();
58 void Language::activate()
60 kDebug() << "activating language" << name();
63 ILanguageSupport *Language::languageSupport()
65 return d->support;
68 QMutex *Language::parseMutex(QThread *thread) const
70 Q_ASSERT(thread);
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()) {
91 it.next();
92 if (!it.value()->tryLock())
93 waitForLock.append(it.value());
96 lock.unlock();
97 // Work through the stragglers. mutexMutex must be unlocked, else we deadlock.
98 foreach (QMutex* mutex, waitForLock)
99 mutex->lock();
102 void Language::unlockAllParseMutexes()
104 QMutexLocker lock(&d->mutexMutex);
106 foreach(QMutex* mutex, d->parseMutexes)
107 mutex->unlock();
110 void Language::threadFinished()
112 Q_ASSERT(sender());
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];
121 mutex->unlock();
122 delete mutex;
123 d->parseMutexes.remove(thread);
128 #include "language.moc"