/*
* Copyright (c) 2006, 2007, 2008 Antti Kantee. All Rights Reserved.
*
* Development of this software was supported by the
* Research Foundation of Helsinki University of Technology
*
* 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 AUTHOR ``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 AUTHOR 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.
*/
#if 0
#define DPRINTF(x) printf x
#else
#define DPRINTF(x)
#endif
/*
* Set the following to 1 to not handle each request on a separate
* stack. This is highly volatile kludge, therefore no external
* interface.
*/
int puffs_fakecc;
/*
* user stuff
*/
/*
* So, we need to get back to where we came from. This can happen in two
* different ways:
* 1) PCC_MLCONT is set, in which case we need to go to the mainloop
* 2) It is not set, and we simply jump to pcc_uc_ret.
*/
void
puffs_cc_yield(struct puffs_cc *pcc)
{
struct puffs_cc *jumpcc;
int rv;
assert(puffs_fakecc == 0);
if ((~pcc->pcc_flags & (PCC_BORROWED|PCC_DONE)) == 0) {
pcc->pcc_flags &= ~(PCC_BORROWED|PCC_DONE);
/*
* see the XXX comment in puffs__cc_cont
*/
puffs__cc_destroy(pcc, 1);
setcontext(&pcc->pcc_uc_ret);
}
pcc->pcc_flags &= ~PCC_BORROWED;
/*
* Internal continue routine. This has slightly different semantics.
* We simply make our cc available in the freelist and jump to the
* indicated pcc.
*/
void
puffs__cc_cont(struct puffs_cc *pcc)
{
struct puffs_cc *mycc;
/*
* XXX: race between setcontext() and recycle if
* we go multithreaded
*/
puffs__cc_destroy(mycc, 1);
pcc->pcc_flags |= PCC_MLCONT;
setcontext(&pcc->pcc_uc);
}
void
puffs_cc_continue(struct puffs_cc *pcc)
{
/* ramble on */
DPRINTF(("puffs_cc_continue: pcc %p\n", pcc));
if (puffs_fakecc) {
pcc->pcc_func(pcc->pcc_farg);
} else {
swapcontext(&pcc->pcc_uc_ret, &pcc->pcc_uc);
}
}
/*
* "Borrows" pcc, *NOT* called from pcc owner. Acts like continue.
* So the idea is to use this, give something the context back to
* run to completion and then jump back to where ever this was called
* from after the op dispatching is complete (or if the pcc decides to
* yield again).
*/
void
puffs__goto(struct puffs_cc *loanpcc)
{
if (puffs_fakecc) {
pcc->pcc_func = func;
pcc->pcc_farg = pcc;
} else {
const long psize = sysconf(_SC_PAGESIZE);
/* link context */
pcc->pcc_uc.uc_link = &pcc->pcc_uc_ret;
/* setup stack
*
* XXX: I guess this should theoretically be preserved by
* swapcontext(). However, it gets lost. So reinit it.
*/
st = &pcc->pcc_uc.uc_stack;
st->ss_sp = ((uint8_t *)(void *)pcc) + psize;
st->ss_size = stacksize - psize;
st->ss_flags = 0;
/*
* Give us an initial context to jump to.
*
* Our manual page says that portable code shouldn't
* rely on being able to pass pointers through makecontext().
* kjk says that NetBSD code doesn't need to worry about this.
* uwe says it would be like putting a "keep away from
* children" sign on a box of toys.
*/
makecontext(&pcc->pcc_uc, (void *)func, 1, (uintptr_t)pcc);
}