I haven't thoroughly vetted it yet, but I'm not wasting the effort until
I have the feedback on what should/shouldn't be changed.
I explicitly decided not to do stacked locks/refcounting. Win32 will not
allow you to 'promote' a sharelock into an exclusivelock. Ergo, it's not
portable.
If folks would take a look at this, and comment;
Index: dbm/sdbm/sdbm.c
===================================================================
RCS file: /home/cvs/apr-util/dbm/sdbm/sdbm.c,v
retrieving revision 1.16
diff -u -r1.16 sdbm.c
--- dbm/sdbm/sdbm.c 2001/05/01 05:37:05 1.16
+++ dbm/sdbm/sdbm.c 2001/05/02 20:59:32
@@ -98,6 +98,7 @@
#define OFF_PAG(off) (apr_off_t) (off) * PBLKSIZ
#define OFF_DIR(off) (apr_off_t) (off) * DBLKSIZ
+
static long masks[] = {
000000000000, 000000000001, 000000000003, 000000000007,
000000000017, 000000000037, 000000000077, 000000000177,
@@ -115,9 +116,9 @@
{
apr_sdbm_t *db = data;
+ if (db->flags & (SDBM_SHARED_LOCK | SDBM_EXCLUSIVE_LOCK))
+ (void) apr_sdbm_unlock(db);
(void) apr_file_close(db->dirf);
- if (!(db->flags & SDBM_SHARED))
- (void) sdbm_unlock(db);
(void) apr_file_close(db->pagf);
free(db);
@@ -163,26 +164,21 @@
* If we fail anywhere, undo everything, return NULL.
*/
- if ((status = apr_file_open(&db->pagf, pagname, flags, perms, p))
+ if ((status = apr_file_open(&db->dirf, dirname, flags, perms, p))
!= APR_SUCCESS)
goto error;
- if ((status = sdbm_lock(db, !(db->flags & SDBM_RDONLY)))
+ if ((status = apr_file_open(&db->pagf, pagname, flags, perms, p))
!= APR_SUCCESS)
goto error;
- if ((status = apr_file_open(&db->dirf, dirname, flags, perms, p))
+ if ((status = apr_sdbm_lock(db, (db->flags & SDBM_RDONLY)
+ ? APR_FLOCK_SHARED
+ : APR_FLOCK_EXCLUSIVE))
!= APR_SUCCESS)
goto error;
/*
- * if we are opened in SHARED mode, unlock ourself
- */
- if (db->flags & SDBM_SHARED)
- if ((status = sdbm_unlock(db)) != APR_SUCCESS)
- goto error;
-
- /*
* need the dirfile size to establish max bit number.
*/
if ((status = apr_file_info_get(&finfo, APR_FINFO_SIZE, db->dirf))
@@ -190,12 +186,17 @@
goto error;
/*
+ * if we are opened in SHARED mode, unlock ourself
+ */
+ if (db->flags & SDBM_SHARED)
+ if ((status = apr_sdbm_unlock(db)) != APR_SUCCESS)
+ goto error;
+
+ /*
* zero size: either a fresh database, or one with a single,
* unsplit data page: dirpage is all zeros.
*/
- db->dirbno = (!finfo.size) ? 0 : -1;
- db->pagbno = -1;
- db->maxbno = finfo.size * BYTESIZ;
+ SDBM_INVALIDATE_CACHE(db, finfo);
/* (apr_pcalloc zeroed the buffers) */
@@ -207,10 +208,11 @@
return APR_SUCCESS;
error:
+ if (db->dirf && db->pagf)
+ (void) apr_sdbm_unlock(db);
if (db->dirf != NULL)
(void) apr_file_close(db->dirf);
if (db->pagf != NULL) {
- (void) sdbm_unlock(db);
(void) apr_file_close(db->pagf);
}
free(db);
@@ -236,11 +238,13 @@
apr_sdbm_datum_t key)
{
apr_status_t status;
+ int impllock = !(db->flags & (SDBM_SHARED_LOCK | SDBM_EXCLUSIVE_LOCK));
+
if (db == NULL || bad(key))
return APR_EINVAL;
- if (db->flags & SDBM_SHARED)
- if ((status = sdbm_lock(db, 0)) != APR_SUCCESS)
+ if (impllock)
+ if ((status = apr_sdbm_lock(db, APR_FLOCK_SHARED)) != APR_SUCCESS)
return ioerr(db, status);
if ((status = getpage(db, exhash(key))) == APR_SUCCESS) {
@@ -250,9 +254,8 @@
else
ioerr(db, status);
- if (db->flags & SDBM_SHARED)
- if ((status = sdbm_unlock(db)) != APR_SUCCESS)
- ioerr(db, status);
+ if (impllock)
+ (void) apr_sdbm_unlock(db);
return status;
}
@@ -274,14 +277,16 @@
apr_status_t apr_sdbm_delete(apr_sdbm_t *db, const apr_sdbm_datum_t key)
{
apr_status_t status;
-
+ int impllock = !(db->flags & SDBM_EXCLUSIVE_LOCK);
+
if (db == NULL || bad(key))
return APR_EINVAL;
if (apr_sdbm_rdonly(db))
return APR_EINVAL;
-
- if (db->flags & SDBM_SHARED)
- if ((status = sdbm_lock(db, 1)) != APR_SUCCESS)
+ if (db->flags & SDBM_SHARED_LOCK)
+ return APR_EINVAL;
+ if (impllock)
+ if ((status = apr_sdbm_lock(db, APR_FLOCK_EXCLUSIVE)) != APR_SUCCESS)
return ioerr(db, status);
if ((status = getpage(db, exhash(key))) == APR_SUCCESS) {
@@ -297,9 +302,8 @@
else
ioerr(db, status);
- if (db->flags & SDBM_SHARED)
- if ((status = sdbm_unlock(db)) != APR_SUCCESS)
- return ioerr(db, status);
+ if (impllock)
+ (void) apr_sdbm_unlock(db);
return status;
}
@@ -310,12 +314,12 @@
int need;
register long hash;
apr_status_t status;
+ int impllock = !(db->flags & SDBM_EXCLUSIVE_LOCK);
if (db == NULL || bad(key))
return APR_EINVAL;
if (apr_sdbm_rdonly(db))
return APR_EINVAL;
-
need = key.dsize + val.dsize;
/*
* is the pair too big (or too small) for this database ??
@@ -323,8 +327,10 @@
if (need < 0 || need > PAIRMAX)
return APR_EINVAL;
- if (db->flags & SDBM_SHARED)
- if ((status = sdbm_lock(db, 1)) != APR_SUCCESS)
+ if (db->flags & SDBM_SHARED_LOCK)
+ return APR_EINVAL;
+ if (impllock)
+ if ((status = apr_sdbm_lock(db, APR_FLOCK_EXCLUSIVE)) != APR_SUCCESS)
return ioerr(db, status);
if ((status = getpage(db, (hash = exhash(key)))) == APR_SUCCESS) {
@@ -357,9 +363,8 @@
}
error:
- if (db->flags & SDBM_SHARED)
- if ((status = sdbm_unlock(db)) != APR_SUCCESS)
- ioerr(db, status);
+ if (impllock)
+ (void) apr_sdbm_unlock(db);
return status;
}
@@ -473,9 +478,10 @@
apr_status_t apr_sdbm_firstkey(apr_sdbm_t *db, apr_sdbm_datum_t *key)
{
apr_status_t status;
+ int impllock = !(db->flags & (SDBM_SHARED_LOCK | SDBM_EXCLUSIVE_LOCK));
- if (db->flags & SDBM_SHARED)
- if ((status = sdbm_lock(db, 0)) != APR_SUCCESS)
+ if (impllock)
+ if ((status = apr_sdbm_lock(db, APR_FLOCK_SHARED)) != APR_SUCCESS)
return ioerr(db, status);
/*
@@ -492,9 +498,8 @@
status = getnext(key, db);
}
- if (db->flags & SDBM_SHARED)
- if ((status = sdbm_unlock(db)) != APR_SUCCESS)
- ioerr(db, status);
+ if (impllock)
+ (void) apr_sdbm_unlock(db);
return status;
}
@@ -502,16 +507,16 @@
apr_status_t apr_sdbm_nextkey(apr_sdbm_t *db, apr_sdbm_datum_t *key)
{
apr_status_t status;
+ int impllock = !(db->flags & (SDBM_SHARED_LOCK | SDBM_EXCLUSIVE_LOCK));
- if (db->flags & SDBM_SHARED)
- if ((status = sdbm_lock(db, 0)) != APR_SUCCESS)
+ if (impllock)
+ if ((status = apr_sdbm_lock(db, APR_FLOCK_SHARED)) != APR_SUCCESS)
return ioerr(db, status);
status = getnext(key, db);
- if (db->flags & SDBM_SHARED)
- if ((status = sdbm_unlock(db)) != APR_SUCCESS)
- ioerr(db, status);
+ if (impllock)
+ (void) apr_sdbm_unlock(db);
return status;
}
Index: dbm/sdbm/sdbm_lock.c
===================================================================
RCS file: /home/cvs/apr-util/dbm/sdbm/sdbm_lock.c,v
retrieving revision 1.6
diff -u -r1.6 sdbm_lock.c
--- dbm/sdbm/sdbm_lock.c 2001/04/30 17:16:19 1.6
+++ dbm/sdbm/sdbm_lock.c 2001/05/02 20:59:32
@@ -52,25 +52,47 @@
* <http://www.apache.org/>.
*/
+#include "apr_file_info.h"
#include "apr_file_io.h"
#include "apr_sdbm.h"
#include "sdbm_private.h"
+#include "sdbm_tune.h"
/* NOTE: this function blocks until it acquires the lock */
-apr_status_t sdbm_lock(apr_sdbm_t *db, int exclusive)
+apr_status_t apr_sdbm_lock(apr_sdbm_t *db, int type)
{
- int type;
+ apr_status_t status;
- if (exclusive)
- type = APR_FLOCK_EXCLUSIVE;
- else
- type = APR_FLOCK_SHARED;
+ if (db->flags & (SDBM_SHARED_LOCK | SDBM_EXCLUSIVE_LOCK))
+ return APR_EINVAL;
+ /*
+ * zero size: either a fresh database, or one with a single,
+ * unsplit data page: dirpage is all zeros.
+ */
+ if ((status = apr_file_lock(db->dirf, type)) == APR_SUCCESS)
+ {
+ apr_finfo_t finfo;
+ if ((status = apr_file_info_get(&finfo, APR_FINFO_SIZE, db->dirf))
+ != APR_SUCCESS) {
+ (void) apr_file_unlock(db->dirf);
+ return status;
+ }
- return apr_file_lock(db->pagf, type);
+ SDBM_INVALIDATE_CACHE(db, finfo);
+
+ if (type == APR_FLOCK_SHARED)
+ db->flags |= SDBM_SHARED_LOCK;
+ else if (type == APR_FLOCK_EXCLUSIVE)
+ db->flags |= SDBM_EXCLUSIVE_LOCK;
+ }
+ return status;
}
-apr_status_t sdbm_unlock(apr_sdbm_t *db)
+apr_status_t apr_sdbm_unlock(apr_sdbm_t *db)
{
- return apr_file_unlock(db->pagf);
+ if (!(db->flags & (SDBM_SHARED_LOCK | SDBM_EXCLUSIVE_LOCK)))
+ return APR_EINVAL;
+ db->flags &= ~(SDBM_SHARED_LOCK | SDBM_EXCLUSIVE_LOCK);
+ return apr_file_unlock(db->dirf);
}
Index: dbm/sdbm/sdbm_private.h
===================================================================
RCS file: /home/cvs/apr-util/dbm/sdbm/sdbm_private.h,v
retrieving revision 1.5
diff -u -r1.5 sdbm_private.h
--- dbm/sdbm/sdbm_private.h 2001/04/30 17:16:23 1.5
+++ dbm/sdbm/sdbm_private.h 2001/05/02 20:59:33
@@ -79,8 +79,10 @@
#define SPLTMAX 10 /* maximum allowed splits */
/* for apr_sdbm_t.flags */
-#define SDBM_RDONLY 0x1 /* data base open read-only */
-#define SDBM_SHARED 0x4 /* data base locks for shared write */
+#define SDBM_RDONLY 0x1 /* data base open read-only */
+#define SDBM_SHARED 0x2 /* data base open for sharing */
+#define SDBM_SHARED_LOCK 0x4 /* data base locks for shared write */
+#define SDBM_EXCLUSIVE_LOCK 0x8 /* data base locks for shared write */
struct apr_sdbm_t {
apr_pool_t *pool;
@@ -100,11 +102,17 @@
apr_status_t status; /* track the specific last error */
};
-apr_status_t sdbm_lock(apr_sdbm_t *db, int exclusive);
-apr_status_t sdbm_unlock(apr_sdbm_t *db);
-
extern const apr_sdbm_datum_t sdbm_nullitem;
long sdbm_hash(const char *str, int len);
+
+/*
+ * zero the cache
+ */
+#define SDBM_INVALIDATE_CACHE(db, finfo) \
+ do { db->dirbno = (!finfo.size) ? 0 : -1; \
+ db->pagbno = -1; \
+ db->maxbno = finfo.size * BYTESIZ; \
+ } while (0);
#endif /* SDBM_PRIVATE_H */
Index: include/apr_sdbm.h
===================================================================
RCS file: /home/cvs/apr-util/include/apr_sdbm.h,v
retrieving revision 1.5
diff -u -r1.5 apr_sdbm.h
--- include/apr_sdbm.h 2001/05/01 05:37:43 1.5
+++ include/apr_sdbm.h 2001/05/02 20:59:45
@@ -88,6 +88,8 @@
apr_pool_t *p);
apr_status_t apr_sdbm_close(apr_sdbm_t *db);
+apr_status_t apr_sdbm_lock(apr_sdbm_t *db, int type);
+apr_status_t apr_sdbm_unlock(apr_sdbm_t *db);
apr_status_t apr_sdbm_fetch(apr_sdbm_t *db, apr_sdbm_datum_t *val,
apr_sdbm_datum_t key);
apr_status_t apr_sdbm_delete(apr_sdbm_t *db, const apr_sdbm_datum_t key);