cvra / platform-abstraction

Platform abstraction layer for microcontrollers
3 stars 6 forks source link

Critical section macros #19

Closed pierluca closed 10 years ago

pierluca commented 10 years ago

For your consideration, here are two implementations of the critical section macros.

The CRITICAL_SECTION_ENTER / CRITICAL_SECTION_EXIT macros enable for checking of correct initialization at compile-time (with a reasonably explicit error message) but essentially behave exactly as the default API. It is possible to forget the _EXIT call.

The CRITICAL_SECTION() { ... } macro overcomes that inconvenience, but I'm not sure it's such a good idea. The implementation should be somewhat efficient but it's rather ugly and adds some useless overhead to the basic calls.

Mocks / testing will follow if you approve this design. Use case is provided in the "main" function.

Sorry for the messy presentation but this was hacked together at 1.10 am based on some notes I jotted down during a long, boring meeting today :-) The for-merge version will be clean.

Stapelzeiger commented 10 years ago

Shouldn't it be possible to have a compiletime error because the CPU-SR variable isn't defined if you didn't call the alloc macro before the enter/exit macros?

pierluca commented 10 years ago

Not reliably. CPU_SR macro's implementation depends on the target architecture.

pierluca commented 10 years ago

I'm satisfied with the current state. Please review code and merge if you deem it okay.

antoinealb commented 10 years ago

Looks good to me.