Re: Can you translate the following pseudocode to Delphi ?





Hello,

Please take a look at his new code with some assembler in masm:

http://groups.google.com/group/comp.programming.threads/browse_thread/thread/538f8af9ae2258ab#


i have changed the assembler code to basm , but when i compile the
following
code , it does compile correctly but it doesn't run correctly, can you
tell
me what's going on ? is the basm assembler code correct ?

here is the new code:


----

program test;
uses windows;
{ Data Types }
type cell = record
ver:integer;
state:real;
end;
{ Global Variables/Prodecures }
{ N must be power of 2 }
const N=1000;

var cells : array[0..(N - 1)] of cell;
var head:integer;
var tail:integer;
procedure init;
var i:integer;
begin
head := 0;
tail := 0;
for i := 0 to N-1 do cells[i].ver := i;
end;

function ATOMIC_STORE(T: POINTER; C:integer ): integer;
assembler;stdcall;
asm
mov ecx, T
mov eax, C
mov [ecx], eax
end;
function ATOMIC_LOAD(T: POINTER): integer; assembler;stdcall;
asm
mov ecx, T
mov eax, [ecx]
end;
function ATOMIC_XADD(T: POINTER; C:integer ): integer;
assembler;stdcall;
asm
mov ecx, T
mov eax, C
lock xadd [ecx], eax
end;

{ Backoff/Blocking Code }
procedure backoff;
begin
sleep(0);
end;
{ Queue Ops }

procedure producer(state:real);
var ver:integer;
var c:^cell;
begin
ver := ATOMIC_XADD(@head, 1);
c := @cells[ver and (N - 1)];
while ATOMIC_LOAD(@c^.ver) <> ver do backoff;
c^.state := state;
ATOMIC_STORE(@c^.ver, ver + 1);
end;

function consumer:real;
var ver:integer;
var c:^cell;
begin
ver := ATOMIC_XADD(@tail, 1);
c := @cells[ver and (N - 1)];
while ATOMIC_LOAD(@c^.ver) <> ver + 1 do backoff;
consumer := c^.state;
ATOMIC_STORE(@c^.ver, ver + N);
end;

begin
producer(24.0);

producer(23.0);

writeln(consumer);

end.

----



On Dec 15, 11:47 pm, "Skybuck Flying" <Windows7I...@xxxxxxxxxxxxxxx>
wrote:
"

"aminer"  wrote in message

news:ebc3f9b8-930d-4d97-a95f-b8c69fbc55b8@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Hello,

I have found the following pseudo code of a very efficient fifo queue
that doen't use any CAS
just an XADD , and i want to ask you if someone can translate it to
Delphi and freepascal.

Here it is:

pseudo-code for a mpmc bounded queue of doubles:
"

If it's pseudo code then it probably will not compile so functions seem to
be missing.

type
cell = record
  ver : longword;
  state : double;
end;

head : longword;
tail : longword;

const
  N : integer = 1000;

var
  cells : array[0..N-1] of cell; // N must be a power of 2

procedure Init();
begin
   head := 0;
   tail := 0;
    for i:= 0 to N-1 do
    begin
       cells[i].ver := I;
    end;
end;

procedure producer( state : double);
var
  ver : longword;
  c : ^cell;
begin
    ver := XADD( @head, 1 );
    c := @cells[ver and (N - 1)];
    while (LOAD(c.ver) != ver) backoff();
    c.state := state;
    STORE(c.ver, ver + 1);
end;

function consumer() : double;
var
  ver : longword;
  c : ^cell;
  state : double;
begin
    ver := XADD( @tail, 1);
    c := @cells[ver and (N - 1)];
    while (LOAD(c.ver) <> ver + 1) backoff();
    state := c.state;
    STORE( c.ver, ver + N);
    result := state;
end;

Bye,
  Skybuck.

.



Relevant Pages