X-Git-Url: https://codewiz.org/gitweb?a=blobdiff_plain;f=bertos%2Fkern%2Fsem.c;h=df74355501e32e7e9d53f0828d596bdf0ffbcfa0;hb=f248bd5d8be37c741ef62386d82e4975723e09c3;hp=ac4e7d969bb4afa5bd505a182a16a59b9a31372f;hpb=64a3e9b44fc4b43a0f69fc580ad8c8d76067f751;p=bertos.git diff --git a/bertos/kern/sem.c b/bertos/kern/sem.c index ac4e7d96..df743555 100644 --- a/bertos/kern/sem.c +++ b/bertos/kern/sem.c @@ -32,7 +32,6 @@ * * \brief Semaphore based synchronization services. * - * \version $Id$ * \author Bernie Innocenti */ @@ -82,9 +81,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 +116,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 @@ -135,7 +134,7 @@ void sem_obtain(struct Semaphore *s) 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 +156,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 +169,6 @@ void sem_release(struct Semaphore *s) */ if (--s->nest_count == 0) { - Process *proc; - /* Disown semaphore */ s->owner = NULL; @@ -178,9 +177,10 @@ void sem_release(struct Semaphore *s) { s->nest_count = 1; s->owner = proc; - ATOMIC(SCHED_ENQUEUE(proc)); } } - proc_permit(); + + if (proc) + ATOMIC(proc_wakeup(proc)); }