#ifndef __threads_h__
#define __threads_h__

#include "/usr/include/pthread.h"

typedef void* (*ThreadFunc)(void* arg);

typedef enum {
    XK_EXTERNAL,
    XK_INTERNAL,
    XK_EVENT,
    XK_SHEPHERD
} thread_type_t;

typedef enum {
    XK_CREATED     = 1<<0,
    XK_RELEASED    = 1<<1,
    XK_WAIT        = 1<<2,
    XK_OWNER       = 1<<3,
    XK_SEM_WAIT    = 1<<4,
    XK_SEM_OWN     = 1<<5,
    XK_DELAYED     = 1<<6,
    XK_YIELDED     = 1<<7,
    XK_DONE        = 1<<8
} thread_status_t;

typedef struct {
    pthread_t       pthread;
    ThreadFunc      func;
    void*           arg;
    thread_type_t   type;
    thread_status_t status;
    pthread_mutex_t mutex;
} xk_thread_t;

extern xk_thread_t* threadSelf( void );

extern pthread_mutex_t        xk_master_mutex;

extern void xk_master_lock(void);
extern void xk_internal_lock(void);
extern void xk_shepherd_lock(void);

extern void xk_all_unlock(void);
#define     xk_master_unlock     xk_all_unlock
#define     xk_internal_unlock   xk_all_unlock
#define     xk_shepherd_unlock   xk_all_unlock

extern int 			threadInitialized;

extern void	threadInit (ThreadFunc init, void*   arg);
extern void	threadStop (void);
extern void	threadYield (void);
extern void	xkThreadCreate (ThreadFunc func, void* arg, char* func_name);
extern void	pThreadCreate (ThreadFunc func, void* arg, char* func_name);

#endif /* __thread_h__ */
