openblt

a hobby OS from the late 90s
git clone http://frotz.net/git/openblt.git
Log | Files | Refs | LICENSE

qsem.c (2266B)


      1 /* $Id: //depot/blt/lib/libc/qsem.c#1 $
      2 **
      3 ** Copyright 1999 Sidney Cammeresi.
      4 ** All rights reserved.
      5 ** 
      6 ** Redistribution and use in source and binary forms, with or without
      7 ** modification, are permitted provided that the following conditions
      8 ** are met:
      9 ** 
     10 ** 1. Redistributions of source code must retain the above copyright notice,
     11 **    this list of conditions and the following disclaimer.
     12 ** 
     13 ** 2. Redistributions in binary form must reproduce the above copyright
     14 **    notice, this list of conditions and the following disclaimer in the
     15 **    documentation and/or other materials provided with the distribution.
     16 ** 
     17 ** 3. The name of the author may not be used to endorse or promote products
     18 **    derived from this software without specific prior written permission.
     19 ** 
     20 ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS' AND ANY EXPRESS OR IMPLIED
     21 ** WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     22 ** MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
     23 ** NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     24 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
     25 ** TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     26 ** PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     27 ** LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     28 ** NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     29 ** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30 */
     31 
     32 #include <stdlib.h>
     33 #include <blt/syscall.h>
     34 #include <blt/qsem.h>
     35 
     36 qsem_t *qsem_create (int count)
     37 {
     38 	qsem_t *s;
     39 
     40 	s = (qsem_t *) malloc (sizeof (qsem_t));
     41 #ifdef I386
     42 	s->mutex = sem_create (count, "qsem_mutex");
     43 	s->count = 0;
     44 #else
     45 	s->mutex = sem_create (0, "qsem_mutex");
     46 	s->count = count;
     47 #endif
     48 	return s;
     49 }
     50 
     51 void qsem_destroy (qsem_t *s)
     52 {
     53 	sem_destroy (s->mutex);
     54 	free (s);
     55 }
     56 
     57 void qsem_acquire (qsem_t *s)
     58 {
     59 #ifdef I386
     60 	/* no atomic_add() in i386 */
     61 	sem_acquire(s->mutex);
     62 #else
     63 	if (atomic_add (&s->count, -1) <= 0)
     64 		sem_acquire (s->mutex);
     65 #endif
     66 }
     67 
     68 void qsem_release (qsem_t *s)
     69 {
     70 #ifdef I386
     71 	/* no atomic_add() in i386 */
     72 	sem_release(s->mutex);
     73 #else
     74 	if (atomic_add (&s->count, 1) < 0)
     75 		sem_release (s->mutex);
     76 #endif
     77 }
     78