Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2020-07

Problem with thread local storage for xtensa-freertos

2 messages · 1 person ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Christo Crause

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

Reply by email

Christo Crause
to Christo Crause

It was an alignment error. I thought the alignment check was active in SysInitThreadvar but a) it was commented out, and b) the ifdef check was incorrect anyway.

AM Christo Crause wrote 7 lines
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.

Reply by email