/****************************************************************************
 *  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: Routines for manipulating intra-process mutexes, which
 *              Windows calls critical sections.
 ****************************************************************************/

#ifndef COMPAT_MUTEX_
#define COMPAT_MUTEX_

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


#include <pthread.h>

// musl does not provide the non portable PTHREAD_MUTEX_RECURSIVE_NP
//#ifndef PTHREAD_MUTEX_RECURSIVE_NP
//	#define	PTHREAD_MUTEX_RECURSIVE_NP		PTHREAD_MUTEX_RECURSIVE
//#endif //PTHREAD_MUTEX_RECURSIVE_NP

#include "compat_lib.h"

/****************************************************************************
 * Name:        struct priv_CPL_Mutex
 * Description: Encapsulates the contents of the CPL_Mutex data type, which
 *              at the moment is just a CRITICAL_SECTION.
 * NOTE: This structure is architecture-dependent. DON'T USE ITS CONTENTS.
 ****************************************************************************/
struct priv_CPL_Mutex {
    pthread_mutex_t mutex;    /* The mutex */
//#ifdef DEBUG
    const char *name;
//#endif
};

typedef struct priv_CPL_Mutex CPL_Mutex;



/****************************************************************************
 *  Function:    CPL_MutexInit
 *  Description: Creates a mutex for later use.
 *  Parameters:  mutex [OUT] A pointer to the mutex to be initialized.
 *  Returns:     Zero on success, or a CPL_GetError() code on failure.
 ****************************************************************************/
INT32U CPL_MutexInit(CPL_Mutex * mutex
			//	#ifdef DEBUG
					, const char *name
			//	#endif
					);

/****************************************************************************
 *  Function:    CPL_MutexLock
 *  Description: Locks a mutex, blocking as long as necessary.
 *  Parameters:  mutex    [IN] A pointer to the mutex to lock.
 *  Returns:     Zero on success, or a CPL_GetError() code on failure.
 ****************************************************************************/
INT32U CPL_MutexLock(CPL_Mutex * mutex);

/****************************************************************************
 *  Function:    CPL_MutexTryLock
 *  Description: Attempts to lock a mutex, returning immediately if the
 *               mutex is already locked.
 *  Parameters:  mutex     [IN] A pointer to the mutex to attempt to lock.
 *  Returns:     Zero on success, or a CPL_GetError() code on failure.
 *               Notably, WAIT_WOULD_BLOCK is returned if it's already locked.
 ****************************************************************************/
INT32U CPL_MutexTryLock(CPL_Mutex * mutex);

/****************************************************************************
 *  Function:    CPL_MutexUnlock
 *  Description: Unlocks a mutex.
 *  Parameters:  mutex     [IN] A pointer to the mutex to unlock.
 *  Returns:     Zero on success, or a CPL_GetError() code on failure.
 ****************************************************************************/
INT32U CPL_MutexUnlock(CPL_Mutex * mutex);

/****************************************************************************
 *  Function:    CPL_MutexDestroy
 *  Description: Destroys a mutex, freeing all resources associated with it.
 *  Parameters:  mutex     [IN] A pointer to the mutex to destroy.
 *  Returns:     Zero on success, or a CPL_GetError() code on failure.
 ****************************************************************************/
INT32U CPL_MutexDestroy(CPL_Mutex * mutex);

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* COMPAT_MUTEX_ */
