/***************************************************************************
 *   Copyright (C) 2005 by Brian Lauber   *
 *   bml8@case.edu   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/


#ifndef __SEMAPHORES_C__
#define __SEMAPHORES_C__

#include <sys/sem.h>

#include "blError.h"

/*******************************************
Wait simply encapsulates the call to semop.
The first line is only evaulated at compile
time.  It creates a static sumbuf structure
containing the semaphore command "reduce the
semaphore count by 1".  At runtime, the
semaphore group and offset are adjusted to
Wait on the appropriate semaphore
*******************************************/
void Wait(const key_t group, const int offset)
{
  static struct sembuf acquire = {0, -1, 0};

  acquire.sem_num = offset;
  Error(   semop(group, &acquire, 1)  );
};


/*******************************************
Signal is almost identical to Wait, except
that the sumbuf structure is preformatted to
increase the semaphore count.
*******************************************/
void Signal(const key_t group, const int offset)
{
  static struct sembuf release = {0, 1, 0};

  release.sem_num = offset;
  Error(   semop(group, &release, 1)   );
}

#endif

