Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
class FiberPool { | ||
def initialize { | ||
@pool = [] | ||
@current = nil | ||
} | ||
|
||
def size { | ||
@pool size | ||
} | ||
|
||
def add: fiber { | ||
@pool << fiber | ||
} | ||
|
||
def remove: fiber { | ||
@pool remove: fiber | ||
} | ||
|
||
def schedule { | ||
Thread new: { | ||
loop: { | ||
while: {@pool size > 0} do: { | ||
@pool each: |f| { | ||
f resume | ||
} | ||
@pool select!: 'alive? | ||
} | ||
Thread sleep: 1000 | ||
} | ||
} | ||
} | ||
} | ||
|
||
class Scheduler { | ||
@@pool = FiberPool new | ||
def Scheduler add: fiber { | ||
@@pool add: fiber | ||
} | ||
|
||
def Scheduler remove: fiber { | ||
@@pool remove: fiber | ||
} | ||
|
||
def Scheduler schedule { | ||
@@pool schedule | ||
} | ||
} |