Use shmat for lock region instead of mmap

vmware
Howard Chu 14 years ago
parent f367441b69
commit a37195f686
  1. 67
      libraries/libmdb/mdb.c

@ -7,6 +7,8 @@
#ifdef HAVE_SYS_FILE_H #ifdef HAVE_SYS_FILE_H
#include <sys/file.h> #include <sys/file.h>
#endif #endif
#include <sys/ipc.h>
#include <sys/shm.h>
#include <assert.h> #include <assert.h>
#include <err.h> #include <err.h>
@ -245,7 +247,7 @@ struct MDB_db {
struct MDB_env { struct MDB_env {
int me_fd; int me_fd;
int me_lfd; key_t me_shmkey;
#define MDB_FIXPADDING 0x01 /* internal */ #define MDB_FIXPADDING 0x01 /* internal */
uint32_t me_flags; uint32_t me_flags;
int me_maxreaders; int me_maxreaders;
@ -908,7 +910,7 @@ fail:
} }
int int
mdbenv_create(MDB_env **env, size_t size) mdbenv_create(MDB_env **env)
{ {
MDB_env *e; MDB_env *e;
@ -1022,50 +1024,40 @@ mdbenv_reader_dest(void *ptr)
int int
mdbenv_open(MDB_env *env, const char *path, unsigned int flags, mode_t mode) mdbenv_open(MDB_env *env, const char *path, unsigned int flags, mode_t mode)
{ {
int oflags, rc, len; int oflags, rc, shmid;
char *lpath; off_t size;
off_t size, rsize;
len = strlen(path); if (F_ISSET(flags, MDB_RDONLY))
lpath = malloc(len + sizeof(".lock")); oflags = O_RDONLY;
sprintf(lpath, "%s.lock", path); else
if ((env->me_lfd = open(lpath, O_RDWR | O_CREAT, mode)) == -1) oflags = O_RDWR | O_CREAT | O_APPEND;
if ((env->me_fd = open(path, oflags, mode)) == -1)
return errno; return errno;
size = lseek(env->me_lfd, 0, SEEK_END); env->me_shmkey = ftok(path, 'm');
rsize = (env->me_maxreaders-1) * sizeof(MDB_reader) + sizeof(MDB_txninfo); size = (env->me_maxreaders-1) * sizeof(MDB_reader) + sizeof(MDB_txninfo);
if (size < rsize) { shmid = shmget(env->me_shmkey, size, IPC_CREAT|IPC_EXCL|mode);
if (ftruncate(env->me_lfd, rsize) != 0) { if (shmid == -1) {
rc = errno; if (errno == EEXIST) {
close(env->me_lfd); shmid = shmget(env->me_shmkey, size, IPC_CREAT|mode);
return rc; if (shmid == -1)
} return errno;
env->me_txns = shmat(shmid, NULL, 0);
} else { } else {
rsize = size;
size = rsize - sizeof(MDB_txninfo);
env->me_maxreaders = size/sizeof(MDB_reader) + 1;
}
env->me_txns = mmap(0, rsize, PROT_READ|PROT_WRITE, MAP_SHARED,
env->me_lfd, 0);
if (env->me_txns == MAP_FAILED)
return errno; return errno;
if (size == 0) { }
} else {
pthread_mutexattr_t mattr; pthread_mutexattr_t mattr;
env->me_txns = shmat(shmid, NULL, 0);
pthread_mutexattr_init(&mattr); pthread_mutexattr_init(&mattr);
pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED); pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
pthread_mutex_init(&env->me_txns->mt_mutex, &mattr); pthread_mutex_init(&env->me_txns->mt_mutex, &mattr);
pthread_mutex_init(&env->me_txns->mt_wmutex, &mattr); pthread_mutex_init(&env->me_txns->mt_wmutex, &mattr);
} }
if (F_ISSET(flags, MDB_RDONLY))
oflags = O_RDONLY;
else
oflags = O_RDWR | O_CREAT | O_APPEND;
if ((env->me_fd = open(path, oflags, mode)) == -1)
return errno;
if ((rc = mdbenv_open2(env, flags)) != MDB_SUCCESS) { if ((rc = mdbenv_open2(env, flags)) != MDB_SUCCESS) {
close(env->me_fd); close(env->me_fd);
env->me_fd = -1; env->me_fd = -1;
@ -1092,10 +1084,8 @@ mdbenv_close(MDB_env *env)
} }
close(env->me_fd); close(env->me_fd);
if (env->me_txns) { if (env->me_txns) {
size_t size = (env->me_maxreaders-1) * sizeof(MDB_reader) + sizeof(MDB_txninfo); shmdt(env->me_txns);
munmap(env->me_txns, size);
} }
close(env->me_lfd);
free(env); free(env);
} }
@ -2507,8 +2497,9 @@ mdbenv_compact(MDB_env *env)
return rc; return rc;
} }
rc = mdbenv_create(&envc, env->me_mapsize); rc = mdbenv_create(&envc);
if (rc) goto failed; if (rc) goto failed;
rc = mdbenv_set_mapsize(envc, env->me_mapsize);
envc->me_fd = fd; envc->me_fd = fd;
rc = mdbenv_open2(envc, env->me_flags); rc = mdbenv_open2(envc, env->me_flags);

Loading…
Cancel
Save