Fixed binary search: no more infinite loops when vendor is unknown.
[tangerine.git] / test / benchmarks / exec / taskswitch.c
blob7c224295e6fe5f44ad2ee1e3e857070abd169649
1 #include <dos/dos.h>
2 #include <intuition/intuition.h>
3 #include <graphics/gfx.h>
4 #include <utility/hooks.h>
6 #include <proto/exec.h>
7 #include <proto/dos.h>
8 #include <proto/intuition.h>
9 #include <proto/graphics.h>
11 #include <clib/alib_protos.h>
13 #include <stdio.h>
14 #include <string.h>
15 #include <stdlib.h>
17 /****************************************************************************************/
19 #define SIGF_READY SIGBREAKF_CTRL_C
21 #define SIGF_INIT SIGBREAKF_CTRL_C
22 #define SIGF_STOP SIGBREAKF_CTRL_D
23 #define SIGF_HELLO SIGBREAKF_CTRL_E
25 /****************************************************************************************/
27 struct IntuitionBase *IntuitionBase;
28 struct GfxBase *GfxBase;
30 static struct Screen *scr;
31 static struct Window *win;
32 static struct Task *task1, *task2, *maintask;
34 static LONG counter;
36 static char s[255];
38 /****************************************************************************************/
40 static void Cleanup(char *msg)
42 WORD rc;
44 if (msg)
46 printf("taskswitchbench: %s\n",msg);
47 rc = RETURN_WARN;
48 } else {
49 rc = RETURN_OK;
52 Forbid();
53 if (task1) DeleteTask(task1);
54 if (task2) DeleteTask(task2);
55 Permit();
57 if (win) CloseWindow(win);
58 if (scr) UnlockPubScreen(0,scr);
60 if (GfxBase) CloseLibrary((struct Library *)GfxBase);
61 if (IntuitionBase) CloseLibrary((struct Library *)IntuitionBase);
63 exit(rc);
66 /****************************************************************************************/
68 static void OpenLibs(void)
70 if (!(IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",39)))
72 Cleanup("Can't open intuition.library V39!");
75 if (!(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",39)))
77 Cleanup("Can't open graphics.library V39!");
82 /****************************************************************************************/
84 static void GetVisual(void)
86 if (!(scr = LockPubScreen(0)))
88 Cleanup("Can't lock pub screen!");
92 /****************************************************************************************/
94 static void MakeWin(void)
96 if (!(win = OpenWindowTags(0,WA_PubScreen,(IPTR)scr,
97 WA_Left,10,
98 WA_Top,10,
99 WA_Width,600,
100 WA_Height,scr->WBorTop + scr->Font->ta_YSize + 1 +
101 scr->WBorBottom +
102 scr->Font->ta_YSize * 2,
103 WA_Title,(IPTR)"Press RETURN key to start and any key to stop!",
104 WA_SimpleRefresh,TRUE,
105 WA_CloseGadget,TRUE,
106 WA_DepthGadget,TRUE,
107 WA_DragBar,TRUE,
108 WA_Activate,TRUE,
109 WA_IDCMP,IDCMP_CLOSEWINDOW |
110 IDCMP_VANILLAKEY |
111 IDCMP_REFRESHWINDOW,
112 TAG_DONE)))
114 Cleanup("Can't open window!");
117 ScreenToFront(win->WScreen);
121 /****************************************************************************************/
123 #define OTHERTASK task2
125 static void Task1(void)
127 for(;;)
129 ULONG sigs;
130 BOOL stop = FALSE;
132 SetSignal(0, SIGF_HELLO);
133 Wait(SIGF_INIT);
134 Signal(maintask, SIGF_READY);
136 while(!stop)
138 sigs = Wait(SIGF_STOP | SIGF_HELLO);
139 if (sigs & SIGF_STOP) stop = TRUE;
140 if (sigs & SIGF_HELLO) Signal(OTHERTASK, SIGF_HELLO);
145 /****************************************************************************************/
147 #undef OTHERTASK
148 #define OTHERTASK task1
150 static void Task2(void)
152 for(;;)
154 ULONG sigs;
155 BOOL stop = FALSE;
157 SetSignal(0, SIGF_HELLO);
158 Wait(SIGF_INIT);
159 Signal(maintask, SIGF_READY);
161 while(!stop)
163 sigs = Wait(SIGF_STOP | SIGF_HELLO);
164 if (sigs & SIGF_STOP) stop = TRUE;
165 if (sigs & SIGF_HELLO)
167 Signal(OTHERTASK, SIGF_HELLO);
168 counter++;
174 /****************************************************************************************/
176 static void MakeTasks(void)
178 maintask = FindTask(NULL);
180 task1 = CreateTask("Task 1", 0, Task1, AROS_STACKSIZE);
181 task2 = CreateTask("Task 2", 0, Task2, AROS_STACKSIZE);
183 if (!task1 || !task2) Cleanup("Can't create tasks!");
187 /****************************************************************************************/
189 static void Action(void)
191 struct RastPort *rp = win->RPort;
192 char *msg = "Starting to benchmark when countdown reaches 0. Countdown: 5";
193 WORD i;
195 WORD x = win->BorderLeft + 8;
196 WORD y = win->BorderTop + rp->TxHeight / 2;
197 WORD len = strlen(msg);
198 WORD pixellen = TextLength(rp, msg, len);
200 Signal(task1, SIGF_INIT);
201 Wait(SIGF_READY);
203 Signal(task2, SIGF_INIT);
204 Wait(SIGF_READY);
206 ModifyIDCMP(win, IDCMP_VANILLAKEY);
208 SetAPen(rp, 0);
209 RectFill(rp, x, y, win->Width - win->BorderRight - 1, y + rp->TxHeight - 1);
211 SetAPen(rp, 1);
212 Move(rp, x, y + rp->TxBaseline);
213 Text(rp, msg, len);
215 x += pixellen - rp->TxWidth;
217 for(i = 5; i>= 0; i--)
219 char c = '0' + i;
221 Delay(25); /* should be 50 (1 sec), but AROS clock is somewhat slow */
223 Move(rp, x, y + rp->TxBaseline);
224 Text(rp, &c, 1);
227 /* start everything by sending hello to first task */
229 Signal(task1, SIGF_HELLO);
231 WaitPort(win->UserPort);
232 ReplyMsg(GetMsg(win->UserPort));
234 /* stop */
235 Signal(task1, SIGF_STOP);
236 Signal(task2, SIGF_STOP);
238 ModifyIDCMP(win, IDCMP_VANILLAKEY | IDCMP_CLOSEWINDOW | IDCMP_REFRESHWINDOW);
240 x -= (pixellen - rp->TxWidth);
242 SetAPen(rp, 0);
243 RectFill(rp, x, y, win->Width - win->BorderRight - 1, y + rp->TxHeight - 1);
245 SetAPen(rp, 1);
247 sprintf(s, "Benchmark result: %ld",counter);
249 Move(rp, x, y + rp->TxBaseline);
250 Text(rp, s, strlen(s));
252 counter = 0;
255 /****************************************************************************************/
257 static void HandleAll(void)
259 struct IntuiMessage *msg;
261 BOOL quitme = FALSE;
263 while(!quitme)
265 WaitPort(win->UserPort);
266 while ((msg = (struct IntuiMessage *)GetMsg(win->UserPort)))
268 switch(msg->Class)
270 case IDCMP_CLOSEWINDOW:
271 quitme = TRUE;
272 break;
274 case IDCMP_REFRESHWINDOW:
275 BeginRefresh(win);
276 EndRefresh(win,TRUE);
277 break;
279 case IDCMP_VANILLAKEY:
280 if (msg->Code == 13) Action();
281 break;
284 ReplyMsg((struct Message *)msg);
289 /****************************************************************************************/
291 int main(void)
293 OpenLibs();
294 GetVisual();
295 MakeWin();
296 MakeTasks();
297 HandleAll();
298 Cleanup(0);
299 return 0;
302 /****************************************************************************************/
303 /****************************************************************************************/
304 /****************************************************************************************/
305 /****************************************************************************************/