Minor clean-up.
[AROS.git] / test / clib / flock.c
blob6dce49ba53a2ea7f5673c63d77df106075b045fc
1 /*
2 Copyright © 1995-2014, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include <sys/file.h>
7 #include <stdio.h>
8 #include <fcntl.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <exec/types.h>
12 #include <proto/dos.h>
13 #include <dos/dos.h>
14 #include <proto/exec.h>
15 #include "test.h"
17 char *tmpname;
18 struct SignalSemaphore sem;
20 #define ITERATIONS 50
21 #define NPROCS 10
23 LONG entry()
25 struct Task *this = FindTask(NULL);
26 Wait(SIGBREAKF_CTRL_C);
27 int *counter = this->tc_UserData;
28 int i, fd;
29 struct Library *DOSBase;
31 DOSBase = OpenLibrary("dos.library", 0);
32 if(!DOSBase)
33 return -1;
35 ObtainSemaphore(&sem);
36 fd = open(tmpname, O_RDONLY);
37 ReleaseSemaphore(&sem);
39 for(i = 0; i < ITERATIONS; i++)
41 if(!flock(fd, LOCK_EX))
43 int tmp = *counter;
44 Delay(1);
45 *counter = tmp + 1;
46 printf("\rprogress: %.2f%%", *counter * 100.0 / (NPROCS * ITERATIONS));
47 flock(fd, LOCK_UN);
49 else
51 close(fd);
52 return -1;
56 close(fd);
58 CloseLibrary(DOSBase);
59 return 0;
62 int main()
64 tmpname = mktemp("T:flockXXXXXX");
65 int fd = open(tmpname, O_CREAT);
66 TEST((fd != -1));
68 TEST((flock(fd, LOCK_SH|LOCK_NB) == 0));
69 TEST((flock(fd, LOCK_UN) == 0));
71 TEST((flock(fd, LOCK_EX|LOCK_NB) == 0));
72 TEST((flock(fd, LOCK_UN) == 0));
74 TEST((flock(fd, LOCK_SH) == 0));
75 TEST((flock(fd, LOCK_UN) == 0));
77 TEST((flock(fd, LOCK_EX) == 0));
78 TEST((flock(fd, LOCK_UN) == 0));
80 close(fd);
82 /* Create NPROCS processes increasing counter ITERATIONS times in an ugly
83 way */
84 int counter = 0;
85 struct Process *procs[NPROCS];
86 ULONG ids[NPROCS];
87 struct TagItem tags[] =
89 { NP_Entry, (IPTR) entry },
90 { NP_Name, (IPTR) "flocker" },
91 { NP_Output, (IPTR) Output() },
92 { NP_CloseOutput, (IPTR) FALSE },
93 { NP_UserData, (IPTR) &counter },
94 { NP_NotifyOnDeath, (IPTR) TRUE },
95 { TAG_DONE, 0 }
98 int i;
99 InitSemaphore(&sem);
100 for(i = 0; i < NPROCS; i++)
102 procs[i] = CreateNewProc(tags);
103 TEST((procs[i]));
104 ids[i] = GetETask(procs[i])->et_UniqueID;
105 Signal((struct Task *)procs[i], SIGBREAKF_CTRL_C);
108 for(i = 0; i < NPROCS; i++)
110 ChildWait(ids[i]);
111 ChildFree(ids[i]);
113 putchar('\n');
115 TEST((counter == NPROCS * ITERATIONS));
117 cleanup();
118 return OK;
121 void cleanup()
123 remove(tmpname);