Selaa lähdekoodia

Added basic code for implementing co-operative multitasking.

Yes, you really can do it in 65 bytes.
tags/v0.9.3
Michael Brown 18 vuotta sitten
vanhempi
commit
23c494d14e
2 muutettua tiedostoa jossa 88 lisäystä ja 0 poistoa
  1. 56
    0
      src/core/process.c
  2. 32
    0
      src/include/gpxe/process.h

+ 56
- 0
src/core/process.c Näytä tiedosto

@@ -0,0 +1,56 @@
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., 675 Mass Ave, Cambridge, MA 02139, USA.
17
+ */
18
+
19
+#include <gpxe/list.h>
20
+#include <gpxe/process.h>
21
+
22
+/** @file
23
+ *
24
+ * Processes
25
+ *
26
+ * We implement a trivial form of cooperative multitasking, in which
27
+ * all processes share a single stack and address space.
28
+ */
29
+
30
+/** Process run queue */
31
+static LIST_HEAD ( run_queue );
32
+
33
+/**
34
+ * Add process to run queue
35
+ *
36
+ * @v process		Process
37
+ */
38
+void schedule ( struct process *process ) {
39
+	list_add_tail ( &process->list, &run_queue );
40
+}
41
+
42
+/**
43
+ * Single-step a single process
44
+ *
45
+ * This removes the first process from the run queue and executes a
46
+ * single step of that process.
47
+ */
48
+void step ( void ) {
49
+	struct process *process;
50
+
51
+	list_for_each_entry ( process, &run_queue, list ) {
52
+		list_del ( &process->list );
53
+		process->step ( process );
54
+		break;
55
+	}
56
+}

+ 32
- 0
src/include/gpxe/process.h Näytä tiedosto

@@ -0,0 +1,32 @@
1
+#ifndef _GPXE_PROCESS_H
2
+#define _GPXE_PROCESS_H
3
+
4
+/** @file
5
+ *
6
+ * Processes
7
+ *
8
+ */
9
+
10
+#include <gpxe/list.h>
11
+
12
+/** A process */
13
+struct process {
14
+	/** List of processes */
15
+	struct list_head list;
16
+	/**
17
+	 * Single-step the process
18
+	 *
19
+	 * This method should execute a single step of the process.
20
+	 * Returning from this method is isomorphic to yielding the
21
+	 * CPU to another process.
22
+	 *
23
+	 * If the process wishes to be executed again, it must re-add
24
+	 * itself to the run queue using schedule().
25
+	 */
26
+	void ( * step ) ( struct process *process );
27
+};
28
+
29
+extern void schedule ( struct process *process );
30
+extern void step ( void );
31
+
32
+#endif /* _GPXE_PROCESS_H */

Loading…
Peruuta
Tallenna