/*
 * $RCSfile: semaphore.c,v $
 
 * x-kernel v3.3

 * This file implements POSIX semaphores for uses with the Linux
 * pthreads library.  If Linux ever implements semaphores stop linking
 * against this file.

 * Note that semaphores are NOT part of the POSIX.1c thread standard
 * but are part of the POSIX.4 real-time extension standard.  Some of
 * this standard exists in Linux 1.3.67 but it is far from a stable
 * kernel at this point.

 * This implementation is a subset of the standard so semaphore cannot
 * be used accross processes.  Nor is sema_trywait() implemented -
 * although it could be.  Also note the lack of any error checking in
 * this code.

 * The implementation of these functions is classic so don't expect
 * many comments.

 * Copyright (c) 1993,1991,1990,1996  Arizona Board of Regents
 
 * $Log: semaphore.c,v $
 * Revision 1.1  1996/05/31 23:02:45  rrp
 * Initial revision
 *
 * Revision 1.1  1996/03/15  22:04:49  mjk
 * Initial revision
 *

 */

#include "semaphore.h"
#include "thread.h"

  

int sema_init(sema_t *sp, int pshared, int value) {
  sp->count = value;
  pthread_mutex_init(&(sp->mutex), NULL);
  pthread_cond_init(&(sp->cond), NULL);
  return 0;
} /* sema_init */

int sema_destroy(sema_t *sp) {
  pthread_mutex_destroy(&(sp->mutex));
  pthread_cond_destroy(&(sp->cond));
  return 0;
} /* sema_destroy */

int sema_wait(sema_t *sp) {
  pthread_mutex_lock(&(sp->mutex));
  while ( sp->count <= 0 )
    pthread_cond_wait(&(sp->cond), &(sp->mutex));
  sp->count--;
  pthread_mutex_unlock(&(sp->mutex));
  return 0;
} /* sema_wait */

int sema_post(sema_t *sp) {
  pthread_mutex_lock(&(sp->mutex));
  sp->count++;
  pthread_mutex_unlock(&(sp->mutex));
  pthread_cond_signal(&(sp->cond));
  return 0;
} /* sema_post */





/* semaphore.c ends here */


