Ev

目次

Ev クラス

はじめに

Ev は静的クラスで、デフォルトのループへのアクセスや各種共通操作へのアクセスを提供します。

クラス概要

Ev
final class Ev {
/* 定数 */
const int Ev::FLAG_AUTO = 0;
const int Ev::FLAG_NOENV = 16777216;
const int Ev::FLAG_FORKCHECK = 33554432;
const int Ev::FLAG_NOINOTIFY = 1048576;
const int Ev::FLAG_SIGNALFD = 2097152;
const int Ev::FLAG_NOSIGMASK = 4194304;
const int Ev::RUN_NOWAIT = 1;
const int Ev::RUN_ONCE = 2;
const int Ev::BREAK_CANCEL = 0;
const int Ev::BREAK_ONE = 1;
const int Ev::BREAK_ALL = 2;
const int Ev::MINPRI = -2;
const int Ev::MAXPRI = 2;
const int Ev::READ = 1;
const int Ev::WRITE = 2;
const int Ev::TIMER = 256;
const int Ev::PERIODIC = 512;
const int Ev::SIGNAL = 1024;
const int Ev::CHILD = 2048;
const int Ev::STAT = 4096;
const int Ev::IDLE = 8192;
const int Ev::PREPARE = 16384;
const int Ev::CHECK = 32768;
const int Ev::EMBED = 65536;
const int Ev::CUSTOM = 16777216;
const int Ev::ERROR = 2147483648;
const int Ev::BACKEND_SELECT = 1;
const int Ev::BACKEND_POLL = 2;
const int Ev::BACKEND_EPOLL = 4;
const int Ev::BACKEND_KQUEUE = 8;
const int Ev::BACKEND_DEVPOLL = 16;
const int Ev::BACKEND_PORT = 32;
const int Ev::BACKEND_ALL = 63;
const int Ev::BACKEND_MASK = 65535;
/* メソッド */
final public static int backend()
final public static int depth()
final public static int embeddableBackends()
final public static void feedSignal( int $signum )
final public static void feedSignalEvent( int $signum )
final public static int iteration()
final public static float now()
final public static void nowUpdate()
final public static int recommendedBackends()
final public static void resume()
final public static void run( int $flags = ?)
final public static void sleep( float $seconds )
final public static void stop( int $how = ?)
final public static int supportedBackends()
final public static void suspend()
final public static float time()
final public static void verify()
}

定義済み定数

ループを作るときに渡すフラグ

Ev::FLAG_AUTO

デフォルトのフラグの値。

Ev::FLAG_NOENV

このフラグを使う (あるいは setuid や setgid でプログラムを実行する) と、 libev は環境変数 LIBEV_FLAGS を見ません。 それ以外の場合 (デフォルト) は、 LIBEV_FLAGS が見つかった場合は その内容でフラグを上書きします。パフォーマンステストやバグの調査のときに有用です。

Ev::FLAG_FORKCHECK

libev が繰り返しのたびにフォークをチェックするようにします。毎回手動で EvLoop::fork を呼ぶことがなくなります。 これを実現するために、ループ内での繰り返しの旅に getpid() を呼んでいます。 そのため、繰り返しの回数が増えるとイベントループがスローダウンするかもしれませんが、 通常は気になるほどではありません。このフラグの設定を上書きしたり、 環境変数 LIBEV_FLAGS で指定したりすることはできません。

Ev::FLAG_NOINOTIFY

このフラグを指定すると、 libev» ev_stat ウォッチャーに inotify API を使わなくなります。 このフラグは inotify ファイルディスクリプタを節約するのに便利です。 これを使わなければ、ev_stat ウォッチャーを使ったループのたびに inotify ハンドルをひとつずつ消費することになります。

Ev::FLAG_SIGNALFD

このフラグを指定すると、 libev» ev_signal (および » ev_child ) ウォッチャーに signalfd API を使おうとします。 この API はシグナルを同期的に配送し、高速に処理したり キューに入ったシグナルデータを取得できるようにしたりします。 また、スレッドを使ったときのシグナル処理も、 スレッド内でシグナルが適切にブロックされている限りはシンプルになります。 デフォルトでは signalfd を使いません。

Ev::FLAG_NOSIGMASK

このフラグを指定すると、 libev はシグナルのマスクを変更しないようにします。 具体的には、シグナルが受信されるまではブロックしないようになるということです。

これは、自前のシグナル処理をするときに便利です。 また、特定のスレッドでだけシグナルを処理するという場合にも有用です。

Ev::run あるいは EvLoop::run に渡すフラグ

