Recent

Author Topic: [Solved] Indivisibly saving a handler address  (Read 1687 times)

MarkMLl

  • Hero Member
  • *****
  • Posts: 8563
[Solved] Indivisibly saving a handler address
« on: March 07, 2024, 12:38:07 pm »
Is it possible to indivisibly save the address of a procedure, something like the commented-out lines in

Code: Pascal  [Select][+][-]
  1. procedure TBeepDemoForm.ButtonStdoutBeepClick(Sender: TObject);
  2.  
  3. var
  4.   savedBeep: TBeepHandler= @doStdoutBeep;
  5.  
  6. begin
  7. //  savedBeep := InterlockedExchange(OnBeep, PtrUInt(savedBeep));
  8.  
  9.   savedBeep := OnBeep;
  10.   OnBeep := @doStdoutBeep;
  11.   Application.ProcessMessages;
  12.   Beep();
  13.   Application.ProcessMessages;
  14.   OnBeep := savedBeep;
  15.  
  16. //  savedBeep := InterlockedExchange(OnBeep, PtrUInt(savedBeep))
  17. end;
  18.  

At present I am getting

Code: [Select]
beepdemocode.pas(193,42) Error: Call by var for arg no. 1 has to match exactly: Got "<procedure variable type of procedure;Register>" expected "LongWord"

with no significant improvement if I introduce @ etc. on the first parameter.

MarkMLl
« Last Edit: March 07, 2024, 03:20:20 pm by MarkMLl »
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

ASerge

  • Hero Member
  • *****
  • Posts: 2481
Re: Indivisibly saving a handler address
« Reply #1 on: March 07, 2024, 12:56:53 pm »
Is it possible to indivisibly save the address of a procedure, something like the commented-out lines in
Like this?
Code: Pascal  [Select][+][-]
  1. {$APPTYPE CONSOLE}
  2.  
  3. procedure PProc;
  4. begin
  5.   Writeln('P');
  6. end;
  7.  
  8. procedure QProc;
  9. begin
  10.   Writeln('Q');
  11. end;
  12.  
  13. var
  14.   P, Q: TProcedure;
  15. begin
  16.   P := @PProc;
  17.   Q := @QProc;
  18.   InterlockedExchange(Pointer(P), Pointer(Q));
  19.   P; // Write Q
  20.   Readln;
  21. end.

Thaddy

  • Hero Member
  • *****
  • Posts: 18953
  • Glad to be alive.
Re: Indivisibly saving a handler address
« Reply #2 on: March 07, 2024, 01:22:33 pm »
Recovered from removal of tumor in tongue following tongue reconstruction with a part from my leg.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8563
Re: Indivisibly saving a handler address
« Reply #3 on: March 07, 2024, 01:53:43 pm »
Is it possible to indivisibly save the address of a procedure, something like the commented-out lines in
Like this?

I'm not sure whether that helps, with OnBeep being predefined and potentially accessible to other parts of the system.

I'm not necessarily saying that I'm worried about the thread-safety of this, but I've got this gut feeling that the more that can be done to stop that procedure address being handled as two 32-bits rather than a single 64-bit.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

MarkMLl

  • Hero Member
  • *****
  • Posts: 8563
Re: Indivisibly saving a handler address
« Reply #4 on: March 07, 2024, 01:54:47 pm »
Or the volatile intrinsic but that is only in trunk.
https://wiki.freepascal.org/FPC_New_Features_Trunk#Support_for_.22volatile.22_intrinsic

Thanks for that but in the interest of keeping a test program usable on older systems I'll probably give it a miss.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

MarkMLl

  • Hero Member
  • *****
  • Posts: 8563
Re: Indivisibly saving a handler address
« Reply #5 on: March 07, 2024, 03:18:59 pm »
Fixed it. The major thing I was doing wrong was not using the special 64-bit variant of the exchange function: inexcusable in light of my criticism of Pascal's sloppy conversions. Hence with a bit of tidying up:

Code: Pascal  [Select][+][-]
  1. function InterlockedExchangeBH(var target: TBeepHandler; const source: TBeepHandler): TBeepHandler; inline;
  2.  
  3. begin
  4. {$if sizeof(TBeepHandler) = 8 }
  5.   result := TBeepHandler(InterlockedExchange64(PtrUInt(target), PtrUInt(source)))
  6. {$else                        }
  7.   result := TBeepHandler(InterlockedExchange(PtrUInt(target), PtrUInt(source)))
  8. {$endif                       }
  9. end;
  10.  
  11.  
  12. procedure TBeepDemoForm.ButtonStdoutBeepClick(Sender: TObject);
  13.  
  14. var
  15.   savedBeep: TBeepHandler = @doStdoutBeep;
  16.  
  17. begin
  18.   savedBeep := InterlockedExchangeBH(OnBeep, savedBeep);
  19.   Application.ProcessMessages;
  20.   Beep();
  21.   Application.ProcessMessages;
  22.   InterlockedExchangeBH(OnBeep, savedBeep)
  23. end;
  24.  

MarkMLl
« Last Edit: March 07, 2024, 03:34:29 pm by MarkMLl »
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Thaddy

  • Hero Member
  • *****
  • Posts: 18953
  • Glad to be alive.
Re: [Solved] Indivisibly saving a handler address
« Reply #6 on: March 07, 2024, 06:33:30 pm »
That is a bit over the top or a bug that you have to do that, since InterlockedExchange has an overload for pointers, i.e,. independent of pointer size.
https://www.freepascal.org/docs-html/rtl/system/interlockedexchange.html
Recovered from removal of tumor in tongue following tongue reconstruction with a part from my leg.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8563
Re: [Solved] Indivisibly saving a handler address
« Reply #7 on: March 07, 2024, 06:47:27 pm »
That is a bit over the top or a bug that you have to do that, since InterlockedExchange has an overload for pointers, i.e,. independent of pointer size.
https://www.freepascal.org/docs-html/rtl/system/interlockedexchange.html

Yes, I'd been referring to that. But the code shown was the only combination I could get working.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

ASerge

  • Hero Member
  • *****
  • Posts: 2481
Re: [Solved] Indivisibly saving a handler address
« Reply #8 on: March 08, 2024, 08:06:49 am »
Yes, I'd been referring to that. But the code shown was the only combination I could get working.
And what is the difference from the example that I gave earlier?

MarkMLl

  • Hero Member
  • *****
  • Posts: 8563
Re: [Solved] Indivisibly saving a handler address
« Reply #9 on: March 08, 2024, 08:40:43 am »
And what is the difference from the example that I gave earlier?

The fact that mine works with a predefined variable outside my control.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

ASerge

  • Hero Member
  • *****
  • Posts: 2481
Re: [Solved] Indivisibly saving a handler address
« Reply #10 on: March 08, 2024, 08:04:45 pm »
The fact that mine works with a predefined variable outside my control.
I still don't understand. If you had made a short example, it might have been clearer.

 

TinyPortal © 2005-2018