1 /*---------------------------------------------------------------------------*
2   Project:  Thread Demo -- No. 4
3   File:     threaddemo4.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: threaddemo4.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 <stdio.h>
33 #include <stdarg.h>
34 #include <revolution.h>
35 
36 
37 OSThread       Thread;
38 u8             ThreadStack[4096];
39 
40 OSMutex        Mutex;
41 
42 //
43 // Synchronous print
44 //
SyncPrint(char * msg)45 static void SyncPrint(char* msg)
46 {
47     OSLockMutex(&Mutex);
48     OSReport(msg);
49     OSUnlockMutex(&Mutex);
50 }
51 
52 //
53 // Another thread competing for OSReport output
54 //
ThreadFunc(void * arg)55 static void * ThreadFunc(void * arg)
56 {
57     #pragma unused(arg)
58     u32 i;
59 
60     for (i = 0; i < 16; i++)
61     {
62         SyncPrint("<Thread1 says Hi!>\n");
63         OSYieldThread();
64     }
65 
66     return 0;
67 }
68 
69 
main(void)70 void main(void)
71 {
72     u32 i;
73 
74     OSInit();
75 
76     //
77     // Creates a new thread. The thread is suspended by default.
78     //
79     OSCreateThread(
80         &Thread,                            // ptr to the thread to init
81         ThreadFunc,                         // ptr to the start routine
82         0,                                  // param passed to start routine
83         ThreadStack + sizeof ThreadStack,   // initial stack address
84         sizeof ThreadStack,                 // stack size
85         16,                                 // scheduling priority
86         OS_THREAD_ATTR_DETACH);             // detached
87 
88 
89     //
90     // Starts the thread
91     //
92     OSResumeThread(&Thread);
93 
94     //
95     // Initializes the mutex
96     //
97     OSInitMutex(&Mutex);
98 
99     //
100     // Main loop
101     //
102     for (i = 0; i < 16; i++)
103     {
104         SyncPrint("<Main thread says Hi!>\n");
105         OSYieldThread();
106     }
107 
108     OSHalt("Demo Complete");
109 }
110