/****************************************************************************
 *  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: Basic functions for starting new threads and manipulating
 *              them in limited ways.
 ****************************************************************************/

#ifndef COMPAT_THREAD_
#define COMPAT_THREAD_


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


#include <pthread.h>

#include "compat_lib.h"

/****************************************************************************
 * Name:        struct priv_CPL_Thread
 * Description: Encapsulates the contents of the CPL_Thread data type, which
 *              at the moment has just two fields, one of which is filled in
 *              but not currently used.
 * NOTE: This structure is architecture-dependent. DON'T USE ITS CONTENTS.
 ****************************************************************************/
struct priv_CPL_Thread
{
    pthread_t threadID;  /* An identifier for the thread */
};

typedef struct priv_CPL_Thread CPL_Thread;
typedef pthread_t CPL_ThreadID;



/****************************************************************************
 *  Function:    CPL_ThreadCreate
 *  Description: Creates a new thread.
 *  Parameters:  thread         [OUT] Where to store the ID of the new thread.
 *               start_routine  [IN]  Where the new thread starts execution.
 *               arg            [IN]  The argument to supply to start_routine.
 *  Returns:     Zero on success, or a CPL_GetError() code on failure.
 ****************************************************************************/
INT32U CPL_ThreadCreate(CPL_Thread * thread,
                        void     *(* start_routine )(void*),
                        void       * arg);

/****************************************************************************
 *  Function:    CPL_ThreadEqual
 *  Description: Determine if two thread objects represent the same thread.
 *  Parameters:  t1         [IN] LHS of the object comparison
 *               t2			[IN] RHS of the object comparison
 *  Returns:     Non-zero (TRUE ) if equal, zero ( FALSE ) otherwise.
 ****************************************************************************/
INT32U CPL_ThreadEqual(const CPL_Thread * const t1,
                       const CPL_Thread * const t2);

/****************************************************************************
 *  Function:    CPL_ThreadDetach
 *  Description: Declares that the exit value of the thread won't be checked.
 *  Parameters:  thread   [IN] The thread whose return value is of no interest.
 *  Returns:     Zero on success, or a CPL_GetError() code on failure.
 ****************************************************************************/
INT32U CPL_ThreadDetach(CPL_Thread * thread);

/****************************************************************************
 *  Function:    CPL_ThreadJoin
 *  Description: Waits for a thread to exit, and retrieves its exit value.
 *  Parameters:  thread    [IN]  The thread whose exit value is desired.
 *               value_ptr [OUT] The location to place the return value.
 *  Returns:     Zero on success, or a CPL_GetError() code on failure.
 ****************************************************************************/
INT32U CPL_ThreadJoin(CPL_Thread * thread,
                      void      ** value_ptr);

/****************************************************************************
 *  Function:    CPL_ThreadExit
 *  Description: Causes the calling thread to exit prematurely.
 *  Parameters:  value_ptr [IN] The value to use as the thread's exit value.
 *  Returns:     This function does not return.
 ****************************************************************************/
void CPL_ThreadExit(void * value_ptr) __attribute__ ((__noreturn__));

static inline CPL_ThreadID CPL_ThreadGetID(void) { return pthread_self(); }

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* COMPAT_THREAD_ */
