2 * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
4 * $Id: lockFile.c,v 1.1 2001/05/18 16:54:06 simonmar Exp $
6 * stdin/stout/stderr Runtime Support
12 #define FD_SETSIZE 256
21 static Lock readLock[FD_SETSIZE];
22 static Lock writeLock[FD_SETSIZE];
24 static int readLocks = 0;
25 static int writeLocks = 0;
28 lockFile(int fd, int for_writing, int exclusive)
34 /* opening a file for writing, check to see whether
35 we don't have any read locks on it already.. */
36 for (i = 0; i < readLocks; i++) {
37 if (readLock[i].inode == sb.st_ino && readLock[i].device == sb.st_dev) {
45 /* If we're determined that there is only a single
46 writer to the file, check to see whether the file
47 hasn't already been opened for writing..
50 for (i = 0; i < writeLocks; i++) {
51 if (writeLock[i].inode == sb.st_ino && writeLock[i].device == sb.st_dev) {
60 /* OK, everything is cool lock-wise, record it and leave. */
62 writeLock[i].device = sb.st_dev;
63 writeLock[i].inode = sb.st_ino;
67 /* For reading, it's simpler - just check to see
68 that there's no-one writing to the underlying file. */
69 for (i = 0; i < writeLocks; i++) {
70 if (writeLock[i].inode == sb.st_ino && writeLock[i].device == sb.st_dev) {
78 /* Fit in new entry, reusing an existing table entry, if possible. */
79 for (i = 0; i < readLocks; i++) {
80 if (readLock[i].inode == sb.st_ino && readLock[i].device == sb.st_dev) {
85 readLock[i].device = sb.st_dev;
86 readLock[i].inode = sb.st_ino;
98 for (i = 0; i < readLocks; i++)
99 if (readLock[i].fd == fd) {
100 while (++i < readLocks)
101 readLock[i - 1] = readLock[i];
106 for (i = 0; i < writeLocks; i++)
107 if (writeLock[i].fd == fd) {
108 while (++i < writeLocks)
109 writeLock[i - 1] = writeLock[i];
113 /* Signal that we did not find an entry */