freebsd-skq/lib/libnetmap/nmctx-pthreads.c
vmaffione cc60dbae46 lib: add libnetmap
This changeset introduces the new libnetmap library for writing
netmap applications.
Before libnetmap, applications could either use the kernel API
directly (e.g. NIOCREGIF/NIOCCTRL) or the simple header-only-library
netmap_user.h (e.g. nm_open(), nm_close(), nm_mmap() etc.)

The new library offers more functionalities than netmap_user.h:
  - Support for complex netmap options, such as external memory
    allocators or per-buffer offsets. This opens the way to future
    extensions.
  - More flexibility in the netmap port bind options, such as
    non-numeric names for pipes, or the ability to specify the netmap
    allocator that must be used for a given port.
  - Automatic tracking of the netmap memory regions in use across the
    open ports.

At the moment there is no man page, but the libnetmap.h header file
has in-depth documentation.

Reviewed by:	hrs
MFC after:	2 weeks
Differential Revision:	https://reviews.freebsd.org/D26171
2020-08-28 20:03:54 +00:00

48 lines
951 B
C

/* $FreeBSD$ */
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <net/netmap_user.h>
#include <pthread.h>
#include "libnetmap.h"
struct nmctx_pthread {
struct nmctx up;
pthread_mutex_t mutex;
};
static struct nmctx_pthread nmctx_pthreadsafe;
static void
nmctx_pthread_lock(struct nmctx *ctx, int lock)
{
struct nmctx_pthread *ctxp =
(struct nmctx_pthread *)ctx;
if (lock) {
pthread_mutex_lock(&ctxp->mutex);
} else {
pthread_mutex_unlock(&ctxp->mutex);
}
}
void __attribute__ ((constructor))
nmctx_set_threadsafe(void)
{
struct nmctx *old;
pthread_mutex_init(&nmctx_pthreadsafe.mutex, NULL);
old = nmctx_set_default(&nmctx_pthreadsafe.up);
nmctx_pthreadsafe.up = *old;
nmctx_pthreadsafe.up.lock = nmctx_pthread_lock;
}
int nmctx_threadsafe;