Revision: 7255
http://svn.sourceforge.net/mahogany/?rev=7255&view=rev
Author: vadz
Date: 2007-04-28 17:14:01 -0700 (Sat, 28 Apr 2007)
Log Message:
-----------
implement locking functions using mutexes under Win32
Modified Paths:
--------------
trunk/M/lib/dspam/src/config.h
trunk/M/lib/dspam/src/hash_drv.c
trunk/M/lib/dspam/src/hash_drv.h
trunk/M/lib/dspam/src/util.c
trunk/M/lib/dspam/src/util.h
Modified: trunk/M/lib/dspam/src/config.h
===================================================================
--- trunk/M/lib/dspam/src/config.h 2007-04-28 14:17:13 UTC (rev 7254)
+++ trunk/M/lib/dspam/src/config.h 2007-04-29 00:14:01 UTC (rev 7255)
@@ -105,4 +105,10 @@
#define LLU_FMT_SPEC "llu"
#endif
+#ifdef _WIN32
+typedef void *_ds_lock_t;
+#else
+typedef FILE *_ds_lock_t;
#endif
+
+#endif
Modified: trunk/M/lib/dspam/src/hash_drv.c
===================================================================
--- trunk/M/lib/dspam/src/hash_drv.c 2007-04-28 14:17:13 UTC (rev 7254)
+++ trunk/M/lib/dspam/src/hash_drv.c 2007-04-29 00:14:01 UTC (rev 7255)
@@ -276,29 +276,23 @@
struct _hash_drv_storage *s,
const char *username)
{
- /* FIXME-WIN32: use a global mutex for locking */
-#ifdef WIN32
- CTX; s; username;
- return 0;
-#else
char filename[MAX_FILENAME_LENGTH];
int r;
_ds_userdir_path(filename, CTX->home, username, "lock");
- _ds_prepare_path_for(filename);
+ s->lock = _ds_open_lock(filename);
- s->lock = fopen(filename, "a");
if (s->lock == NULL) {
LOG(LOG_ERR, ERR_IO_FILE_OPEN, filename, strerror(errno));
return EFAILURE;
}
- r = _ds_get_fcntl_lock(fileno(s->lock));
+ r = _ds_acquire_lock(s->lock);
if (r) {
- fclose(s->lock);
+ _ds_close_lock(s->lock);
+ s->lock = NULL;
LOG(LOG_ERR, ERR_IO_LOCK, filename, r, strerror(errno));
}
return r;
-#endif
}
int
@@ -306,25 +300,20 @@
struct _hash_drv_storage *s,
const char *username)
{
- /* FIXME-WIN32: use a global mutex for locking */
-#ifdef WIN32
- s; username;
- return 0;
-#else
int r;
if (username == NULL)
return 0;
- r = _ds_free_fcntl_lock(fileno(s->lock));
+ r = _ds_release_lock(s->lock);
if (!r) {
- fclose(s->lock);
+ _ds_close_lock(s->lock);
+ s->lock = NULL;
} else {
LOG(LOG_ERR, ERR_IO_LOCK_FREE, username, r, strerror(errno));
}
return r;
-#endif
}
int _hash_drv_open(
Modified: trunk/M/lib/dspam/src/hash_drv.h
===================================================================
--- trunk/M/lib/dspam/src/hash_drv.h 2007-04-28 14:17:13 UTC (rev 7254)
+++ trunk/M/lib/dspam/src/hash_drv.h 2007-04-29 00:14:01 UTC (rev 7255)
@@ -30,6 +30,7 @@
#include "config.h"
#include "nodetree.h"
#include "libdspam.h"
+#include "libdspam.h"
#define HASH_REC_MAX 98317
#define HASH_EXTENT_MAX 49157
@@ -59,7 +60,7 @@
struct _hash_drv_storage
{
hash_drv_map_t map;
- FILE *lock;
+ _ds_lock_t lock;
int dbh_attached;
unsigned long offset_nexttoken;
Modified: trunk/M/lib/dspam/src/util.c
===================================================================
--- trunk/M/lib/dspam/src/util.c 2007-04-28 14:17:13 UTC (rev 7254)
+++ trunk/M/lib/dspam/src/util.c 2007-04-29 00:14:01 UTC (rev 7255)
@@ -624,12 +624,74 @@
return MIN(s, 1.0);
}
-int _ds_get_fcntl_lock(int fd) {
#ifdef _WIN32
- UNUSED(fd);
+_ds_lock_t _ds_open_lock(const char *name)
+{
+ _ds_lock_t h;
+
+ /* the mutex name can't contain backslashes, so change them to slashes */
+ char *p,
+ *n = strdup(name);
+ for ( p = n; *p; p++ )
+ {
+ if ( *p == '\\' )
+ *p = '/';
+ }
+
+ h = CreateMutex(NULL, FALSE, n);
+ free(n);
+
+ return h;
+}
+
+int _ds_acquire_lock(_ds_lock_t lock)
+{
+ UNUSED(lock);
+
+ /* we don't have to do anything to acquire it, we just need to check if we
+ * opened an existing mutex or created a new one */
+ return GetLastError() != ERROR_ALREADY_EXISTS;
+}
+
+int _ds_release_lock(_ds_lock_t lock)
+{
+ UNUSED(lock);
+
+ /* nothing to do */
return 0;
-#else
+}
+
+void _ds_close_lock(_ds_lock_t lock)
+{
+ CloseHandle(lock);
+}
+
+#else /* !_WIN32 */
+
+_ds_lock_t _ds_open_lock(const char *name)
+{
+ _ds_prepare_path_for(name);
+
+ return fopen(name, "a");
+}
+
+int _ds_acquire_lock(_ds_lock_t lock)
+{
+ return _ds_get_fcntl_lock(fileno(lock));
+}
+
+int _ds_release_lock(_ds_lock_t lock)
+{
+ return _ds_free_fcntl_lock(fileno(lock));
+}
+
+void _ds_close_lock(_ds_lock_t lock)
+{
+ fclose(lock);
+}
+
+int _ds_get_fcntl_lock(int fd) {
struct flock f;
f.l_type = F_WRLCK;
@@ -638,15 +700,9 @@
f.l_len = 0;
return fcntl(fd, F_SETLKW, &f);
-#endif
}
int _ds_free_fcntl_lock(int fd) {
-#ifdef _WIN32
- UNUSED(fd);
-
- return 0;
-#else
struct flock f;
f.l_type = F_UNLCK;
@@ -655,9 +711,10 @@
f.l_len = 0;
return fcntl(fd, F_SETLKW, &f);
-#endif
}
+#endif /* _WIN32/!_WIN32 */
+
int _ds_pow2(int exp) {
int j = 1, i;
if (!exp)
Modified: trunk/M/lib/dspam/src/util.h
===================================================================
--- trunk/M/lib/dspam/src/util.h 2007-04-28 14:17:13 UTC (rev 7254)
+++ trunk/M/lib/dspam/src/util.h 2007-04-29 00:14:01 UTC (rev 7255)
@@ -27,6 +27,7 @@
#include <auto-config.h>
#endif
+#include "config.h"
#include <sys/types.h>
#ifndef _WIN32
@@ -111,8 +112,31 @@
double _ds_gettime(void);
+/*
+ * Functions for working with locks.
+ *
+ * We use lock files under Unix and named mutexes under Win32. A lock must
+ * first be opened, then an attempt to acquire it can be made and, whether it
+ * succeeded or not, it must be closed, after releasing it if we did acquire
+ * it, later.
+ */
+
+/* Initializes the object used for locking, must call _ds_close_lock() later. */
+_ds_lock_t _ds_open_lock(const char *name);
+
+/* Try to acquire the lock opened by _ds_open_lock(). Return 0 on success. */
+int _ds_acquire_lock(_ds_lock_t lock);
+
+/* Release the lock acquired previously by _ds_acquire_lock() */
+int _ds_release_lock(_ds_lock_t lock);
+
+/* Close the lock object. Should release it before if we had locked it. */
+void _ds_close_lock(_ds_lock_t lock);
+
+#ifndef _WIN32
int _ds_get_fcntl_lock (int fd);
int _ds_free_fcntl_lock (int fd);
+#endif
unsigned long long _ds_getcrc64
(const char *);
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
lmpx.com only provides a reader for public news (NNTP) servers. It is not
affiliated with the servers or forums shown here and is not responsible for
the content of articles, which is written by their respective authors.