tdestroy_in_loopcond.nim 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. discard """
  2. output: '''400 true'''
  3. cmd: "nim c --gc:orc $file"
  4. """
  5. type HeapQueue*[T] = object
  6. data: seq[T]
  7. proc len*[T](heap: HeapQueue[T]): int {.inline.} =
  8. heap.data.len
  9. proc `[]`*[T](heap: HeapQueue[T], i: Natural): T {.inline.} =
  10. heap.data[i]
  11. proc push*[T](heap: var HeapQueue[T], item: T) =
  12. heap.data.add(item)
  13. proc pop*[T](heap: var HeapQueue[T]): T =
  14. result = heap.data.pop
  15. proc clear*[T](heap: var HeapQueue[T]) = heap.data.setLen 0
  16. type
  17. Future = ref object of RootObj
  18. s: string
  19. callme: proc()
  20. var called = 0
  21. proc consume(f: Future) =
  22. inc called
  23. proc newFuture(s: string): Future =
  24. var r: Future
  25. r = Future(s: s, callme: proc() =
  26. consume r)
  27. result = r
  28. var q: HeapQueue[tuple[finishAt: int64, fut: Future]]
  29. proc sleep(f: int64): Future =
  30. q.push (finishAt: f, fut: newFuture("async-sleep"))
  31. proc processTimers =
  32. # Pop the timers in the order in which they will expire (smaller `finishAt`).
  33. var count = q.len
  34. let t = high(int64)
  35. while count > 0 and t >= q[0].finishAt:
  36. q.pop().fut.callme()
  37. dec count
  38. var futures: seq[Future]
  39. proc main =
  40. for i in 1..200:
  41. futures.add sleep(56020904056300)
  42. futures.add sleep(56020804337500)
  43. #futures.add sleep(2.0)
  44. #futures.add sleep(4.0)
  45. processTimers()
  46. #q.pop()[1].callme()
  47. #q.pop()[1].callme()
  48. futures.setLen 0
  49. q.clear()
  50. main()
  51. GC_fullCollect()
  52. echo called, " ", getOccupiedMem() < 160