/***************************************************************************
 *   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 __SHARED_H__
#define __SHARED_H__

#include <unistd.h>     // Required for process forking
#include <sys/types.h>  // Required for pid_t and such like that
#include <sys/ipc.h>    // Contains constants for inter-process communication
#include <sys/sem.h>    // Semaphores
#include <sys/shm.h>    // shared memory

#include "blEnv.h"      // Used to set / get environment variables


/*******************************************
Unix fails to define this anywhere, so we
need to define it within our program
*******************************************/
union semun
{
  int              val;
  struct semid_ds *buf;
  unsigned short  *array;
};


/*******************************************
By default, shared memory is just a block
of bytes.  In order to structure the data
in variables, we need to "overlay" the
shared memory with a data structure.
See shmat() statement.
*******************************************/
struct SharedVars
{
  int CarsWaiting;
  int TrainsCrossing;
  int Light;
};


/*******************************************
This enum allows us to refer to semaphores
by names instead of index numbers
*******************************************/
enum eSEMAPHORES {sCAR, sTRAIN, sCROSSING};
enum eLIGHT {GREEN = 0, YELLOW, RED};


/*******************************************
To avoid using the semop() command, we will
define our own Wait() and Signal() operations
for semaphores.
*******************************************/
void Wait(const key_t group, const int sem);
void Signal(const key_t group, const int sem);



/*******************************************
These functions allow for shared memory and
semaphore id's to be passed between processes
*******************************************/
int SetS_Group(key_t group);
int SetM_Group(key_t group);
int GetS_Group(void);
int GetM_Group(void);


/*******************************************
Lastly, this is a convenient function to
randomize the timer.
*******************************************/
void blRandomize(void);

#endif

