Rune - Fix array passing, optimize constant array indices
[rune.git] / tests / threads.d
1 #!/usr/local/bin/rune -x
2 #
3 #       Demonstrate synchronous threads vs asynchronous threads.  A threaded
4 #       procedure is synchronous by default, meaning that it executes in an
5 #       ultra-light-weight fashion using the machine context of the caller
6 #       and only switching away if it blocks.  For example, if you have a
7 #       ton of GUI gadgets, each implemented as a thread, you may desire to
8 #       implement them as synchronous entities within a single asynchronous
9 #       container to reduce machine overhead.
10 #
11 #       An asynchronous thread creates an actual machine thread to manage
12 #       the thread.
13
14 import "sys";
15 import <stdio>;
16
17 alias stdio.File *stdout = stdio.stdout;
18
19 int
20 main(int ac, char **av)
21 {
22         int i;
23
24         stdout->setmode(stdio.File.M_FULL);
25
26         stdout->show(dosync("this is td=0000", 0));
27         stdout->show(dosync("this is td=0001", 1));
28         stdout->show(dosync("this is td=0002", 2));
29         stdout->show(dosync("this is td=0003", 3));
30         stdout->show(dosync("this is td=0004", 4));
31         stdout->show(dosync("this is td=0005", 5));
32         stdout->show(dosync("this is td=0006", 6));
33         stdout->show(dosync("this is td=0007", 7));
34         stdout->show(dosync("this is td=0008", 8));
35         stdout->show(dosync("this is td=0009", 9));
36
37         stdout->show(doasync("this is atd=0000", 0));
38         stdout->show(doasync("this is atd=0001", 1));
39         stdout->show(doasync("this is atd=0002", 2));
40         stdout->show(doasync("this is atd=0003", 3));
41         stdout->show(doasync("this is atd=0004", 4));
42         stdout->show(doasync("this is atd=0005", 5));
43         stdout->show(doasync("this is atd=0006", 6));
44         stdout->show(doasync("this is atd=0007", 7));
45         stdout->show(doasync("this is atd=0008", 8));
46         stdout->show(doasync("this is atd=0009", 9));
47
48         Thread.waitThreads();
49
50         stdout->show(dosync("this is td=0000", 0));
51         stdout->show(dosync("this is td=0001", 1));
52         stdout->show(dosync("this is td=0002", 2));
53         stdout->show(dosync("this is td=0003", 3));
54         stdout->show(dosync("this is td=0004", 4));
55         stdout->show(dosync("this is td=0005", 5));
56         stdout->show(dosync("this is td=0006", 6));
57         stdout->show(dosync("this is td=0007", 7));
58         stdout->show(dosync("this is td=0008", 8));
59         stdout->show(dosync("this is td=0009", 9));
60
61         stdout->show(doasync("this is atd=0000", 0));
62         stdout->show(doasync("this is atd=0001", 1));
63         stdout->show(doasync("this is atd=0002", 2));
64         stdout->show(doasync("this is atd=0003", 3));
65         stdout->show(doasync("this is atd=0004", 4));
66         stdout->show(doasync("this is atd=0005", 5));
67         stdout->show(doasync("this is atd=0006", 6));
68         stdout->show(doasync("this is atd=0007", 7));
69         stdout->show(doasync("this is atd=0008", 8));
70         stdout->show(doasync("this is atd=0009", 9));
71 }
72
73 thread
74 int
75 dosync(const char *str, int x)
76 {
77         int i;
78         int copyx = x;
79         const char *copy = str;
80
81         result(x + 1);
82
83         for (i = 0; i < 10000; ++i) {
84                 stdout->show(copy, i);
85                 thread_schedule immediate;
86         }
87         stdout->fflush();
88 }
89
90 preempt thread
91 int
92 doasync(const char *str, int x)
93 {
94         int i;
95         int copyx = x;
96         const char *copy = str;
97         char buf[32];
98         char *pp = &buf[0];
99
100         result(x + 1);
101
102         for (i = 0; i < 10000; ++i) {
103                 stdout->show(copy, i);
104         }
105         stdout->fflush();
106 }