1 /*---------------------------------------------------------------------------*
2   Project:  Thread Demo -- No. 5
3   File:     threaddemo5.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: threaddemo5.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 <string.h>
33 #include <revolution.h>
34 
35 #define  BUFFER_SIZE  4
36 
37 //
38 // Bounded buffer data structure
39 //
40 OSMutex  Mutex;
41 OSCond   CondNotFull;
42 OSCond   CondNotEmpty;
43 s32      Buffer[BUFFER_SIZE];
44 s32      Count;
45 
46 OSThread Thread;
47 u8       ThreadStack[4096];
48 
Get(void)49 static s32 Get(void)
50 {
51     s32 item;
52 
53     OSLockMutex(&Mutex);
54     while (Count == 0)
55     {
56         OSWaitCond(&CondNotEmpty, &Mutex);
57     }
58     item = Buffer[0];
59     --Count;
60     memmove(&Buffer[0], &Buffer[1], sizeof(s32) * Count);
61     OSUnlockMutex(&Mutex);
62     OSSignalCond(&CondNotFull);
63     return item;
64 }
65 
Put(s32 item)66 static void Put(s32 item)
67 {
68     OSLockMutex(&Mutex);
69     while (BUFFER_SIZE <= Count)
70     {
71         OSWaitCond(&CondNotFull, &Mutex);
72     }
73     Buffer[Count] = item;
74     ++Count;
75     OSUnlockMutex(&Mutex);
76     OSSignalCond(&CondNotEmpty);
77 }
78 
Func(void * param)79 static void* Func(void* param)
80 {
81     #pragma unused (param)
82     s32 item;
83 
84     for (item = 0; item < 16; item++)
85     {
86         Put(item);
87     }
88 
89     return 0;
90 }
91 
main(void)92 void main(void)
93 {
94     s32 i;
95 
96     OSInit();
97     VIInit();
98 
99     //
100     // Initializes mutex and condition variables
101     //
102     OSInitMutex(&Mutex);
103     OSInitCond(&CondNotFull);
104     OSInitCond(&CondNotEmpty);
105 
106     //
107     // Creates a new thread. The thread is suspended by default.
108     //
109     OSCreateThread(
110         &Thread,                            // ptr to the thread to init
111         Func,                               // ptr to the start routine
112         0,                                  // param passed to start routine
113         ThreadStack + sizeof ThreadStack,   // initial stack address
114         sizeof ThreadStack,                 // stack size
115         16,                                 // scheduling priority
116         OS_THREAD_ATTR_DETACH);             // detached
117 
118 
119     //
120     // Starts the thread
121     //
122     OSResumeThread(&Thread);
123 
124     //
125     // Main loop
126     //
127     for (i = 0; i < 16; i++)
128     {
129         s32 item;
130 
131         item = Get();
132         OSReport("%d\n", item);
133         VIWaitForRetrace();                 // Sleep till next V-sync
134     }
135 
136     OSHalt("Demo complete\n");
137 }
138