#ifndef	AMUTEX_H
#define	AMUTEX_H

#ifdef	ARCH_THREADING_SUPPORTED

#ifdef	CONFIG_ARCH_MUTEX_SYSVIPC

#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>

/*
 * SysV - based implementation provides no means for recursive mutexes
 */

#ifdef	ARCH_RECURSIVE_MUTEX
#undef	ARCH_RECURSIVE_MUTEX
#endif	/* ARCH_RECURSIVE_MUTEX */

typedef	pid_t	arch_thread_id_t;

#define	ARCH_INVALID_THREAD_ID		((pid_t)(-1))

typedef struct arch_mutex_s
{
	int					sem_fd;
	arch_thread_id_t	owner;		/* for recursion extension */
	int					lock_count; /* for recursion extension */
}	arch_mutex_t, *arch_mutex_p;

#elif	defined(CONFIG_ARCH_MUTEX_PTHREAD) /* !CONFIG_ARCH_MUTEX_SYSVIPC */

#include <pthread.h>

typedef	pthread_t	arch_thread_id_t;

#define	ARCH_INVALID_THREAD_ID		((pthread_t)(-1)) /* is this correct ? */

#if	defined(CONFIG_ARCH_MUTEX_PTHREAD_USE_SETTYPE) || \
	defined(CONFIG_ARCH_MUTEX_PTHREAD_USE_SETKIND_NP)

#ifndef	ARCH_RECURSIVE_MUTEX
#define	ARCH_RECURSIVE_MUTEX
#endif	/* !ARCH_RECURSIVE_MUTEX */

#else	/* !RECURSIVE */

#ifdef	ARCH_RECURSIVE_MUTEX
#undef	ARCH_RECURSIVE_MUTEX
#endif	/* ARCH_RECURSIVE_MUTEX */

#endif	/* !RECURSIVE */ 

typedef struct arch_mutex_s
{
	pthread_mutex_t		mutex;
	pthread_mutexattr_t	attr;
#ifndef	ARCH_RECURSIVE_MUTEX
	arch_thread_id_t	owner;
	int					lock_count; /* for recursion extension */
#endif	/* !ARCH_RECURSIVE_MUTEX */
}	arch_mutex_t, *arch_mutex_p;

#else	/* !CONFIG_ARCH_MUTEX_PTHREAD */

#error	"amutex.c: either SYSV or POSIX implementation has to be selected"

#endif	/* !CONFIG_ARCH_MUTEX_PTHREAD */

#endif	/* ARCH_THREADING_SUPPORTED */

#endif	/* !AMUTEX_H */
