Workaround for includes conflict that stopped compilation with GCC 3.
[cake.git] / test / clib / flock.c
blob6675320b7271a5ad18d4c27f77de08b2f6625a43
1 #include <sys/file.h>
2 #include <stdio.h>
3 #include <fcntl.h>
4 #include <stdlib.h>
5 #include <exec/types.h>
6 #include <proto/dos.h>
7 #include <dos/dos.h>
8 #include <proto/exec.h>
9 #include "test.h"
11 char *tmpname;
13 #define ITERATIONS 50
14 #define NPROCS 10
16 LONG entry()
18 struct Task *this = FindTask(NULL);
19 Wait(SIGBREAKF_CTRL_C);
20 int *counter = this->tc_UserData;
21 int i;
22 struct Library *aroscbase;
24 aroscbase = OpenLibrary("arosc.library", 0);
25 if(!aroscbase)
26 return -1;
28 for(i = 0; i < ITERATIONS; i++)
30 int fd = open(tmpname, 0);
31 if(!flock(fd, LOCK_EX))
33 int tmp = *counter;
34 Delay(1);
35 *counter = tmp + 1;
36 printf("\rprogress: %.2f%%", *counter * 100.0 / (NPROCS * ITERATIONS));
37 flock(fd, LOCK_UN);
39 else
41 close(fd);
42 return -1;
44 close(fd);
47 CloseLibrary(aroscbase);
48 return 0;
51 int fd;
53 int main()
55 tmpname = mktemp("T:flockXXXXXX");
56 int fd = open(tmpname, O_CREAT);
57 TEST((fd != -1));
59 TEST((flock(fd, LOCK_SH|LOCK_NB) == 0));
60 TEST((flock(fd, LOCK_UN) == 0));
62 TEST((flock(fd, LOCK_EX|LOCK_NB) == 0));
63 TEST((flock(fd, LOCK_UN) == 0));
65 TEST((flock(fd, LOCK_SH) == 0));
66 TEST((flock(fd, LOCK_UN) == 0));
68 TEST((flock(fd, LOCK_EX) == 0));
69 TEST((flock(fd, LOCK_UN) == 0));
71 /* Create NPROCS processes increasing counter ITERATIONS times in an ugly
72 way */
73 int counter = 0;
74 struct Process *procs[NPROCS];
75 APTR ids[NPROCS];
76 struct TagItem tags[] =
78 { NP_Entry, (IPTR) entry },
79 { NP_Name, (IPTR) "flocker" },
80 { NP_Output, (IPTR) Output() },
81 { NP_CloseOutput, (IPTR) FALSE },
82 { NP_UserData, (IPTR) &counter },
83 { NP_NotifyOnDeath, (IPTR) TRUE },
84 { TAG_DONE, 0 }
87 int i;
88 for(i = 0; i < NPROCS; i++)
90 procs[i] = CreateNewProc(tags);
91 TEST((procs[i]));
92 ids[i] = GetETask(procs[i])->et_UniqueID;
93 Signal(procs[i], SIGBREAKF_CTRL_C);
96 for(i = 0; i < NPROCS; i++)
98 ChildWait(ids[i]);
99 ChildFree(ids[i]);
101 putchar('\n');
103 TEST((counter == NPROCS * ITERATIONS));
105 cleanup();
106 return OK;
109 void cleanup()
111 close(fd);
112 remove(tmpname);