From 2a0ba856de23293e7d82ca43cdd1d51d2cee82a7 Mon Sep 17 00:00:00 2001
From: Ian Lynagh <igloo@earth.li>
Date: Wed, 26 Oct 2011 23:27:17 +0100
Subject: [PATCH] Stop enforcing single-writer/multi-reader file access

---
 includes/Rts.h          |    1 -
 includes/rts/FileLock.h |   24 --------
 rts/Linker.c            |    2 -
 rts/RtsStartup.c        |   11 ----
 rts/posix/FileLock.c    |  145 -----------------------------------------------
 5 files changed, 0 insertions(+), 183 deletions(-)
 delete mode 100644 includes/rts/FileLock.h
 delete mode 100644 rts/posix/FileLock.c

diff --git a/includes/Rts.h b/includes/Rts.h
index 91ec76d..dadd526 100644
--- a/includes/Rts.h
+++ b/includes/Rts.h
@@ -202,7 +202,6 @@ void _assertFail(const char *filename, unsigned int linenum)
 #include "rts/Hpc.h"
 #include "rts/Flags.h"
 #include "rts/Adjustor.h"
-#include "rts/FileLock.h"
 #include "rts/Globals.h"
 #include "rts/IOManager.h"
 #include "rts/Linker.h"
diff --git a/includes/rts/FileLock.h b/includes/rts/FileLock.h
deleted file mode 100644
index a7d8d3c..0000000
--- a/includes/rts/FileLock.h
+++ /dev/null
@@ -1,24 +0,0 @@
-/* -----------------------------------------------------------------------------
- *
- * (c) The GHC Team, 2007-2009
- *
- * File locking support as required by Haskell
- *
- * Do not #include this file directly: #include "Rts.h" instead.
- *
- * To understand the structure of the RTS headers, see the wiki:
- *   http://hackage.haskell.org/trac/ghc/wiki/Commentary/SourceTree/Includes
- *
- * ---------------------------------------------------------------------------*/
-
-#ifndef RTS_FILELOCK_H
-#define RTS_FILELOCK_H
-
-#ifdef HAVE_SYS_TYPES_H
-#include <sys/types.h>
-#endif
-
-int  lockFile(int fd, dev_t dev, ino_t ino, int for_writing);
-int  unlockFile(int fd);
-
-#endif /* RTS_FILELOCK_H */
diff --git a/rts/Linker.c b/rts/Linker.c
index f5b90d4..acf1336 100644
--- a/rts/Linker.c
+++ b/rts/Linker.c
@@ -269,8 +269,6 @@ typedef struct _RtsSymbolVal {
       SymI_HasProto(__hscore_get_saved_termios) \
       SymI_HasProto(__hscore_set_saved_termios) \
       SymI_HasProto(shutdownHaskellAndSignal)   \
-      SymI_HasProto(lockFile)                   \
-      SymI_HasProto(unlockFile)                 \
       SymI_HasProto(signal_handlers)            \
       SymI_HasProto(stg_sig_install)            \
       SymI_HasProto(rtsTimerSignal)             \
diff --git a/rts/RtsStartup.c b/rts/RtsStartup.c
index dc62143..d0bc96d 100644
--- a/rts/RtsStartup.c
+++ b/rts/RtsStartup.c
@@ -52,7 +52,6 @@ void exitLinker( void );	// there is no Linker.h file to include
 
 #if !defined(mingw32_HOST_OS)
 #include "posix/TTY.h"
-#include "posix/FileLock.h"
 #endif
 
 #ifdef HAVE_UNISTD_H
@@ -189,11 +188,6 @@ hs_init(int *argc, char **argv[])
     /* initialise the shared Typeable store */
     initGlobalStore();
 
-    /* initialise file locking, if necessary */
-#if !defined(mingw32_HOST_OS)    
-    initFileLocking();
-#endif
-
 #if defined(DEBUG)
     /* initialise thread label table (tso->char*) */
     initThreadLabelTable();
@@ -348,11 +342,6 @@ hs_exit_(rtsBool wait_foreign)
     /* free linker data */
     exitLinker();
 
-    /* free file locking tables, if necessary */
-#if !defined(mingw32_HOST_OS)    
-    freeFileLocking();
-#endif
-
     /* free the stable pointer table */
     exitStablePtrTable();
 
diff --git a/rts/posix/FileLock.c b/rts/posix/FileLock.c
deleted file mode 100644
index cb36366..0000000
--- a/rts/posix/FileLock.c
+++ /dev/null
@@ -1,145 +0,0 @@
-/* -----------------------------------------------------------------------------
- *
- * (c) The GHC Team, 2007
- *
- * File locking support as required by Haskell
- *
- * ---------------------------------------------------------------------------*/
- 
-#include "PosixSource.h"
-#include "Rts.h"
-
-#include "FileLock.h"
-#include "Hash.h"
-#include "RtsUtils.h"
-
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <unistd.h>
-#include <errno.h>
-
-typedef struct {
-    dev_t device;
-    ino_t inode;
-    int   readers; // >0 : readers,  <0 : writers
-} Lock;
-
-// Two hash tables.  The first maps objects (device/inode pairs) to
-// Lock objects containing the number of active readers or writers.  The
-// second maps file descriptors to lock objects, so that we can unlock
-// by FD without needing to fstat() again.
-static HashTable *obj_hash;
-static HashTable *fd_hash;
-
-#ifdef THREADED_RTS
-static Mutex file_lock_mutex;
-#endif
-
-static int cmpLocks(StgWord w1, StgWord w2)
-{
-    Lock *l1 = (Lock *)w1;
-    Lock *l2 = (Lock *)w2;
-    return (l1->device == l2->device && l1->inode == l2->inode);
-}
-
-static int hashLock(HashTable *table, StgWord w)
-{
-    Lock *l = (Lock *)w;
-    // Just xor the dev_t with the ino_t, hope this is good enough.
-    return hashWord(table, (StgWord)l->inode ^ (StgWord)l->device);
-}
-
-void
-initFileLocking(void)
-{
-    obj_hash = allocHashTable_(hashLock, cmpLocks);
-    fd_hash  = allocHashTable(); /* ordinary word-based table */
-#ifdef THREADED_RTS
-    initMutex(&file_lock_mutex);
-#endif
-}
-
-static void
-freeLock(void *lock)
-{
-    stgFree(lock);
-}
-
-void
-freeFileLocking(void)
-{
-    freeHashTable(obj_hash, freeLock);
-    freeHashTable(fd_hash,  NULL);
-#ifdef THREADED_RTS
-    closeMutex(&file_lock_mutex);
-#endif
-}
-
-int
-lockFile(int fd, dev_t dev, ino_t ino, int for_writing)
-{
-    Lock key, *lock;
-
-    ACQUIRE_LOCK(&file_lock_mutex);
-
-    key.device = dev;
-    key.inode  = ino;
-
-    lock = lookupHashTable(obj_hash, (StgWord)&key);
-
-    if (lock == NULL)
-    {
-        lock = stgMallocBytes(sizeof(Lock), "lockFile");
-        lock->device = dev;
-        lock->inode  = ino;
-        lock->readers = for_writing ? -1 : 1;
-        insertHashTable(obj_hash, (StgWord)lock, (void *)lock);
-        insertHashTable(fd_hash, fd, lock);
-        RELEASE_LOCK(&file_lock_mutex);
-        return 0;
-    }
-    else
-    {
-        // single-writer/multi-reader locking:
-        if (for_writing || lock->readers < 0) {
-            RELEASE_LOCK(&file_lock_mutex);
-            return -1;
-        }
-        insertHashTable(fd_hash, fd, lock);
-        lock->readers++;
-        RELEASE_LOCK(&file_lock_mutex);
-        return 0;
-    }
-}
-
-int
-unlockFile(int fd)
-{
-    Lock *lock;
-
-    ACQUIRE_LOCK(&file_lock_mutex);
-
-    lock = lookupHashTable(fd_hash, fd);
-    if (lock == NULL) {
-        // errorBelch("unlockFile: fd %d not found", fd); 
-        // This is normal: we didn't know when calling unlockFile
-        // whether this FD referred to a locked file or not.
-        RELEASE_LOCK(&file_lock_mutex);
-        return 1;
-    }
-
-    if (lock->readers < 0) {
-        lock->readers++;
-    } else {
-        lock->readers--;
-    }
-
-    if (lock->readers == 0) {
-        removeHashTable(obj_hash, (StgWord)lock, NULL);
-        stgFree(lock);
-    }
-    removeHashTable(fd_hash, fd, NULL);
-
-    RELEASE_LOCK(&file_lock_mutex);
-    return 0;
-}
-- 
1.7.2.5

