tpunt / phactor

An implementation of the Actor model for PHP
BSD 3-Clause "New" or "Revised" License
61 stars 5 forks source link

Internal Non-Blocking APIs #12

Open tpunt opened 6 years ago

tpunt commented 6 years ago

Work has begun on the internal non-blocking APIs (see the non-blocking-io branch).

These APIs will allow for the automatic interruption of an actor when an IO operation is performed, where the actor will be placed into a blocked state. Once the IO operation has been performed, the actor will be rescheduled to continue its execution. This control flow should be seamless to the developer, and will enable for CPU usage to be maximised by allowing for other actors to continue executing (when previously the thread would be blocked by IO-blocking operations).

libuv is being utilised for these APIs. I have not yet decided whether to bundle it in with this extension's source code, or make it as another external dependency (like pthreads).

The currently planned APIs include:

<?php

namespace phactor;

class FileHandle
{
    function read(int $length) : string;
    function write(string $data) : bool;
}

Example:

<?php

use phactor\{ActorSystem, Actor, ActorRef, FileHandle};

class Test extends Actor
{
    public function receive()
    {
        try {
            $fh = new FileHandle(__FILE__); // causes a context switch to open the file
        } catch (Throwable $t) {
            var_dump($t->getMessage());
        }
        ActorSystem::shutdown();
    }
}

$actorSystem = new ActorSystem();

new ActorRef(Test::class);