csect.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #ifndef UTL_SYS_CRIT_SECT_H
  2. #define UTL_SYS_CRIT_SECT_H
  3. #include <stdint.h>
  4. #include <string.h> // NULL
  5. #define CSECT_IMPLEMENTATION_SIMPLE 1
  6. #if CSECT_IMPLEMENTATION_SIMPLE == 0
  7. struct __sCRITICALSECTION
  8. {
  9. const uint32_t __reserved1[1];
  10. const uint16_t __reserved2[1];
  11. };
  12. typedef struct __sCRITICALSECTION xCSect_t;
  13. typedef struct __sCRITICALSECTION xCSectCnt_t;
  14. void csect_create( xCSect_t * pObj ); // create a critical section
  15. void csect_delete( xCSect_t * pObj ); // delete a critical section
  16. void csect_enter( xCSect_t * pObj ); // enter a critical section
  17. void csect_leave( xCSect_t * pObj ); // leave a critical section
  18. void csectcnt_create( xCSectCnt_t * pObj ); // create a counting critical section
  19. void csectcnt_delete( xCSectCnt_t * pObj ); // delete a counting critical section
  20. void csectcnt_enter( xCSectCnt_t * pObj ); // enter a counting critical section
  21. void csectcnt_leave( xCSectCnt_t * pObj ); // leave a counting critical section
  22. #else
  23. void csect_enter();
  24. void csect_leave();
  25. #define disable_interrupt() csect_enter()
  26. #define enable_interrupt() csect_leave()
  27. #define DI() csect_enter();
  28. #define EI() csect_leave();
  29. #define __DI__ { csect_enter();
  30. #define __EI__ csect_leave(); }
  31. #define __EI__Return__ {csect_leave(); return;} }
  32. #define __EI__ReturnArg__(arg) {csect_leave(); return(arg);} }
  33. // Usage:
  34. //
  35. // Use csect_enter/DI and csect_leave/EI as a classical critical section like
  36. // you used __disable_interrupt/__enable_interrupt.
  37. // Or use safe version __DI__/__EI__, that requires to close the scope with __DI__,
  38. // you can use it as multiline section, or single-line compact section:
  39. // __DI__ ...user-command... __EI__
  40. // or
  41. // __DI__
  42. // ..user-command-1..
  43. // ..user-command-2..
  44. // etc
  45. // __EI__
  46. #endif
  47. #endif