2006-04-29 18:42:09 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of the
|
|
|
|
* License, or any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <gpxe/list.h>
|
2007-07-04 00:09:56 +02:00
|
|
|
#include <gpxe/init.h>
|
2006-04-29 18:42:09 +02:00
|
|
|
#include <gpxe/process.h>
|
|
|
|
|
|
|
|
/** @file
|
|
|
|
*
|
|
|
|
* Processes
|
|
|
|
*
|
|
|
|
* We implement a trivial form of cooperative multitasking, in which
|
|
|
|
* all processes share a single stack and address space.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** Process run queue */
|
|
|
|
static LIST_HEAD ( run_queue );
|
|
|
|
|
2007-07-03 21:09:14 +02:00
|
|
|
/** Registered permanent processes */
|
|
|
|
static struct process processes[0]
|
|
|
|
__table_start ( struct process, processes );
|
|
|
|
static struct process processes_end[0]
|
|
|
|
__table_end ( struct process, processes );
|
|
|
|
|
2006-04-29 18:42:09 +02:00
|
|
|
/**
|
2007-05-26 17:00:56 +02:00
|
|
|
* Add process to process list
|
2006-04-29 18:42:09 +02:00
|
|
|
*
|
|
|
|
* @v process Process
|
|
|
|
*/
|
2007-05-26 17:00:56 +02:00
|
|
|
void process_add ( struct process *process ) {
|
2007-07-10 07:24:30 +02:00
|
|
|
DBGC ( process, "PROCESS %p starting\n", process );
|
2007-05-26 17:00:56 +02:00
|
|
|
ref_get ( process->refcnt );
|
2006-04-29 18:42:09 +02:00
|
|
|
list_add_tail ( &process->list, &run_queue );
|
|
|
|
}
|
|
|
|
|
2007-05-26 17:00:56 +02:00
|
|
|
/**
|
|
|
|
* Remove process from process list
|
|
|
|
*
|
|
|
|
* @v process Process
|
|
|
|
*
|
|
|
|
* It is safe to call process_del() multiple times; further calls will
|
|
|
|
* have no effect.
|
|
|
|
*/
|
|
|
|
void process_del ( struct process *process ) {
|
|
|
|
if ( ! list_empty ( &process->list ) ) {
|
2007-07-10 07:24:30 +02:00
|
|
|
DBGC ( process, "PROCESS %p stopping\n", process );
|
2007-05-26 17:00:56 +02:00
|
|
|
list_del ( &process->list );
|
|
|
|
INIT_LIST_HEAD ( &process->list );
|
|
|
|
ref_put ( process->refcnt );
|
2007-07-10 07:24:30 +02:00
|
|
|
} else {
|
|
|
|
DBGC ( process, "PROCESS %p already stopped\n", process );
|
2007-05-26 17:00:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-04-29 18:42:09 +02:00
|
|
|
/**
|
|
|
|
* Single-step a single process
|
|
|
|
*
|
2007-05-26 17:00:56 +02:00
|
|
|
* This executes a single step of the first process in the run queue,
|
|
|
|
* and moves the process to the end of the run queue.
|
2006-04-29 18:42:09 +02:00
|
|
|
*/
|
|
|
|
void step ( void ) {
|
|
|
|
struct process *process;
|
|
|
|
|
|
|
|
list_for_each_entry ( process, &run_queue, list ) {
|
|
|
|
list_del ( &process->list );
|
2007-05-26 17:00:56 +02:00
|
|
|
list_add_tail ( &process->list, &run_queue );
|
2008-10-12 16:06:07 +02:00
|
|
|
DBGC2 ( process, "PROCESS %p executing\n", process );
|
2006-04-29 18:42:09 +02:00
|
|
|
process->step ( process );
|
2008-10-12 16:06:07 +02:00
|
|
|
DBGC2 ( process, "PROCESS %p finished executing\n", process );
|
2006-04-29 18:42:09 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-07-03 21:09:14 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialise processes
|
|
|
|
*
|
|
|
|
*/
|
2007-07-04 00:09:56 +02:00
|
|
|
static void init_processes ( void ) {
|
2007-07-03 21:09:14 +02:00
|
|
|
struct process *process;
|
|
|
|
|
|
|
|
for ( process = processes ; process < processes_end ; process++ ) {
|
|
|
|
process_add ( process );
|
|
|
|
}
|
|
|
|
}
|
2007-07-04 00:09:56 +02:00
|
|
|
|
|
|
|
/** Process initialiser */
|
|
|
|
struct init_fn process_init_fn __init_fn ( INIT_NORMAL ) = {
|
|
|
|
.initialise = init_processes,
|
|
|
|
};
|