/****************************************************************************
 *  INTEL CONFIDENTIAL
 *  Copyright 2006 Intel Corporation All Rights Reserved.
 *
 *  The source code contained or described herein and all documents related
 *  to the source code ("Material") are owned by Intel Corporation or its
 *  suppliers or licensors. Title to the Material remains with Intel
 *  Corporation or its suppliers and licensors. The Material may contain
 *  trade secrets and proprietary and confidential information of Intel
 *  Corporation and its suppliers and licensors, and is protected by
 *  worldwide copyright and trade secret laws and treaty provisions. No part
 *  of the Material may be used, copied, reproduced, modified, published,
 *  uploaded, posted, transmitted, distributed, or disclosed in any way
 *  without Intel's prior express written permission.
 *
 *  No license under any patent, copyright, trade secret or other
 *  intellectual property right is granted to or conferred upon you by
 *  disclosure or delivery of the Materials, either expressly, by
 *  implication, inducement, estoppel or otherwise. Any license under such
 *  intellectual property rights must be express and approved by Intel in
 *  writing.
 *
 *  Unless otherwise agreed by Intel in writing, you may not remove or alter
 *  this notice or any other notice embedded in Materials by Intel or Intel's
 *  suppliers or licensors in any way.
 ****************************************************************************/

/****************************************************************************
 * Description: Defines semaphores and the functions that manipulate them.
 ****************************************************************************/

#ifndef COMPAT_SEM_
#define COMPAT_SEM_

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */


#include <semaphore.h>

#include "compat_lib.h"

/****************************************************************************
 * Name:        struct priv_CPL_Sem
 * Description: Encapsulates the contents of the CPL_Semaphore data type, which
 *              at the moment is just a handle to the semaphore itself.
 * NOTE: This structure is architecture-dependent. DON'T USE ITS CONTENTS.
 ****************************************************************************/
struct priv_CPL_Sem {
    sem_t semaphore;   /* A handle to the semaphore */
//#ifdef DEBUG
    const char *name;
//#endif
};

typedef struct priv_CPL_Sem CPL_Semaphore;



/****************************************************************************
 *  Function:    CPL_SemInit
 *  Description: Initializes a semaphore.
 *  Parameters:  sem     [OUT] The semaphore to initialize.
 *               value    [IN] The initial count of the semaphore.
 *  Returns:     Zero on success, or a CPL_GetError() code on failure.
 ****************************************************************************/
INT32U CPL_SemInit(CPL_Semaphore * sem,
                   INT32U          value
			//	#ifdef DEBUG
				   , const char *name
			//	#endif
				   );

/****************************************************************************
 *  Function:    CPL_SemWait
 *  Description: Waits for a semaphore to have a positive count (if necessary),
 *               then decreases the count and returns.
 *  Parameters:  sem    [IN] The semaphore to wait for.
 *  Returns:     Zero on success, or a CPL_GetError() code on failure.
 ****************************************************************************/
INT32U CPL_SemWait(CPL_Semaphore * sem);

/****************************************************************************
 *  Function:    CPL_SemWaitTimeout
 *  Description: Waits for the specified timeout for a semaphore to have a
 *               positive count (if necessary), then decreases the count and
 *               returns.
 *  Parameters:  sem     [IN] The semaphore to wait for.
 *               timeout [IN] The timeout value in milliseconds
 *  Returns:     Zero on success, or errno code on failure.
 ****************************************************************************/
INT32U CPL_SemWaitTimeout(CPL_Semaphore * sem, INT32U timeout);

/****************************************************************************
 *  Function:    CPL_SemTryWait
 *  Description: If the semaphore has a positive count, decrements it and
 *               returns success.  Otherwise, returns failure.
 *  Parameters:  sem    [IN] The semaphore to check the value of.
 *  Returns:     Zero on success, or a CPL_GetError() code on failure.  If the
 *               semaphore had a count of zero, it returns CPL_WARN_WOULDBLOCK.
 ****************************************************************************/
INT32U CPL_SemTryWait(CPL_Semaphore * sem);

/****************************************************************************
 *  Function:    CPL_SemRelease
 *  Description: Increments the semaphore's count.
 *  Parameters:  sem    [IN] The semaphore whose count is to be incremented.
 *  Returns:     Zero on success, or a CPL_GetError() code on failure.
 ****************************************************************************/
INT32U CPL_SemRelease(CPL_Semaphore * sem);

/****************************************************************************
 *  Function:    CPL_SemDestroy
 *  Description: Destroys the semaphore.
 *  Parameters:  sem    [IN] The semaphore to destroy.
 *  Returns:     Zero on success, or a CPL_GetError() code on failure.
 ****************************************************************************/
INT32U CPL_SemDestroy(CPL_Semaphore * sem);

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* COMPAT_SEM_ */