Ev::RUN_NOWAIT

イベントループが新しいイベントを探し、 新しいイベントや未処理のイベントを処理するけれども、 もしイベントが見つからない場合は、最初のループですぐに呼び出し元に制御を戻します。 イベントを待つためにプロセスをブロックしたりはしません。 これは、時間のかかる計算をしているときのポーリングなどに便利です。 プログラムの動きを止めることなく新しいイベントを処理できます。

Ev::RUN_ONCE

イベントループが (必要なら待ち続けてでも) 新しいイベントを探し、 その新しいイベントや未処理のイベントを処理します。 少なくとも一つのイベントがやってくるまで (libev 自身のイベント間隔になるでしょう。 そのため、ユーザー低位のコールバックが呼ばれる保証はありません) プロセスをブロックし ループ処理の終了後に呼び出し元に制御を戻します。

Ev::stop あるいは EvLoop::stop に渡すフラグ

Ev::BREAK_CANCEL

ブレーク操作をキャンセルします。

Ev::BREAK_ONE

一番内側の Ev::run (あるいは EvLoop::run ) の呼び出しを返します。

Ev::BREAK_ALL

ネストしたすべての Ev::run (あるいは EvLoop::run ) の呼び出しを返します。

ウォッチャーの優先度

Ev::MINPRI

最小の優先度。

Ev::MAXPRI

最大の優先度。

受信したイベントのビットマスク

Ev::READ

EvIo ウォッチャーが読み込み可能になったときのファイルディスクリプタ。

Ev::WRITE

EvIo ウォッチャーが書き込み可能になったときのファイルディスクリプタ。

Ev::TIMER

EvTimer ウォッチャーがタイムアウトしました。

Ev::PERIODIC

EvPeriodic ウォッチャーがタイムアウトしました。

Ev::SIGNAL

EvSignal::__construct のコンストラクタで指定したシグナルを受信しました。

Ev::CHILD

EvChild::__construct のコンストラクタで指定した pid が、状態の変更を受信しました。

Ev::STAT

EvStat ウォッチャーで指定したパスが、その属性を変更しました。

Ev::IDLE

他のウォッチャーが何もすることがなくなったときに、 EvIdle ウォッチャーが動作します。

Ev::PREPARE

すべての EvPrepare ウォッチャーが、 Ev::run の開始前に起動しました。つまり、 イベントループがスリープしたり新しいイベントのポーリングをしたりする直前に EvPrepare ウォッチャーが起動するということになります。

Ev::CHECK

すべての EvCheck ウォッチャーが、 Ev::run が新しいイベントをとりまとめた直後 (ただし受信したイベントのコールバックをキューに入れる前) にキューに入ります。つまり、 イベントループ内で優先度が同じ (あるいはより低い) ウォッチャーより前に EvCheck ウォッチャーが起動するということになります。

Ev::EMBED

EvEmbed で指定した埋め込みイベントループのウォッチャーが注目する必要があります。

Ev::CUSTOM

まだ libev で送信したりそれ以外の使いかたをしたりはしていませんが、 libev ユーザーが ( EvWatcher::feed などで) ウォッチャーに通知するために使えます。

Ev::ERROR

指定されていないエラーが発生し、ウォッチャーが停止しました。 これが発生する原因として考えられるのは、 libev がメモリ不足でウォッチャーをきちんと開始できなかった場合や ファイルディスクリプタが閉じてしまった場合などがあります。 libev は、これらに関してはアプリケーションのバグとみなします。 詳細は » ANATOMY OF A WATCHER を参照ください。

バックエンドのフラグ

Ev::BACKEND_SELECT

select(2) バックエンド

Ev::BACKEND_POLL

poll(2) バックエンド

Ev::BACKEND_EPOLL

Linux 限定の epoll(7) バックエンド。2.6.9 以前のカーネルでもそれ以降のカーネルでも使えます。

Ev::BACKEND_KQUEUE

kqueue バックエンド。大半の BSD システムで利用します。 EvEmbed ウォッチャーを使って、 kqueue バックエンドのループを別のループに埋め込むことができます。 たとえば、 kqueue バックエンドのループを作って、 それをソケットに対してだけ使うこともできます。

Ev::BACKEND_DEVPOLL

Solaris 8 バックエンド。現時点では未実装です。

Ev::BACKEND_PORT

Solaris 10 のイベントポート機構で、規模が大きくなっても対応できます。

Ev::BACKEND_ALL

