1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - OS - demos - yield-1
3   File:     main.c
4 
5   Copyright 2003-2008 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   $Date:: 2008-04-02#$
14   $Rev: 5266 $
15   $Author: yada $
16  *---------------------------------------------------------------------------*/
17 #include <nitro.h>
18 
19 /* ***************************************************************
20    --- Test for OS_YieldThread() ---
21 
22   If not use OS_YieldThread(), the result will be:
23      Thread 3
24      Thread 2
25      Thread 1
26      Thread 3
27      Thread 2
28      Thread 1
29         :
30 
31    If use OS_YieldThread(), the result will be:
32      Thread 3
33      Thread 2
34      Thread 3
35      Thread 2
36      Thread 3
37      Thread 2
38         :
39 
40   Please try it.
41  * ****************************************************************/
42 
43 //================================================================================
44 #define	STACK_SIZE	   1024
45 #define	THREAD1_PRIO   20
46 #define	THREAD2_PRIO   20
47 #define	THREAD3_PRIO   20
48 
49 OSThread thread1;
50 OSThread thread2;
51 OSThread thread3;
52 OSThread* launcherThread;
53 
54 u64 stack1[STACK_SIZE / sizeof(u64)];
55 u64 stack2[STACK_SIZE / sizeof(u64)];
56 u64 stack3[STACK_SIZE / sizeof(u64)];
57 
58 int cnt;
59 
60 void startTest(void);
61 void stopTest(void);
62 void proc1(void *arg);
63 void proc2(void *arg);
64 void proc3(void *arg);
65 
66 BOOL useYield;
67 
68 //================================================================================
69 /*---------------------------------------------------------------------------*
70   Name:         NitroMain
71 
72   Description:  main
73 
74   Arguments:    None
75 
76   Returns:      None
77  *---------------------------------------------------------------------------*/
NitroMain(void)78 void NitroMain(void)
79 {
80     OS_Init();
81     OS_InitThread();
82 
83 	launcherThread = OS_GetCurrentThread();
84 
85 	//================ TEST1
86 	OS_Printf("================TEST1: use OS_YieldThread()\n");
87 	cnt = 0;
88 	useYield = TRUE;
89 	startTest();
90 	OS_SleepThread(NULL);
91 	stopTest();
92 
93 	//================ TEST2
94 	OS_Printf("================TEST2: not use OS_YieldThread()\n");
95 	cnt = 0;
96 	useYield = FALSE;
97 	startTest();
98 	OS_SleepThread(NULL);
99 	stopTest();
100 
101     OS_Printf("==== Finish sample.\n");
102     OS_Terminate();
103 }
104 
105 //--------------------------------------------------------------------------------
106 //    startTest
107 //
startTest(void)108 void startTest(void)
109 {
110     OS_CreateThread(&thread1, proc1, (void *)0, stack1 + STACK_SIZE / sizeof(u64), STACK_SIZE, THREAD1_PRIO);
111     OS_CreateThread(&thread2, proc2, (void *)0, stack2 + STACK_SIZE / sizeof(u64), STACK_SIZE, THREAD2_PRIO);
112     OS_CreateThread(&thread3, proc3, (void *)0, stack3 + STACK_SIZE / sizeof(u64), STACK_SIZE, THREAD3_PRIO);
113 
114     OS_DumpThreadList();
115 
116     (void)OS_DisableScheduler();
117     {
118         OS_WakeupThreadDirect(&thread1);
119         OS_WakeupThreadDirect(&thread2);
120         OS_WakeupThreadDirect(&thread3);
121     }
122     (void)OS_EnableScheduler();
123     OS_RescheduleThread();
124 }
125 
126 //--------------------------------------------------------------------------------
127 //    stopTest
128 //
stopTest(void)129 void stopTest(void)
130 {
131     (void)OS_DisableScheduler();
132 	{
133 		OS_DestroyThread(&thread1);
134 		OS_DestroyThread(&thread2);
135 		OS_DestroyThread(&thread3);
136 	}
137     (void)OS_EnableScheduler();
138     OS_RescheduleThread();
139 }
140 
141 //--------------------------------------------------------------------------------
142 //    proc1
143 //
proc1(void * arg)144 void proc1(void *arg)
145 {
146 #pragma unused( arg )
147     while (cnt<10)
148     {
149         OS_Printf("Thread 1\n");
150 
151 		if ( useYield )
152 		{
153 			OS_YieldThread();
154 		}
155 		else
156 		{
157 			(void)OS_DisableScheduler();
158 			{
159 				OS_WakeupThreadDirect(&thread2);
160 				OS_WakeupThreadDirect(&thread3);
161 			}
162 			(void)OS_EnableScheduler();
163 			OS_SleepThread(NULL);
164 		}
165 		cnt++;
166     }
167 	OS_WakeupThreadDirect(launcherThread);
168 }
169 
170 //--------------------------------------------------------------------------------
171 //    proc2
172 //
proc2(void * arg)173 void proc2(void *arg)
174 {
175 #pragma unused( arg )
176     while (cnt<10)
177     {
178         OS_Printf("Thread 2\n");
179 
180 		if ( useYield )
181 		{
182 			OS_YieldThread();
183 		}
184 		else
185 		{
186 			(void)OS_DisableScheduler();
187 			{
188 				OS_WakeupThreadDirect(&thread1);
189 				OS_WakeupThreadDirect(&thread3);
190 			}
191 			(void)OS_EnableScheduler();
192 			OS_SleepThread(NULL);
193 		}
194 		cnt++;
195     }
196 	OS_WakeupThreadDirect(launcherThread);
197 }
198 
199 //--------------------------------------------------------------------------------
200 //    proc3
201 //
proc3(void * arg)202 void proc3(void *arg)
203 {
204 #pragma unused( arg )
205     while (cnt<10)
206     {
207         OS_Printf("Thread 3\n");
208 
209 		if ( useYield )
210 		{
211 			OS_YieldThread();
212 		}
213 		else
214 		{
215 			(void)OS_DisableScheduler();
216 			{
217 				OS_WakeupThreadDirect(&thread1);
218 				OS_WakeupThreadDirect(&thread2);
219 			}
220 			(void)OS_EnableScheduler();
221 			OS_SleepThread(NULL);
222 		}
223 		cnt++;
224     }
225 	OS_WakeupThreadDirect(launcherThread);
226 }
227 
228 /*====== End of main.c ======*/
229