<commit_before>var WriteQueue = require("./write-queue");
var Util = require("./common-util");

/*  This module provides implements a FIFO scheduler
    which assumes the existence of three types of async tasks:

    1. ordered tasks which must be executed sequentially
    2. unordered tasks which can be executed in parallel
    3. blocking tasks which must block the execution of all other tasks

    The scheduler assumes there will be many resources identified by strings,
    and that the constraints described above will only apply in the context
    of identical string ids.

    Many blocking tasks may be executed in parallel so long as they
    concern resources identified by different ids.

USAGE:

    const schedule = require("./schedule")();

    // schedule two sequential tasks using the resource 'pewpew'
    schedule.ordered('pewpew', function (next) {
        appendToFile('beep\n', next);
    });
    schedule.ordered('pewpew', function (next) {
        appendToFile('boop\n', next);
    });

    // schedule a task that can happen whenever
    schedule.unordered('pewpew', function (next) {
        displayFileSize(next);
    });

    // schedule a blocking task which will wait
    // until the all unordered tasks have completed before commencing
    schedule.blocking('pewpew', function (next) {
        deleteFile(next);
    });

    // this will be queued for after the blocking task
    schedule.ordered('pewpew', function (next) {
        appendFile('boom', next);
    });

*/

// return a uid which is not already in a map
var unusedUid = function (set) {
    var uid = Util.uid();
    if (set[uid]) { return unusedUid(); }
    return uid;
};

// return an existing session, creating one if it does not already exist
var lookup = function (map, id) {
    return (map[id] = map[id] || {
        //blocking: [],
        active: {},
        blocked: {},
    });
};

var isEmpty = function (map) {
    for (var key in map) {
        if (map.hasOwnProperty(key)) { return false; }
    }
    return true;
};