壊れているものも含めて、すべてのバックエンドを試します。 これを明示的に使うことは推奨しません。ビット演算子を使って、 Ev::BACKEND_ALL & ~ Ev::BACKEND_KQUEUE のようにするか、 Ev::recommendedBackends を使うか、あるいは何もバックエンドを指定しないようにしましょう。

Ev::BACKEND_MASK

これはバックエンドではなく、 flags の中のすべてのバックエンドビットをマスクするものです。 たとえば、環境変数 LIBEV_FLAGS を変更するときに使います。

注意:

デフォルトのループの場合は、モジュールの初期化時に Ev が (もし使えれば) pthread_atfork を使って » ev_loop_fork の呼び出しを登録します。

注意:

Ev クラスには、 デフォルトのイベントループ にアクセスするメソッドが用意されています ( Ev::iterationEv::depth など)。 カスタムループ ( EvLoop::__construct で作ったもの) の場合、これらの値にアクセスするには EvLoop クラスのプロパティやメソッドを使います。

デフォルトのイベントループ自体のインスタンスを取得するには EvLoop::defaultLoop メソッドを使います。

EvCheck クラス

はじめに

EvPrepare ウォッチャーと EvCheck ウォッチャーは、 通常はペアで使います。 EvPrepare ウォッチャーはプロセスをブロックする前に起動し、 EvCheck はブロックした後に起動します。

EvLoop::run あるいはそれに類するメソッドや関数 (現在のイベントループに入るもの) は、EvPrepare ウォッチャーや EvCheck ウォッチャーからは呼べません。 現在のループ以外のループは問題ありません。 その理由は、これらのウォッチャーの中では再帰をチェックする必要がないからです。 つまり、処理の流れは常に EvPrepare -> ブロック -> EvCheck となるので、これらのウォッチャーは常にペアで呼ばれ、ブロッキング呼び出しの前後を囲むことになります。

主な利用目的は他のイベント機構を libev に統合することで、 少し高度な使いかたになります。変数の変更を追跡したり 自前のウォッチャーを実装したり、net-snmp やコルーチンライブラリを統合したりといった、 さまざまな用途で使えます。 また、何らかのデータをキャッシュしてブロッキングの前にフラッシュするなどの用途で使えることもあります。

EvCheck には最高の優先度 (Ev::MAXPRI) を与えることを推奨します。 これで、ポーリングの後は他のあらゆるウォッチャーよりも先に実行されることが保証されます (EvPrepare ウォッチャーの場合は無鐘鋳です)。

また、EvCheck ウォッチャーはイベントをアクティブにしたり送信したりしてはいけません。 libev では対応してはいますが、 他の EvCheck ウォッチャーが仕事をする前に実行される可能性があります。

クラス概要

EvCheck
class EvCheck extends EvWatcher {
/* 継承したプロパティ */
public $is_active;
public $data;
public $is_pending;
public $priority;
/* メソッド */
public __construct( callable $callback , mixed $data = ?, int $priority = ?)
final public static object createStopped( string $callback , string $data = ?, string $priority = ?)
/* 継承したメソッド */
public int EvWatcher::clear()
public void EvWatcher::feed( int $revents )
public EvLoop EvWatcher::getLoop()
public void EvWatcher::invoke( int $revents )
public bool EvWatcher::keepalive( bool $value = ?)
public void EvWatcher::setCallback( callable $callback )
public void EvWatcher::start()
public void EvWatcher::stop()
}

The EvChild class

はじめに

EvChild ウォッチャーは、子プロセスの状態の変更 (たいていは、停止したり終了したりといったもの) に反応してこのプロセスが SIGCHLD を受け取ったときに起動します。 EvChild ウォッチャーは、子をフォークした後 (おそらくはすでに終了した後) でインストールしてもかまいません。 ただし、まだイベントループに入っていないとき (あるいはウォッチャーから引き続いているとき) に限ります。 つまり、フォークした直後に子プロセス用のウォッチャーを登録するのはかまいませんが、 フォークした後でイベントループを何回か処理した後で登録したり、次のコールバックの実行時に登録したりすることはできません。

EvChild ウォッチャーは、 デフォルトループ の中でだけ登録できます。

クラス概要

