Recent

Author Topic: [SOLVED] what is function declocked(var l: longint) : boolean; ?  (Read 414 times)

Key-Real

  • Full Member
  • ***
  • Posts: 242
what

function declocked(var l: longint) : boolean;
procedure inclocked(var l: longint);

in the CPU rtl

do?
« Last Edit: April 22, 2024, 12:39:16 pm by Key-Real »

cdbc

  • Hero Member
  • *****
  • Posts: 1162
    • http://www.cdbc.dk
Re: what is function declocked(var l: longint) : boolean; ?
« Reply #1 on: April 22, 2024, 11:53:05 am »
Hi
AFAIK, it /tries/ to do an 'InterlockedDecrement', but uses an asm-lock in the process, mind you you, I'm not that strong in asm...
You can have a look for yourself, 'TInterfacedObject' uses it in procedure AfterConstruction', from there you can follow it to its asm-code.
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9987
  • Debugger - SynEdit - and more
    • wiki
Re: what is function declocked(var l: longint) : boolean; ?
« Reply #2 on: April 22, 2024, 12:14:59 pm »
From the code review (of one of the cpu units / not checked others).

It checks if you app is multi threaded, if not it does a normal "decrement by one". If yes, then it does a "locked" decrement.

"locked" here means: thread save.

If 2 threads do "Somevalue := Somevalue - 1" (or +1 / or other op), then both threads need to read the memory into a register, perform the op, and write it back to mem.
The following can happen:
- Thread1 read
- Thread2 read (the same value, thread 2 has not yet written back)
- Thread1 decrement register
- Thread1 write back
- Thread2 decrement register
- Thread2 write back

In the last step, Thread2 overwrites the value of Thread1. All together the value will have gone down by only ONE. Even though both threads should have decremented it by 1 and it should be down by TWO.

"locked" ops avoid that => but they are slower.

 

TinyPortal © 2005-2018