/* 
 * File:   common.h
 * Author: blackpanther
 *
 * Created on November 7, 2010, 12:35 PM
 */

#ifndef COMMON_H
#define	COMMON_H

#define UNIQUE_SEM_KEY      444
#define NUMBER_OF_SEMAPHORE   1
#define USED_BARRIER          0

#define NO_FLAG               0

#define MAX_LOAD             10

#define handle(a,mess,code_error)   \
    if(a){                          \
        perror(mess);               \
        return code_error;          \
    }

/* ! Not natively defined !*/
union semun
{
    int val;                  /* valeur pour SETVAL */
    struct semid_ds *buf;     /* buffer pour IPC_STAT, IPC_SET */
    unsigned short *array;    /* table  pour GETALL, SETALL */
                              /* Sp�cificit� Linux : */
    struct seminfo *__buf;    /* buffer pour IPC_INFO */
};

/* MAN SEMOP
 * If  sem_op  is  a positive integer, the operation adds this value to the semaphore value (semval).  Furthermore, if SEM_UNDO is specified for this
   operation, the system updates the process undo count (semadj) for this semaphore.  This operation can always proceed — it never forces  a  process
   to wait.  The calling process must have alter permission on the semaphore set.

   If sem_op is zero, the process must have read permission on the semaphore set.  This is a "wait-for-zero" operation: if semval is zero, the opera‐
   tion can immediately proceed.  Otherwise, if IPC_NOWAIT is specified in sem_flg, semop() fails with errno set to EAGAIN (and none  of  the  opera‐
   tions  in  sops is performed).  Otherwise semzcnt (the count of processes waiting until this semaphore's value becomes zero) is incremented by one
   and the process sleeps until one of the following occurs:

   ·  semval becomes 0, at which time the value of semzcnt is decremented.

   ·  The semaphore set is removed: semop() fails, with errno set to EIDRM.

   ·  The calling process catches a signal: the value of semzcnt is decremented and semop() fails, with errno set to EINTR.

   ·  The time limit specified by timeout in a semtimedop() call expires: semop() fails, with errno set to EAGAIN.

   If sem_op is less than zero, the process must have alter permission on the semaphore set.  If semval is greater than  or  equal  to  the  absolute
   value  of sem_op, the operation can proceed immediately: the absolute value of sem_op is subtracted from semval, and, if SEM_UNDO is specified for
   this operation, the system updates the process undo count (semadj) for this semaphore.  If the absolute value of sem_op is  greater  than  semval,
   and IPC_NOWAIT is specified in sem_flg, semop() fails, with errno set to EAGAIN (and none of the operations in sops is performed).  Otherwise sem‐
   ncnt (the counter of processes waiting for this semaphore's value to increase) is incremented by one and the process sleeps until one of the  fol‐
   lowing occurs:

   ·  semval  becomes greater than or equal to the absolute value of sem_op, at which time the value of semncnt is decremented, the absolute value of
      sem_op is subtracted from semval and, if SEM_UNDO is specified for this operation, the system updates the process undo count (semadj) for  this
      semaphore.

   ·  The semaphore set is removed from the system: semop() fails, with errno set to EIDRM.

   ·  The calling process catches a signal: the value of semncnt is decremented and semop() fails, with errno set to EINTR.

   ·  The time limit specified by timeout in a semtimedop() call expires: the system call fails, with errno set to EAGAIN.

 */
#endif	/* COMMON_H */