EvChild
class EvChild extends EvWatcher {
/* プロパティ */
public $pid;
public $rpid;
public $rstatus;
/* 継承したプロパティ */
public $is_active;
public $data;
public $is_pending;
public $priority;
/* メソッド */
public __construct(
     int $pid ,
     bool $trace ,
     callable $callback ,
     mixed $data = null ,
     int $priority = 0
)
final public static object createStopped(
     int $pid ,
     bool $trace ,
     callable $callback ,
     mixed $data = ?,
     int $priority = ?
)
public void set( int $pid , bool $trace )
/* 継承したメソッド */
public int EvWatcher::clear()
public void EvWatcher::feed( int $revents )
public EvLoop EvWatcher::getLoop()
public void EvWatcher::invoke( int $revents )
public bool EvWatcher::keepalive( bool $value = ?)
public void EvWatcher::setCallback( callable $callback )
public void EvWatcher::start()
public void EvWatcher::stop()
}

プロパティ

pid

読み込み専用。 このウォッチャーが注目するプロセス ID。0 の場合はあらゆるプロセス ID を表します。

rpid

読み込み専用。 状態の変更を検出するプロセスの ID。

rstatus

読み込み専用rpid のプロセス終了ステータス。

EvEmbed クラス

はじめに

イベントループを別のイベントループに埋め込むために使います。

クラス概要

EvEmbed
class EvEmbed extends EvWatcher {
/* プロパティ */
public $embed;
/* メソッド */
public __construct(
     object $other ,
     callable $callback = ?,
     mixed $data = ?,
     int $priority = ?
)
final public static void createStopped(
     object $other ,
     callable $callback = ?,
     mixed $data = ?,
     int $priority = ?
)
public void set( object $other )
public void sweep()
/* 継承したメソッド */
public int EvWatcher::clear()
public void EvWatcher::feed( int $revents )
public EvLoop EvWatcher::getLoop()
public void EvWatcher::invoke( int $revents )
public bool EvWatcher::keepalive( bool $value = ?)
public void EvWatcher::setCallback( callable $callback )
public void EvWatcher::start()
public void EvWatcher::stop()
}

プロパティ

is_active

data

is_pending

priority

embed

EvFork クラス

はじめに

Fork ウォッチャーが呼ばれるのは、fork() を検出したときです (通常は、誰かが EvLoop::fork を呼んで libev に通知します)。 起動するのは、イベントループが次の処理をブロックする前、 そして EvCheck ウォッチャーが呼ばれる前になります。 また、フォークした後の子プロセス内でだけ起動します。 誰かが間違ったプロセスで EvLoop::fork を呼んだとしても、fork ハンドラが起動してしまうことに注意しましょう。

クラス概要

EvFork
class EvFork extends EvWatcher {
/* 継承したプロパティ */
public $is_active;
public $data;
public $is_pending;
public $priority;
/* メソッド */
public __construct( callable $callback , mixed $data = null , int $priority = 0 )
final public static object createStopped( string $callback , string $data = ?, string $priority = ?)
/* 継承したメソッド */
public int EvWatcher::clear()
public void EvWatcher::feed( int $revents )
public EvLoop EvWatcher::getLoop()
public void EvWatcher::invoke( int $revents )
public bool EvWatcher::keepalive( bool $value = ?)
public void EvWatcher::setCallback( callable $callback )
public void EvWatcher::start()
public void EvWatcher::stop()
}

The EvIdle class

はじめに

EvIdle watchers trigger events when no other events of the same or higher priority are pending ( EvPrepare , EvCheck and other EvIdle watchers do not count as receiving events ).

Thus, as long as the process is busy handling sockets or timeouts(or even signals) of the same or higher priority it will not be triggered. But when the process is in idle(or only lower-priority watchers are pending), the EvIdle watchers are being called once per event loop iteration - until stopped, that is, or the process receives more events and becomes busy again with higher priority stuff.

Apart from keeping the process non-blocking(which is a useful on its own sometimes), EvIdle watchers are a good place to do "pseudo-background processing" , or delay processing stuff to after the event loop has handled all outstanding events.

The most noticeable effect is that as long as any idle watchers are active, the process will not block when waiting for new events.

クラス概要

EvIdle
class EvIdle extends EvWatcher {
/* 継承したプロパティ */
public $is_active;
public $data;
public $is_pending;
public $priority;
/* メソッド */
public __construct( callable $callback , mixed $data = ?, int $priority = ?)
final public static object createStopped( string $callback , mixed $data = ?, int $priority = ?)
/* 継承したメソッド */
public int EvWatcher::clear()
public void EvWatcher::feed( int $revents )
public EvLoop EvWatcher::getLoop()
public void EvWatcher::invoke( int $revents )
public bool EvWatcher::keepalive( bool $value = ?)
public void EvWatcher::setCallback( callable $callback )
public void EvWatcher::start()
public void EvWatcher::stop()
}

The EvIo class

はじめに

