/**********************************************************************
***********************************************************************
**                                                                   **
**  NOTICE:                                                          **
**                                                                   **
**  Significant portions of UniTree Release 1 software were          **
**  developed using private DISCOS funding, however, part of this    **
**  program resulted from work developed under a United States       **
**  Government contract and the following legend applies to the      **
**  government developed portions of the code.                       **
**                                                                   **
**  The Government retains for itself and others acting on its       **
**  behalf a paid-up, non-exclusive, irrevocable worldwide license   **
**  in such copyrighted data to reproduce, prepare derivative works, **
**  distribute copies to the public, and perform publicly and        **
**  display publicly, by or on behalf of the Government.             **
**                                                                   **
**  Neither the Government, nor any of its employees, makes and      **
**  warranty, expressed or implied, or assumes any legal liability   **
**  or responsibilty for the accuracy, completeness, or usefulness   **
**  of any information, apparatus, product, or process disclosed,    **
**  or represents that its use would not infringe privately owned    **
**  rights.                                                          **
**                                                                   **
***********************************************************************
**********************************************************************/

/**********************************************************************
**                                                                   **
**           (C) Copyright 1990  General Atomics                     **
**                    All Rights Reserved.                           **
**                                                                   **
**********************************************************************/

#ifndef lint 
static char	sccs_id[] = "@(#)sem.c	1.1 (UNITREE) 1/23/91"; 
#endif /* not lint*/ 
 
/*
*	sem.c: Semaphore library.  Compile with:
*		cc -c -I/usr/include/llnl/l -I/usr/include/llnl/lincs sem.c
*		ar cr libsem.a sem.o
*/

#include <elros/ipads.h>

#define _SEM_C_
#include <elros/sem.h>

#define NULL 0


/********************************
*				*
*	PSem is used to P a	*
*	semaphore.		*
*				*
*********************************/

void PSem(Sem)
Semaphore	*Sem;
{
	WaitingTaskNode	wnode;

	Lock();
	if((--Sem->Value) < 0)
	{
	   wnode.WaitingTaskID = EGO;
	   wnode.Next = NULL;
	   if(Sem->Head == NULL)
	      Sem->Head = &wnode;
	   else
	      Sem->Tail->Next = &wnode;
	   Sem->Tail = &wnode;
	   while(wnode.WaitingTaskID){
	       Unlock();
	       SLEEP;
	       Lock();
	       }
	}
        Unlock();
}

/********************************
*				*
*	VSem is used to V a	*
*	semaphore.		*
*				*
*********************************/

void VSem(Sem)
Semaphore	*Sem;
{
	register WaitingTaskNode	*nodeptr;

	Lock();
	if((++Sem->Value) <= 0)
	{
	   nodeptr = Sem->Head;
	   Sem->Head = nodeptr->Next;
	   Wake(nodeptr->WaitingTaskID);
	   nodeptr->WaitingTaskID = 0;
	   if(Sem->Head == NULL)
	      Sem->Tail = NULL;
	}
	Unlock();
}

/********************************
*				*
*	InitSem is used	        *
*	to set a sem to a	*
*	certain value.		*
*				*
*********************************/

void InitSem(Sem,Value)
Semaphore	*Sem;
int		Value;
{
	Sem->Value = Value;
	Sem->Head = NULL;
	Sem->Tail = NULL;
}

/********************************
*				*
*	ValueSem returns the	*
*       value of the 		*
*	semaphore's counter.	*
*				*
*********************************/

int ValueSem(Sem)
Semaphore	*Sem;
{
	return(Sem->Value);
}
