@@ -74,7 +74,24 @@ Create a timer that wakes up tasks waiting for it (by calling [`wait`](@ref) on
74
74
Waiting tasks are woken after an initial delay of at least `delay` seconds, and then repeating after
75
75
at least `interval` seconds again elapse. If `interval` is equal to `0`, the timer is only triggered
76
76
once. 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
+ ```
78
95
79
96
!!! note
80
97
`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
84
101
A `Timer` requires yield points to update its state. For instance, `isopen(t::Timer)` cannot be
85
102
used to timeout a non-yielding while loop.
86
103
104
+ !!! compat "Julia 1.12
105
+ The `timeout` and `interval` readable properties were added in Julia 1.12.
106
+
87
107
"""
88
108
mutable struct Timer
89
109
@atomic handle:: Ptr{Cvoid}
90
110
cond:: ThreadSynchronizer
91
111
@atomic isopen:: Bool
92
112
@atomic set:: Bool
113
+ timeout_ms:: UInt64
114
+ interval_ms:: UInt64
93
115
94
116
function Timer (timeout:: Real ; interval:: Real = 0.0 )
95
117
timeout ≥ 0 || throw (ArgumentError (" timer cannot have negative timeout of $timeout seconds" ))
@@ -99,7 +121,7 @@ mutable struct Timer
99
121
intervalms = ceil (UInt64, interval * 1000 )
100
122
loop = eventloop ()
101
123
102
- this = new (Libc. malloc (_sizeof_uv_timer), ThreadSynchronizer (), true , false )
124
+ this = new (Libc. malloc (_sizeof_uv_timer), ThreadSynchronizer (), true , false , timeoutms, intervalms )
103
125
associate_julia_struct (this. handle, this)
104
126
iolock_begin ()
105
127
err = ccall (:uv_timer_init , Cint, (Ptr{Cvoid}, Ptr{Cvoid}), loop, this)
@@ -114,6 +136,24 @@ mutable struct Timer
114
136
return this
115
137
end
116
138
end
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
117
157
118
158
unsafe_convert (:: Type{Ptr{Cvoid}} , t:: Timer ) = t. handle
119
159
unsafe_convert (:: Type{Ptr{Cvoid}} , async:: AsyncCondition ) = async. handle
0 commit comments