@@ -74,7 +74,24 @@ Create a timer that wakes up tasks waiting for it (by calling [`wait`](@ref) on
7474Waiting tasks are woken after an initial delay of at least `delay` seconds, and then repeating after
7575at least `interval` seconds again elapse. If `interval` is equal to `0`, the timer is only triggered
7676once. When the timer is closed (by [`close`](@ref)) waiting tasks are woken with an error. Use
77- [`isopen`](@ref) to check whether a timer is still active.
77+ [`isopen`](@ref) to check whether a timer is still active. Use `t.timeout` and `t.interval` to read
78+ the setup conditions of a `Timer` `t`.
79+
80+ ```julia-repl
81+ julia> t = Timer(1.0; interval=0.5)
82+ Timer (open, timeout: 1.0 s, interval: 0.5 s) @0x000000010f4e6e90
83+
84+ julia> isopen(t)
85+ true
86+
87+ julia> t.timeout
88+ 1.0
89+
90+ julia> close(t)
91+
92+ julia> isopen(t)
93+ false
94+ ```
7895
7996!!! note
8097 `interval` is subject to accumulating time skew. If you need precise events at a particular
@@ -84,12 +101,17 @@ once. When the timer is closed (by [`close`](@ref)) waiting tasks are woken with
84101 A `Timer` requires yield points to update its state. For instance, `isopen(t::Timer)` cannot be
85102 used to timeout a non-yielding while loop.
86103
104+ !!! compat "Julia 1.12
105+ The `timeout` and `interval` readable properties were added in Julia 1.12.
106+
87107"""
88108mutable struct Timer
89109 @atomic handle:: Ptr{Cvoid}
90110 cond:: ThreadSynchronizer
91111 @atomic isopen:: Bool
92112 @atomic set:: Bool
113+ timeout_ms:: UInt64
114+ interval_ms:: UInt64
93115
94116 function Timer (timeout:: Real ; interval:: Real = 0.0 )
95117 timeout ≥ 0 || throw (ArgumentError (" timer cannot have negative timeout of $timeout seconds" ))
@@ -99,7 +121,7 @@ mutable struct Timer
99121 intervalms = ceil (UInt64, interval * 1000 )
100122 loop = eventloop ()
101123
102- this = new (Libc. malloc (_sizeof_uv_timer), ThreadSynchronizer (), true , false )
124+ this = new (Libc. malloc (_sizeof_uv_timer), ThreadSynchronizer (), true , false , timeoutms, intervalms )
103125 associate_julia_struct (this. handle, this)
104126 iolock_begin ()
105127 err = ccall (:uv_timer_init , Cint, (Ptr{Cvoid}, Ptr{Cvoid}), loop, this)
@@ -114,6 +136,24 @@ mutable struct Timer
114136 return this
115137 end
116138end
139+ function getproperty (t:: Timer , f:: Symbol )
140+ if f == :timeout
141+ t. timeout_ms == 0 && return 0.0
142+ return (t. timeout_ms - 1 ) / 1000 # remove the +1ms compensation from the constructor
143+ elseif f == :interval
144+ return t. interval_ms / 1000
145+ else
146+ return getfield (t, f)
147+ end
148+ end
149+ propertynames (:: Timer ) = (:handle , :cond , :isopen , :set , :timeout , :timeout_ms , :interval , :interval_ms )
150+
151+ function show (io:: IO , t:: Timer )
152+ state = isopen (t) ? " open" : " closed"
153+ interval = t. interval
154+ interval_str = interval > 0 ? " , interval: $(t. interval) s" : " "
155+ print (io, " Timer ($state , timeout: $(t. timeout) s$interval_str ) @0x$(string (convert (UInt, pointer_from_objref (t)), base = 16 , pad = Sys. WORD_SIZE>> 2 )) " )
156+ end
117157
118158unsafe_convert (:: Type{Ptr{Cvoid}} , t:: Timer ) = t. handle
119159unsafe_convert (:: Type{Ptr{Cvoid}} , async:: AsyncCondition ) = async. handle
0 commit comments