Libraryless. Click here for Pure Java version (9246L/51K).
1 | persistable sclass BoolVar { |
2 | bool a; // you can access this directly if you use one thread |
3 | |
4 | *() {} |
5 | *(bool *a) {} |
6 | |
7 | public synchronized void set(bool v default true) { |
8 | if (v != a) { |
9 | a = v; |
10 | notifyAll(); |
11 | } |
12 | } |
13 | |
14 | public synchronized bool get() { ret a; } |
15 | //public synchronized bool has() { true; } |
16 | public void clear aka unset() { set(false); } |
17 | |
18 | bool waitUntilTrue () { ret waitForValue(true); } |
19 | bool waitUntilFalse() { ret waitForValue(false); } |
20 | |
21 | bool waitUntilTrue (double timeoutSeconds) { ret waitForValue(timeoutSeconds, true); } |
22 | bool waitUntilFalse(double timeoutSeconds) { ret waitForValue(timeoutSeconds, false); } |
23 | |
24 | synchronized bool waitForValue(bool wantedValue) ctex { |
25 | while (a != wantedValue) |
26 | wait(); |
27 | ret a; |
28 | } |
29 | |
30 | synchronized bool waitForValue(double timeoutSeconds, bool wantedValue) ctex { |
31 | if (timeoutSeconds == infinity()) |
32 | ret waitForValue(wantedValue); |
33 | |
34 | long destTime = sysNowPlusSeconds_noPing(timeoutSeconds), remainingTime; |
35 | while (a != wantedValue |
36 | && (remainingTime = destTime-sysNow_noPing()) > 0) |
37 | wait(remainingTime); |
38 | |
39 | ret a; |
40 | } |
41 | |
42 | public synchronized bool getAndSet(bool b) { |
43 | var value = a; |
44 | set(b); |
45 | ret value; |
46 | } |
47 | |
48 | public synchronized bool waitUntilTrueAndClear() { |
49 | if (!waitUntilTrue()) false; |
50 | set(false); |
51 | true; |
52 | } |
53 | |
54 | public synchronized bool waitUntilTrueAndClear(double timeoutSeconds) { |
55 | if (!waitUntilTrue(timeoutSeconds)) false; |
56 | set(false); |
57 | true; |
58 | } |
59 | |
60 | O mutex() { this; } |
61 | } |
download show line numbers debug dex old transpilations
Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj
No comments. add comment
Snippet ID: | #1033321 |
Snippet name: | BoolVar (never null!) |
Eternal ID of this version: | #1033321/25 |
Text MD5: | bbd2e3cce0b0e75b4fe505ea7e150729 |
Transpilation MD5: | d338f663d9351d80378265a411ce1c86 |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2022-09-01 21:36:43 |
Source code size: | 1617 bytes / 61 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 178 / 374 |
Version history: | 24 change(s) |
Referenced in: | [show references] |