File indexing completed on 2024-05-19 15:27:52

0001 /*
0002     This file is part of KGraphViewer.
0003     Copyright (C) 2010  Gael de Chalendar <kleag@free.fr>
0004 
0005     This program is free software; you can redistribute it and/or
0006     modify it under the terms of the GNU General Public License as
0007     published by the Free Software Foundation; either version 2 of
0008     the License, or (at your option) any later version.
0009 
0010     This program is distributed in the hope that it will be useful,
0011     but WITHOUT ANY WARRANTY; without even the implied warranty of
0012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013     GNU General Public License for more details.
0014 
0015     You should have received a copy of the GNU General Public License
0016     along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017 
0018 */
0019 
0020 #include "loadagraphthread.h"
0021 #include "kgraphviewerlib_debug.h"
0022 
0023 #include <QDebug>
0024 
0025 void LoadAGraphThread::run()
0026 {
0027     qCDebug(KGRAPHVIEWERLIB_LOG) << m_dotFileName;
0028     FILE *fp = fopen(m_dotFileName.toUtf8().data(), "r");
0029     if (!fp) {
0030         qCWarning(KGRAPHVIEWERLIB_LOG) << "Failed to open file " << m_dotFileName;
0031         return;
0032     }
0033     m_g = agread(fp, nullptr);
0034     if (!m_g) {
0035         qCWarning(KGRAPHVIEWERLIB_LOG) << "Failed to read file, retrying to work around graphviz bug(?)";
0036         rewind(fp);
0037         m_g = agread(fp, nullptr);
0038     }
0039     if (m_g == nullptr) {
0040         qCWarning(KGRAPHVIEWERLIB_LOG) << "Failed to read file " << m_dotFileName;
0041     }
0042     fclose(fp);
0043 }
0044 
0045 void LoadAGraphThread::loadFile(const QString &dotFileName)
0046 {
0047     // FIXME: deadlock possible
0048     // if thread is still running or queued finished signal of the thread has not
0049     // yet been delivered so its handler who would release the semaphore,
0050     // then the semaphore can not be acquired and this will block the (main) thread
0051     // which called LoadAGraphThread::loadFile().
0052     // That one though very much might have also been the one which before invoked this
0053     // method and thus the still running thread or yet-to-be delivered finished signal.
0054     // But being blocked now, it will not reach its event loop where the queued finished
0055     // signal of the thread would be processed and delivered, so in the further processing
0056     // by the signal handler this semaphore would be released
0057     // -> blocked ourselves without any escape
0058     sem.acquire();
0059     m_dotFileName = dotFileName;
0060     m_g = nullptr;
0061     start();
0062 }
0063 
0064 #include "moc_loadagraphthread.cpp"