Back to home page

Quest Cross Reference

 
 

    


Warning, cross-references for /kernel/smp/semaphore.c need to be fixed.

0001 /*                    The Quest Operating System
0002  *  Copyright (C) 2005-2010  Richard West, Boston University
0003  *
0004  *  This program is free software: you can redistribute it and/or modify
0005  *  it under the terms of the GNU General Public License as published by
0006  *  the Free Software Foundation, either version 3 of the License, or
0007  *  (at your option) any later version.
0008  *
0009  *  This program is distributed in the hope that it will be useful,
0010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0012  *  GNU General Public License for more details.
0013  *
0014  *  You should have received a copy of the GNU General Public License
0015  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
0016  */
0017 
0018 #include "kernel.h"
0019 #include "smp/spinlock.h"
0020 #include "sched/sched.h"
0021 
0022 int
0023 semaphore_init (semaphore * sem, int max, int init)
0024 {
0025   sem->s = init;
0026   sem->max = max;
0027   sem->waitqueue = 0;
0028   spinlock_init (&sem->lock);
0029   return 0;
0030 }
0031 
0032 int
0033 semaphore_signal (semaphore * sem, int s)
0034 {
0035   int status = 0;
0036   spinlock_lock (&sem->lock);
0037   sem->s += s;
0038   if (sem->s > sem->max) {
0039     sem->s = sem->max;
0040     status = -1;
0041   }
0042   /* wake up waiters */
0043   wakeup_queue (&sem->waitqueue);
0044   spinlock_unlock (&sem->lock);
0045   return status;
0046 }
0047 
0048 /* timeout: millisec, (-1) for indefinite */
0049 int
0050 semaphore_wait (semaphore * sem, int s, s16 timeout)
0051 {
0052   for (;;) {
0053     spinlock_lock (&sem->lock);
0054     if (sem->s >= s) {
0055       sem->s -= s;
0056       spinlock_unlock (&sem->lock);
0057       return 0;
0058     } else {
0059       queue_append (&sem->waitqueue, str ());
0060       spinlock_unlock (&sem->lock);
0061       schedule ();
0062     }
0063   }
0064 }
0065 
0066 int
0067 semaphore_destroy (semaphore * sem)
0068 {
0069   return 0;
0070 }
0071 
0072 /*
0073  * Local Variables:
0074  * indent-tabs-mode: nil
0075  * mode: C
0076  * c-file-style: "gnu"
0077  * c-basic-offset: 2
0078  * End:
0079  */
0080 
0081 /* vi: set et sw=2 sts=2: */