/*-
* Copyright (c) 2010-2020 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This material is based upon work partially supported by The
* NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
int
npf_worker_addfunc(npf_t *npf, npf_workfunc_t work)
{
KASSERTMSG(npf->worker_flags == 0,
"the task must be added before the npf_worker_enlist() call");
for (unsigned i = 0; i < NPF_MAX_WORKS; i++) {
if (npf->worker_funcs[i] == NULL) {
npf->worker_funcs[i] = work;
return 0;
}
}
return -1;
}
/*
* npf_worker_discharge: remove the NPF instance the list for workers.
*
* => May block waiting for a worker to finish processing the instance.
*/
void
npf_worker_discharge(npf_t *npf)
{
npf_workerinfo_t *winfo = worker_info;
/*
* Notify the worker(s) that we are removing this instance.
*/
mutex_enter(&winfo->lock);
KASSERT(npf->worker_flags & WFLAG_ACTIVE);
npf->worker_flags |= WFLAG_REMOVE;
cv_broadcast(&winfo->cv);
/* Wait for a worker to process this request. */
while (npf->worker_flags & WFLAG_ACTIVE) {
cv_wait(&winfo->exit_cv, &winfo->lock);
}
mutex_exit(&winfo->lock);
KASSERT(npf->worker_flags == 0);
}
/*
* Remove the NPF instance:
* - Release any structures owned by the worker.
* - Remove the instance from the list.
* - Notify any thread waiting for removal to complete.
*/
if (npf->worker_flags & WFLAG_INITED) {
npfk_thread_unregister(npf);
}
LIST_REMOVE(npf, worker_entry);
npf->worker_flags = 0;
cv_broadcast(&winfo->exit_cv);
}
/*
* npf_worker: the main worker loop, processing enlisted NPF instances.
*
* XXX: Currently, npf_workerinfo_t::lock would serialize all workers,
* so there is no point to have more than one worker; but there might
* not be much point anyway.
*/
static void
npf_worker(void *arg)
{
npf_workerinfo_t *winfo = arg;
npf_t *npf;
mutex_enter(&winfo->lock);
for (;;) {
unsigned wait_time = NPF_GC_MAXWAIT;
/*
* Iterate all instances. We do not use LIST_FOREACH here,
* since the instance can be removed.
*/
npf = LIST_FIRST(&winfo->instances);
while (npf) {
npf_t *next = LIST_NEXT(npf, worker_entry);
unsigned i_wait_time = process_npf_instance(winfo, npf);
wait_time = MIN(wait_time, i_wait_time);
npf = next;
}
/*
* Sleep and periodically wake up, unless we get notified.
*/
if (winfo->exit) {
break;
}
cv_timedwait(&winfo->cv, &winfo->lock, mstohz(wait_time));
}
mutex_exit(&winfo->lock);
KASSERTMSG(LIST_EMPTY(&winfo->instances),
"NPF instances must be discharged before the npfk_sysfini() call");