// XXX enforce asynchrony everywhere
module.exports = function () {
    // every scheduler instance has its own queue
    var queue = WriteQueue();

    // ordered tasks don't require any extra logic
    var Ordered = function (id, task) {
        queue(id, task);
    };

    // unordered and blocking tasks need a little extra state
    var map = {};

    // regular garbage collection keeps memory consumption low
    var collectGarbage = function (id) {
        // avoid using 'lookup' since it creates a session implicitly
        var local = map[id];
        // bail out if no session
        if (!local) { return; }
        // bail out if there are blocking or active tasks
        if (local.lock) { return; }
        if (!isEmpty(local.active)) { return; }
        // if there are no pending actions then delete the session
        delete map[id];
    };

    // unordered tasks run immediately if there are no blocking tasks scheduled
    // or immediately after blocking tasks finish
    var runImmediately = function (local, task) {
        // set a flag in the map of active unordered tasks
        // to prevent blocking tasks from running until you finish
        var uid = unusedUid(local.active);
        local.active[uid] = true;

        task(function () {
            // remove the flag you set to indicate that your task completed
            delete local.active[uid];
            // don't do anything if other unordered tasks are still running
            if (!isEmpty(local.active)) { return; }
            // bail out if there are no blocking tasks scheduled or ready
            if (typeof(local.waiting) !== 'function') {
                return void collectGarbage();
            }
            local.waiting();
        });
    };

    var runOnceUnblocked = function (local, task) {
        var uid = unusedUid(local.blocked);
        local.blocked[uid] = function () {
            runImmediately(local, task);
        };
    };

    // 'unordered' tasks are scheduled to run in after the most recently received blocking task
    // or immediately and in parallel if there are no blocking tasks scheduled.
    var Unordered = function (id, task) {
        var local = lookup(map, id);
        if (local.lock) { return runOnceUnblocked(local, task); }
        runImmediately(local, task);
    };

    var runBlocked = function (local) {
        for (var task in local.blocked) {
            runImmediately(local, local.blocked[task]);
        }
    };

    // 'blocking' tasks must be run alone.
    // They are queued alongside ordered tasks,<commit_msg>throw in a little asynchrony<commit_after>var WriteQueue = require("./write-queue");
var Util = require("./common-util");

/*  This module provides implements a FIFO scheduler
    which assumes the existence of three types of async tasks:

    1. ordered tasks which must be executed sequentially
    2. unordered tasks which can be executed in parallel
    3. blocking tasks which must block the execution of all other tasks

    The scheduler assumes there will be many resources identified by strings,
    and that the constraints described above will only apply in the context
    of identical string ids.

    Many blocking tasks may be executed in parallel so long as they
    concern resources identified by different ids.

USAGE:

    const schedule = require("./schedule")();

    // schedule two sequential tasks using the resource 'pewpew'
    schedule.ordered('pewpew', function (next) {
        appendToFile('beep\n', next);
    });
    schedule.ordered('pewpew', function (next) {
        appendToFile('boop\n', next);
    });

    // schedule a task that can happen whenever
    schedule.unordered('pewpew', function (next) {
        displayFileSize(next);
    });

    // schedule a blocking task which will wait
    // until the all unordered tasks have completed before commencing
    schedule.blocking('pewpew', function (next) {
        deleteFile(next);
    });

    // this will be queued for after the blocking task
    schedule.ordered('pewpew', function (next) {
        appendFile('boom', next);
    });

*/

// return a uid which is not already in a map
var unusedUid = function (set) {
    var uid = Util.uid();
    if (set[uid]) { return unusedUid(); }
    return uid;
};

// return an existing session, creating one if it does not already exist
var lookup = function (map, id) {
    return (map[id] = map[id] || {
        //blocking: [],
        active: {},
        blocked: {},
    });
};

var isEmpty = function (map) {
    for (var key in map) {
        if (map.hasOwnProperty(key)) { return false; }
    }
    return true;
};

module.exports = function () {
    // every scheduler instance has its own queue
    var queue = WriteQueue();

    // ordered tasks don't require any extra logic
    var Ordered = function (id, task) {
        queue(id, task);
    };

    // unordered and blocking tasks need a little extra state
    var map = {};

    // regular garbage collection keeps memory consumption low
    var collectGarbage = function (id) {
        // avoid using 'lookup' since it creates a session implicitly
        var local = map[id];
        // bail out if no session
        if (!local) { return; }
        // bail out if there are blocking or active tasks
        if (local.lock) { return; }
        if (!isEmpty(local.active)) { return; }
        // if there are no pending actions then delete the session
        delete map[id];
    };

    // unordered tasks run immediately if there are no blocking tasks scheduled
    // or immediately after blocking tasks finish
    var runImmediately = function (local, task) {
        // set a flag in the map of active unordered tasks
        // to prevent blocking tasks from running until you finish
        var uid = unusedUid(local.active);
        local.active[uid] = true;

        task(function () {
            // remove the flag you set to indicate that your task completed
            delete local.active[uid];
            // don't do anything if other unordered tasks are still running
            if (!isEmpty(local.active)) { return; }
            // bail out if there are no blocking tasks scheduled or ready
            if (typeof(local.waiting) !== 'function') {
                return void collectGarbage();
            }
            setTimeout(local.waiting);
        });
    };

    var runOnceUnblocked = function (local, task) {
        var uid = unusedUid(local.blocked);
        local.blocked[uid] = function () {
            runImmediately(local, task);
        };
    };

    // 'unordered' tasks are scheduled to run in after the most recently received blocking task
    // or immediately and in parallel if there are no blocking tasks scheduled.
    var Unordered = function (id, task) {
        var local = lookup(map, id);
        if (local.lock) { return runOnceUnblocked(local, task); }
        runImmediately(local, task);
    };

    var runBlocked = function (local) {
        for (var task in local.blocked) {
            runImmediately(local, local.blocked[task]);
        }
    };

    // 'blocking' tasks must be run alone.
    // They are queued alongside ordered tasks,
{'fraction_non_alphanumeric': 0.09680920338615151, 'fraction_numerical': 0.0006511829824180595, 'mean_word_length': 2.4003690036900367, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2077853', 'n_tokens_mistral': 2437, 'n_tokens_neox': 2375, 'n_words': 983}