proc_rename(), proc_forbid(), proc_permit(): New functions.
[bertos.git] / kern / sem.c
index 98646e939047200abe5cd7bf64876aaafc3812c2..9e5905f6ae4db7c34777c0a5b7d04d752ef03d97 100755 (executable)
@@ -3,7 +3,7 @@
  * <!--
  * Copyright 2001,2004 Develer S.r.l. (http://www.develer.com/)
  * Copyright 1999,2000,2001 Bernardo Innocenti <bernie@develer.com>
- * All Rights Reserved.
+ * This file is part of DevLib - See devlib/README for information.
  * -->
  *
  * \brief Semaphore based synchronization services.
  * \author Bernardo Innocenti <bernie@develer.com>
  */
 
-/*
- * $Log$
- * Revision 1.1  2004/05/23 17:27:00  bernie
- * Import kern/ subdirectory.
- *
- */
+/*#*
+ *#* $Log$
+ *#* Revision 1.4  2004/08/25 14:12:09  rasky
+ *#* Aggiornato il comment block dei log RCS
+ *#*
+ *#* Revision 1.3  2004/08/08 05:53:23  bernie
+ *#* Use DISABLE_IRQSAVE/ENABLE_IRQRESTORE; Cleanup documentation.
+ *#*
+ *#* Revision 1.2  2004/06/03 11:27:09  bernie
+ *#* Add dual-license information.
+ *#*
+ *#* Revision 1.1  2004/05/23 17:27:00  bernie
+ *#* Import kern/ subdirectory.
+ *#*
+ *#*/
 
 #include "sem.h"
 #include "proc.h"
@@ -46,10 +55,14 @@ void sem_init(struct Semaphore *s)
  *
  * \note   each call to sem_attempt() must be matched by a
  *         call to sem_release().
+ *
+ * \see sem_obtain() sem_release()
  */
 bool sem_attempt(struct Semaphore *s)
 {
-       DISABLE_INTS;
+       cpuflags_t flags;
+       DISABLE_IRQSAVE(flags);
+
        if ((!s->owner) || (s->owner == CurrentProcess))
        {
                s->owner = CurrentProcess;
@@ -57,7 +70,8 @@ bool sem_attempt(struct Semaphore *s)
                ENABLE_INTS;
                return true;
        }
-       ENABLE_INTS;
+
+       ENABLE_IRQRESTORE(flags);
        return false;
 }
 
@@ -76,30 +90,34 @@ bool sem_attempt(struct Semaphore *s)
  *       the most common case: the semaphore is free or locked
  *       by the calling process itself. Rearranging this code
  *       is probably a bad idea.
+ *
+ * \sa sem_release() sem_attempt()
  */
 void sem_obtain(struct Semaphore *s)
 {
-       DISABLE_INTS;
+       cpuflags_t flags;
+       DISABLE_IRQSAVE(flags);
 
        /* Is the semaphore already locked by another process? */
-       if (s->owner && (s->owner != CurrentProcess))
+       if (UNLIKELY(s->owner && (s->owner != CurrentProcess)))
        {
                /* Append calling process to the wait queue */
                ADDTAIL(&s->wait_queue, (Node *)CurrentProcess);
-               ENABLE_INTS;
+               ENABLE_IRQRESTORE(flags);
 
-               /* We will awake only when the current owner calls
-                * ReleaseSemaphore(). Then, the semaphore will already
+               /*
+                * We will wake up only when the current owner calls
+                * sem_release(). Then, the semaphore will already
                 * be locked for us.
                 */
                proc_schedule();
        }
        else
        {
-               /* The semaphore is free: lock it */
+               /* The semaphore was free: lock it */
                s->owner = CurrentProcess;
                s->nest_count++;
-               ENABLE_INTS;
+               ENABLE_IRQRESTORE(flags);
        }
 }
 
@@ -114,12 +132,16 @@ void sem_obtain(struct Semaphore *s)
  *       the most common case: the semaphore has been locked just
  *       once and nobody else was waiting for it. Rearranging
  *       this code is probably a bad idea.
+ *
+ * \sa sem_obtain() sem_attempt()
  */
 void sem_release(struct Semaphore *s)
 {
-       DISABLE_INTS;
+       cpuflags_t flags;
+       DISABLE_IRQSAVE(flags);
 
-       /* Decremement nesting count and check if the semaphore
+       /*
+        * Decremement nesting count and check if the semaphore
         * has been fully unlocked
         */
        if (--s->nest_count == 0)
@@ -139,6 +161,6 @@ void sem_release(struct Semaphore *s)
                }
        }
 
-       ENABLE_INTS;
+       ENABLE_IRQRESTORE(flags);
 }