From e5add3d1f8deaa6afb49ebdc40f4bcecb912a0b0 Mon Sep 17 00:00:00 2001 From: Ian Martin Date: Wed, 11 Jun 2014 18:44:59 -0400 Subject: [PATCH] Make protothread semaphores interrupt-safe. --- core/sys/pt-sem.h | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/core/sys/pt-sem.h b/core/sys/pt-sem.h index 75d66452a..27effa57a 100644 --- a/core/sys/pt-sem.h +++ b/core/sys/pt-sem.h @@ -162,9 +162,11 @@ PT_THREAD(driver_thread(struct pt *pt)) #include "sys/pt.h" struct pt_sem { - unsigned int count; + unsigned int head, tail; }; +#define PT_SEM_COUNT(s) ((s)->head - (s)->tail) + /** * Initialize a semaphore * @@ -179,7 +181,11 @@ struct pt_sem { * \param c (unsigned int) The initial count of the semaphore. * \hideinitializer */ -#define PT_SEM_INIT(s, c) (s)->count = c +#define PT_SEM_INIT(s, c) \ + do { \ + (s)->tail = 0; \ + (s)->head = (c); \ + } while(0) /** * Wait for a semaphore @@ -199,8 +205,8 @@ struct pt_sem { */ #define PT_SEM_WAIT(pt, s) \ do { \ - PT_WAIT_UNTIL(pt, (s)->count > 0); \ - --(s)->count; \ + PT_WAIT_UNTIL(pt, PT_SEM_COUNT(s) > 0); \ + ++(s)->tail; \ } while(0) /** @@ -218,7 +224,7 @@ struct pt_sem { * * \hideinitializer */ -#define PT_SEM_SIGNAL(pt, s) ++(s)->count +#define PT_SEM_SIGNAL(pt, s) (++(s)->head) #endif /* PT_SEM_H_ */