diff options
| author | Vincenzo Maffione <vmaffione@FreeBSD.org> | 2020-08-28 20:03:54 +0000 |
|---|---|---|
| committer | Vincenzo Maffione <vmaffione@FreeBSD.org> | 2020-08-28 20:03:54 +0000 |
| commit | 5c4f8d801ca081e38ca3bfd9d36bfcd55329d703 (patch) | |
| tree | f2a7c9bc34c99fe6d803c5ca8cba2f66a05ea6f5 /lib/libnetmap/nmctx-pthreads.c | |
| parent | 609de97e0490003fcb6a5288c1112a058940a210 (diff) | |
Notes
Diffstat (limited to 'lib/libnetmap/nmctx-pthreads.c')
| -rw-r--r-- | lib/libnetmap/nmctx-pthreads.c | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/lib/libnetmap/nmctx-pthreads.c b/lib/libnetmap/nmctx-pthreads.c new file mode 100644 index 000000000000..8aa9322369c1 --- /dev/null +++ b/lib/libnetmap/nmctx-pthreads.c @@ -0,0 +1,47 @@ +/* $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; |
