Busy working on threading and TLS, started with a stand-alone unit along
the lines of cthreads and using OS memory allocation for storing the TLS
blocks. After initializing (calling SysInitMultithreading) a block of
memory is allocated and threadvars copied.  However there appears to be a
two byte offset in the location of the threadvars after relocating them,
see test and output below.

program testTLS;
uses fthreads;

threadvar
  x, y, z: uint32;

begin
  x := $11ABCDEF;
  y := $99887766;
  z := $55443322;
  writeln('Initial x location: ', HexStr(@x), '. Value = ',
HexStr(pointer(x)));
  writeln('Initial y location: ', HexStr(@y), '. Value = ',
HexStr(pointer(y)));
  writeln('Initial z location: ', HexStr(@z), '. Value = ',
HexStr(pointer(z)));

  writeln('Initializing threads...');
  fthreads.SysInitMultithreading;

  writeln('Current x location: ', HexStr(@x), '. Value = ',
HexStr(pointer(x)));
  writeln('Current y location: ', HexStr(@y), '. Value = ',
HexStr(pointer(y)));
  writeln('Current z location: ', HexStr(@z), '. Value = ',
HexStr(pointer(z)));

  repeat  until false;
end.

Output:
Initial x location: 3FFB268C. Value = 11ABCDEF
Initial y location: 3FFB2694. Value = 99887766
Initial z location: 3FFB269C. Value = 55443322
Initializing threads...
Current x location: 3FFB7E12. Value = CDEF0000
Current y location: 3FFB7E16. Value = 776611AB
Current z location: 3FFB7E1A. Value = 33229988

This is a little endian system so the output can be explained by shifting
the pointer references back (before the actual location) by two bytes.

Somewhere between how FPC presents the pointers to the thread vars, how the
RTL access the pointers and how the new implementation allocates and
presents the storage pointer there appears to be a mismatch.  One thing I
would like to investigate is how the compiler packs the thread vars and how
the relocated data is interpreted by the RTL.

Any hints on what to look for?

Attached the fthreads unit for reference.

Best regards,
Christo

Attachment: fthreads.pas
Description: Binary data

_______________________________________________
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel

Reply via email to