arosc.library: Compiler delint
[AROS.git] / test / clib / flock.c
blob5ebcac75d021de13bf704314211c9f4e9b8c9a24
1 #include <sys/file.h>
2 #include <stdio.h>
3 #include <fcntl.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6 #include <exec/types.h>
7 #include <proto/dos.h>
8 #include <dos/dos.h>
9 #include <proto/exec.h>
10 #include "test.h"
12 char *tmpname;
13 struct SignalSemaphore sem;
15 #define ITERATIONS 50
16 #define NPROCS 10
18 LONG entry()
20 struct Task *this = FindTask(NULL);
21 Wait(SIGBREAKF_CTRL_C);
22 int *counter = this->tc_UserData;
23 int i, fd;
24 struct Library *DOSBase;
26 DOSBase = OpenLibrary("dos.library", 0);
27 if(!DOSBase)
28 return -1;
30 ObtainSemaphore(&sem);
31 fd = open(tmpname, O_RDONLY);
32 ReleaseSemaphore(&sem);
34 for(i = 0; i < ITERATIONS; i++)
36 if(!flock(fd, LOCK_EX))
38 int tmp = *counter;
39 Delay(1);
40 *counter = tmp + 1;
41 printf("\rprogress: %.2f%%", *counter * 100.0 / (NPROCS * ITERATIONS));
42 flock(fd, LOCK_UN);
44 else
46 close(fd);
47 return -1;
51 close(fd);
53 CloseLibrary(DOSBase);
54 return 0;
57 int main()
59 tmpname = mktemp("T:flockXXXXXX");
60 int fd = open(tmpname, O_CREAT);
61 TEST((fd != -1));
63 TEST((flock(fd, LOCK_SH|LOCK_NB) == 0));
64 TEST((flock(fd, LOCK_UN) == 0));
66 TEST((flock(fd, LOCK_EX|LOCK_NB) == 0));
67 TEST((flock(fd, LOCK_UN) == 0));
69 TEST((flock(fd, LOCK_SH) == 0));
70 TEST((flock(fd, LOCK_UN) == 0));
72 TEST((flock(fd, LOCK_EX) == 0));
73 TEST((flock(fd, LOCK_UN) == 0));
75 close(fd);
77 /* Create NPROCS processes increasing counter ITERATIONS times in an ugly
78 way */
79 int counter = 0;
80 struct Process *procs[NPROCS];
81 ULONG ids[NPROCS];
82 struct TagItem tags[] =
84 { NP_Entry, (IPTR) entry },
85 { NP_Name, (IPTR) "flocker" },
86 { NP_Output, (IPTR) Output() },
87 { NP_CloseOutput, (IPTR) FALSE },
88 { NP_UserData, (IPTR) &counter },
89 { NP_NotifyOnDeath, (IPTR) TRUE },
90 { TAG_DONE, 0 }
93 int i;
94 InitSemaphore(&sem);
95 for(i = 0; i < NPROCS; i++)
97 procs[i] = CreateNewProc(tags);
98 TEST((procs[i]));
99 ids[i] = GetETask(procs[i])->et_UniqueID;
100 Signal((struct Task *)procs[i], SIGBREAKF_CTRL_C);
103 for(i = 0; i < NPROCS; i++)
105 ChildWait(ids[i]);
106 ChildFree(ids[i]);
108 putchar('\n');
110 TEST((counter == NPROCS * ITERATIONS));
112 cleanup();
113 return OK;
116 void cleanup()
118 remove(tmpname);