Merge branch 'chronosFutureDuration' into das-dht2

This commit is contained in:
Csaba Kiraly 2023-08-14 15:02:47 +02:00
commit ca3ce6e8cc
No known key found for this signature in database
GPG Key ID: 0FE274EE8C95166E
1 changed files with 16 additions and 0 deletions

View File

@ -27,6 +27,9 @@ const
when defined(chronosStackTrace): when defined(chronosStackTrace):
type StackTrace = string type StackTrace = string
when defined(chronosFutureDuration):
import ./timer
type type
FutureState* {.pure.} = enum FutureState* {.pure.} = enum
Pending, Finished, Cancelled, Failed Pending, Finished, Cancelled, Failed
@ -40,6 +43,9 @@ type
error*: ref CatchableError ## Stored exception error*: ref CatchableError ## Stored exception
mustCancel*: bool mustCancel*: bool
id*: uint id*: uint
when defined(chronosFutureDuration):
startTime: Moment
duration: Duration
when defined(chronosStackTrace): when defined(chronosStackTrace):
errorStackTrace*: StackTrace errorStackTrace*: StackTrace
@ -106,6 +112,9 @@ template setupFutureBase(loc: ptr SrcLoc) =
futureList.head = result futureList.head = result
futureList.count.inc() futureList.count.inc()
when defined(chronosFutureDuration):
result.startTime = Moment.now()
proc newFutureImpl[T](loc: ptr SrcLoc): Future[T] = proc newFutureImpl[T](loc: ptr SrcLoc): Future[T] =
setupFutureBase(loc) setupFutureBase(loc)
@ -142,6 +151,8 @@ proc finished*(future: FutureBase): bool {.inline.} =
## Determines whether ``future`` has completed, i.e. ``future`` state changed ## Determines whether ``future`` has completed, i.e. ``future`` state changed
## from state ``Pending`` to one of the states (``Finished``, ``Cancelled``, ## from state ``Pending`` to one of the states (``Finished``, ``Cancelled``,
## ``Failed``). ## ``Failed``).
when defined(chronosFutureDuration):
future.duration = Moment.now() - future.startTime
result = (future.state != FutureState.Pending) result = (future.state != FutureState.Pending)
proc cancelled*(future: FutureBase): bool {.inline.} = proc cancelled*(future: FutureBase): bool {.inline.} =
@ -1036,3 +1047,8 @@ proc race*(futs: varargs[FutureBase]): Future[FutureBase] =
retFuture.cancelCallback = cancellation retFuture.cancelCallback = cancellation
return retFuture return retFuture
when defined(chronosFutureDuration):
func duration*(future: FutureBase): Duration =
## Duration between future start and finish
future.duration