Blame cli/threadexecutor.h

Packit 2035a7
/*
Packit 2035a7
 * Cppcheck - A tool for static C/C++ code analysis
Packit 2035a7
 * Copyright (C) 2007-2018 Cppcheck team.
Packit 2035a7
 *
Packit 2035a7
 * This program is free software: you can redistribute it and/or modify
Packit 2035a7
 * it under the terms of the GNU General Public License as published by
Packit 2035a7
 * the Free Software Foundation, either version 3 of the License, or
Packit 2035a7
 * (at your option) any later version.
Packit 2035a7
 *
Packit 2035a7
 * This program is distributed in the hope that it will be useful,
Packit 2035a7
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 2035a7
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 2035a7
 * GNU General Public License for more details.
Packit 2035a7
 *
Packit 2035a7
 * You should have received a copy of the GNU General Public License
Packit 2035a7
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
Packit 2035a7
 */
Packit 2035a7
Packit 2035a7
#ifndef THREADEXECUTOR_H
Packit 2035a7
#define THREADEXECUTOR_H
Packit 2035a7
Packit 2035a7
#include "errorlogger.h"
Packit 2035a7
#include "importproject.h"
Packit 2035a7
Packit 2035a7
#include <cstddef>
Packit 2035a7
#include <list>
Packit 2035a7
#include <map>
Packit 2035a7
#include <string>
Packit 2035a7
Packit 2035a7
#if (defined(__GNUC__) || defined(__sun)) && !defined(__MINGW32__)
Packit 2035a7
#define THREADING_MODEL_FORK
Packit 2035a7
#elif defined(_WIN32)
Packit 2035a7
#define THREADING_MODEL_WIN
Packit 2035a7
#include <windows.h>
Packit 2035a7
#endif
Packit 2035a7
Packit 2035a7
class Settings;
Packit 2035a7
Packit 2035a7
/// @addtogroup CLI
Packit 2035a7
/// @{
Packit 2035a7
Packit 2035a7
/**
Packit 2035a7
 * This class will take a list of filenames and settings and check then
Packit 2035a7
 * all files using threads.
Packit 2035a7
 */
