CS は、比較およびスワップ の以前の値と現行値が 等しいかどうかを示す FIXED BINARY(31) 値を戻します。
|
CS は、「現行」値と「以前の」値を比較します。 これらの値が等しい場合は、「現行」値を上書きして「新しい」値が コピーされ、値 0 が戻されます。 これらの値が等しくない場合は、「以前の」値を上書きして「現行」値が コピーされ、値 1 が戻されます。
したがって、CS は次の PL/I 関数としてインプリメントできますが、 この場合 CS はアトミックではなくなります。:
cs: proc( old_Addr, current_Addr, new )
returns( fixed bin(31) byvalue )
options( byvalue );
dcl old_Addr pointer;
dcl current_Addr pointer;
dcl new fixed bin(31);
dcl old fixed bin(31) based(old_addr);
dcl current fixed bin(31) based(current_addr);
if current = old then
do;
current = new;
return( 0 );
end;
else
do;
old = current;
return( 1 );
end;
end;
z/OS 上では、CS 組み込み関数は CS 命令を インプリメントしています。この関数について詳しくは、「Principles of Operations」の 付録を参照してください。
Intel 上では、CDS 組み込み関数は cmpxchg4 命令を使用します。 cmpxchg4 関数は、「現行」値、「新しい」値、および「以前の」値 のアドレスを受け取ります。 関数は元の「現行」値を戻し、「現行」値が「以前の」値と等しい場合だけ、 「現行」値を「新しい」値に更新します。
このため Intel 上では、CS 組み込み関数は 次のインライン関数によってインプリメントされます。
cs: proc( old_Addr, current_Addr, new )
returns( fixed bin(31) byvalue )
options( byvalue );
dcl old_Addr pointer;
dcl current_Addr pointer;
dcl new fixed bin(31);
dcl old fixed bin(31) based(old_addr);
dcl current fixed bin(31) based(current_addr);
if cmpxchg4( current_Addr, new, old ) = old then
do;
return( 0 );
end;
else
do;
old = current;
return( 1 );
end;
end;