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., 675 Mass Ave, Cambridge, MA 02139, USA.
19 #include <gpxe/list.h>
20 #include <gpxe/init.h>
21 #include <gpxe/process.h>
27 * We implement a trivial form of cooperative multitasking, in which
28 * all processes share a single stack and address space.
31 /** Process run queue */
32 static LIST_HEAD ( run_queue
);
34 /** Registered permanent processes */
35 static struct process processes
[0]
36 __table_start ( struct process
, processes
);
37 static struct process processes_end
[0]
38 __table_end ( struct process
, processes
);
41 * Add process to process list
45 void process_add ( struct process
*process
) {
46 ref_get ( process
->refcnt
);
47 list_add_tail ( &process
->list
, &run_queue
);
51 * Remove process from process list
55 * It is safe to call process_del() multiple times; further calls will
58 void process_del ( struct process
*process
) {
59 if ( ! list_empty ( &process
->list
) ) {
60 list_del ( &process
->list
);
61 INIT_LIST_HEAD ( &process
->list
);
62 ref_put ( process
->refcnt
);
67 * Single-step a single process
69 * This executes a single step of the first process in the run queue,
70 * and moves the process to the end of the run queue.
73 struct process
*process
;
75 list_for_each_entry ( process
, &run_queue
, list
) {
76 list_del ( &process
->list
);
77 list_add_tail ( &process
->list
, &run_queue
);
78 process
->step ( process
);
84 * Initialise processes
87 static void init_processes ( void ) {
88 struct process
*process
;
90 for ( process
= processes
; process
< processes_end
; process
++ ) {
91 process_add ( process
);
95 /** Process initialiser */
96 struct init_fn process_init_fn
__init_fn ( INIT_NORMAL
) = {
97 .initialise
= init_processes
,