編集
クイック単方向バージョン:
初期化:
init()
{
ready = 0;
done = 1;
}
ライター:
send()
{
while (!done)
sleep();
/* copy data in */
done = 0;
ready = 1;
}
読者:
poll()
{
while (1)
{
if (ready)
{
recv();
}
sleep();
}
}
recv()
{
/* copy data out */
ready = 0;
done = 1;
}
共有メモリを介してメッセージ パッシング システムを構築します (両方のプロセッサでキャッシュを解除するか、キャッシュ フラッシュ/無効化呼び出しを使用することにより、一貫性を保つ必要があります)。
共有メモリ構造には、(少なくとも) 次のフィールドが必要です。
- 現在の所有者
- メッセージがアクティブです (のように、読む必要があります)
- リクエストの使用フィールド
フローはおそらく次のようになります: (send/recv 同期が同時に実行されないと仮定)
poll()
{
/* you're better off using interrupts instead, if you have them */
while(1)
{
if (current_owner == me)
{
if (active)
{
recv();
}
else if (!request[me] && request[other])
{
request[other] = 0;
current_owner = other;
}
}
sleep();
}
}
recv()
{
/* copy data... */
active = 0;
/* check if we still want it */
if (!request[me] && request[other])
{
request[other] = 0;
current_owner = other;
}
}
send()
{
request[me] = 1;
while (current_owner != me || active)
{
sleep();
}
request[me] = 0;
/* copy data in... */
/* pass to other side */
active = 1;
current_owner = other;
}