2 * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 FILE_LICENCE ( GPL2_OR_LATER );
22 #include <ipxe/list.h>
23 #include <ipxe/init.h>
24 #include <ipxe/process.h>
30 * We implement a trivial form of cooperative multitasking, in which
31 * all processes share a single stack and address space.
34 /** Process run queue */
35 static LIST_HEAD ( run_queue );
38 * Get pointer to object containing process
41 * @ret object Containing object
43 void * process_object ( struct process *process ) {
44 return ( ( ( void * ) process ) - process->desc->offset );
48 * Add process to process list
52 * It is safe to call process_add() multiple times; further calls will
55 void process_add ( struct process *process ) {
56 if ( ! process_running ( process ) ) {
57 DBGC ( PROC_COL ( process ), "PROCESS " PROC_FMT
58 " starting\n", PROC_DBG ( process ) );
59 ref_get ( process->refcnt );
60 list_add_tail ( &process->list, &run_queue );
62 DBGC ( PROC_COL ( process ), "PROCESS " PROC_FMT
63 " already started\n", PROC_DBG ( process ) );
68 * Remove process from process list
72 * It is safe to call process_del() multiple times; further calls will
75 void process_del ( struct process *process ) {
76 if ( process_running ( process ) ) {
77 DBGC ( PROC_COL ( process ), "PROCESS " PROC_FMT
78 " stopping\n", PROC_DBG ( process ) );
79 list_del ( &process->list );
80 INIT_LIST_HEAD ( &process->list );
81 ref_put ( process->refcnt );
83 DBGC ( PROC_COL ( process ), "PROCESS " PROC_FMT
84 " already stopped\n", PROC_DBG ( process ) );
89 * Single-step a single process
91 * This executes a single step of the first process in the run queue,
92 * and moves the process to the end of the run queue.
95 struct process *process;
96 struct process_descriptor *desc;
99 if ( ( process = list_first_entry ( &run_queue, struct process,
101 ref_get ( process->refcnt ); /* Inhibit destruction mid-step */
102 desc = process->desc;
103 object = process_object ( process );
104 if ( desc->reschedule ) {
105 list_del ( &process->list );
106 list_add_tail ( &process->list, &run_queue );
108 process_del ( process );
110 DBGC2 ( PROC_COL ( process ), "PROCESS " PROC_FMT
111 " executing\n", PROC_DBG ( process ) );
112 desc->step ( object );
113 DBGC2 ( PROC_COL ( process ), "PROCESS " PROC_FMT
114 " finished executing\n", PROC_DBG ( process ) );
115 ref_put ( process->refcnt ); /* Allow destruction */
120 * Initialise processes
123 static void init_processes ( void ) {
124 struct process *process;
126 for_each_table_entry ( process, PERMANENT_PROCESSES )
127 process_add ( process );
130 /** Process initialiser */
131 struct init_fn process_init_fn __init_fn ( INIT_NORMAL ) = {
132 .initialise = init_processes,