ParserService.h 2.94 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*****************************************************************************
 * Media Library
 *****************************************************************************
 * Copyright (C) 2015 Hugo Beauzée-Luyssen, Videolabs
 *
 * Authors: Hugo Beauzée-Luyssen<hugo@beauzee.fr>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#pragma once

#include <atomic>
26
#include "compat/ConditionVariable.h"
27 28 29
#include <queue>

#include "Task.h"
30
#include "medialibrary/Types.h"
31
#include "compat/Mutex.h"
32
#include "compat/Thread.h"
33
#include "File.h"
34

35 36 37
namespace medialibrary
{

38
class IParserCb;
39
class ModificationNotifier;
40
class MediaLibrary;
41 42 43 44 45

class ParserService
{
public:
    ParserService();
46
    virtual ~ParserService() = default;
47 48 49 50

    void start();
    void pause();
    void resume();
51 52 53 54 55 56 57 58 59 60 61 62
    ///
    /// \brief signalStop Will trigger the threads for termination.
    /// This doesn't wait for the threads to be done, but ensure they won't
    /// queue another operation.
    /// This is usefull to ask all the threads to terminate asynchronously, before
    /// waiting for them to actually stop in the stop() method.
    ///
    void signalStop();
    ///
    /// \brief stop Effectively wait the the underlying threads to join.
    ///
    void stop();
63 64
    void parse( std::unique_ptr<parser::Task> t );
    void initialize( MediaLibrary* mediaLibrary, IParserCb* parserCb );
65
    bool isIdle() const;
66 67

protected:
68
    uint8_t nbNativeThreads() const;
69 70 71
    /// Can be overriden to run service dependent initializations
    virtual bool initialize();
    virtual parser::Task::Status run( parser::Task& task ) = 0;
72
    virtual const char* name() const = 0;
73
    virtual uint8_t nbThreads() const = 0;
74
    virtual bool isCompleted( const parser::Task& task ) const = 0;
75 76 77 78

private:
    // Thread(s) entry point
    void mainloop();
79
    void setIdle( bool isIdle );
80

81
protected:
82
    MediaLibrary* m_ml;
83
    IMediaLibraryCb* m_cb;
84
    std::shared_ptr<ModificationNotifier> m_notifier;
85 86

private:
87 88 89
    IParserCb* m_parserCb;
    bool m_stopParser;
    bool m_paused;
90
    bool m_idle;
91
    compat::ConditionVariable m_cond;
92
    std::queue<std::unique_ptr<parser::Task>> m_tasks;
93
    std::vector<compat::Thread> m_threads;
94
    compat::Mutex m_lock;
95
};
96 97

}