EvIo watchers check whether a file descriptor(or socket, or a stream castable to numeric file descriptor) is readable or writable in each iteration of the event loop, or, more precisely, when reading would not block the process and writing would at least be able to write some data. This behaviour is called level-triggering because events are kept receiving as long as the condition persists. To stop receiving events just stop the watcher.

The number of read and/or write event watchers per fd is unlimited. Setting all file descriptors to non-blocking mode is also usually a good idea(but not required).

Another thing to watch out for is that it is quite easy to receive false readiness notifications, i.e. the callback might be called with Ev::READ but a subsequent read() will actually block because there is no data. It is very easy to get into this situation. Thus it is best to always use non-blocking I/O: An extra read() returning EAGAIN (or similar) is far preferable to a program hanging until some data arrives.

If for some reason it is impossible to run the fd in non-blocking mode, then separately re-test whether a file descriptor is really ready. Some people additionally use SIGALRM and an interval timer, just to be sure thry won't block infinitely.

Always consider using non-blocking mode.

クラス概要

EvIo
class EvIo extends EvWatcher {
/* プロパティ */
public $fd;
public $events;
/* 継承したプロパティ */
public $is_active;
public $data;
public $is_pending;
public $priority;
/* メソッド */
public __construct(
     mixed $fd ,
     int $events ,
     callable $callback ,
     mixed $data = ?,
     int $priority = ?
)
final public static EvIo createStopped(
     mixed $fd ,
     int $events ,
     callable $callback ,
     mixed $data = null ,
     int $priority = 0
)
public void set( mixed $fd , int $events )
/* 継承したメソッド */
public int EvWatcher::clear()
public void EvWatcher::feed( int $revents )
public EvLoop EvWatcher::getLoop()
public void EvWatcher::invoke( int $revents )
public bool EvWatcher::keepalive( bool $value = ?)
public void EvWatcher::setCallback( callable $callback )
public void EvWatcher::start()
public void EvWatcher::stop()
}

プロパティ

fd

events

The EvLoop class

はじめに

Represents an event loop that is always distinct from the default loop . Unlike the default loop , it cannot handle EvChild watchers.

Having threads we have to create a loop per thread, and use the default loop in the parent thread.

The default event loop is initialized automatically by Ev . It is accessible via methods of the Ev class, or via EvLoop::defaultLoop method.

クラス概要

EvLoop
final class EvLoop {
/* プロパティ */
public $data;
public $backend;
public $is_default_loop;
public $iteration;
public $pending;
public $io_interval;
public $timeout_interval;
public $depth;
/* メソッド */
public __construct(
     int $flags = ?,
     mixed $data = NULL ,
     float $io_interval = 0.0 ,
     float $timeout_interval = 0.0
)
public int backend()
final public EvCheck check( string $callback , string $data = ?, string $priority = ?)
final public EvChild child(
     string $pid ,
     string $trace ,
     string $callback ,
     string $data = ?,
     string $priority = ?
)
public static EvLoop defaultLoop(
     int $flags = Ev::FLAG_AUTO ,
     mixed $data = NULL ,
     float $io_interval = 0. ,
     float $timeout_interval = 0.
)
final public EvEmbed embed(
     string $other ,
     string $callback = ?,
     string $data = ?,
     string $priority = ?
)
final public EvFork fork( callable $callback , mixed $data = null , int $priority = 0 )
final public EvIdle idle( callable $callback , mixed $data = null , int $priority = 0 )
public void invokePending()
final public EvIo io(
     mixed $fd ,
     int $events ,
     callable $callback ,
     mixed $data = null ,
     int $priority = 0
)
public void loopFork()
public float now()
public void nowUpdate()
final public EvPeriodic periodic(
     float $offset ,
     float $interval ,
     callable $callback ,
     mixed $data = null ,
     int $priority = 0
)
final public EvPrepare prepare( callable $callback , mixed $data = null , int $priority = 0 )
public void resume()
public void run( int $flags = 0 )
final public EvSignal signal(
     int $signum ,
     callable $callback ,
     mixed $data = null ,
     int $priority = 0
)
final public EvStat stat(
     string $path ,
     float $interval ,
     callable $callback ,
     mixed $data = null ,
     int $priority = 0
)
public void stop( int $how = ?)
public void suspend()
final public EvTimer timer(
     float $after ,
     float $repeat ,
     callable $callback ,
     mixed $data = null ,
     int $priority = 0
)
public void verify()
}

プロパティ

data

Custom data attached to loop

backend

Readonly . The backend flags indicating the event backend in use.

is_default_loop

Readonly . true if it is the default event loop.

