Description
The generated C code takes some care to copy the values of all the "external" streams from shared global variables to private globals before beginning the remainder of its work. This is intended as a defense against concurrent or interrupt-handling code modifying the variables. According to the Copilot 3 manual:
Copying of the external variables is necessary to make the code reentrant: it can continue
its original execution after being interrupted. If this was not done, the execution of a monitor
might be interrupted and resumed after variables used by the monitor have been updated.
This can cause the same conceptual value to be different within one execution step, breaking
the assumption of causality, which is disastrous from the point of view of reliability.
This copying step is necessary, but not sufficient. At a minimum, the external global variables should be marked volatile
, which indicates to the compiler that loads and stores involving these variables must not be hoisted, omitted or delayed. Probably these initial copies should also (optionally) be wrapped in a critical section to allow operating environments to mask interrupts or take a mutex while the copies are occurring.
I suggest we add an option to CSettings
, which, when set, will wrap this initial copying process in functions such as step_start_critical_section()
and step_end_critical_section()
which are intended to be implemented by the environment to take whatever steps might necessary to ensure reliable operation.