[process] Pass containing object pointer to process step() methods

Give the step() method a pointer to the containing object, rather than
a pointer to the process.  This is consistent with the operation of
interface methods, and allows a single function to serve as both an
interface method and a process step() method.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
This commit is contained in:
Michael Brown
2011-06-24 14:14:41 +01:00
parent ba3633782b
commit e01ec74601
16 changed files with 229 additions and 101 deletions

View File

@@ -26,15 +26,7 @@ static void hw_finished ( struct hw *hw, int rc ) {
process_del ( &hw->process );
}
static struct interface_operation hw_xfer_operations[] = {
INTF_OP ( intf_close, struct hw *, hw_finished ),
};
static struct interface_descriptor hw_xfer_desc =
INTF_DESC ( struct hw, xfer, hw_xfer_operations );
static void hw_step ( struct process *process ) {
struct hw *hw = container_of ( process, struct hw, process );
static void hw_step ( struct hw *hw ) {
int rc;
if ( xfer_window ( &hw->xfer ) ) {
@@ -43,6 +35,16 @@ static void hw_step ( struct process *process ) {
}
}
static struct interface_operation hw_xfer_operations[] = {
INTF_OP ( intf_close, struct hw *, hw_finished ),
};
static struct interface_descriptor hw_xfer_desc =
INTF_DESC ( struct hw, xfer, hw_xfer_operations );
static struct process_descriptor hw_process_desc =
PROC_DESC ( struct hw, process, hw_step );
static int hw_open ( struct interface *xfer, struct uri *uri __unused ) {
struct hw *hw;
@@ -52,7 +54,7 @@ static int hw_open ( struct interface *xfer, struct uri *uri __unused ) {
return -ENOMEM;
ref_init ( &hw->refcnt, NULL );
intf_init ( &hw->xfer, &hw_xfer_desc, &hw->refcnt );
process_init ( &hw->process, hw_step, &hw->refcnt );
process_init ( &hw->process, &hw_process_desc, &hw->refcnt );
/* Attach parent interface, mortalise self, and return */
intf_plug_plug ( &hw->xfer, xfer );

View File

@@ -33,6 +33,16 @@ FILE_LICENCE ( GPL2_OR_LATER );
/** Process run queue */
static LIST_HEAD ( run_queue );
/**
* Get pointer to object containing process
*
* @v process Process
* @ret object Containing object
*/
void * process_object ( struct process *process ) {
return ( ( ( void * ) process ) - process->desc->offset );
}
/**
* Add process to process list
*
@@ -43,13 +53,13 @@ static LIST_HEAD ( run_queue );
*/
void process_add ( struct process *process ) {
if ( ! process_running ( process ) ) {
DBGC ( process, "PROCESS %p (%p) starting\n",
process, process->step );
DBGC ( PROC_COL ( process ), "PROCESS " PROC_FMT
" starting\n", PROC_DBG ( process ) );
ref_get ( process->refcnt );
list_add_tail ( &process->list, &run_queue );
} else {
DBGC ( process, "PROCESS %p (%p) already started\n",
process, process->step );
DBGC ( PROC_COL ( process ), "PROCESS " PROC_FMT
" already started\n", PROC_DBG ( process ) );
}
}
@@ -63,14 +73,14 @@ void process_add ( struct process *process ) {
*/
void process_del ( struct process *process ) {
if ( process_running ( process ) ) {
DBGC ( process, "PROCESS %p (%p) stopping\n",
process, process->step );
DBGC ( PROC_COL ( process ), "PROCESS " PROC_FMT
" stopping\n", PROC_DBG ( process ) );
list_del ( &process->list );
INIT_LIST_HEAD ( &process->list );
ref_put ( process->refcnt );
} else {
DBGC ( process, "PROCESS %p (%p) already stopped\n",
process, process->step );
DBGC ( PROC_COL ( process ), "PROCESS " PROC_FMT
" already stopped\n", PROC_DBG ( process ) );
}
}
@@ -82,17 +92,21 @@ void process_del ( struct process *process ) {
*/
void step ( void ) {
struct process *process;
struct process_descriptor *desc;
void *object;
if ( ( process = list_first_entry ( &run_queue, struct process,
list ) ) ) {
ref_get ( process->refcnt ); /* Inhibit destruction mid-step */
desc = process->desc;
object = process_object ( process );
list_del ( &process->list );
list_add_tail ( &process->list, &run_queue );
ref_get ( process->refcnt ); /* Inhibit destruction mid-step */
DBGC2 ( process, "PROCESS %p (%p) executing\n",
process, process->step );
process->step ( process );
DBGC2 ( process, "PROCESS %p (%p) finished executing\n",
process, process->step );
DBGC2 ( PROC_COL ( process ), "PROCESS " PROC_FMT
" executing\n", PROC_DBG ( process ) );
desc->step ( object );
DBGC2 ( PROC_COL ( process ), "PROCESS " PROC_FMT
" finished executing\n", PROC_DBG ( process ) );
ref_put ( process->refcnt ); /* Allow destruction */
}
}

View File

@@ -86,16 +86,17 @@ struct numeric_resolv {
int rc;
};
static void numeric_step ( struct process *process ) {
struct numeric_resolv *numeric =
container_of ( process, struct numeric_resolv, process );
static void numeric_step ( struct numeric_resolv *numeric ) {
process_del ( process );
process_del ( &numeric->process );
if ( numeric->rc == 0 )
resolv_done ( &numeric->resolv, &numeric->sa );
intf_shutdown ( &numeric->resolv, numeric->rc );
}
static struct process_descriptor numeric_process_desc =
PROC_DESC ( struct numeric_resolv, process, numeric_step );
static int numeric_resolv ( struct interface *resolv,
const char *name, struct sockaddr *sa ) {
struct numeric_resolv *numeric;
@@ -107,7 +108,8 @@ static int numeric_resolv ( struct interface *resolv,
return -ENOMEM;
ref_init ( &numeric->refcnt, NULL );
intf_init ( &numeric->resolv, &null_intf_desc, &numeric->refcnt );
process_init ( &numeric->process, numeric_step, &numeric->refcnt );
process_init ( &numeric->process, &numeric_process_desc,
&numeric->refcnt );
memcpy ( &numeric->sa, sa, sizeof ( numeric->sa ) );
DBGC ( numeric, "NUMERIC %p attempting to resolve \"%s\"\n",