/*
 *               Copyright (C) 2005, BroadOn Communications Corp.
 *
 *  These coded instructions, statements, and computer programs contain
 *  unpublished  proprietary information of BroadOn Communications Corp.,
 *  and  are protected by Federal copyright law. They may not be disclosed
 *  to  third  parties or copied or duplicated in any form, in whole or in
 *  part, without the prior written consent of BroadOn Communications Corp.
 *
 */
#ifndef __THREAD_H__
#define __THREAD_H__

#define IOS_THREAD_MAX  48

#define IOS_THREAD_CONTEXT        0
#define IOS_THREAD_NEXT          68
#define IOS_THREAD_INITPRIORITY  72
#define IOS_THREAD_PRIORITY      76
#define IOS_THREAD_STATE         80
#define IOS_THREAD_PID           84
#define IOS_THREAD_ATTRIBUTE     88
#define IOS_THREAD_VAL           92
#define IOS_THREAD_JOINQUEUE     96
#define IOS_THREAD_QUEUE         100
#define IOS_THREAD_EXTRA         104
#define IOS_THREAD_SYSCALL_STACK 108
#define IOS_THREAD_STACK_BOTTOM  IOS_THREAD_SYSCALL_STACK+IOS_CONTEXT_SIZE

#define IOS_CONTEXT_CPSR         0
#define IOS_CONTEXT_R0           4
#define IOS_CONTEXT_R1           8
#define IOS_CONTEXT_R2           12
#define IOS_CONTEXT_R3           16
#define IOS_CONTEXT_R4           20
#define IOS_CONTEXT_R5           24
#define IOS_CONTEXT_R6           28
#define IOS_CONTEXT_R7           32
#define IOS_CONTEXT_R8           36
#define IOS_CONTEXT_R9           40
#define IOS_CONTEXT_R10          44
#define IOS_CONTEXT_R11          48
#define IOS_CONTEXT_R12          52
#define IOS_CONTEXT_R13          56
#define IOS_CONTEXT_R14          60
#define IOS_CONTEXT_R15          64
#define IOS_CONTEXT_SIZE         68
#define IOS_CONTEXT_SP   IOS_CONTEXT_R13
#define IOS_CONTEXT_LR   IOS_CONTEXT_R14
#define IOS_CONTEXT_PC   IOS_CONTEXT_R15

#define IOS_STATE_FREE    0
#define IOS_STATE_READY   1
#define IOS_STATE_RUNNING 2
#define IOS_STATE_STOPPED 3
#define IOS_STATE_WAITING 4
#define IOS_STATE_DEAD    5
#define IOS_STATE_FAULTED 6

#if defined (_LANGUAGE_C)
typedef struct IOSContext {
    u32 cpsr;
    u32 r[13];
    u32 sp;
    u32 lr;
    u32 pc;
} IOSContext;

typedef struct IOSThread {
    IOSContext context;
    struct IOSThread *next;
    s32 initPriority;
    s32 priority;
    u32 state;
    u32 pid;
    u32 attribute;
    void *val;
    struct IOSThread *joinQueue;
    struct IOSThread **queue;
    IOSContext extra;
    u32 syscallStack;
#ifdef IOS_STACK_CHECK
    u32 stackBottom;
#endif
} IOSThread;

extern IOSThread __iosThreadTail;
extern IOSThread *__iosRunningThread[2];

#define THIS_THREAD (__iosRunningThread[0])
#define LOCK_THREADS  __iosDisableInt
#define UNLOCK_THREADS(mask)  __iosRestoreInt((mask))

IOSThreadId iosGetThreadId(void);
IOSProcessId iosGetProcessId(void);
void __iosRestartThread(IOSThread **queue, IOSError rv);
void __iosInitThreads(void);

extern IOSUid __iosUids[IOS_PROCESS_MAX];
extern IOSGid __iosGids[IOS_PROCESS_MAX];
#endif

#endif /*__THREAD_H__*/
