diff options
author | simonmar <unknown> | 2001-05-18 16:54:11 +0000 |
---|---|---|
committer | simonmar <unknown> | 2001-05-18 16:54:11 +0000 |
commit | d9af408e5c512501cfa991f5e4a76c9154bca917 (patch) | |
tree | 627365b7dad9c2a5e1d892a1f631690b46e24a11 /ghc/lib/std/cbits/lockFile.c | |
parent | 5f6f90850d5c82dc56d13bbc035d635e1cb2106b (diff) | |
download | haskell-d9af408e5c512501cfa991f5e4a76c9154bca917.tar.gz |
[project @ 2001-05-18 16:54:04 by simonmar]
I/O library rewrite
-------------------
This commit replaces the old C/Haskell I/O implementation with a new
Haskell-only one using the new FFI & hsc2hs.
main points:
- lots of code deleted: we're about 3000 lines of C lighter,
but the amount of Haskell code is about the same.
- performance is ok: some operations are faster, others are
slower. There's still some tuning to do, though.
- the new library is designed to handle read/write streams
much better: a read/write stream gets a special kind of
handle internally called a "DuplexHandle", which actually
contains two separate handles, one for writing and one for
reading. The upshot is that you can do simultaneous reading
and writing to/from a socket or FIFO without any locking
problems. The effect is similar to calling socketToHandle
twice, except that finalization works properly (creating
two separate Handles could lead to the socket being closed
too early when one of the Handles is GC'd).
- hConnectTo and withHandleFor are gone (no one responded to
my mail on GHC users, but we can always bring 'em back if
necessary).
- I made a half-hearted attempt at keeping the system-specific
code in one place: see PrelPosix.hsc.
- I've rearranged the I/O tests and added lots more.
ghc/tests/lib/IO now contains Haskell 98-only IO tests,
ghc/test/lib/{IOExts, Directory, Time} now contain tests for
the relevant libraries. I haven't quite finished in here yet,
the IO tests work but the others don't yet.
- I haven't done anything about Unicode yet, but now we can
start to discuss what needs doing here. The new library
is using MutableByteArrays for its buffers because that
turned out to be a *lot* easier (and quicker) than malloc'd
buffers - I hope this won't cause trouble for unicode
translations though.
WARNING: Windows users refrain from updating until we've had a chance
to fix any issues that arise.
Testing: the basic H98 stuff has been pretty thoroughly tested, but
the new duplex handle stuff is still a little green.
Diffstat (limited to 'ghc/lib/std/cbits/lockFile.c')
-rw-r--r-- | ghc/lib/std/cbits/lockFile.c | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/ghc/lib/std/cbits/lockFile.c b/ghc/lib/std/cbits/lockFile.c new file mode 100644 index 0000000000..14a0a38854 --- /dev/null +++ b/ghc/lib/std/cbits/lockFile.c @@ -0,0 +1,115 @@ +/* + * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998 + * + * $Id: lockFile.c,v 1.1 2001/05/18 16:54:06 simonmar Exp $ + * + * stdin/stout/stderr Runtime Support + */ + +#include "HsStd.h" + +#ifndef FD_SETSIZE +#define FD_SETSIZE 256 +#endif + +typedef struct { + dev_t device; + ino_t inode; + int fd; +} Lock; + +static Lock readLock[FD_SETSIZE]; +static Lock writeLock[FD_SETSIZE]; + +static int readLocks = 0; +static int writeLocks = 0; + +int +lockFile(int fd, int for_writing, int exclusive) +{ + int i; + struct stat sb; + + if (for_writing) { + /* opening a file for writing, check to see whether + we don't have any read locks on it already.. */ + for (i = 0; i < readLocks; i++) { + if (readLock[i].inode == sb.st_ino && readLock[i].device == sb.st_dev) { +#ifndef __MINGW32__ + return -1; +#else + break; +#endif + } + } + /* If we're determined that there is only a single + writer to the file, check to see whether the file + hasn't already been opened for writing.. + */ + if (exclusive) { + for (i = 0; i < writeLocks; i++) { + if (writeLock[i].inode == sb.st_ino && writeLock[i].device == sb.st_dev) { +#ifndef __MINGW32__ + return -1; +#else + break; +#endif + } + } + } + /* OK, everything is cool lock-wise, record it and leave. */ + i = writeLocks++; + writeLock[i].device = sb.st_dev; + writeLock[i].inode = sb.st_ino; + writeLock[i].fd = fd; + return 0; + } else { + /* For reading, it's simpler - just check to see + that there's no-one writing to the underlying file. */ + for (i = 0; i < writeLocks; i++) { + if (writeLock[i].inode == sb.st_ino && writeLock[i].device == sb.st_dev) { +#ifndef __MINGW32__ + return -1; +#else + break; +#endif + } + } + /* Fit in new entry, reusing an existing table entry, if possible. */ + for (i = 0; i < readLocks; i++) { + if (readLock[i].inode == sb.st_ino && readLock[i].device == sb.st_dev) { + return 0; + } + } + i = readLocks++; + readLock[i].device = sb.st_dev; + readLock[i].inode = sb.st_ino; + readLock[i].fd = fd; + return 0; + } + +} + +int +unlockFile(int fd) +{ + int i; + + for (i = 0; i < readLocks; i++) + if (readLock[i].fd == fd) { + while (++i < readLocks) + readLock[i - 1] = readLock[i]; + readLocks--; + return 0; + } + + for (i = 0; i < writeLocks; i++) + if (writeLock[i].fd == fd) { + while (++i < writeLocks) + writeLock[i - 1] = writeLock[i]; + writeLocks--; + return 0; + } + /* Signal that we did not find an entry */ + return 1; +} |