Skip to content

qt: run all medialibrary operations in a thread

Pierre Lamot requested to merge chub/vlc:qt/run-on-ml-thread-dev into master

While counting and loading the data from view was already done in a background thread, many operations where done directly on the medialibrary from the Qt thread. this MR aims to move all theses operations in a background thread

This MR offers a common pattern that allows to run ML operation in a background thread and have the result on a callback.

Code for the code that is executed on the ML thread and as callback on the UI thread is inlined with lambdas, this allows to avoid declaring structures and managing connections to get the result, which makes the code clearer in my opinion, code is declared where it's used.

sample usage:

    //structure used to pass data from the ML thread to the UI thread
    struct Ctx {
        bool success;
    };
    m_mediaLib->runOnMLThread<Ctx>(
    //reference to the object making the call, this is used to ensure that
    //the caller still exists before calling the UI callback
    this,
    //ML thread, data needed from the caller are passed in the capture of the lambda
    //capturing this or any of its member is not allowed here
    //the callback have in parametters the ml instance and a context that will be shared with
    //the UI callback  
    [url, indexed](vlc_medialibrary_t* ml, Ctx& ctx)
    {
        int res;
        if ( indexed )
            res = vlc_ml_add_folder(  ml, qtu( url ) );
        else
            res = vlc_ml_remove_folder( ml, qtu( url ) );
        ctx.success = (res == VLC_SUCCESS);
    },
    //UI callback, capturing this is allowed, (runOnMLThread will ensure that `this`
    //still exists at this point
    //the callback has as parametters the request id and the shared context
    [this, indexed](quint64, Ctx& ctx){
        if (ctx.success)
        {
            m_indexed = indexed;
            emit isIndexedChanged();
        }
    },
    //allow to specify if the task should be run on a specific thread, null (default) will run on any thread
    "ML_FOLDER_ADD_QUEUE");

updates

V2) Remove Object dependency in CoverGenerator, factorize thumbnail updated events handling, provide runOnMLThread version without UI callback

V3) Rebase fail -_-'

V4) don't call UI callback when task is canceled; fix medialib not deleted on shutdown when all tasks can be canceled; typos and nits

V5) rebase on master (after benjamin's changes), remove findInCache(int), add rejection mechanims, + minor changes

v6) Added the ability to run tasks on a dedicated thread in order to warranty that theses tasks will be executed sequentially

v7) update MLThreadPool implementation

v8) fix nits

v9) rebase on master, fix beginResetModel/endResetModel scope, delete queue when removing last tasks from MLThreadPool::tryTake, nits

v10) apply same logic as beginResetModel/endResetModel fixes for row removal/moves

Edited by Pierre Lamot

Merge request reports