summaryrefslogtreecommitdiffstats
path: root/src/mpq.h
diff options
context:
space:
mode:
authorMatt Dunwoodie <ncon@mail.noconroy.net>2019-09-11 22:05:02 +1000
committerMatt Dunwoodie <ncon@mail.noconroy.net>2019-09-11 22:13:50 +1000
commit44965bebb7b205ca91d1b941c4451fd6f0e93a80 (patch)
treedc86dfbd82cae31fbd7b7c73ddb91fc2d8356183 /src/mpq.h
parentFix lock bug in wg_handshake_merge, cleanup (diff)
downloadwireguard-openbsd-44965bebb7b205ca91d1b941c4451fd6f0e93a80.tar.xz
wireguard-openbsd-44965bebb7b205ca91d1b941c4451fd6f0e93a80.zip
Remove mpq.h
It was fun while it lasted, but it will be better to use the ifq.h stuffs.
Diffstat (limited to 'src/mpq.h')
-rw-r--r--src/mpq.h241
1 files changed, 0 insertions, 241 deletions
diff --git a/src/mpq.h b/src/mpq.h
deleted file mode 100644
index d7c8a19a0c0..00000000000
--- a/src/mpq.h
+++ /dev/null
@@ -1,241 +0,0 @@
-/*
- * Copyright (c) 2019 Matt Dunwoodie <ncon@noconroy.net>
- *
- * 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.
- */
-
-#ifndef _MPQ_H_
-#define _MPQ_H_
-
-#include <sys/param.h>
-#include <sys/kthread.h>
-#include <sys/mutex.h>
-#include <sys/systm.h>
-
-struct mpq {
- struct mutex mpq_mtx;
- size_t mpq_size, mpq_itemssize, mpq_phead, mpq_shead, mpq_tail;
- size_t mpq_nthreads, mpq_wantthreads;
- void *(*mpq_parallel_fn)(void *);
- void (*mpq_serial_fn)(void *);
- struct mpi {
- enum mpi_state {
- MPI_EMPTY = 0,
- MPI_BARRIER,
- MPI_WAITING,
- MPI_DEAD,
- MPI_OK,
- } mpi_state;
- void *mpi_data;
- } *mpq_items;
-};
-
-void mpq_init(struct mpq *, int, void *(*)(void *), void (*)(void *));
-size_t mpq_size(struct mpq *, size_t);
-void mpq_nthreads(struct mpq *, size_t);
-void mpq_barrier(struct mpq *);
-void mpq_destroy(struct mpq *);
-int mpq_enqueue(struct mpq *, void *);
-
-#define MPQ_DEFAULT_SIZE 256
-#define MPQ_DEFAULT_THREADS ncpus
-
-#define MPQ_ITEM(mpq, item) (&(mpq)->mpq_items[(mpq)->item])
-#define MPQ_INC(mpq, item) ((mpq)->item = ((mpq)->item + 1) % (mpq)->mpq_itemssize)
-#define MPQ_LOAD(mpq, item) ((mpq)->item <= (mpq)->mpq_tail ? \
- (mpq)->mpq_tail - (mpq)->item : (mpq)->mpq_itemssize - (mpq)->item + (mpq)->mpq_tail)
-
-
-/* Internal functions */
-void
-_mpq_thread(struct mpq *mpq)
-{
- void *data;
- size_t tid;
- struct mpi *mpi;
- void (*serial_fn)(void *);
-
- mtx_enter(&mpq->mpq_mtx);
- tid = mpq->mpq_nthreads++;
- while (1) {
- if (tid >= mpq->mpq_wantthreads) {
- mpq->mpq_nthreads--;
- mtx_leave(&mpq->mpq_mtx);
- kthread_exit(0);
- }
-
- while (MPQ_LOAD(mpq, mpq_phead) > 0) {
-
- mpi = MPQ_ITEM(mpq, mpq_phead);
- MPQ_INC(mpq, mpq_phead);
-
- if (mpi->mpi_state == MPI_BARRIER)
- continue;
-
- if (mpi->mpi_state != MPI_WAITING)
- panic("invalid state: %d", mpi->mpi_state);
-
- mtx_leave(&mpq->mpq_mtx);
- data = mpq->mpq_parallel_fn(mpi->mpi_data);
- mtx_enter(&mpq->mpq_mtx);
- mpi->mpi_data = data;
- mpi->mpi_state = (data == NULL) ? MPI_DEAD : MPI_OK;
- }
-
- while (MPQ_LOAD(mpq, mpq_shead) > 0) {
-
- mpi = MPQ_ITEM(mpq, mpq_shead);
- if (mpi->mpi_state == MPI_WAITING)
- break;
-
- if (mpi->mpi_state == MPI_OK) {
- if (mpq->mpq_serial_fn == NULL)
- break;
- serial_fn = mpq->mpq_serial_fn;
- mpq->mpq_serial_fn = NULL;
-
- mtx_leave(&mpq->mpq_mtx);
- serial_fn(mpi->mpi_data);
- mtx_enter(&mpq->mpq_mtx);
-
- mpq->mpq_serial_fn = serial_fn;
- } else if (mpi->mpi_state == MPI_BARRIER) {
- *(int *) mpi->mpi_data = 0;
- wakeup(mpi->mpi_data);
- } else if (mpi->mpi_state == MPI_DEAD) {
- /* pass */
- } else {
- panic("invalid state: %d", mpi->mpi_state);
- }
-
- mpi->mpi_state = MPI_EMPTY;
- MPQ_INC(mpq, mpq_shead);
- }
-
- msleep(&mpq->mpq_phead, &mpq->mpq_mtx, PWAIT, "bored", 0);
- }
-}
-
-/* External functions */
-void
-mpq_init(struct mpq *mpq, int ipl, void *(*parallel)(void *), void (*serial)(void *))
-{
- bzero(mpq, sizeof(*mpq));
- mtx_init(&mpq->mpq_mtx, ipl);
- mpq->mpq_parallel_fn = parallel;
- mpq->mpq_serial_fn = serial;
-
- mpq_size(mpq, MPQ_DEFAULT_SIZE);
- mpq_nthreads(mpq, MPQ_DEFAULT_THREADS);
-}
-
-size_t
-mpq_size(struct mpq *mpq, size_t size)
-{
- size_t oldsize;
- struct mpi *mpi, *tmp;
-
- mpi = mallocarray(size, sizeof(struct mpi), M_DEVBUF, M_WAITOK);
-
- mtx_enter(&mpq->mpq_mtx);
-
- if (size > mpq->mpq_itemssize) {
-
- bzero(mpi, size * sizeof(struct mpi));
-
- if (mpq->mpq_items != NULL)
- memcpy(mpi, mpq->mpq_items, mpq->mpq_itemssize * sizeof(struct mpi));
-
- mpq->mpq_itemssize = size;
- tmp = mpq->mpq_items;
- mpq->mpq_items = mpi;
- mpi = tmp;
- }
-
- oldsize = mpq->mpq_size;
- mpq->mpq_size = size;
-
- mtx_leave(&mpq->mpq_mtx);
-
- free(mpi, M_DEVBUF, 0);
-
- return oldsize;
-}
-
-void
-mpq_nthreads(struct mpq *mpq, size_t nthreads)
-{
- size_t old_nthreads;
- mtx_enter(&mpq->mpq_mtx);
- old_nthreads = mpq->mpq_wantthreads;
- mpq->mpq_wantthreads = nthreads;
- mtx_leave(&mpq->mpq_mtx);
- wakeup(&mpq->mpq_phead);
- while (old_nthreads++ < nthreads)
- kthread_create((void (*)(void *))_mpq_thread, mpq, NULL, "wg");
-}
-
-void
-mpq_barrier(struct mpq *mpq)
-{
- int b = 1;
- mtx_enter(&mpq->mpq_mtx);
- while (1) {
- if (MPQ_ITEM(mpq, mpq_tail)->mpi_state == MPI_EMPTY) {
- MPQ_ITEM(mpq, mpq_tail)->mpi_data = &b;
- MPQ_ITEM(mpq, mpq_tail)->mpi_state = MPI_BARRIER;
- MPQ_INC(mpq, mpq_tail);
- break;
- }
- msleep(&b, &mpq->mpq_mtx, PWAIT, "mpq_barrier", 1);
- }
- mtx_leave(&mpq->mpq_mtx);
- wakeup_one(&mpq->mpq_phead);
-
- while (b)
- tsleep(&b, PWAIT, "mpq_barrier", 1);
-}
-
-void
-mpq_destroy(struct mpq *mpq)
-{
- mpq_size(mpq, 0);
- mpq_barrier(mpq);
- mpq_nthreads(mpq, 0);
-}
-
-int
-mpq_enqueue(struct mpq *mpq, void *data)
-{
- int dropped = 0;
-
- mtx_enter(&mpq->mpq_mtx);
- if (MPQ_LOAD(mpq, mpq_shead) < mpq->mpq_size - 1) {
- if (MPQ_ITEM(mpq, mpq_tail)->mpi_state != MPI_EMPTY)
- panic("invalid state: %d", MPQ_ITEM(mpq, mpq_tail)->mpi_state);
- assert(MPQ_ITEM(mpq, mpq_tail)->mpi_state == MPI_EMPTY);
- MPQ_ITEM(mpq, mpq_tail)->mpi_data = data;
- MPQ_ITEM(mpq, mpq_tail)->mpi_state = MPI_WAITING;
- MPQ_INC(mpq, mpq_tail);
- } else {
- dropped = 1;
- }
- mtx_leave(&mpq->mpq_mtx);
-
- if (!dropped)
- wakeup_one(&mpq->mpq_phead);
-
- return dropped;
-}
-
-#endif /* _MPQ_H_ */