File: main.bs

package info (click to toggle)
storm-lang 0.7.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,028 kB
  • sloc: ansic: 261,471; cpp: 140,432; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (155 lines) | stat: -rw-r--r-- 3,082 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
Nat computeSum(Nat n, Str progressText) {
	Nat sum = 0;
	for (Nat i = 1; i <= n; i++) {
		sum += i;
		print("${progressText}: at ${i}, sum ${sum}");
		sleep(1 s);
	}
	return sum;
}

Word fibonacci(Nat n) {
	if (n < 2)
		return n;
	else
		return fibonacci(n - 1) + fibonacci(n - 2);
}

thread FibA;
thread FibB;

Word fibonacciA(Nat n) on FibA {
	return fibonacci(n);
}

Word fibonacciB(Nat n) on FibB {
	return fibonacci(n);
}

class Data {
	Nat value;

	init() {}

	init(Data other) {
		init { value = other.value; }
		print("Copied data!");
	}
}

thread MyThread;

Data modifyData(Data toModify) on MyThread {
	toModify.value = 10;
	return toModify;
}

class ActorData on MyThread {
	Nat value;

	void set(Nat newValue) {
		value = newValue;
	}

	void add(Nat toAdd) {
		value = value + toAdd;
	}
}

ActorData modifyData(ActorData toModify) on MyThread {
	toModify.value = 10;
	return toModify;
}

thread MyOtherThread;

void addData(ActorData toUpdate, Nat toAdd) on MyOtherThread {
	toUpdate.set(toUpdate.value + toAdd);
}

void addData2(ActorData toUpdate, Nat toAdd) on MyOtherThread {
	toUpdate.add(toAdd);
}

void main() {
	{
		print("-- Spawn ---");

		Future<Nat> sum3 = spawn computeSum(3, "To 3");
		Nat sum5 = computeSum(5, "To 5");
		print("Sum of 1 to 3 is: ${sum3.result}");
		print("Sum of 1 to 5 is: ${sum5}");
	}

	{
		print("-- Threads, same OS thread --");

		Moment start;
		Future<Word> value1 = spawn fibonacci(40);
		Future<Word> value2 = spawn fibonacci(41);
		print("Finished fibonacci(40)=${value1.result}");
		print("Finished fibonacci(41)=${value2.result}");
		Moment end;
		print("Total time: ${end - start}");
	}

	{
		print("-- Threads, different OS threads --");

		Moment start;
		Future<Word> value1 = spawn fibonacciA(40);
		Future<Word> value2 = spawn fibonacciB(41);
		print("Finished fibonacci(40)=${value1.result}");
		print("Finished fibonacci(41)=${value2.result}");
		Moment end;
		print("Total time: ${end - start}");
	}

	{
		print("-- Sharing data --");

		Data data;
		Future<Data> modify = spawn modifyData(data);
		data.value = 20;
		Data modified = modify.result();
		print("After modifications: ${data.value}");
		print("Returned object: ${modified.value}");
		print("Are they the same? ${data is modified}");
	}

	{
		print("-- Sharing data with actors --");

		ActorData data;
		Future<ActorData> modify = spawn modifyData(data);
		data.set(20);
		ActorData modified = modify.result();
		print("After modifications: ${data.value}");
		print("Returned object: ${modified.value}");
		print("Are they the same? ${data is modified}");
	}

	{
		print("-- Thread-safe operations, incorrect --");

		ActorData data;
		Future<void> modify = spawn addData(data, 20);
		for (Nat i = 0; i < 10; i++) {
			data.set(data.value + 10);
		}
		modify.result();
		print("Final result: ${data.value}");
	}

	{
		print("-- Thread-safe operations, correct --");

		ActorData data;
		Future<void> modify = spawn addData2(data, 20);
		for (Nat i = 0; i < 10; i++) {
			data.add(10);
		}
		modify.result();
		print("Final result: ${data.value}");
	}
}