Main Loop Hooks/ja

From Free Pascal wiki
Revision as of 16:27, 19 March 2014 by TheCreativeCAT (talk | contribs) (→‎Solution details: Translation added)
Jump to navigationJump to search

English (en) français (fr) 日本語 (ja) slovenčina (sk) 中文(中国大陆)‎ (zh_CN)

問題について

プログラムの中で何らかのイヴェント(あるいはソケットやパイプなど)を待たなければならなくなったとします。しかし、これをメイン (GUI) スレッドの中で行い、しかも GUI ブロックせずまたマルチスレッドにもしたくないとしたらどうしたらいいでしょうか。解決策は、新たな「ハンドル」を追加し、それをメインイヴェントループで監視することです。

解決法の詳細

ユニット LCLIntf にこれを果たすための三つの関数が追加されています。それらは:

AddEventHandler(AHandle: THandle; AFlags: dword; 
     AEventHandler: TWaitHandleEvent; AData: PtrInt): PEventHandler;
RemoveEventHandler(var AHandler: PEventHandler);
SetEventHandlerFlags(AHandler: PEventHandler; NewFlags: dword);

TWaitHandleEvent 型は、ユニット InterfaceBase の中でこう宣言されています:

TWaitHandleEvent = procedure(AData: PtrInt; AFlags: dword) of object;

AddEventHandler は、メインイヴェントループで監視できるようにハンドルを追加します。ハンドルが「シグナル」されると、AEventHandler として渡された手続きが呼び出されます。このとき、イヴェントハンドラが登録される時に渡された AData と、OS 依存の AFlags が渡されます。AddEventHandler に渡したフラグは SetEventHandlerFlags によって変更することができます。AddEventHandlerが返すハンドラ(ポインタ)は保存しておき、RemoveEventHandler によってイヴェントの監視を終了する際に同関数に渡さなければなりません。フラグが用意されている主な理由は、様々な種類のイヴェントを切り替えながら監視するための手段としてです(読む、書く、エラー)。

RemoveEventHandler は、指定のハンドルの監視を停止します。引数として AHandler をとり、これには AddEventHandler が返したポインタを渡します。RemoveEventHandler は制御を返す前に AHandler を nil に設定します。

SetEventHandlerFlags は、アクティヴなイヴェントハンドラへのフラグを変更します。現在の所、Windows では無効です(下記)が、他のプラットフォームでは利用されており、監視すべきイヴェントの種類を ON/OFF するための手段を提供しています。

Windows

The AFlags parameter in AddEventHandler is unused, and the AFlags in TWaitHandleEvent will be 0, in the current implementation.

Win32 supports the following handle types, basically the things supported by MsgWaitForMultipleObjects:

  • change notifications (for files and directories)
  • console input
  • events: signalled with SetEvent winapi function
  • mutexes: signalled when it is not owned anymore
  • processes: signalled when they terminate
  • semaphore: signalled when it's count is greater than zero
  • threads: signalled when they terminate
  • timers: signalled when expired, see SetWaitableTimer

Gtk/Unix

The AFlags parameter in AddEventHandler specifies the condition in which the handle should be signalled, with the possible values being the ones documented in the GIOCondition type in the glib reference.

In the callback, the AFlags will contain the condition, of the above referenced GIOCondition type, that was satisfied.

Gtk/unix supports the following handle types:

  • files
  • sockets
  • pipes

Note that win32 does not support sockets or pipes (at least, their use is "discouraged" by MSDN). Suggestion: use WSAEventSelect to create an event associated with a socket and pass the event handle.

Qt4

The Qt4 interface is internally different from Gtk and Win32, but has been emulated within LCL as close as possible to the GTK logic. Even the flags passed have been made compatible (GIO_IN, GIO_OUT and GIO_ERR). The only actual difference for the user is that should 2+ event types happen on the same socket "at once" the callback will not be called with an OR-ed version of the flags, but instead called twice in a row. This is a Qt4 limitation and the possibility of calling EXCEPTION event before a READ or WRITE event is to my knowledge unavoidable. The Qt4 interface was added in Lazarus v0.9.29 trunk (so it should appear in 0.9.30 stable).

Flags

Currently the FLAGS parameters are widgetset specific. On windows, this parameter is completely unused (see above), but on Gtk, Qt and others it is used to set what type of events to watch.

Pipes and Process Termination

To provide a cross-platform solution for pipes (not well supported on win32) and processes (not well supported on gtk/unix) additional functions have been added:

TChildExitReason = (cerExit, cerSignal);
TPipeReason = (prDataAvailable, prBroken, prCanWrite);
TPipeReasons = set of TPipeReason;
 
TChildExitEvent = procedure(AData: PtrInt; AReason: TChildExitReason; AInfo: dword) of object;
TPipeEvent = procedure(AData: PtrInt; AReasons: TPipeReasons) of object;

function AddProcessEventHandler(AHandle: THandle; 
  AEventHandler: TChildExitEvent; AData: PtrInt): PProcessEventHandler;
procedure RemoveProcessEventHandler(var AHandler: PProcessEventHandler);
 
function AddPipeEventHandler(AHandle: THandle; 
  AEventHandler: TPipeEvent; AData: PtrInt): PPipeEventHandler;
procedure RemovePipeEventHandler(var AHandler: PPipeEventHandler);

When a process terminates the event handler specified will be called. AInfo will contain the exit code if AReason is cerExit, or (on unix only) the termination signal if AReason is cerSignal. For gtk/unix, use the PID to watch as AHandle. Internally, a signal handler is installed to catch the SIGCHLD signal. On win32, AddEventHandler is used to watch process termination.

To watch a pipe for incoming data, pass the file descriptor (unix) or the pipe read-end handle to AddPipeEventHandler with a custom chosen event handler method as AEventHandler. On gtk/unix, AddEventHandler is called to watch the file descriptor for traffic. On win32, the message loop will timeout every 100 msecs to check all watched pipes for available data; if data is available, the event handler is called.

See also