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 DBGC ( process
, "PROCESS %p starting\n", process
);
47 ref_get ( process
->refcnt
);
48 list_add_tail ( &process
->list
, &run_queue
);
52 * Remove process from process list
56 * It is safe to call process_del() multiple times; further calls will
59 void process_del ( struct process
*process
) {
60 if ( ! list_empty ( &process
->list
) ) {
61 DBGC ( process
, "PROCESS %p stopping\n", process
);
62 list_del ( &process
->list
);
63 INIT_LIST_HEAD ( &process
->list
);
64 ref_put ( process
->refcnt
);
66 DBGC ( process
, "PROCESS %p already stopped\n", process
);
71 * Single-step a single process
73 * This executes a single step of the first process in the run queue,
74 * and moves the process to the end of the run queue.
77 struct process
*process
;
79 list_for_each_entry ( process
, &run_queue
, list
) {
80 list_del ( &process
->list
);
81 list_add_tail ( &process
->list
, &run_queue
);
82 process
->step ( process
);
88 * Initialise processes
91 static void init_processes ( void ) {
92 struct process
*process
;
94 for ( process
= processes
; process
< processes_end
; process
++ ) {
95 process_add ( process
);
99 /** Process initialiser */
100 struct init_fn process_init_fn
__init_fn ( INIT_NORMAL
) = {
101 .initialise
= init_processes
,