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
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
26 #include <ipxe/list.h>
27 #include <ipxe/init.h>
28 #include <ipxe/process.h>
34 * We implement a trivial form of cooperative multitasking, in which
35 * all processes share a single stack and address space.
38 /** Process run queue */
39 static LIST_HEAD ( run_queue );
42 * Get pointer to object containing process
45 * @ret object Containing object
47 void * process_object ( struct process *process ) {
48 return ( ( ( void * ) process ) - process->desc->offset );
52 * Add process to process list
56 * It is safe to call process_add() multiple times; further calls will
59 void process_add ( struct process *process ) {
60 if ( ! process_running ( process ) ) {
61 DBGC ( PROC_COL ( process ), "PROCESS " PROC_FMT
62 " starting\n", PROC_DBG ( process ) );
63 ref_get ( process->refcnt );
64 list_add_tail ( &process->list, &run_queue );
66 DBGC ( PROC_COL ( process ), "PROCESS " PROC_FMT
67 " already started\n", PROC_DBG ( process ) );
72 * Remove process from process list
76 * It is safe to call process_del() multiple times; further calls will
79 void process_del ( struct process *process ) {
80 if ( process_running ( process ) ) {
81 DBGC ( PROC_COL ( process ), "PROCESS " PROC_FMT
82 " stopping\n", PROC_DBG ( process ) );
83 list_del ( &process->list );
84 INIT_LIST_HEAD ( &process->list );
85 ref_put ( process->refcnt );
87 DBGC ( PROC_COL ( process ), "PROCESS " PROC_FMT
88 " already stopped\n", PROC_DBG ( process ) );
93 * Single-step a single process
95 * This executes a single step of the first process in the run queue,
96 * and moves the process to the end of the run queue.
99 struct process *process;
100 struct process_descriptor *desc;
103 if ( ( process = list_first_entry ( &run_queue, struct process,
105 ref_get ( process->refcnt ); /* Inhibit destruction mid-step */
106 desc = process->desc;
107 object = process_object ( process );
108 if ( desc->reschedule ) {
109 list_del ( &process->list );
110 list_add_tail ( &process->list, &run_queue );
112 process_del ( process );
114 DBGC2 ( PROC_COL ( process ), "PROCESS " PROC_FMT
115 " executing\n", PROC_DBG ( process ) );
116 desc->step ( object );
117 DBGC2 ( PROC_COL ( process ), "PROCESS " PROC_FMT
118 " finished executing\n", PROC_DBG ( process ) );
119 ref_put ( process->refcnt ); /* Allow destruction */
124 * Initialise processes
127 static void init_processes ( void ) {
128 struct process *process;
130 for_each_table_entry ( process, PERMANENT_PROCESSES )
131 process_add ( process );
134 /** Process initialiser */
135 struct init_fn process_init_fn __init_fn ( INIT_NORMAL ) = {
136 .initialise = init_processes,