2 * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
4 * $Id: lockFile.c,v 1.2 2002/02/07 11:13:30 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)
33 while (fstat(fd, &sb) < 0) {
38 /* fstat()ing socket fd's seems to fail with CRT's fstat(),
39 so let's just silently return and hope for the best..
47 /* opening a file for writing, check to see whether
48 we don't have any read locks on it already.. */
49 for (i = 0; i < readLocks; i++) {
50 if (readLock[i].inode == sb.st_ino && readLock[i].device == sb.st_dev) {
58 /* If we're determined that there is only a single
59 writer to the file, check to see whether the file
60 hasn't already been opened for writing..
63 for (i = 0; i < writeLocks; i++) {
64 if (writeLock[i].inode == sb.st_ino && writeLock[i].device == sb.st_dev) {
73 /* OK, everything is cool lock-wise, record it and leave. */
75 writeLock[i].device = sb.st_dev;
76 writeLock[i].inode = sb.st_ino;
80 /* For reading, it's simpler - just check to see
81 that there's no-one writing to the underlying file. */
82 for (i = 0; i < writeLocks; i++) {
83 if (writeLock[i].inode == sb.st_ino && writeLock[i].device == sb.st_dev) {
91 /* Fit in new entry, reusing an existing table entry, if possible. */
92 for (i = 0; i < readLocks; i++) {
93 if (readLock[i].inode == sb.st_ino && readLock[i].device == sb.st_dev) {
98 readLock[i].device = sb.st_dev;
99 readLock[i].inode = sb.st_ino;
111 for (i = 0; i < readLocks; i++)
112 if (readLock[i].fd == fd) {
113 while (++i < readLocks)
114 readLock[i - 1] = readLock[i];
119 for (i = 0; i < writeLocks; i++)
120 if (writeLock[i].fd == fd) {
121 while (++i < writeLocks)
122 writeLock[i - 1] = writeLock[i];
126 /* Signal that we did not find an entry */