File tree Expand file tree Collapse file tree 2 files changed +37
-0
lines changed
src/com/annimon/ownlang/lib/modules Expand file tree Collapse file tree 2 files changed +37
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -18,6 +18,7 @@ public void init() {
18
18
Functions .set ("time" , new std_time ());
19
19
Functions .set ("sleep" , new std_sleep ());
20
20
Functions .set ("thread" , new std_thread ());
21
+ Functions .set ("sync" , new std_sync ());
21
22
22
23
// String
23
24
Functions .set ("sprintf" , new std_sprintf ());
You can’t perform that action at this time.
0 commit comments