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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
|
use core;
use core:debug;
use lang:bs:macro;
// Declare a named thread. Threads are a special kind of global variables, since
// the compiler have to impose stricter checks on threads compared to regular global
// variables. For example, a named thread may not mutate like a global variable may do
// in some cases during updates. This would break thread-safety.
thread Other;
// Should be able to access threads as regular values.
Thread getThread() {
Other;
}
Thread getCompilerThread() {
Compiler;
}
Int postInt() {
otherIntFn(1);
}
Int otherIntFn(Int v) on Other {
8 + v;
}
Int postDbgVal() {
DbgVal val(10);
otherDbgValFn(val);
}
Int otherDbgValFn(DbgVal v) on Other {
8 + v.get;
}
Int postObject() {
CloneDerived d;
CloneDerived z = otherObjectFn(d);
// Should be 13. d is copied 2 times.
if (disjoint(d, z)) {
z.c;
} else {
0;
}
}
CloneDerived otherObjectFn(CloneDerived o) on Other {
o.c = o.c + o.a + o.b.get;
o;
}
// Try with a value as well!
value ThreadVal {
CloneDerived v;
Int z;
init() {
init() {
z = 10;
v();
}
}
}
Int postVal() {
ThreadVal v;
ThreadVal r = otherValFn(v);
if (disjoint(v.v, r.v)) {
r.v.c + r.z;
} else {
0;
}
}
ThreadVal otherValFn(ThreadVal v) on Other {
v.v = otherObjectFn(v.v);
v.z = 20;
v;
}
Int postMaybeVal() {
ThreadVal v;
ThreadVal? maybe = v;
ThreadVal r = otherMaybeValFn(maybe);
if (disjoint(v.v, r.v)) {
r.v.c + r.z;
} else {
0;
}
}
ThreadVal otherMaybeValFn(ThreadVal? v) on Other {
if (v) {
v.v = otherObjectFn(v.v);
v.z = 20;
v;
} else {
ThreadVal();
}
}
class ThreadObj on Other {
Int v;
Int get() {
v;
}
void set(Int z) {
v = z;
}
}
class DynActor on ? {
Int v;
init(Thread) {
init();
}
Int get() {
v;
}
void set(Int z) {
v = z;
}
}
class DerDynActor extends DynActor {
init(Thread, Int initial) {
init();
set(initial);
}
}
// This would not work unless 'o' is a threaded object.
void setValue(ThreadObj o) on Other {
o.set(20);
}
void setValue(DbgActor o, Int to) on Other {
o.set(to);
}
void setValue(DynActor o, Int to) on Other {
o.set(to);
}
Int findValue(ThreadObj o) on Other {
o.get;
}
Int findValue(DbgActor o) on Other {
o.get;
}
Int findValue(DynActor o) on Other {
o.get;
}
Int threadObj() {
ThreadObj z;
setValue(z);
findValue(z);
}
Int threadActor() {
DbgActor a(10);
setValue(a, 20);
findValue(a);
}
Int actorObj() {
DynActor z = DynActor(Other);
setValue(z, 31);
findValue(z);
}
Int actorDerObj() {
DynActor z = DerDynActor(Other, 22);
findValue(z);
}
// Test some future magic!
Int basicFuture() {
CloneDerived original;
Future<CloneDerived> fut;
fut.post(original);
CloneDerived c = fut.result;
CloneDerived d = fut.result;
if (disjoint(original, c)) {
// These should be different as well!
if (disjoint(c, d)) {
c.c;
} else {
0;
}
} else {
0;
}
}
Int valueFuture() {
CloneVal val;
Future<CloneVal> fut;
fut.post(val);
CloneVal r = fut.result;
if (disjoint(val.z, r.z)) {
r.z.get;
} else {
0;
}
}
Int intFuture() {
Future<Int> fut;
fut.post(22);
fut.result;
}
void noResultFuture() {
Future<CloneVal> fut;
fut.post(CloneVal());
// We're never getting the result back, make sure we do not crash too badly at least!
}
// Test spawn-async.
Int asyncPostInt() {
var r = spawn otherIntFn(1);
r.result;
}
Int asyncPostObject() {
CloneDerived d;
var f = spawn otherObjectFn(d);
CloneDerived z = f.result;
if (disjoint(d, z)) {
z.c;
} else {
0;
}
}
Int asyncPostObject2() {
CloneDerived d;
// Alternate syntax
var f = spawn d.otherObjectFn();
CloneDerived z = f.result;
if (disjoint(d, z)) {
z.c;
} else {
0;
}
}
Int asyncPostVal() {
ThreadVal v;
ThreadVal r = (spawn otherValFn(v)).result;
if (disjoint(v.v, r.v)) {
r.v.c + r.z;
} else {
0;
}
}
Int asyncObject() {
CloneDerived d;
var fut = spawn otherObjectFn(d);
CloneDerived z = fut.result;
// Should be 13. d is copied 3 times.
if (disjoint(d, z)) {
z.c;
} else {
0;
}
}
void voidThreadFn() on Other {
// Nothing here!
}
void spawnVoid() {
var fut = spawn voidThreadFn();
fut.result;
}
Int captureVal(CloneDerived v) {
v.c;
}
Int captureValO(CloneDerived v) on Other {
v.c;
}
CloneDerived returnVal() {
CloneDerived();
}
Int asyncPostSame() {
CloneDerived v;
var future = spawn captureVal(v);
future.result;
}
Int asyncReturnSame() {
var future = spawn returnVal();
future.result.c;
}
Int asyncPostSameThread() {
asyncPostSameI();
}
Int asyncPostSameI() on Other {
CloneDerived v;
var future = spawn captureValO(v);
future.result;
}
Int asyncPostExplicit() {
CloneDerived v;
var future = spawn(Other) captureVal(v);
future.result;
}
void asyncPostExplictError() {
CloneDerived v;
// Should cause an error:
spawn(Compiler) captureValO(v);
}
class ActorVar on Other {
// Public value that we will try to get from another thread.
CloneDerived value;
init() {
init();
}
}
Int threadVarAccess() {
// Try to access a variable inside an object on another thread. We should receive a clone!
ActorVar o;
o.value.c;
}
void threadVarAssign() {
// This does not currently work, and the compiler should tell us that.
ActorVar o;
o.value = CloneDerived();
}
class OnOther on Other {
Int echo(CloneDerived x) {
x.c;
}
// Similar to 'customThis', but actually a member.
Int defaultThis() {
CloneDerived d;
echo(d);
}
}
Int customThis() {
CloneDerived d;
OnOther this;
// The compiler must see that we *need* a copy here, even though we use an implicit "this".
echo(d);
}
Int defaultThis() {
OnOther z;
z.defaultThis();
}
|