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

34
LINES

< > BotCompany Repo | #1032578 // Fiber - "light weight" thread structure

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

Libraryless. Click here for Pure Java version (4186L/24K).

sclass Fiber {
  Thread runningInThread;
  long grabCount;
  
  final Map<IVar, O> fiberLocalValues = syncWeakMap();

  synchronized void grab {
    Thread t = currentThread();
    if (runningInThread != null && runningInThread != t)
      failWithVars("Trying to assign virtual thread to 2 threads", +thread1 := runningInThread, thread2 := t);
    runningInThread = t;
    grabCount++;
  }
  
  synchronized void release {
    Thread t = currentThread();
    if (runningInThread != t)
      failWithVars("Broken release", +thread1 := runningInThread, thread2 := t);
    if (--grabCount == 0)
      runningInThread = null;
  }
  
  O getFiberLocalValue(IVar fiberLocal) {
    ret fiberLocalValues.get(fiberLocal);
  }
  
  void setFiberLocalValue(IVar fiberLocal, O value) {
    fiberLocalValues.put(fiberLocal, value);
  }
  
  void foretFiberLocalValue(IVar fiberLocal) {
    fiberLocalValues.remove(fiberLocal);
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1032578
Snippet name: Fiber - "light weight" thread structure
Eternal ID of this version: #1032578/9
Text MD5: 7d4d4d0c038728e5dbb66654f7664a7b
Transpilation MD5: 5b637d4895289099fdb4eb26e477ce8a
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-09-23 05:12:13
Source code size: 953 bytes / 34 lines
Pitched / IR pitched: No / No
Views / Downloads: 99 / 210
Version history: 8 change(s)
Referenced in: [show references]