273
|
1 Design
|
|
2 ======
|
272
|
3
|
273
|
4 OS
|
|
5 --
|
|
6
|
|
7 Processes / threads
|
|
8 ~~~~~~~~~~~~~~~~~~~
|
272
|
9
|
|
10 Processes are completely seperated and fully pre-emptive.
|
|
11 This means a process can be unscheduled at any moment.
|
|
12
|
|
13 Threads are co-operative. This means they yield control
|
|
14 voluntary. This means that mutexes and locks are not required.
|
|
15 This is done with the built-in language feature called tasks.
|
|
16
|
|
17 If some heavy duty task must be performed, either way spawn
|
|
18 a new process, or yield frequently from this hard labour.
|
|
19
|
273
|
20 tasks
|
|
21 ~~~~~
|
272
|
22
|
273
|
23 Consider the following:
|
|
24
|
|
25 .. code::
|
272
|
26
|
273
|
27 function int insanemath(int a)
|
272
|
28 {
|
273
|
29 while (a > 0)
|
|
30 {
|
|
31 a = a -1;
|
|
32 resume agent1;
|
|
33 }
|
|
34 return a - 1;
|
272
|
35 }
|
|
36
|
273
|
37 task agent1()
|
|
38 {
|
|
39 start agent2;
|
|
40 }
|
272
|
41
|
273
|
42 task agent2()
|
|
43 {
|
|
44 insanemath(55);
|
|
45 insanemath(44);
|
|
46 }
|
272
|
47
|
273
|
48 task main()
|
|
49 {
|
|
50 start agent1;
|
|
51 join agent1;
|
|
52 }
|
|
53
|
272
|
54
|
|
55 Say to tasks are running in concurrent / parallel.
|
|
56
|
|
57
|
|
58
|
|
59 Stack layout for tasks.
|
|
60 ||
|
|
61 ||
|
|
62 \/
|
|
63 +---------+
|
|
64 | return address
|
|
65 | locals
|
|
66 |
|
|
67 +------
|
|
68 | return address
|
|
69 | locals
|
|
70 |
|
|
71 +---
|
|
72
|
|
73 Assembly code for the functions above:
|
|
74
|
273
|
75 .. code::
|
|
76
|
|
77 .code
|
|
78 insanemath:
|
|
79 L1:
|
|
80 load r0, sp - 4
|
|
81 cmp r0, 0
|
|
82 jl L2
|
|
83 dec r0
|
|
84 store r0, sp - 4
|
|
85 jmp L1
|
|
86 L2:
|
|
87 ret
|
272
|
88
|
273
|
89 agent1:
|
|
90 hlt?
|
272
|
91
|
273
|
92 agent2:
|
|
93 hlt?
|
272
|
94
|
273
|
95 main:
|
|
96 jmp agent1
|
272
|
97
|
273
|
98 .data
|
|
99 agent1_task:
|
|
100 dd 0
|
|
101 agent2_task:
|
|
102 dd 0
|
272
|
103
|