Rune - Further Object abstraction work
[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 totalCount = 0;
20 int *totalCountP = &totalCount;
21
22 int
23 main(int ac, string_p *av)
24 {
25     int i;
26
27     stdout->setmode(stdio.File.M_FULL);
28
29     stdout->show(dosync("this is td=0000", 0));
30     stdout->show(dosync("this is td=0001", 1));
31     stdout->show(dosync("this is td=0002", 2));
32     stdout->show(dosync("this is td=0003", 3));
33     stdout->show(dosync("this is td=0004", 4));
34     stdout->show(dosync("this is td=0005", 5));
35     stdout->show(dosync("this is td=0006", 6));
36     stdout->show(dosync("this is td=0007", 7));
37     stdout->show(dosync("this is td=0008", 8));
38     stdout->show(dosync("this is td=0009", 9));
39
40     stdout->show(doasync("this is atd=0000", 0));
41     stdout->show(doasync("this is atd=0001", 1));
42     stdout->show(doasync("this is atd=0002", 2));
43     stdout->show(doasync("this is atd=0003", 3));
44     stdout->show(doasync("this is atd=0004", 4));
45     stdout->show(doasync("this is atd=0005", 5));
46     stdout->show(doasync("this is atd=0006", 6));
47     stdout->show(doasync("this is atd=0007", 7));
48     stdout->show(doasync("this is atd=0008", 8));
49     stdout->show(doasync("this is atd=0009", 9));
50
51     Thread.waitThreads();
52
53     stdout->show(dosync("this is td=0000", 0));
54     stdout->show(dosync("this is td=0001", 1));
55     stdout->show(dosync("this is td=0002", 2));
56     stdout->show(dosync("this is td=0003", 3));
57     stdout->show(dosync("this is td=0004", 4));
58     stdout->show(dosync("this is td=0005", 5));
59     stdout->show(dosync("this is td=0006", 6));
60     stdout->show(dosync("this is td=0007", 7));
61     stdout->show(dosync("this is td=0008", 8));
62     stdout->show(dosync("this is td=0009", 9));
63
64     stdout->show(doasync("this is atd=0000", 0));
65     stdout->show(doasync("this is atd=0001", 1));
66     stdout->show(doasync("this is atd=0002", 2));
67     stdout->show(doasync("this is atd=0003", 3));
68     stdout->show(doasync("this is atd=0004", 4));
69     stdout->show(doasync("this is atd=0005", 5));
70     stdout->show(doasync("this is atd=0006", 6));
71     stdout->show(doasync("this is atd=0007", 7));
72     stdout->show(doasync("this is atd=0008", 8));
73     stdout->show(doasync("this is atd=0009", 9));
74
75     Thread.waitThreads();
76     # Thread.mssleep(1000);
77
78     stdout->format("TotalCount %d\n", *totalCountP);
79     stdout->fflush();
80 }
81
82 thread
83 int
84 dosync(const char *str, int x)
85 {
86     int i;
87     int copyx = x;
88     const char *copy = str;
89
90     stdout->fflush();
91     result(x + 1);
92
93     for (i = 0; i < 10000; ++i) {
94         # stdout->show(copy, i);
95         thread_schedule immediate;
96     }
97     # stdout->fflush();
98 }
99
100 preempt thread
101 int
102 doasync(const char *str, int x)
103 {
104     int i;
105     int copyx = x;
106     const char *copy = str;
107     char buf[32];
108     char *pp = &buf[0];
109
110     stdout->fflush();
111     result(x + 1);
112
113     for (i = 0; i < 10000; ++i) {
114         doasync2();
115         # stdout->show(copy, i);
116     }
117     # stdout->fflush();
118     # Thread.mssleep(1000);
119 }
120
121 preempt thread
122 int
123 doasync2(void)
124 {
125     result;
126     ++*totalCountP;
127     # ++totalCount;
128 }