iteration

The current iteration count of the loop. See Ev::iteration

pending

The number of pending watchers. 0 indicates that there are no watchers pending.

io_interval

Higher io_interval allows libev to spend more time collecting EvIo events, so more events can be handled per iteration, at the cost of increasing latency. Timeouts (both EvPeriodic and EvTimer ) will not be affected. Setting this to a non-zero value will introduce an additional sleep() call into most loop iterations. The sleep time ensures that libev will not poll for EvIo events more often than once per this interval, on average. Many programs can usually benefit by setting the io_interval to a value near 0.1 , which is often enough for interactive servers(not for games). It usually doesn't make much sense to set it to a lower value than 0.01 , as this approaches the timing granularity of most systems.

See also » FUNCTIONS CONTROLLING EVENT LOOPS .

timeout_interval

Higher timeout_interval allows libev to spend more time collecting timeouts, at the expense of increased latency/jitter/inexactness(the watcher callback will be called later). EvIo watchers will not be affected. Setting this to a non-null value will not introduce any overhead in libev . See also » FUNCTIONS CONTROLLING EVENT LOOPS .

depth

The recursion depth. See Ev::depth .

The EvPeriodic class

はじめに

Periodic watchers are also timers of a kind, but they are very versatile.

Unlike EvTimer , EvPeriodic watchers are not based on real time(or relative time, the physical time that passes) but on wall clock time(absolute time, calendar or clock). The difference is that wall clock time can run faster or slower than real time, and time jumps are not uncommon(e.g. when adjusting it).

EvPeriodic watcher can be configured to trigger after some specific point in time. For example, if an EvPeriodic watcher is configured to trigger "in 10 seconds" (e.g. EvLoop::now + 10.0 , i.e. an absolute time, not a delay), and the system clock is reset to January of the previous year , then it will take a year or more to trigger the event (unlike an EvTimer , which would still trigger roughly 10 seconds after starting it as it uses a relative timeout).

As with timers, the callback is guaranteed to be invoked only when the point in time where it is supposed to trigger has passed. If multiple timers become ready during the same loop iteration then the ones with earlier time-out values are invoked before ones with later time-out values (but this is no longer true when a callback calls EvLoop::run recursively).

クラス概要

EvPeriodic
class EvPeriodic extends EvWatcher {
/* プロパティ */
public $offset;
public $interval;
/* 継承したプロパティ */
public $is_active;
public $data;
public $is_pending;
public $priority;
/* メソッド */
public __construct(
     float $offset ,
     string $interval ,
     callable $reschedule_cb ,
     callable $callback ,
     mixed $data = null ,
     int $priority = 0
)
public void again()
public float at()
final public static EvPeriodic createStopped(
     float $offset ,
     float $interval ,
     callable $reschedule_cb ,
     callable $callback ,
     mixed $data = null ,
     int $priority = 0
)
public void set( float $offset , float $interval )
/* 継承したメソッド */
public int EvWatcher::clear()
public void EvWatcher::feed( int $revents )
public EvLoop EvWatcher::getLoop()
public void EvWatcher::invoke( int $revents )
public bool EvWatcher::keepalive( bool $value = ?)
public void EvWatcher::setCallback( callable $callback )
public void EvWatcher::start()
public void EvWatcher::stop()
}

プロパティ

offset

When repeating, this contains the offset value, otherwise this is the absolute point in time(the offset value passed to EvPeriodic::set , although libev might modify this value for better numerical stability).

interval

The current interval value. Can be modified any time, but changes only take effect when the periodic timer fires or EvPeriodic::again is being called.

EvPrepare クラス

はじめに

EvPrepare ウォッチャーと EvCheck ウォッチャーは、 通常はペアで使います。 EvPrepare ウォッチャーはプロセスをブロックする前に起動し、 EvCheck はブロックした後に起動します。

EvLoop::run あるいはそれに類するメソッドや関数 (現在のイベントループに入るもの) は、EvPrepare ウォッチャーや EvCheck ウォッチャーからは呼べません。 現在のループ以外のループは問題ありません。 その理由は、これらのウォッチャーの中では再帰をチェックする必要がないからです。 つまり、処理の流れは常に EvPrepare -> ブロック -> EvCheck となるので、これらのウォッチャーは常にペアで呼ばれ、ブロッキング呼び出しの前後を囲むことになります。

主な利用目的は他のイベント機構を libev に統合することで、 少し高度な使いかたになります。変数の変更を追跡したり 自前のウォッチャーを実装したり、net-snmp やコルーチンライブラリを統合したりといった、 さまざまな用途で使えます。 また、何らかのデータをキャッシュしてブロッキングの前にフラッシュするなどの用途で使えることもあります。

