Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / core / process.c
1 /*
2  * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
3  *
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.
8  *
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.
13  *
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
17  * 02110-1301, USA.
18  */
19
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 #include <ipxe/list.h>
23 #include <ipxe/init.h>
24 #include <ipxe/process.h>
25
26 /** @file
27  *
28  * Processes
29  *
30  * We implement a trivial form of cooperative multitasking, in which
31  * all processes share a single stack and address space.
32  */
33
34 /** Process run queue */
35 static LIST_HEAD ( run_queue );
36
37 /**
38  * Get pointer to object containing process
39  *
40  * @v process           Process
41  * @ret object          Containing object
42  */
43 void * process_object ( struct process *process ) {
44         return ( ( ( void * ) process ) - process->desc->offset );
45 }
46
47 /**
48  * Add process to process list
49  *
50  * @v process           Process
51  *
52  * It is safe to call process_add() multiple times; further calls will
53  * have no effect.
54  */
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 );
61         } else {
62                 DBGC ( PROC_COL ( process ), "PROCESS " PROC_FMT
63                        " already started\n", PROC_DBG ( process ) );
64         }
65 }
66
67 /**
68  * Remove process from process list
69  *
70  * @v process           Process
71  *
72  * It is safe to call process_del() multiple times; further calls will
73  * have no effect.
74  */
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 );
82         } else {
83                 DBGC ( PROC_COL ( process ), "PROCESS " PROC_FMT
84                        " already stopped\n", PROC_DBG ( process ) );
85         }
86 }
87
88 /**
89  * Single-step a single process
90  *
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.
93  */
94 void step ( void ) {
95         struct process *process;
96         struct process_descriptor *desc;
97         void *object;
98
99         if ( ( process = list_first_entry ( &run_queue, struct process,
100                                             list ) ) ) {
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 );
107                 } else {
108                         process_del ( process );
109                 }
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 */
116         }
117 }
118
119 /**
120  * Initialise processes
121  *
122  */
123 static void init_processes ( void ) {
124         struct process *process;
125
126         for_each_table_entry ( process, PERMANENT_PROCESSES )
127                 process_add ( process );
128 }
129
130 /** Process initialiser */
131 struct init_fn process_init_fn __init_fn ( INIT_NORMAL ) = {
132         .initialise = init_processes,
133 };