Added some (default) icons for files/dirs used by boot floppies.
[cake.git] / test / semaphoretest.c
blobee2ea3f6888f74906b3d2d0936d684a50b6deaeb
1 /*
2 Copyright © 1995-2002, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include <proto/exec.h>
7 #include <exec/memory.h>
8 #include <exec/tasks.h>
9 #include <exec/semaphores.h>
10 #include <clib/exec_protos.h>
11 #include <stdio.h>
13 struct SignalSemaphore *ss;
14 int u[3];
16 #define STACKSIZE 4096
18 static void entry(void)
20 int i,k,os=2;
21 u[2]=1;
22 ObtainSemaphoreShared(&ss[2]);
23 u[2]=2;
25 for(i=0;i<10;i++)
26 for(k=0;k<3;k++)
28 u[k]=1;
29 ObtainSemaphoreShared(&ss[k]);
30 u[k]=2;
31 ReleaseSemaphore(&ss[os]);
32 u[os]=0;
34 os=k;
37 Wait(0);/* Let the parent remove me */
40 int main(int argc, char* argv[])
42 struct Task *t;
43 struct MsgPort *mp;
44 struct SemaphoreMessage *sm;
46 mp=CreateMsgPort();
47 if(mp!=NULL)
49 sm=(struct SemaphoreMessage *)CreateIORequest(mp,sizeof(struct SemaphoreMessage));
50 if(sm!=NULL)
52 ss=(struct SignalSemaphore *)AllocMem(3*sizeof(struct SignalSemaphore),
53 MEMF_PUBLIC|MEMF_CLEAR);
54 if(ss!=NULL)
56 InitSemaphore(&ss[0]);
57 InitSemaphore(&ss[1]);
58 InitSemaphore(&ss[2]);
59 ObtainSemaphore(&ss[2]);
60 printf("task 1: got %d\n",2);
62 t=(struct Task *)AllocMem(sizeof(struct Task), MEMF_PUBLIC|MEMF_CLEAR);
63 if(t!=NULL)
65 UBYTE *s;
66 s=(UBYTE *)AllocMem(STACKSIZE, MEMF_PUBLIC|MEMF_CLEAR);
67 if(s!=NULL)
69 t->tc_Node.ln_Type=NT_TASK;
70 t->tc_Node.ln_Pri=1;
71 t->tc_Node.ln_Name="new task";
72 t->tc_SPLower=s;
73 t->tc_SPUpper=s+STACKSIZE;
74 #if AROS_STACK_GROWS_DOWNWARDS
75 t->tc_SPReg=(UBYTE *)t->tc_SPUpper-SP_OFFSET;
76 #else
77 t->tc_SPReg=(UBYTE *)t->tc_SPLower-SP_OFFSET;
78 #endif
79 NEWLIST(&t->tc_MemEntry);
80 AddTask(t,&entry,NULL);
83 int k,os=2;
84 for(k=0;k<3;k++)
86 printf("task 2: %d %d %d\n",u[0],u[1],u[2]);
87 ObtainSemaphore(&ss[k]);
88 printf("task 1: got %d\n",k);
89 printf("task 2: %d %d %d\n",u[0],u[1],u[2]);
90 ReleaseSemaphore(&ss[os]);
91 printf("task 1: released %d\n",os);
93 os=k;
95 printf("task 2: %d %d %d\n",u[0],u[1],u[2]);
97 ObtainSemaphoreShared(&ss[0]);
98 printf("task 1: got shared %d\n",0);
99 printf("task 2: %d %d %d\n",u[0],u[1],u[2]);
100 ObtainSemaphoreShared(&ss[1]);
101 printf("task 1: got shared %d\n",1);
102 printf("task 2: %d %d %d\n",u[0],u[1],u[2]);
104 ReleaseSemaphore(&ss[0]);
105 printf("task 1: released %d\n",0);
106 printf("task 2: %d %d %d\n",u[0],u[1],u[2]);
107 ReleaseSemaphore(&ss[1]);
108 printf("task 1: released %d\n",1);
109 printf("task 2: %d %d %d\n",u[0],u[1],u[2]);
111 for(k=0;k<3;k++)
112 if(AttemptSemaphore(&ss[k]))
114 printf("task 1: got %d\n",k);
115 printf("task 2: %d %d %d\n",u[0],u[1],u[2]);
116 ReleaseSemaphore(&ss[k]);
117 printf("task 1: released %d\n",k);
118 printf("task 2: %d %d %d\n",u[0],u[1],u[2]);
121 for(k=0;k<3;k++)
122 if(k!=os&&AttemptSemaphoreShared(&ss[k]))
124 printf("task 1: got %d\n",k);
125 printf("task 2: %d %d %d\n",u[0],u[1],u[2]);
126 ReleaseSemaphore(&ss[k]);
127 printf("task 1: released %d\n",k);
128 printf("task 2: %d %d %d\n",u[0],u[1],u[2]);
131 for(k=0;k<3;k++)
133 sm->ssm_Message.mn_Node.ln_Name=(char *)SM_EXCLUSIVE;
134 Procure(&ss[k],sm);
135 printf("task 1: posted request for %d\n",k);
136 printf("task 2: %d %d %d\n",u[0],u[1],u[2]);
137 if(GetMsg(mp)!=NULL)
138 printf("task 1: got it\n");
139 else
140 printf("task 1: didn't get it\n");
141 printf("task 2: %d %d %d\n",u[0],u[1],u[2]);
142 Vacate(&ss[k],sm);
143 printf("task 1: released %d\n",k);
144 printf("task 2: %d %d %d\n",u[0],u[1],u[2]);
147 for(k=0;k<3;k++)
148 if(k!=os)
150 sm->ssm_Message.mn_Node.ln_Name=(char *)SM_SHARED;
151 Procure(&ss[k],sm);
152 printf("task 1: posted request for %d\n",k);
153 printf("task 2: %d %d %d\n",u[0],u[1],u[2]);
154 if(GetMsg(mp)!=NULL)
155 printf("task 1: got it\n");
156 else
157 printf("task 1: didn't get it\n");
158 printf("task 2: %d %d %d\n",u[0],u[1],u[2]);
159 Vacate(&ss[k],sm);
160 printf("task 1: released %d\n",k);
161 printf("task 2: %d %d %d\n",u[0],u[1],u[2]);
164 RemTask(t);
166 FreeMem(s,STACKSIZE);
168 FreeMem(t,sizeof(struct Task));
170 FreeMem(ss,3*sizeof(struct SignalSemaphore));
172 DeleteIORequest((struct IORequest *)sm);
174 DeleteMsgPort(mp);
176 return 0;