EvCheck には最高の優先度 (Ev::MAXPRI) を与えることを推奨します。 これで、ポーリングの後は他のあらゆるウォッチャーよりも先に実行されることが保証されます (EvPrepare ウォッチャーの場合は無鐘鋳です)。

また、EvCheck ウォッチャーはイベントをアクティブにしたり送信したりしてはいけません。 libev では対応してはいますが、 他の EvCheck ウォッチャーが仕事をする前に実行される可能性があります。

クラス概要

EvPrepare
class EvPrepare extends EvWatcher {
/* 継承したプロパティ */
public $is_active;
public $data;
public $is_pending;
public $priority;
/* メソッド */
public __construct( string $callback , string $data = ?, string $priority = ?)
final public static EvPrepare createStopped( callable $callback , mixed $data = null , int $priority = 0 )
/* 継承したメソッド */
public int EvWatcher::clear()
public void EvWatcher::feed( int $revents )
public EvLoop EvWatcher::getLoop()
public void EvWatcher::invoke( int $revents )
public bool EvWatcher::keepalive( bool $value = ?)
public void EvWatcher::setCallback( callable $callback )
public void EvWatcher::start()
public void EvWatcher::stop()
}

The EvSignal class

はじめに

EvSignal watchers will trigger an event when the process receives a specific signal one or more times. Even though signals are very asynchronous, libev will try its best to deliver signals synchronously, i.e. as part of the normal event processing, like any other event.

There is no limit for the number of watchers for the same signal, but only within the same loop, i.e. one can watch for SIGINT in the default loop and for SIGIO in another loop, but it is not allowed to watch for SIGINT in both the default loop and another loop at the same time. At the moment, SIGCHLD is permanently tied to the default loop.

If possible and supported, libev will install its handlers with SA_RESTART (or equivalent) behaviour enabled, so system calls should not be unduly interrupted. In case of a problem with system calls getting interrupted by signals, all the signals can be blocked in an EvCheck watcher and unblocked in a EvPrepare watcher.

クラス概要

EvSignal
class EvSignal extends EvWatcher {
/* プロパティ */
public $signum;
/* 継承したプロパティ */
public $is_active;
public $data;
public $is_pending;
public $priority;
/* メソッド */
public __construct(
     int $signum ,
     callable $callback ,
     mixed $data = null ,
     int $priority = 0
)
final public static EvSignal createStopped(
     int $signum ,
     callable $callback ,
     mixed $data = null ,
     int $priority = 0
)
public void set( int $signum )
/* 継承したメソッド */
public int EvWatcher::clear()
public void EvWatcher::feed( int $revents )
public EvLoop EvWatcher::getLoop()
public void EvWatcher::invoke( int $revents )
public bool EvWatcher::keepalive( bool $value = ?)
public void EvWatcher::setCallback( callable $callback )
public void EvWatcher::start()
public void EvWatcher::stop()
}

プロパティ

signum

Signal number. See the constants exported by pcntl extension. See also signal(7) man page.

The EvStat class

はじめに

EvStat monitors a file system path for attribute changes. It calls stat() on that path in regular intervals (or when the OS signals it changed) and sees if it changed compared to the last time, invoking the callback if it did.

The path does not need to exist: changing from "path exists" to "path does not exist" is a status change like any other. The condition "path does not exist" is signified by the 'nlink' item being 0(returned by EvStat::attr method).

The path must not end in a slash or contain special components such as '.' or .. . The path should be absolute: if it is relative and the working directory changes, then the behaviour is undefined.

Since there is no portable change notification interface available, the portable implementation simply calls stat() regularly on the path to see if it changed somehow. For this case a recommended polling interval can be specified. If one specifies a polling interval of 0.0 (highly recommended) then a suitable, unspecified default value will be used(which could be expected to be around 5 seconds, although this might change dynamically). libev will also impose a minimum interval which is currently around 0.1 , but that’s usually overkill.

This watcher type is not meant for massive numbers of EvStat watchers, as even with OS-supported change notifications, this can be resource-intensive.

クラス概要

