Recent

Author Topic: Volatile  (Read 739 times)

dseligo

  • Hero Member
  • *****
  • Posts: 1196
Volatile
« on: October 05, 2022, 10:32:55 am »
In C, when you use variable in interrupt routine, you have to declare variable with 'volatile' (so it always load/save variable in SRAM):
Code: C  [Select][+][-]
  1. volatile int test;

Is something similar needed in Free pascal and if it is, how to do it?

RayoGlauco

  • Full Member
  • ***
  • Posts: 176
  • Beers: 1567
Re: Volatile
« Reply #1 on: October 05, 2022, 11:12:09 am »
To err is human, but to really mess things up, you need a computer.

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Volatile
« Reply #2 on: October 05, 2022, 11:41:37 am »
Yes there is something available in trunk/main: https://wiki.freepascal.org/FPC_New_Features_Trunk#Support_for_.22volatile.22_intrinsic

It comes with a link to an example:
https://gitlab.com/freepascal.org/fpc/source/-/blob/main/tests/test/tmt1.pp

You need trunk/main, it is not in 3.2.2 or fixes 3.2.3

Also note the remark that in the future it may be done through an attribute.
« Last Edit: October 05, 2022, 11:49:57 am by Thaddy »
Specialize a type, not a var.

dseligo

  • Hero Member
  • *****
  • Posts: 1196
Re: Volatile
« Reply #3 on: October 05, 2022, 11:48:24 am »
Thank you all, I'll test it.

ccrause

  • Hero Member
  • *****
  • Posts: 845
Re: Volatile
« Reply #4 on: October 05, 2022, 01:29:46 pm »
In C, when you use variable in interrupt routine, you have to declare variable with 'volatile' (so it always load/save variable in SRAM):
Code: C  [Select][+][-]
  1. volatile int test;

Is something similar needed in Free pascal and if it is, how to do it?
It is not (yet?) required, since FPC will reload global variables. As Thaddy mentioned, volatile is available in the development branch, so eventually it will/may be added to a stable release.  At that time it may be prudent to wrap data access with the volatile intrinsic (when necessary) to guard against overly aggressive compiler optimization interfering with the programmer's assumptions.

 

TinyPortal © 2005-2018