Sha256: abc669dbdf68ec1f3e72a18522d72fa428deba09e0a9a6e96e5603e7945b3c03

Contents?: true

Size: 1.16 KB

Versions: 5

Compression:

Stored size: 1.16 KB

Contents

class FiberPool {
  def initialize {
    @pool = []
    @scheduling = false
    @mutex = Mutex new()
  }

  def size {
    @mutex synchronize() {
      @pool size
    }
  }

  def add: fiber {
    @mutex synchronize() {
      @pool << fiber
    }
  }

  def remove: fiber {
    @mutex synchronize() {
      @pool remove: fiber
    }
  }

  def scheduling? {
    @scheduling
  }

  def pool {
    @mutex synchronize() {
      pool = @pool
    }
  }

  def cleanup_pool {
    @mutex synchronize() {
      @pool select!: 'alive?
    }
  }

  def schedule {
    @scheduling = true
    Thread new: {
      loop: {
        while: {pool size > 0} do: {
          pool each: |f| {
            unless: (f asleep?) do: {
              sleep_time = f resume
              { f sleep: sleep_time } if: sleep_time
            }
          }
          cleanup_pool
        }
        Thread sleep: 1
      }
    }
  }
}

class Scheduler {
  @@pool = FiberPool new
  def Scheduler add: fiber {
    @@pool add: fiber
    unless: (@@pool scheduling?) do: {
      schedule
    }
    nil
  }

  def Scheduler remove: fiber {
    @@pool remove: fiber
  }

  def Scheduler schedule {
    @@pool schedule
  }
}

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
fancy-0.5.0 lib/fiber_pool.fy
fancy-0.4.0 lib/fiber_pool.fy
fancy-0.3.3 lib/fiber_pool.fy
fancy-0.3.2 lib/fiber_pool.fy
fancy-0.3.1 lib/fiber_pool.fy