main () { int r; r = sdb_lock (".lock"); printf ("%d\n", r); r = sdb_lock (".lock"); printf ("%d\n", r); sdb_unlock (".lock"); r = sdb_lock (".lock"); printf ("%d\n", r); }
// TODO: use mmap instead of read.. much faster! SDB_VISIBLE Sdb* sdb_new (const char *dir, int lock) { Sdb* s; if (lock && !sdb_lock (sdb_lockfile (dir))) return NULL; s = malloc (sizeof (Sdb)); if (dir && *dir) { s->dir = strdup (dir); s->fd = open (dir, O_RDONLY|O_BINARY); // if (s->fd == -1) // must fail if we cant open for write in sync } else { s->dir = NULL; s->fd = -1; } s->fdump = -1; s->ndump = NULL; s->ns = ls_new (); s->ht = ht_new (); s->lock = lock; s->expire = 0LL; //s->ht->list->free = (SdbListFree)sdb_kv_free; // if open fails ignore cdb_init (&s->db, s->fd); cdb_findstart (&s->db); return s; }
SDB_VISIBLE void sdb_file (Sdb* s, const char *dir) { if (s->lock) sdb_unlock (sdb_lockfile (s->dir)); free (s->dir); s->dir = (dir && *dir)? strdup (dir): NULL; if (s->lock) sdb_lock (sdb_lockfile (s->dir)); }
SDB_API int sdb_lock_wait(const char *s) { // TODO use flock() here // wait forever here? while (!sdb_lock (s)) { // TODO: if waiting too much return 0 #if __SDB_WINDOWS__ Sleep (500); // hack #else // TODO use lockf() here .. flock is not much useful (fd, LOCK_EX); sleep (1); // hack #endif } return 1; }
void sdb_lock_wait(const char *s) { // TODO use flock() here while (!sdb_lock (s)) { // Sleep (100); // W32 } }