Resurrected incomplete Intel graphics driver in case anyone wants to
[cake.git] / test / threads / broadcastcondition.c
blobbca2a309084dfceef549e2e3178ac684f8e4a938
1 #include <exec/memory.h>
2 #include <libraries/thread.h>
3 #include <proto/exec.h>
4 #include <proto/dos.h>
5 #include <proto/thread.h>
6 #include <stdio.h>
7 #include <stdint.h>
9 struct thread_data {
10 void *mutex;
11 void *cond;
14 void *waiter_thread(void *data) {
15 struct thread_data *td = (struct thread_data *) data;
16 uint32_t id = CurrentThread();
18 printf("[%d] starting, locking the mutex\n", id);
19 LockMutex(td->mutex);
21 printf("[%d] waiting on the condition\n", id);
22 WaitCondition(td->cond, td->mutex);
24 printf("[%d] condition signalled, unlocking the mutex\n", id);
25 UnlockMutex(td->mutex);
27 printf("[%d] all done, exiting\n", id);
29 return NULL;
32 int main (int argc, char **argv) {
33 struct thread_data *td;
34 int i;
36 td = AllocMem(sizeof(struct thread_data), MEMF_PUBLIC | MEMF_CLEAR);
38 printf("creating mutex\n");
39 td->mutex = CreateMutex();
41 printf("creating condition\n");
42 td->cond = CreateCondition();
44 printf("starting waiter threads\n");
45 for (i = 0; i < 5; i++)
46 CreateThread(waiter_thread, (void *) td);
48 printf("sleeping for 2s\n");
49 Delay(100);
51 printf("signalling condition\n");
52 SignalCondition(td->cond);
54 printf("sleeping for 2s\n");
55 Delay(100);
57 printf("broadcasting condition\n");
58 BroadcastCondition(td->cond);
60 printf("waiting for threads to exit\n");
61 WaitAllThreads();
63 printf("destroying the condition\n");
64 DestroyCondition(td->cond);
66 printf("destroying the mutex\n");
67 DestroyMutex(td->mutex);
69 FreeMem(td, sizeof(struct thread_data));
71 printf("all done\n");
73 return 0;