You might have noticed that our Spin proctypes have been getting kind of long and complicated. Can we break them up at all?
Spin doesn’t have functions. If it had functions, it would have to model the stack and do a bunch more complicated things.
Spin does have macros.
inline change_color(C) {
colors[myId] = C;
printf("Light %d changed to %d\n", myId, C);
}
Note that myId
needs to be in scope wherever the macro is used.
Spin does straightforward syntactic substitution wherever you call change_color()
inline change_color(C) {
d_step {
colors[myId] = C;
printf("Light %d changed to %d\n", myId, C);
}
}
This is sort of like an atomic
block that we saw previously. But it actually merges the lines into one deterministic step in the state machine. This simplifies the search of the finite state machine (because there will be fewer states and steps), which can drastically improve performance.
Q: Is there a difference between #define
and inline
macros?
A: Tim is not sure. It might be that #define
can’t take parameters. Normally, he just uses #define
for simple things like constants
Q: Do these macros expand recursively?
A: No.