Skip to content

Commit 5e1f520

Browse files
committed
Добавлена фукнция std::sync
1 parent b0396b6 commit 5e1f520

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.annimon.ownlang.lib.modules.functions;
2+
3+
import com.annimon.ownlang.exceptions.TypeException;
4+
import com.annimon.ownlang.lib.*;
5+
import java.util.concurrent.BlockingQueue;
6+
import java.util.concurrent.LinkedBlockingQueue;
7+
8+
public final class std_sync implements Function {
9+
10+
@Override
11+
public Value execute(Value... args) {
12+
Arguments.check(1, args.length);
13+
if (args[0].type() != Types.FUNCTION) {
14+
throw new TypeException(args[0].toString() + " is not a function");
15+
}
16+
17+
final BlockingQueue<Value> queue = new LinkedBlockingQueue<>(2);
18+
final Function synchronizer = (sArgs) -> {
19+
try {
20+
queue.put(sArgs[0]);
21+
} catch (InterruptedException ex) {
22+
Thread.currentThread().interrupt();
23+
}
24+
return NumberValue.ZERO;
25+
};
26+
final Function callback = ((FunctionValue) args[0]).getValue();
27+
callback.execute(new FunctionValue(synchronizer));
28+
29+
try {
30+
return queue.take();
31+
} catch (InterruptedException ex) {
32+
throw new RuntimeException(ex);
33+
}
34+
}
35+
36+
}

src/com/annimon/ownlang/lib/modules/std.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public void init() {
1818
Functions.set("time", new std_time());
1919
Functions.set("sleep", new std_sleep());
2020
Functions.set("thread", new std_thread());
21+
Functions.set("sync", new std_sync());
2122

2223
// String
2324
Functions.set("sprintf", new std_sprintf());

0 commit comments

Comments
 (0)