/* ***********************IMPORTANT *********************************************
 *  Copyright (C) 2004-2007 Thierry Lagarde <tlagarde@users.sourceforge.net>
 *  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.
 *
 *
 *                      ********************
 *
 *
 *  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 Library General Public License for more details.
 *
 *
 *                      ********************
 *
 *
 * You are highly encouraged to send your changes to <tlagarde@users.sourceforge.net>
 * for possible incorporation into the main distribution.
 * By sending these changes to <tlagarde@users.sourceforge.net>,
 * it is assumed that you are offering <tlagarde@users.sourceforge.net>
 * the unlimited, non-exclusive right to reuse, modify, and relicense the code.
 *
 *
 *                      ********************
 *
 *  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.
 **************************************************************************************/
#include "Semaphore.h"
#include "Com/Initialisation_Com.h"
#include "Debug.h"


#include <errno.h>

void Attendre_Semaphore (sem_t * sem)
{
	ERROR_if (sem == NULL);
	if (sem_wait (sem) == -1) {
		switch (errno) {
		    case EINTR:
			    debug_printf (D_ERRO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "EINTR : (A signal interrupted)  - %s", strerror (errno));
		    case EINVAL:
			    debug_printf (D_ERRO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "EINVAL : (Does not refer to a valid semaphore)  - %s", strerror (errno));
		    case EAGAIN:
			    debug_printf (D_ERRO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "EAGAIN  - %s", strerror (errno));
		    default:
			    debug_printf (D_ERRO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "%s", strerror (errno));
			    break;
		}
	}
}

void Liberation_Semaphore (sem_t * sem)
{
	ERROR_if (sem == NULL);
	if (sem_post (sem) == -1) {
		switch (errno) {
		    case EINVAL:
			    debug_printf (D_ERRO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "EINVAL : (Does not refer to a valid semaphore)  - %s", strerror (errno));
		    default:
			    debug_printf (D_ERRO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "%s", strerror (errno));
			    break;
		}
	}
}

int Valeur_Semaphore (sem_t * sem)
{
	int       valeur;
	
	ERROR_if (sem == NULL);
	sem_getvalue (sem, &valeur);
	return valeur;
}


void Initialisation_Semaphore (void)
{
	sem_init (&Semaphore_Debug, 0, 1);
	sem_init (&Semaphore_Thread, 0, 1);
	sem_init (&Semaphore_Etat_Thread, 0, 1);
	sem_init (&Semaphore_Log_File, 0, 1);
}





	
