changeset 590:4eedc18634f5

Use _locking() to emulate fcntl() locks This is a rather terrible emulation; _locking() doesn't support nonexclusive locks, and also doesn't document any useful return codes. Oh well; it's a start.
author mas01cr
date Tue, 11 Aug 2009 21:42:18 +0000
parents 9119f2fa3efe
children 0242e5d1643c
files audioDB-internals.h audioDB.h close.cpp common.cpp create.cpp lock.cpp
diffstat 6 files changed, 31 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/audioDB-internals.h	Tue Aug 11 21:42:13 2009 +0000
+++ b/audioDB-internals.h	Tue Aug 11 21:42:18 2009 +0000
@@ -1,12 +1,23 @@
+#if defined(WIN32)
+#include <sys/locking.h>
+#endif
+#if !defined(WIN32)
 #include <sys/mman.h>
+#endif
 #include <sys/types.h>
 
 #include <errno.h>
 #include <fcntl.h>
+#if defined(WIN32)
+#include <io.h>
+#endif
 #include <limits.h>
 #include <math.h>
 #include <string.h>
 #include <unistd.h>
+#if defined(WIN32)
+#include <windows.h>
+#endif
 
 #include <algorithm>
 #include <iostream>
--- a/audioDB.h	Tue Aug 11 21:42:13 2009 +0000
+++ b/audioDB.h	Tue Aug 11 21:42:18 2009 +0000
@@ -278,8 +278,6 @@
   void cleanup();
   ~audioDB();
   int processArgs(const unsigned argc, const char* argv[]);
-  void get_lock(int fd, bool exclusive);
-  void release_lock(int fd);
   void create(const char* dbName);
   void insert(const char* dbName, const char* inFile);
   void batchinsert(const char* dbName, const char* inFile);
--- a/close.cpp	Tue Aug 11 21:42:13 2009 +0000
+++ b/close.cpp	Tue Aug 11 21:42:18 2009 +0000
@@ -13,6 +13,7 @@
   if(adb->cached_lsh) {
     delete adb->cached_lsh;
   }
+  divest_lock(adb->fd);
   close(adb->fd);
   free(adb);
 }
--- a/common.cpp	Tue Aug 11 21:42:13 2009 +0000
+++ b/common.cpp	Tue Aug 11 21:42:18 2009 +0000
@@ -10,18 +10,6 @@
 }
 #endif
 
-void audioDB::get_lock(int fd, bool exclusive) {
-  if(acquire_lock(fd, exclusive)) {
-    error("fcntl lock error", "", "fcntl");
-  }
-}
-
-void audioDB::release_lock(int fd) {
-  if (divest_lock(fd)) {
-    error("fcntl unlock error", "", "fcntl");
-  }
-}
-
 void audioDB::error(const char* a, const char* b, const char *sysFunc) {
  
 
--- a/create.cpp	Tue Aug 11 21:42:13 2009 +0000
+++ b/create.cpp	Tue Aug 11 21:42:18 2009 +0000
@@ -49,9 +49,6 @@
   if ((fd = open(path, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) < 0) {
     goto error;
   }
-  if (acquire_lock(fd, true)) {
-    goto error;
-  }
 
   header = (adb_header_t *) malloc(sizeof(adb_header_t));
   if(!header) {
--- a/lock.cpp	Tue Aug 11 21:42:13 2009 +0000
+++ b/lock.cpp	Tue Aug 11 21:42:18 2009 +0000
@@ -4,13 +4,14 @@
 #include "audioDB-internals.h"
 
 int acquire_lock(int fd, bool exclusive) {
+#if !defined(WIN32)
   struct flock lock;
   int status;
   
   lock.l_type = exclusive ? F_WRLCK : F_RDLCK;
   lock.l_whence = SEEK_SET;
   lock.l_start = 0;
-  lock.l_len = 0; /* "the whole file" */
+  lock.l_len = ADB_HEADER_SIZE;
 
  retry:
   do {
@@ -26,15 +27,31 @@
     }
   }
   return 0;
+#else
+  /* _locking() only supports exclusive locks */
+  int status;
+
+ retry:
+  status = _locking(fd, _LK_NBLCK, ADB_HEADER_SIZE);
+  if(status) {
+    Sleep(1000);
+    goto retry;
+  }
+  return 0;
+#endif
 }
 
 int divest_lock(int fd) {
+#if !defined(WIN32)
   struct flock lock;
 
   lock.l_type = F_UNLCK;
   lock.l_whence = SEEK_SET;
   lock.l_start = 0;
-  lock.l_len = 0;
+  lock.l_len = ADB_HEADER_SIZE;
 
   return fcntl(fd, F_SETLKW, &lock);
+#else
+  return _locking(fd, _LK_UNLCK, ADB_HEADER_SIZE);
+#endif
 }