Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

61
LINES

< > BotCompany Repo | #1033321 // BoolVar (never null!)

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (9246L/51K).

persistable sclass BoolVar {
  bool a; // you can access this directly if you use one thread
  
  *() {}
  *(bool *a) {}
  
  public synchronized void set(bool v default true) {
    if (v != a) {
      a = v;
      notifyAll();
    }
  }
  
  public synchronized bool get() { ret a; }
  //public synchronized bool has() { true; }
  public void clear aka unset() { set(false); }
  
  bool waitUntilTrue () { ret waitForValue(true); }
  bool waitUntilFalse() { ret waitForValue(false); }
  
  bool waitUntilTrue (double timeoutSeconds) { ret waitForValue(timeoutSeconds, true); }
  bool waitUntilFalse(double timeoutSeconds) { ret waitForValue(timeoutSeconds, false); }
  
  synchronized bool waitForValue(bool wantedValue) ctex {
    while (a != wantedValue)
      wait();
    ret a;
  }
  
  synchronized bool waitForValue(double timeoutSeconds, bool wantedValue) ctex {
    if (timeoutSeconds == infinity())
      ret waitForValue(wantedValue);
      
    long destTime = sysNowPlusSeconds_noPing(timeoutSeconds), remainingTime;
    while (a != wantedValue
      && (remainingTime = destTime-sysNow_noPing()) > 0)
      wait(remainingTime);

    ret a;
  }
  
  public synchronized bool getAndSet(bool b) {
    var value = a;
    set(b);
    ret value;
  }
  
  public synchronized bool waitUntilTrueAndClear() {
    if (!waitUntilTrue()) false;
    set(false);
    true;
  }
  
  public synchronized bool waitUntilTrueAndClear(double timeoutSeconds) {
    if (!waitUntilTrue(timeoutSeconds)) false;
    set(false);
    true;
  }
  
  O mutex() { this; }
}

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: 173 / 368
Version history: 24 change(s)
Referenced in: [show references]