Ejemplo n.º 1
0
/**
 * Sets the priority of a process.
 *
 * This allows for controlling the order of processes rather than using FIFO.
 *
 * Priority uses an ascending order where 0 > 1.
 *
 * Processes registered with a high priority will be executed before those with
 * a low or default priority.
 *
 * Process priority is handy when multiple process will execute and their order
 * is important.
 *
 * .. note::
 *
 *    This is different from an interrupt.
 *
 *    Installed interrupts will still be executed before or after a
 *    prioritized process.
 *
 * @param  integer  $priority  Priority to assign
 *
 * @param  callable|process  $process  PHP Callable or \XPSPL\Process.
 *
 * @return  object  Process
 *
 * @example
 *
 * Example #1 Basic Usage
 *
 * This installs multiple process each with a seperate ascending priority.
 *
 * .. code-block:: php
 *
 *    <?php
 *
 *    xp_signal(XP_SIG('foo'), priority(0, function(){
 *        echo 'foo';
 *    }));
 *
 *    xp_signal(XP_SIG('foo'), priority(3, function(){
 *        echo 'bar';
 *    }));
 *
 *    xp_signal(XP_SIG('foo'), priority(5, function(){
 *        echo 'hello';
 *    }));
 *
 *    xp_signal(XP_SIG('foo'), priority(10, function(){
 *        echo 'world';
 *    }));
 *
 * The above code will output.
 *
 * .. code-block:: php
 *
 *    foobarhelloworld
 */
function xp_priority($priority, $process)
{
    if (!$process instanceof \XPSPL\Process) {
        $process = new \XPSPL\Process($process);
    }
    $process->set_priority($priority);
    return $process;
}
Ejemplo n.º 2
0
}, "process construction");
unittest\test(function ($test) {
    $process = new XPSPL\Process(function () {
    });
    $test->false($process->is_exhausted());
    $process->decrement_exhaust();
    $test->false($process->is_exhausted());
    $process = new XPSPL\Process(function () {
    }, 2);
    $process->decrement_exhaust();
    $test->false($process->is_exhausted());
    $process->decrement_exhaust();
    $test->true($process->is_exhausted());
    $process = new XPSPL\Process(function () {
    }, null);
    for ($i = 0; $i != 5; $i++) {
        $process->decrement_exhaust();
    }
    $test->false($process->is_exhausted());
    $process = new XPSPL\Process(function () {
    }, 0);
    $test->true($process->is_exhausted());
}, "Process exhaustion");
unittest\test(function ($test) {
    $process = new XPSPL\Process(function () {
    });
    $process->set_priority(100);
    $test->equal(100, $process->get_priority());
    $process->set_priority('a');
    $test->equal($process->get_priority(), XPSPL_PROCESS_DEFAULT_PRIORITY);
}, "Process Priority");