Double Buffering : Double-Buffered Statechart

Double-Buffered Statechart

The following Statechart helps to illustrate how the Double Buffer Analysis program works.

.

When you run the Double Buffer Analysis program on the above Statechart, it reports that you only have to double buffer data items Y and T. There is no need to double buffer X and Z.

The Double Buffer Analysis program uses the following rules to arrive at its recommendations:

In general, you need double buffering in two cases:

In both cases, the obtained result may depend on the order of execution of concurrent components.

For example, suppose that upon default the Statechart enters the states L1 and R1 (X = 6 and
Y = 10). Then, the execution of a step may produce these results:

Similarly, after performing a step that involves transition from L2 to L1 and transition from R2 to R1, data-item T have value 5 or 4 depending on the order of the components execution.

With double buffering, values assigned during a step are deferred until the end of the step so that all actions in the step are executed with values the data had at the beginning of the step. In this case, at the end of the step Y = 6 and Z = 11.

Because of double-buffering, assignments are implemented in generated code through calls to special services:

seti(&Y, X);
seti(&Z, Y+1);
 

rather than by direct C assignments:

Y = X;
Z = Y + 1;
 

The Double Buffer Analysis program also finds elements in the model for which double buffering can be safely removed. For example, in the sample Statechart, the following elements do not need double buffering:

Data-item Z is an output only, i.e., it is only assigned a value in the model, but never used in it. Therefore, there is no read-write racing. Since there is only one assignment to Z, there is also no write-write racing.
Data-item X has an assignment and use of X, but they both belong to the same component and are never executed at the same step. Therefore, X does not need double buffering.