future.monkey2 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. Namespace std.fiber
  2. #if __TARGET__<>"emscripten"
  3. #rem monkeydoc Futures provides support for simple fiber synchronization.
  4. A future allows you to synchronize two fibers by providing a way for one fiber to signal to another that an operation has completed.
  5. The general usage pattern of future is:
  6. * Fiber A creates a future and passes it to fiber B.
  7. * Fiber A then calls [[Get]] on the future. This will suspend Fiber A.
  8. * Fiber B performs some operation, then calls [[Set]] on the future. This will resume Fiber A.
  9. A future can be resued - each time [[Get]] is called, the fiber will suspend until another fiber calls [[Set]].
  10. #end
  11. Class Future<T>
  12. #rem monkeydoc Creates a new future.
  13. #end
  14. Method New()
  15. _fiber=Fiber.Current()
  16. End
  17. #rem monkeydoc Sets the future's value.
  18. #end
  19. Method Set( value:T )
  20. _value=value
  21. _fiber.Resume()
  22. End
  23. #rem monkeydoc Gets the future's value.
  24. #end
  25. Method Get:T()
  26. Fiber.Suspend()
  27. Return _value
  28. End
  29. Private
  30. Field _fiber:Fiber
  31. Field _value:T
  32. End
  33. #endif