New generic lock-free map replaces the kqlist/kqtree. It does not handle overwriting an existing entry; need futher work on this part.

git-svn-id: svn://svn.code.sf.net/p/libkqueue/code/trunk@412 fb4e3144-bc1c-4b72-a658-5bcd248dd7f7
This commit is contained in:
mheily 2011-02-03 04:45:40 +00:00
parent cf0338fdf6
commit 1c6f8dece7
5 changed files with 106 additions and 40 deletions

View File

@ -5,7 +5,7 @@ abi_minor="0"
abi_version="$abi_major.$abi_minor"
cflags="-fpic -Wall -Werror -g -O2 -std=c99 -D_XOPEN_SOURCE=600"
ldflags=""
sources="src/common/filter.c src/common/knote.c
sources="src/common/filter.c src/common/knote.c src/common/map.c
src/common/kevent.c src/common/kqueue.c"
libdepends=""
deps="src/common/private.h"

View File

@ -27,17 +27,7 @@
int KQUEUE_DEBUG = 0;
/*
* Fast path (lock-free) for kqueue descriptors < KQLIST_MAX
*/
#define KQLIST_MAX 512
static struct kqueue *kqlist[KQLIST_MAX];
/*
* Slow path for kqueue descriptors > KQLIST_MAX
*/
static RB_HEAD(kqt, kqueue) kqtree = RB_INITIALIZER(&kqtree);
static pthread_rwlock_t kqtree_mtx;
static struct map *kqmap;
int CONSTRUCTOR
_libkqueue_init(void)
@ -51,20 +41,20 @@ _libkqueue_init(void)
KQUEUE_DEBUG = (getenv("KQUEUE_DEBUG") == NULL) ? 0 : 1;
#endif
pthread_rwlock_init(&kqtree_mtx, NULL);
kqmap = map_new(INT_MAX);
if (kqmap == NULL)
abort();
dbg_puts("library initialization complete");
return (0);
}
#if DEADWOOD
static int
kqueue_cmp(struct kqueue *a, struct kqueue *b)
{
return memcmp(&a->kq_id, &b->kq_id, sizeof(int));
}
RB_GENERATE(kqt, kqueue, entries, kqueue_cmp)
/* Must hold the kqtree_mtx when calling this */
void
kqueue_free(struct kqueue *kq)
@ -75,25 +65,12 @@ kqueue_free(struct kqueue *kq)
free(kq);
}
#endif
struct kqueue *
kqueue_lookup(int kq)
{
struct kqueue query;
struct kqueue *ent = NULL;
if (slowpath(kq < 0)) {
return (NULL);
}
if (fastpath(kq < KQLIST_MAX)) {
ent = kqlist[kq];
} else {
query.kq_id = kq;
pthread_rwlock_rdlock(&kqtree_mtx);
ent = RB_FIND(kqt, &kqtree, &query);
pthread_rwlock_unlock(&kqtree_mtx);
}
return (ent);
return ((struct kqueue *) map_lookup(kqmap, kq));
}
int VISIBLE
@ -110,14 +87,13 @@ kqueue(void)
return (-1);
}
if (kq->kq_id < KQLIST_MAX) {
kqlist[kq->kq_id] = kq;
} else {
pthread_rwlock_wrlock(&kqtree_mtx);
RB_INSERT(kqt, &kqtree, kq);
pthread_rwlock_unlock(&kqtree_mtx);
dbg_printf("created kqueue, fd=%d", kq->kq_id);
if (map_insert(kqmap, kq->kq_id, kq) < 0) {
dbg_puts("map insertion failed");
kqops.kqueue_free(kq);
return (-1);
}
dbg_printf("created kqueue, fd=%d", kq->kq_id);
return (kq->kq_id);
}

80
src/common/map.c Normal file
View File

@ -0,0 +1,80 @@
/*
* Copyright (c) 2011 Mark Heily <mark@heily.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "private.h"
struct map {
volatile size_t len;
void **data;
};
struct map *
map_new(size_t len)
{
struct map *dst;
dst = calloc(1, sizeof(*dst));
if (dst == NULL)
return (NULL);
dst->data = mmap(NULL, len * sizeof(void *), PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_NORESERVE | MAP_ANON, -1, 0);
if (dst->data == MAP_FAILED) {
dbg_perror("mmap(2)");
free(dst);
return (NULL);
}
dst->len = len;
return (dst);
}
int
map_insert(struct map *m, int idx, void *ptr)
{
if (slowpath(idx < 0 || idx > m->len))
return (-1);
if (__sync_val_compare_and_swap(&(m->data[idx]), 0, ptr) == NULL) {
dbg_printf("inserted %p in location %d", ptr, idx);
return (0);
} else {
dbg_printf("tried to insert a value into a non-empty location %d (value=%p)",
idx,
m->data[idx]);
return (-1);
}
}
void *
map_lookup(struct map *m, int idx)
{
if (slowpath(idx < 0 || idx > m->len))
return (NULL);
return m->data[idx];
}
int
map_delete(struct map *m, int idx)
{
if (slowpath(idx < 0 || idx > m->len))
return (-1);
//TODO: use CAS and fail if entry is NULL
m->data[idx] = NULL;
return (0);
}

View File

@ -28,6 +28,7 @@
struct kqueue;
struct kevent;
struct knote;
struct map;
struct eventfd;
struct evfilt_data;
@ -216,10 +217,15 @@ int kevent_wait(struct kqueue *, const struct timespec *);
int kevent_copyout(struct kqueue *, int, struct kevent *, int);
void kevent_free(struct kqueue *);
const char *kevent_dump(const struct kevent *);
struct kqueue * kqueue_lookup(int);
int kqueue_validate(struct kqueue *);
struct map *map_new(size_t);
int map_insert(struct map *, int, void *);
void *map_lookup(struct map *, int);
int map_delete(struct map *, int);
void map_free(struct map *);
int CONSTRUCTOR _libkqueue_init(void);
#endif /* ! _KQUEUE_PRIVATE_H */

View File

@ -17,6 +17,9 @@
#ifndef _KQUEUE_POSIX_PLATFORM_H
#define _KQUEUE_POSIX_PLATFORM_H
/* Required by glibc for MAP_ANON */
#define __USE_MISC 1
#include "../../include/sys/event.h"
/*
@ -48,6 +51,7 @@
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <unistd.h>
/*