/*
 * voipSem.h
 *
 * Copyright (C) 2005, Ranch Networks, Inc.
 *
 * 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.
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 *
 * Author : Srivatsa Chivukula <srivatsa@ranchnetworks.com>
 *
 */

#ifndef _VOIP_SEM_H_
#define _VOIP_SEM_H_

#if defined VXWORKS
#include <semLib.h>
#elif defined WIN32
#include <windows.h>
#elif defined POSIX
#include <pthread.h>
#include <stdlib.h>
#endif

#if defined VXWORKS
#define SEMAPHORE_IS_INVALID 	0
#define WINAPI
#elif defined WIN32
#define SEM_ID					HANDLE
#define WAIT_FOREVER			INFINITE
#define SEMAPHORE_IS_INVALID 	NULL
#define SEM_Q_FIFO				0
#define OK						0
#define SEM_FULL				-1
#elif defined POSIX
#define SEM_ID					pthread_mutex_t*
#define WAIT_FOREVER			0
#define SEMAPHORE_IS_INVALID 	NULL
#define WINAPI
#define SEM_Q_FIFO				0
#define OK						0
#define SEM_FULL				-1
#endif

#define QUEUE_IS_INVALID	0

/*
 *	Utilities to manipulate Semaphores such as Binary & Counting
 */

class voipMutexSemaphore
{
   public:
	voipMutexSemaphore( void );
	~voipMutexSemaphore( void );

	int grab ( unsigned long wait_time );
	int release ( void );
	int destroy ( void );
#ifndef WIN32
	int flush ( void );
#endif
	int qType( void );
	SEM_ID handle( void );

	int create( int qtype=SEM_Q_FIFO );
	int state( void );

   protected:
	int setQType( int qType );
	int setSemId( SEM_ID semId );

   private:
	SEM_ID semId;
	int q;
};

#endif // _VOIP_SEM_H_
