Wrap use of mutexes with call-with-blocked-asyncs

As this will hopefully avoid problems with mutexes when using fibers.
This commit is contained in:
Christopher Baines 2023-11-12 13:37:24 +00:00
parent 35dc26c0ea
commit fbd64347fb

View file

@ -294,12 +294,14 @@ values are the values."
(canonicalise-label-values label-values)) (canonicalise-label-values label-values))
(hash (hash
(metric-values metric))) (metric-values metric)))
(call-with-blocked-asyncs
(lambda ()
(with-mutex (metric-mutex metric) (with-mutex (metric-mutex metric)
(hash-set! hash (hash-set! hash
canonical-labels canonical-labels
(+ by (+ by
(or (hash-ref hash canonical-labels) (or (hash-ref hash canonical-labels)
0)))))) 0))))))))
(define* (metric-decrement metric (define* (metric-decrement metric
#:key #:key
@ -320,12 +322,14 @@ values are the values."
(canonicalise-label-values label-values)) (canonicalise-label-values label-values))
(hash (hash
(metric-values metric))) (metric-values metric)))
(call-with-blocked-asyncs
(lambda ()
(with-mutex (metric-mutex metric) (with-mutex (metric-mutex metric)
(hash-set! hash (hash-set! hash
canonical-labels canonical-labels
(+ (* -1 by) (+ (* -1 by)
(or (hash-ref hash canonical-labels) (or (hash-ref hash canonical-labels)
0)))))) 0))))))))
(define* (metric-set metric value (define* (metric-set metric value
#:key (label-values '())) #:key (label-values '()))
@ -340,10 +344,12 @@ values are the values."
'(gauge)) '(gauge))
(error "can only set gauge metrics")) (error "can only set gauge metrics"))
(call-with-blocked-asyncs
(lambda ()
(with-mutex (metric-mutex metric) (with-mutex (metric-mutex metric)
(hash-set! (metric-values metric) (hash-set! (metric-values metric)
(canonicalise-label-values label-values) (canonicalise-label-values label-values)
value))) value)))))
(define* (metric-observe metric value (define* (metric-observe metric value
#:key (label-values '())) #:key (label-values '()))
@ -363,6 +369,8 @@ values are the values."
(hash (hash
(metric-values metric))) (metric-values metric)))
(call-with-blocked-asyncs
(lambda ()
(with-mutex (metric-mutex metric) (with-mutex (metric-mutex metric)
(let* ((buckets (let* ((buckets
(histogram-metric-type-buckets (metric-type metric))) (histogram-metric-type-buckets (metric-type metric)))
@ -395,7 +403,7 @@ values are the values."
(vector-ref bucket-values-vector (vector-ref bucket-values-vector
index))))) index)))))
(iota (length buckets)) (iota (length buckets))
buckets)))))) buckets))))))))
(define* (call-with-duration-metric registry metric-name thunk (define* (call-with-duration-metric registry metric-name thunk
#:key #:key
@ -413,6 +421,8 @@ The metric with the name @var{metric-name} is fetched from the
" "
(let* ((metric (let* ((metric
(or (metrics-registry-fetch-metric registry metric-name) (or (metrics-registry-fetch-metric registry metric-name)
(call-with-blocked-asyncs
(lambda ()
(monitor (monitor
;; Check once more in case another thread has created ;; Check once more in case another thread has created
;; the metric while this thread was waiting for the ;; the metric while this thread was waiting for the
@ -424,7 +434,7 @@ The metric with the name @var{metric-name} is fetched from the
#:buckets buckets #:buckets buckets
#:docstring docstring #:docstring docstring
#:labels labels #:labels labels
#:label-preset-values label-preset-values))))) #:label-preset-values label-preset-values)))))))
(start-time (get-internal-real-time))) (start-time (get-internal-real-time)))
(call-with-values (call-with-values
thunk thunk