1 /*---------------------------------------------------------------------------*
2   Project:  Thread Demo -- No. 1
3   File:     threaddemo1.c
4 
5   Copyright 1998, 1999 Nintendo.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Log: threaddemo1.c,v $
14   Revision 1.2  02/20/2006 04:13:11  mitu
15   changed include path from dolphin/ to revolution/.
16 
17   Revision 1.1  01/13/2006 11:24:13  hiratsu
18   Initial check in.
19 
20 
21     2     6/07/00 6:12p Tian
22     Updated OSCreateThread and idle function APIs
23 
24     1     2/16/00 2:23p Tian
25     Cleaned up and moved to demos
26 
27     1     2/08/00 5:11p Shiki
28     Initial check-in.
29   $NoKeywords: $
30  *---------------------------------------------------------------------------*/
31 
32 #include <revolution.h>
33 
34 OSThread      Thread;
35 u8            ThreadStack[4096];
36 volatile u64  Sum;
37 
Func(void * param)38 static void* Func(void* param)
39 {
40     #pragma unused( param )
41     u64 n;
42 
43     //
44     // Do background job
45     //
46     for (n = 1; n <= 1000000; n++)
47     {
48         Sum += n;
49     }
50 
51     //
52     // Exits
53     //
54     return 0;
55 }
56 
main(void)57 void main(void)
58 {
59 
60     OSInit();
61     VIInit();
62 
63     OSReport("Thread Demo 1: Basic Thread Creation\n");
64 
65     //
66     // Creates a new thread. The thread is suspended by default.
67     //
68     OSCreateThread(
69         &Thread,                            // ptr to the thread to init
70         Func,                               // ptr to the start routine
71         0,                                  // param passed to start routine
72         ThreadStack + sizeof ThreadStack,   // initial stack address
73         sizeof ThreadStack,                 // stack size
74         31,                                 // scheduling priority
75         OS_THREAD_ATTR_DETACH);             // detached by default
76 
77     //
78     // Starts the thread
79     //
80     OSResumeThread(&Thread);
81 
82     //
83     // Loop until the thread exits
84     //
85     do
86     {
87         OSReport("Sum of 1 to 1000000 >= %llu after %u V-syncs\n",
88                  Sum,
89                  VIGetRetraceCount());
90         VIWaitForRetrace();                 // Sleep till next V-sync
91     } while (!OSIsThreadTerminated(&Thread));
92 
93     OSReport("Sum of 1 to 1000000 == %llu after %u V-syncs\n",
94              Sum,
95              VIGetRetraceCount());
96 
97     OSHalt("Demo complete\n");
98 }
99