Packit 2035a7
class ThreadExecutor : public ErrorLogger {
Packit 2035a7
public:
Packit 2035a7
    ThreadExecutor(const std::map<std::string, std::size_t> &files, Settings &settings, ErrorLogger &errorLogger);
Packit 2035a7
    virtual ~ThreadExecutor();
Packit 2035a7
    unsigned int check();
Packit 2035a7
Packit 2035a7
    virtual void reportOut(const std::string &outmsg);
Packit 2035a7
    virtual void reportErr(const ErrorLogger::ErrorMessage &msg;;
Packit 2035a7
    virtual void reportInfo(const ErrorLogger::ErrorMessage &msg;;
Packit 2035a7
Packit 2035a7
    /**
Packit 2035a7
     * @brief Add content to a file, to be used in unit testing.
Packit 2035a7
     *
Packit 2035a7
     * @param path File name (used as a key to link with real file).
Packit 2035a7
     * @param content If the file would be a real file, this should be
Packit 2035a7
     * the content of the file.
Packit 2035a7
     */
Packit 2035a7
    void addFileContent(const std::string &path, const std::string &content);
Packit 2035a7
Packit 2035a7
private:
Packit 2035a7
    const std::map<std::string, std::size_t> &_files;
Packit 2035a7
    Settings &_settings;
Packit 2035a7
    ErrorLogger &_errorLogger;
Packit 2035a7
    unsigned int _fileCount;
Packit 2035a7
Packit 2035a7
#if defined(THREADING_MODEL_FORK)
Packit 2035a7
Packit 2035a7
    /** @brief Key is file name, and value is the content of the file */
Packit 2035a7
    std::map<std::string, std::string> _fileContents;
Packit 2035a7
private:
Packit 2035a7
    enum PipeSignal {REPORT_OUT='1',REPORT_ERROR='2', REPORT_INFO='3', CHILD_END='4'};
Packit 2035a7
Packit 2035a7
    /**
Packit 2035a7
     * Read from the pipe, parse and handle what ever is in there.
Packit 2035a7
     *@return -1 in case of error
Packit 2035a7
     *         0 if there is nothing in the pipe to be read
Packit 2035a7
     *         1 if we did read something
Packit 2035a7
     */
Packit 2035a7
    int handleRead(int rpipe, unsigned int &result);
Packit 2035a7
    void writeToPipe(PipeSignal type, const std::string &data);
Packit 2035a7
    /**
Packit 2035a7
     * Write end of status pipe, different for each child.
Packit 2035a7
     * Not used in master process.
Packit 2035a7
     */
Packit 2035a7
    std::list<std::string> _errorList;
Packit 2035a7
    int _wpipe;
Packit 2035a7
Packit 2035a7
    /**
Packit 2035a7
     * @brief Check load average condition
Packit 2035a7
     * @param nchildren - count of currently ran children
Packit 2035a7
     * @return true - if new process can be started
Packit 2035a7
     */
Packit 2035a7
    bool checkLoadAverage(size_t nchildren);
Packit 2035a7
Packit 2035a7
public:
Packit 2035a7
    /**
Packit 2035a7
     * @return true if support for threads exist.
Packit 2035a7
     */
Packit 2035a7
    static bool isEnabled() {
Packit 2035a7
        return true;
Packit 2035a7
    }
Packit 2035a7
Packit 2035a7
#elif defined(THREADING_MODEL_WIN)
Packit 2035a7
Packit 2035a7
private:
Packit 2035a7
    enum MessageType {REPORT_ERROR, REPORT_INFO};
Packit 2035a7
Packit 2035a7
    std::map<std::string, std::string> _fileContents;
Packit 2035a7
    std::map<std::string, std::size_t>::const_iterator _itNextFile;
Packit 2035a7
    std::list<ImportProject::FileSettings>::const_iterator _itNextFileSettings;
Packit 2035a7
    std::size_t _processedFiles;
Packit 2035a7
    std::size_t _totalFiles;
Packit 2035a7
    std::size_t _processedSize;
Packit 2035a7
    std::size_t _totalFileSize;
Packit 2035a7
    CRITICAL_SECTION _fileSync;
Packit 2035a7
Packit 2035a7
    std::list<std::string> _errorList;
Packit 2035a7
    CRITICAL_SECTION _errorSync;
Packit 2035a7
Packit 2035a7
    CRITICAL_SECTION _reportSync;
Packit 2035a7
Packit 2035a7
    void report(const ErrorLogger::ErrorMessage &msg, MessageType msgType);
Packit 2035a7
Packit 2035a7
    static unsigned __stdcall threadProc(void*);
Packit 2035a7
Packit 2035a7
public:
Packit 2035a7
    /**
Packit 2035a7
     * @return true if support for threads exist.
Packit 2035a7
     */
Packit 2035a7
    static bool isEnabled() {
Packit 2035a7
        return true;
Packit 2035a7
    }
Packit 2035a7
#else
Packit 2035a7
public:
Packit 2035a7
    /**
Packit 2035a7
     * @return true if support for threads exist.
Packit 2035a7
     */
Packit 2035a7
    static bool isEnabled() {
Packit 2035a7
        return false;
Packit 2035a7
    }
Packit 2035a7
#endif
Packit 2035a7
Packit 2035a7
private:
Packit 2035a7
    /** disabled copy constructor */
Packit 2035a7
    ThreadExecutor(const ThreadExecutor &);
Packit 2035a7
Packit 2035a7
    /** disabled assignment operator */
Packit 2035a7
    void operator=(const ThreadExecutor &);
Packit 2035a7
};
Packit 2035a7
Packit 2035a7
/// @}
Packit 2035a7
Packit 2035a7
#endif // THREADEXECUTOR_H