Solaris 動的トレースガイド

節固有変数

D プログラムの節ごとに再利用可能な記憶域を持つ D 変数も定義できます。節固有変数は、C、C++、Java 言語プログラムの自動変数とよく似ており、1 回の関数呼び出しが完了するまで有効です。節固有変数は、すべての D プログラム変数と同じく、最初の割り当て時に作成されます。これらの変数を参照し、割り当てるには、特殊な識別子 this に、演算子 -> を適用します。

BEGIN
{
this->secs = timestamp / 1000000000;
...
}

this int x;   /* an integer clause-local variable */
this char c;  /* a character clause-local variable */

BEGIN
{
this->x = 123;
this->c = 'D';
}

例 3–2 clause.d: 節固有変数

int me;			/* an integer global variable */
this int foo;		/* an integer clause-local variable */

tick-1sec
{
/*
* Set foo to be 10 if and only if this is the first clause executed.
*/
this->foo = (me % 3 == 0) ? 10 : this->foo;
printf("Clause 1 is number %d; foo is %d\n", me++ % 3, this->foo++);
}

tick-1sec
{
/*
* Set foo to be 20 if and only if this is the first clause executed.
*/
this->foo = (me % 3 == 0) ? 20 : this->foo;
printf("Clause 2 is number %d; foo is %d\n", me++ % 3, this->foo++);
}

tick-1sec
{
/*
* Set foo to be 30 if and only if this is the first clause executed.
*/
this->foo = (me % 3 == 0) ? 30 : this->foo;
printf("Clause 3 is number %d; foo is %d\n", me++ % 3, this->foo++);
}

 # dtrace -q -s clause.d Clause 1 is number 0; foo is 10 Clause 2 is number 1; foo is 11 Clause 3 is number 2; foo is 12 Clause 1 is number 0; foo is 10 Clause 2 is number 1; foo is 11 Clause 3 is number 2; foo is 12 Clause 1 is number 0; foo is 10 Clause 2 is number 1; foo is 11 Clause 3 is number 2; foo is 12 Clause 1 is number 0; foo is 10 Clause 2 is number 1; foo is 11 Clause 3 is number 2; foo is 12 ^C