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).

1  
sclass Fiber {
2  
  Thread runningInThread;
3  
  long grabCount;
4  
  
5  
  final Map<IVar, O> fiberLocalValues = syncWeakMap();
6  
7  
  synchronized void grab {
8  
    Thread t = currentThread();
9  
    if (runningInThread != null && runningInThread != t)
10  
      failWithVars("Trying to assign virtual thread to 2 threads", +thread1 := runningInThread, thread2 := t);
11  
    runningInThread = t;
12  
    grabCount++;
13  
  }
14  
  
15  
  synchronized void release {
16  
    Thread t = currentThread();
17  
    if (runningInThread != t)
18  
      failWithVars("Broken release", +thread1 := runningInThread, thread2 := t);
19  
    if (--grabCount == 0)
20  
      runningInThread = null;
21  
  }
22  
  
23  
  O getFiberLocalValue(IVar fiberLocal) {
24  
    ret fiberLocalValues.get(fiberLocal);
25  
  }
26  
  
27  
  void setFiberLocalValue(IVar fiberLocal, O value) {
28  
    fiberLocalValues.put(fiberLocal, value);
29  
  }
30  
  
31  
  void foretFiberLocalValue(IVar fiberLocal) {
32  
    fiberLocalValues.remove(fiberLocal);
33  
  }
34  
}

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: 100 / 212
Version history: 8 change(s)
Referenced in: [show references]