/*
 *               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.
 *
 */
 /* XXX horti
  * The idle thread creates 1 additional thread, which runs the 
  * function testthr(). You can put whatever test you want in here (create
  * more threads, etc. I will put in a cleaner way to run tests later.
  */
#include <hw.h>
#include <iostypes.h>
#include <ioslibc.h>
#include <arm.h>
#include "thread.h"
#include "diag/mmu.h"
#include "diag/proto.h"
extern void iosYieldThread(void);
IOSThreadId
iosCreateThread(IOSEntryProc entry,
               void *arg, 
               void *stack, 
               u32   stackSize,
               u32   priority,
               u32   attribute);
s32 iosGetThreadPriority(IOSThreadId id);
IOSError iosSetThreadPriority(IOSThreadId id, u32 priority);
IOSError iosDestroyThread(IOSThreadId id, void *val);
IOSThreadId iosGetThreadId(void);
IOSError iosStartThread(IOSThreadId id);

u8 iosshStack[1024]; /* 128 is hardwired as stack size for this thread
                        * when the idle thread starts it. Please change
                        * it if you change this.
                        */
#define STACK_SIZE 256
u8 stack2[STACK_SIZE];
u8 stack3[STACK_SIZE];

void thread2(u32);

int
thread_test(void)
{
    u32 id;
    u32 i = 0;
    u32 pri = iosGetThreadPriority(0);

    printf("T1: Hello, I'm thread #1\n");

    printf("T1: Before iosCreateThread\n");
    id = iosCreateThread((IOSEntryProc)thread2, (void*)200, stack2+STACK_SIZE, STACK_SIZE, pri, IOS_THREAD_CREATE_DETACHED);
    printf("T1: After iosCreateThread: %c\n", '0'+id);


    printf("T1: Before iosStartThread\n");
    iosStartThread(id);
    printf("T1: After iosStartThread\n");


    while(1) {
        printf("T1: Yielding: %d\n", i++);
        iosYieldThread();
    }
    return 0;
}

void thread2(u32 arg)
 {   u32 i = 0;
    printf("T2: Howdy, I'm thread #2: arg = %d\n", arg);
    while (1) {
        printf("T2: Yielding %u\n", i++);
        iosYieldThread();
    }
}

/* Thread created by idle process */
void
iossh(u32 arg)
{
    //enableICache ();
    thread_test();
}
