FIX #2398: file locking wasn't thread-safe
[ghc-hetmet.git] / rts / posix / FileLock.c
1 /* -----------------------------------------------------------------------------
2  *
3  * (c) The GHC Team, 2007
4  *
5  * File locking support as required by Haskell 98
6  *
7  * ---------------------------------------------------------------------------*/
8  
9 #include "Rts.h"
10 #include "Hash.h"
11 #include "FileLock.h"
12 #include "RtsUtils.h"
13 #include "OSThreads.h"
14
15 #include <unistd.h>
16 #include <sys/stat.h>
17 #include <errno.h>
18
19 typedef struct {
20     dev_t device;
21     ino_t inode;
22     int   readers; // >0 : readers,  <0 : writers
23 } Lock;
24
25 // Two hash tables.  The first maps objects (device/inode pairs) to
26 // Lock objects containing the number of active readers or writers.  The
27 // second maps file descriptors to lock objects, so that we can unlock
28 // by FD without needing to fstat() again.
29 static HashTable *obj_hash;
30 static HashTable *fd_hash;
31
32 #ifdef THREADED_RTS
33 static Mutex file_lock_mutex;
34 #endif
35
36 static int cmpLocks(StgWord w1, StgWord w2)
37 {
38     Lock *l1 = (Lock *)w1;
39     Lock *l2 = (Lock *)w2;
40     return (l1->device == l2->device && l1->inode == l2->inode);
41 }
42
43 static int hashLock(HashTable *table, StgWord w)
44 {
45     Lock *l = (Lock *)w;
46     // Just xor the dev_t with the ino_t, hope this is good enough.
47     return hashWord(table, (StgWord)l->inode ^ (StgWord)l->device);
48 }
49
50 void
51 initFileLocking(void)
52 {
53     obj_hash = allocHashTable_(hashLock, cmpLocks);
54     fd_hash  = allocHashTable(); /* ordinary word-based table */
55 }
56
57 static void
58 freeLock(void *lock)
59 {
60     stgFree(lock);
61 }
62
63 void
64 freeFileLocking(void)
65 {
66     freeHashTable(obj_hash, freeLock);
67     freeHashTable(fd_hash,  NULL);
68 }
69
70 int
71 lockFile(int fd, dev_t dev, ino_t ino, int for_writing)
72 {
73     Lock key, *lock;
74
75     ACQUIRE_LOCK(&file_lock_mutex);
76
77     key.device = dev;
78     key.inode  = ino;
79
80     lock = lookupHashTable(obj_hash, (StgWord)&key);
81
82     if (lock == NULL)
83     {
84         lock = stgMallocBytes(sizeof(Lock), "lockFile");
85         lock->device = dev;
86         lock->inode  = ino;
87         lock->readers = for_writing ? -1 : 1;
88         insertHashTable(obj_hash, (StgWord)lock, (void *)lock);
89         insertHashTable(fd_hash, fd, lock);
90         RELEASE_LOCK(&file_lock_mutex);
91         return 0;
92     }
93     else
94     {
95         // single-writer/multi-reader locking:
96         if (for_writing || lock->readers < 0) {
97             RELEASE_LOCK(&file_lock_mutex);
98             return -1;
99         }
100         insertHashTable(fd_hash, fd, lock);
101         lock->readers++;
102         RELEASE_LOCK(&file_lock_mutex);
103         return 0;
104     }
105 }
106
107 int
108 unlockFile(int fd)
109 {
110     Lock *lock;
111
112     ACQUIRE_LOCK(&file_lock_mutex);
113
114     lock = lookupHashTable(fd_hash, fd);
115     if (lock == NULL) {
116         // errorBelch("unlockFile: fd %d not found", fd); 
117         // This is normal: we didn't know when calling unlockFile
118         // whether this FD referred to a locked file or not.
119         RELEASE_LOCK(&file_lock_mutex);
120         return 1;
121     }
122
123     if (lock->readers < 0) {
124         lock->readers++;
125     } else {
126         lock->readers--;
127     }
128
129     if (lock->readers == 0) {
130         removeHashTable(obj_hash, (StgWord)lock, NULL);
131         stgFree(lock);
132     }
133     removeHashTable(fd_hash, fd, NULL);
134
135     RELEASE_LOCK(&file_lock_mutex);
136     return 0;
137 }