Copyright clean-up (part 1):
[AROS.git] / test / benchmarks / exec / taskswitch.c
blob35c67ec79000b0caf4f180495a6433cdfc7a3e88
1 /*
2 Copyright © 1995-2014, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include <dos/dos.h>
7 #include <intuition/intuition.h>
8 #include <graphics/gfx.h>
9 #include <utility/hooks.h>
11 #include <proto/exec.h>
12 #include <proto/dos.h>
13 #include <proto/intuition.h>
14 #include <proto/graphics.h>
16 #include <clib/alib_protos.h>
18 #include <stdio.h>
19 #include <string.h>
20 #include <stdlib.h>
22 /****************************************************************************************/
24 #define SIGF_READY SIGBREAKF_CTRL_C
26 #define SIGF_INIT SIGBREAKF_CTRL_C
27 #define SIGF_STOP SIGBREAKF_CTRL_D
28 #define SIGF_HELLO SIGBREAKF_CTRL_E
30 /****************************************************************************************/
32 struct IntuitionBase *IntuitionBase;
33 struct GfxBase *GfxBase;
35 static struct Screen *scr;
36 static struct Window *win;
37 static struct Task *task1, *task2, *maintask;
39 static LONG counter;
41 static char s[255];
43 /****************************************************************************************/
45 static void Cleanup(char *msg)
47 WORD rc;
49 if (msg)
51 printf("taskswitchbench: %s\n",msg);
52 rc = RETURN_WARN;
53 } else {
54 rc = RETURN_OK;
57 Forbid();
58 if (task1) DeleteTask(task1);
59 if (task2) DeleteTask(task2);
60 Permit();
62 if (win) CloseWindow(win);
63 if (scr) UnlockPubScreen(0,scr);
65 if (GfxBase) CloseLibrary((struct Library *)GfxBase);
66 if (IntuitionBase) CloseLibrary((struct Library *)IntuitionBase);
68 exit(rc);
71 /****************************************************************************************/
73 static void OpenLibs(void)
75 if (!(IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",39)))
77 Cleanup("Can't open intuition.library V39!");
80 if (!(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",39)))
82 Cleanup("Can't open graphics.library V39!");
87 /****************************************************************************************/
89 static void GetVisual(void)
91 if (!(scr = LockPubScreen(0)))
93 Cleanup("Can't lock pub screen!");
97 /****************************************************************************************/
99 static void MakeWin(void)
101 if (!(win = OpenWindowTags(0,WA_PubScreen,(IPTR)scr,
102 WA_Left,10,
103 WA_Top,10,
104 WA_Width,600,
105 WA_Height,scr->WBorTop + scr->Font->ta_YSize + 1 +
106 scr->WBorBottom +
107 scr->Font->ta_YSize * 2,
108 WA_Title,(IPTR)"Press RETURN key to start and any key to stop!",
109 WA_SimpleRefresh,TRUE,
110 WA_CloseGadget,TRUE,
111 WA_DepthGadget,TRUE,
112 WA_DragBar,TRUE,
113 WA_Activate,TRUE,
114 WA_IDCMP,IDCMP_CLOSEWINDOW |
115 IDCMP_VANILLAKEY |
116 IDCMP_REFRESHWINDOW,
117 TAG_DONE)))
119 Cleanup("Can't open window!");
122 ScreenToFront(win->WScreen);
126 /****************************************************************************************/
128 #define OTHERTASK task2
130 static void Task1(void)
132 for(;;)
134 ULONG sigs;
135 BOOL stop = FALSE;
137 SetSignal(0, SIGF_HELLO);
138 Wait(SIGF_INIT);
139 Signal(maintask, SIGF_READY);
141 while(!stop)
143 sigs = Wait(SIGF_STOP | SIGF_HELLO);
144 if (sigs & SIGF_STOP) stop = TRUE;
145 if (sigs & SIGF_HELLO) Signal(OTHERTASK, SIGF_HELLO);
150 /****************************************************************************************/
152 #undef OTHERTASK
153 #define OTHERTASK task1
155 static void Task2(void)
157 for(;;)
159 ULONG sigs;
160 BOOL stop = FALSE;
162 SetSignal(0, SIGF_HELLO);
163 Wait(SIGF_INIT);
164 Signal(maintask, SIGF_READY);
166 while(!stop)
168 sigs = Wait(SIGF_STOP | SIGF_HELLO);
169 if (sigs & SIGF_STOP) stop = TRUE;
170 if (sigs & SIGF_HELLO)
172 Signal(OTHERTASK, SIGF_HELLO);
173 counter++;
179 /****************************************************************************************/
181 static void MakeTasks(void)
183 maintask = FindTask(NULL);
185 task1 = CreateTask("Task 1", 0, Task1, AROS_STACKSIZE);
186 task2 = CreateTask("Task 2", 0, Task2, AROS_STACKSIZE);
188 if (!task1 || !task2) Cleanup("Can't create tasks!");
192 /****************************************************************************************/
194 static void Action(void)
196 struct RastPort *rp = win->RPort;
197 char *msg = "Starting to benchmark when countdown reaches 0. Countdown: 5";
198 WORD i;
200 WORD x = win->BorderLeft + 8;
201 WORD y = win->BorderTop + rp->TxHeight / 2;
202 WORD len = strlen(msg);
203 WORD pixellen = TextLength(rp, msg, len);
205 Signal(task1, SIGF_INIT);
206 Wait(SIGF_READY);
208 Signal(task2, SIGF_INIT);
209 Wait(SIGF_READY);
211 ModifyIDCMP(win, IDCMP_VANILLAKEY);
213 SetAPen(rp, 0);
214 RectFill(rp, x, y, win->Width - win->BorderRight - 1, y + rp->TxHeight - 1);
216 SetAPen(rp, 1);
217 Move(rp, x, y + rp->TxBaseline);
218 Text(rp, msg, len);
220 x += pixellen - rp->TxWidth;
222 for(i = 5; i>= 0; i--)
224 char c = '0' + i;
226 Delay(25); /* should be 50 (1 sec), but AROS clock is somewhat slow */
228 Move(rp, x, y + rp->TxBaseline);
229 Text(rp, &c, 1);
232 /* start everything by sending hello to first task */
234 Signal(task1, SIGF_HELLO);
236 WaitPort(win->UserPort);
237 ReplyMsg(GetMsg(win->UserPort));
239 /* stop */
240 Signal(task1, SIGF_STOP);
241 Signal(task2, SIGF_STOP);
243 ModifyIDCMP(win, IDCMP_VANILLAKEY | IDCMP_CLOSEWINDOW | IDCMP_REFRESHWINDOW);
245 x -= (pixellen - rp->TxWidth);
247 SetAPen(rp, 0);
248 RectFill(rp, x, y, win->Width - win->BorderRight - 1, y + rp->TxHeight - 1);
250 SetAPen(rp, 1);
252 sprintf(s, "Benchmark result: %ld",counter);
254 Move(rp, x, y + rp->TxBaseline);
255 Text(rp, s, strlen(s));
257 counter = 0;
260 /****************************************************************************************/
262 static void HandleAll(void)
264 struct IntuiMessage *msg;
266 BOOL quitme = FALSE;
268 while(!quitme)
270 WaitPort(win->UserPort);
271 while ((msg = (struct IntuiMessage *)GetMsg(win->UserPort)))
273 switch(msg->Class)
275 case IDCMP_CLOSEWINDOW:
276 quitme = TRUE;
277 break;
279 case IDCMP_REFRESHWINDOW:
280 BeginRefresh(win);
281 EndRefresh(win,TRUE);
282 break;
284 case IDCMP_VANILLAKEY:
285 if (msg->Code == 13) Action();
286 break;
289 ReplyMsg((struct Message *)msg);
294 /****************************************************************************************/
296 int main(void)
298 OpenLibs();
299 GetVisual();
300 MakeWin();
301 MakeTasks();
302 HandleAll();
303 Cleanup(0);
304 return 0;
307 /****************************************************************************************/
308 /****************************************************************************************/
309 /****************************************************************************************/
310 /****************************************************************************************/