/*
 * $Id: types.h,v 1.10 2001/05/15 07:22:49 k-abe Exp $
 *
 * This file is part of PTL, Portable Thread Library.
 *
 * Copyright (C) 1997-2000 Kota Abe
 */

#ifndef __PTL_SYS_TYPES_H__
#define __PTL_SYS_TYPES_H__

#include <sys/pthread-init.h>

/* ---------------------------------------------------------------- */
#ifdef __PTL_RENAMED__
#define __PTL_ALREADY_RENAMED_SYS_TYPES_H
#else
#include <sys/pthread-before.h>
#endif

#ifdef __GNUC__
#include <00root-gcc/sys/types.h>
#else
#include <00root-system/sys/types.h>
#endif

#include <sys/pthread-after.h>
/* ---------------------------------------------------------------- */

#include <sys/pthread-extsym.h>

/****************************************************************
 *
 *	14.1.3  Type Definition
 *
 ****************************************************************/

#if !defined(HAVE_CLOCKID_T) && !defined(_PTL_CLOCKID_T)
#define _PTL_CLOCKID_T
/* Used for clock ID type in the clock and timer functions */
typedef int	clockid_t;
#endif

#ifndef HAVE_PID_T
/* Used for process IDs and process group IDs */
typedef int	pid_t;
#endif

#ifndef HAVE_SSIZE_T
/* Used by functions that return a count of bytes (memory space) or
   an error indication */
typedef long	ssize_t;
#endif

#ifndef HAVE_MODE_T
/* Used for some file attributes, for example file type, file access
   permissions */
typedef unsigned short	mode_t;
#endif

#include <sys/pthread-priority.h>

/*
 * Struct sched_param should be declared in <sched.h>.
 */
struct sched_param {
    int sched_priority;		/* Process execution scheduling priority */
};

#define _PTHREAD_SCHED_PARAM_INITIALIZER {0}

/*
 * NOTE
 *
 * For historical reason, following types are struct.
 *
 *	pthread_mutexattr_t
 *	pthread_condattr_t
 */


/* Used to identify a thread */
typedef int	pthread_t;


/* Used to identify a thread attribute object */
typedef int	pthread_attr_t;


/* Used to identify a mutex attributes object */
typedef struct _pthread_mutexattr_t {
#ifdef _PTHREAD_THREADS_PROCESS_SHARED
    int 	pshared;
#endif
    int		protocol;
    int		ceiling;
} pthread_mutexattr_t;

#define _PTHREAD_MUTEXATTR_INITIALIZER {	\
    PTHREAD_PRIO_NONE,				\
    _PTHREAD_PRI_FIFO_MAX			\
}


typedef struct _pthread_prio_queue {
    struct _pthread_tcb		*top;
} _pthread_prio_queue;

#define _PTHREAD_PRIO_QUEUE_INITIALIZER {0}


/* Used to identify a mutex object */
typedef struct _pthread_mutex_t {
    struct _pthread_tcb		*owner;
    _pthread_prio_queue		waiter;
    struct _pthread_mutex_t	*next; /* Link over all mutexes */
    int				statically_named;
    char			*name; /* Mutex's name */
    pthread_mutexattr_t		attr;

    /*
     * Original attribute of the owner to restore after priority boosted
     */
    int				boosted;
    int				owner_schedpolicy;
    struct sched_param		owner_param;
} pthread_mutex_t;

#define PTHREAD_MUTEX_INITIALIZER {				\
    0,					/* owner */		\
    _PTHREAD_PRIO_QUEUE_INITIALIZER,	/* waiter */		\
    0,					/* next */		\
    0,					/* statically_named */	\
    0,					/* name */		\
    _PTHREAD_MUTEXATTR_INITIALIZER,	/* attr */		\
    0, 					/* boosted */		\
    0,					/* owner_schedpolicy */	\
    _PTHREAD_SCHED_PARAM_INITIALIZER	/* owner_param */	\
}

/* PTL extension.  Initialize a mutex with a name */
#define PTHREAD_MUTEX_INITIALIZER_WITH_NAME_NP(name) {		\
    0,					/* owner */		\
    _PTHREAD_PRIO_QUEUE_INITIALIZER,	/* waiter */		\
    0,					/* next */		\
    1,					/* statically_named */	\
    (name),				/* name */		\
    _PTHREAD_MUTEXATTR_INITIALIZER,	/* attr */		\
    0, 					/* boosted */		\
    0,					/* owner_schedpolicy */	\
    _PTHREAD_SCHED_PARAM_INITIALIZER	/* owner_param */	\
}


/* Used to identify a condition attributes object */
typedef struct _pthread_condattr_t {
#ifdef _PTHREAD_THREADS_PROCESS_SHARED
    int pshared;
#else
    int dummy;		/* make sizeof(this struct) non 0 */
#endif
} pthread_condattr_t;

#define _PTHREAD_CONDATTR_INITIALIZER {0}


/* Used to identify a condition object */
typedef struct _pthread_cond_t {
    struct _pthread_cond_t	*next; /* Link over all conds */
    _pthread_prio_queue		waiter;
    pthread_condattr_t		attr;
    char			*name; /* Cond's name */
} pthread_cond_t;

#define PTHREAD_COND_INITIALIZER {		\
    0,						\
    _PTHREAD_PRIO_QUEUE_INITIALIZER,		\
    _PTHREAD_CONDATTR_INITIALIZER,		\
    0						\
}

/* Used for dynamic package initialization */
typedef struct pthread_once_t {
    int			state;
    pthread_mutex_t	mutex;
} pthread_once_t;

#define PTHREAD_ONCE_INIT	{0, PTHREAD_MUTEX_INITIALIZER}


/* Used for thread-specific data keys */
typedef int	pthread_key_t;

/* ---------------------------------------------------------------- */
#ifdef __PTL_ALREADY_RENAMED_SYS_TYPES_H
#include <sys/pthread-before.h>
#undef __PTL_ALREADY_RENAMED_SYS_TYPES_H
#else
#include <sys/pthread-after.h>
#endif
/* ---------------------------------------------------------------- */

#endif /* __PTL_SYS_TYPES_H__ */
