Rune - Add multi-character single-quoted constants
[rune.git] / tests / flock.d
1 #!/usr/local/bin/rune -x
2 #
3
4 import "sys";
5 import <stdio>;
6
7 int
8 main(int ac, char **av)
9 {
10         Fs fs;
11         size_t off;
12
13         if (ac == 1) {
14                 stdio.stdout->format("Supply filename for flock test\n");
15                 Sys.exit(1);
16         }
17
18         if (fs.open(av[1], O_RDWR) < 0) {
19                 stdio.stdout->format("open(%s) failed error %d\n",
20                                      av[1], fs.error);
21         } else if (fs.flock(LOCK_EX | LOCK_NB) < 0) {
22                 stdio.stdout->format("flock() failed error %d\n",
23                                      fs.error);
24         } else {
25                 stdio.stdout->format("flock() suceeded error %d\n",
26                                      fs.error);
27                 stdio.stdout->format("sleeping for 2 seconds\n");
28                 Thread.mssleep(2000);
29                 if (fs.flock(LOCK_UN) < 0)
30                         stdio.stdout->format("flock(LOCK_UN) "
31                                              "failed error %d\n",
32                                              fs.error);
33         }
34
35         # Should obtain lock on each loop and destructor should
36         # release it on each loop.
37         #
38         for (off = 0; off < 1024Z; off += 32Z) {
39                 FdLock fl;
40
41                 fl.start = off;
42                 fl.len = 32;
43                 fl.type = fl.F_WRLCK;
44                 fl.fd = fs.fd;
45                 stdio.stdout->format("lock %d\n", off);
46                 fl.fsetlkw();
47                 stdio.stdout->format("result %d --> %d type=%d\n", off, fl.error, fl.type);
48                 Thread.mssleep(2000);
49                 stdio.stdout->format("unlock %d\n", off);
50         }
51 }