EvStat
class EvStat extends EvWatcher {
/* プロパティ */
public $path;
public $interval;
/* 継承したプロパティ */
public $is_active;
public $data;
public $is_pending;
public $priority;
/* メソッド */
public __construct(
     string $path ,
     float $interval ,
     callable $callback ,
     mixed $data = null ,
     int $priority = 0
)
public array attr()
final public static void createStopped(
     string $path ,
     float $interval ,
     callable $callback ,
     mixed $data = null ,
     int $priority = 0
)
public void prev()
public void set( string $path , float $interval )
public bool stat()
/* 継承したメソッド */
public int EvWatcher::clear()
public void EvWatcher::feed( int $revents )
public EvLoop EvWatcher::getLoop()
public void EvWatcher::invoke( int $revents )
public bool EvWatcher::keepalive( bool $value = ?)
public void EvWatcher::setCallback( callable $callback )
public void EvWatcher::start()
public void EvWatcher::stop()
}

プロパティ

interval

Readonly . Hint on how quickly a change is expected to be detected and should normally be specified as 0.0 to let libev choose a suitable value.

path

Readonly . The path to wait for status changes on.

The EvTimer class

はじめに

EvTimer watchers are simple relative timers that generate an event after a given time, and optionally repeating in regular intervals after that.

The timers are based on real time, that is, if one registers an event that times out after an hour and resets the system clock to January last year , it will still time out after(roughly) one hour. "Roughly" because detecting time jumps is hard, and some inaccuracies are unavoidable.

The callback is guaranteed to be invoked only after its timeout has passed (not at, so on systems with very low-resolution clocks this might introduce a small delay). If multiple timers become ready during the same loop iteration then the ones with earlier time-out values are invoked before ones of the same priority with later time-out values (but this is no longer true when a callback calls EvLoop::run recursively).

The timer itself will do a best-effort at avoiding drift, that is, if a timer is configured to trigger every 10 seconds, then it will normally trigger at exactly 10 second intervals. If, however, the script cannot keep up with the timer because it takes longer than those 10 seconds to do) the timer will not fire more than once per event loop iteration.

クラス概要

EvTimer
class EvTimer extends EvWatcher {
/* プロパティ */
public $repeat;
public $remaining;
/* 継承したプロパティ */
public $is_active;
public $data;
public $is_pending;
public $priority;
/* メソッド */
public __construct(
     float $after ,
     float $repeat ,
     callable $callback ,
     mixed $data = null ,
     int $priority = 0
)
public void again()
final public static EvTimer createStopped(
     float $after ,
     float $repeat ,
     callable $callback ,
     mixed $data = null ,
     int $priority = 0
)
public void set( float $after , float $repeat )
/* 継承したメソッド */
public int EvWatcher::clear()
public void EvWatcher::feed( int $revents )
public EvLoop EvWatcher::getLoop()
public void EvWatcher::invoke( int $revents )
public bool EvWatcher::keepalive( bool $value = ?)
public void EvWatcher::setCallback( callable $callback )
public void EvWatcher::start()
public void EvWatcher::stop()
}

プロパティ

repeat

If repeat is 0.0 , then it will automatically be stopped once the timeout is reached. If it is positive, then the timer will automatically be configured to trigger again every repeat seconds later, until stopped manually.

remaining

Returns the remaining time until a timer fires. If the timer is active, then this time is relative to the current event loop time, otherwise it's the timeout value currently configured.

That is, after instanciating an EvTimer with an after value of 5.0 and repeat value of 7.0 , remaining returns 5.0 . When the timer is started and one second passes, remaining will return 4.0 . When the timer expires and is restarted, it will return roughly 7.0 (likely slightly less as callback invocation takes some time too), and so on.

The EvWatcher class

はじめに

EvWatcher is a base class for all watchers( EvCheck , EvChild etc.). Since EvWatcher 's constructor is abstract , one can't(and don't need to) create EvWatcher objects directly.

クラス概要

EvWatcher
abstract class EvWatcher {
/* プロパティ */
public $is_active;
public $data;
public $is_pending;
public $priority;
/* メソッド */
abstract public __construct()
public int clear()
public void feed( int $revents )
public EvLoop getLoop()
public void invoke( int $revents )
public bool keepalive( bool $value = ?)
public void setCallback( callable $callback )
public void start()
public void stop()
}

プロパティ

is_active

Readonly . true if the watcher is active. false otherwise.

data

User custom data associated with the watcher

is_pending

Readonly .true if the watcher is pending, i.e. it has outstanding events, but its callback has not yet been invoked. false otherwise. As long, as a watcher is pending(but not active), one must not change its priority.

priority

int between Ev::MINPRI and Ev::MAXPRI . Pending watchers with higher priority will be invoked before watchers with lower priority, but priority will not keep watchers from being executed(except for EvIdle watchers). EvIdle watchers provide functionality to suppress invocation when higher priority events are pending.