X-Git-Url: https://codewiz.org/gitweb?a=blobdiff_plain;f=bertos%2Fkern%2Fsem.c;h=3c645e957ae0723f2b863fb6a0c395eab64a7f17;hb=35be7aa348f671b0ed50ee43d903444edc4892af;hp=0f31fa067b9fff651945b60076adaf3e855e05f4;hpb=791e167e053bdd9250d34a9a5ccae6ccde4d6679;p=bertos.git diff --git a/bertos/kern/sem.c b/bertos/kern/sem.c index 0f31fa06..3c645e95 100644 --- a/bertos/kern/sem.c +++ b/bertos/kern/sem.c @@ -27,22 +27,23 @@ * the GNU General Public License. * * Copyright 2001, 2004 Develer S.r.l. (http://www.develer.com/) - * Copyright 1999, 2000, 2001 Bernardo Innocenti - * + * Copyright 1999, 2000, 2001 Bernie Innocenti * --> * * \brief Semaphore based synchronization services. * * \version $Id$ - * - * \author Bernardo Innocenti + * \author Bernie Innocenti */ #include "sem.h" +#include + +#include // ASSERT_IRQ_DISABLED() + #include #include #include -#include INLINE void sem_verify(struct Semaphore *s) { @@ -53,7 +54,6 @@ INLINE void sem_verify(struct Semaphore *s) ASSERT(s->nest_count < 128); // heuristic max } - /** * \brief Initialize a Semaphore structure. */ @@ -82,9 +82,9 @@ bool sem_attempt(struct Semaphore *s) proc_forbid(); sem_verify(s); - if ((!s->owner) || (s->owner == CurrentProcess)) + if ((!s->owner) || (s->owner == current_process)) { - s->owner = CurrentProcess; + s->owner = current_process; s->nest_count++; result = true; } @@ -117,10 +117,10 @@ void sem_obtain(struct Semaphore *s) sem_verify(s); /* Is the semaphore already locked by another process? */ - if (UNLIKELY(s->owner && (s->owner != CurrentProcess))) + if (UNLIKELY(s->owner && (s->owner != current_process))) { /* Append calling process to the wait queue */ - ADDTAIL(&s->wait_queue, (Node *)CurrentProcess); + ADDTAIL(&s->wait_queue, (Node *)current_process); /* * We will wake up only when the current owner calls @@ -128,14 +128,14 @@ void sem_obtain(struct Semaphore *s) * be locked for us. */ proc_permit(); - proc_schedule(); + proc_switch(); } else { ASSERT(LIST_EMPTY(&s->wait_queue)); /* The semaphore was free: lock it */ - s->owner = CurrentProcess; + s->owner = current_process; s->nest_count++; proc_permit(); } @@ -157,10 +157,12 @@ void sem_obtain(struct Semaphore *s) */ void sem_release(struct Semaphore *s) { + Process *proc = NULL; + proc_forbid(); sem_verify(s); - ASSERT(s->owner == CurrentProcess); + ASSERT(s->owner == current_process); /* * Decrement nesting count and check if the semaphore @@ -168,8 +170,6 @@ void sem_release(struct Semaphore *s) */ if (--s->nest_count == 0) { - Process *proc; - /* Disown semaphore */ s->owner = NULL; @@ -178,9 +178,10 @@ void sem_release(struct Semaphore *s) { s->nest_count = 1; s->owner = proc; - SCHED_ENQUEUE(proc); } } - proc_permit(); + + if (proc) + ATOMIC(proc_wakeup(proc)); }