12
12
* GNU Lesser General Public License for more details.
13
13
*)
14
14
15
+ module Delay = Xapi_stdext_threads.Threadext. Delay
16
+
15
17
let with_lock = Xapi_stdext_threads.Threadext.Mutex. execute
16
18
17
19
(* This exception is setup to be raised on sigint by Process.initialise,
@@ -57,10 +59,20 @@ type state =
57
59
| Cancelled
58
60
| Stopped of [`New | `Cancelled | `Failed of exn ]
59
61
60
- type t = {mutable state : state ; lock : Mutex .t ; condition : Condition .t }
62
+ type t = {
63
+ mutable state : state
64
+ ; lock : Mutex .t
65
+ ; condition : Condition .t
66
+ ; delay : Delay .t
67
+ }
61
68
62
69
let make () =
63
- {state= Stopped `New ; lock= Mutex. create () ; condition= Condition. create () }
70
+ {
71
+ state= Stopped `New
72
+ ; lock= Mutex. create ()
73
+ ; condition= Condition. create ()
74
+ ; delay= Delay. make ()
75
+ }
64
76
65
77
let choose_protocol = function
66
78
| Rrd_interface. V1 ->
@@ -69,13 +81,20 @@ let choose_protocol = function
69
81
Rrd_protocol_v2. protocol
70
82
71
83
let wait_until_next_reading (module D : Debug.DEBUG ) ~neg_shift ~uid ~protocol
72
- ~overdue_count =
84
+ ~overdue_count ~ reporter =
73
85
let next_reading = RRDD.Plugin.Local. register uid Rrd. Five_Seconds protocol in
74
86
let wait_time = next_reading -. neg_shift in
75
87
let wait_time = if wait_time < 0.1 then wait_time +. 5. else wait_time in
76
88
(* overdue count - 0 if there is no overdue; +1 if there is overdue *)
77
89
if wait_time > 0. then (
78
- Thread. delay wait_time ; 0
90
+ ( match reporter with
91
+ | Some reporter ->
92
+ let (_ : bool ) = Delay. wait reporter.delay wait_time in
93
+ ()
94
+ | None ->
95
+ Thread. delay wait_time
96
+ ) ;
97
+ 0
79
98
) else (
80
99
if overdue_count > 1 then (
81
100
(* if register returns negative more than once in a succession,
@@ -84,7 +103,12 @@ let wait_until_next_reading (module D : Debug.DEBUG) ~neg_shift ~uid ~protocol
84
103
D. debug
85
104
" rrdd says next reading is overdue, seems like rrdd is busy;\n \
86
105
\t\t\t\t Backing off for %.1f seconds" backoff_time ;
87
- Thread. delay backoff_time
106
+ match reporter with
107
+ | Some reporter ->
108
+ let (_ : bool ) = Delay. wait reporter.delay backoff_time in
109
+ ()
110
+ | None ->
111
+ Thread. delay backoff_time
88
112
) else
89
113
D. debug " rrdd says next reading is overdue by %.1f seconds; not sleeping"
90
114
(-. wait_time) ;
@@ -147,8 +171,10 @@ let cancel ~reporter =
147
171
match reporter.state with
148
172
| Running ->
149
173
reporter.state < - Cancelled ;
174
+ Delay. signal reporter.delay ;
150
175
Condition. wait reporter.condition reporter.lock
151
176
| Cancelled ->
177
+ Delay. signal reporter.delay ;
152
178
Condition. wait reporter.condition reporter.lock
153
179
| Stopped _ ->
154
180
()
0 commit comments