2 Copyright © 1995-2002, The AROS Development Team. All rights reserved.
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>
13 struct SignalSemaphore
*ss
;
16 #define STACKSIZE 4096
18 static void entry(void)
22 ObtainSemaphoreShared(&ss
[2]);
29 ObtainSemaphoreShared(&ss
[k
]);
31 ReleaseSemaphore(&ss
[os
]);
37 Wait(0);/* Let the parent remove me */
40 int main(int argc
, char* argv
[])
44 struct SemaphoreMessage
*sm
;
49 sm
=(struct SemaphoreMessage
*)CreateIORequest(mp
,sizeof(struct SemaphoreMessage
));
52 ss
=(struct SignalSemaphore
*)AllocMem(3*sizeof(struct SignalSemaphore
),
53 MEMF_PUBLIC
|MEMF_CLEAR
);
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
);
66 s
=(UBYTE
*)AllocMem(STACKSIZE
, MEMF_PUBLIC
|MEMF_CLEAR
);
69 t
->tc_Node
.ln_Type
=NT_TASK
;
71 t
->tc_Node
.ln_Name
="new task";
73 t
->tc_SPUpper
=s
+STACKSIZE
;
74 #if AROS_STACK_GROWS_DOWNWARDS
75 t
->tc_SPReg
=(UBYTE
*)t
->tc_SPUpper
-SP_OFFSET
;
77 t
->tc_SPReg
=(UBYTE
*)t
->tc_SPLower
-SP_OFFSET
;
79 NEWLIST(&t
->tc_MemEntry
);
80 AddTask(t
,&entry
,NULL
);
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
);
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]);
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]);
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]);
133 sm
->ssm_Message
.mn_Node
.ln_Name
=(char *)SM_EXCLUSIVE
;
135 printf("task 1: posted request for %d\n",k
);
136 printf("task 2: %d %d %d\n",u
[0],u
[1],u
[2]);
138 printf("task 1: got it\n");
140 printf("task 1: didn't get it\n");
141 printf("task 2: %d %d %d\n",u
[0],u
[1],u
[2]);
143 printf("task 1: released %d\n",k
);
144 printf("task 2: %d %d %d\n",u
[0],u
[1],u
[2]);
150 sm
->ssm_Message
.mn_Node
.ln_Name
=(char *)SM_SHARED
;
152 printf("task 1: posted request for %d\n",k
);
153 printf("task 2: %d %d %d\n",u
[0],u
[1],u
[2]);
155 printf("task 1: got it\n");
157 printf("task 1: didn't get it\n");
158 printf("task 2: %d %d %d\n",u
[0],u
[1],u
[2]);
160 printf("task 1: released %d\n",k
);
161 printf("task 2: %d %d %d\n",u
[0],u
[1],u
[2]);
166 FreeMem(s
,STACKSIZE
);
168 FreeMem(t
,sizeof(struct Task
));
170 FreeMem(ss
,3*sizeof(struct SignalSemaphore
));
172 DeleteIORequest((struct IORequest
*)sm
);