Skip to content

Commit

Permalink
cleanup: locking improvements
Browse files Browse the repository at this point in the history
[cherry-picked from
sharplispers@dcdc5ad]

- don't use bordeaux-threads package (add explicit package prefix)
- serialized appender lock is now recursive - fixes 7max#8
- add names to locks for easier identification (in case of deadlock detection)
  • Loading branch information
dkochmanski authored and Madhu committed Nov 13, 2024
1 parent b550c1a commit b22002b
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 10 deletions.
8 changes: 4 additions & 4 deletions src/appender.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ APPENDER-DO-APPEND was called, and writes its output to null sink"))
appender if it encounters an error matching ERROR-TYPE"))

(defclass serialized-appender (appender)
((%lock :initform (make-lock)))
((%lock :initform (bt:make-recursive-lock "Log4CL serialized appender lock")))
(:documentation "Appender that serializes itself using a lock"))

(defclass stream-appender (serialized-appender)
Expand Down Expand Up @@ -193,7 +193,7 @@ unless IMMEDAITE-FLUSH property is set."
;; flush then.
(when (> (+ since-last-flush *hierarchy-watcher-heartbeat*)
flush-interval)
(with-lock-held (%lock)
(bt:with-recursive-lock-held (%lock)
(setf %last-flush-time time
%output-since-flush nil)
(finish-output (appender-stream appender)))))))))
Expand Down Expand Up @@ -224,7 +224,7 @@ been any output. TIME will be used to mark the time of the flush"
appender
(when (and (not immediate-flush)
%output-since-flush)
(with-lock-held (%lock)
(bt:with-recursive-lock-held (%lock)
(setf %last-flush-time time
%output-since-flush nil)
(finish-output (appender-stream appender))))))
Expand All @@ -248,7 +248,7 @@ time of the flush with TIME"
(defmethod appender-do-append :around
((this serialized-appender) logger level log-func)
(declare (ignore logger level log-func))
(with-lock-held ((slot-value this '%lock))
(bt:with-recursive-lock-held ((slot-value this '%lock))
(call-next-method)))

(defmethod appender-do-append ((this stream-appender) logger level log-func)
Expand Down
4 changes: 2 additions & 2 deletions src/hierarchy-base.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ appenders")
indexed by this variable. Can be assigned directly or ")

(defvar *hierarchy-lock*
(make-recursive-lock "hierarchy-lock")
(bt:make-recursive-lock "Log4CL global configuration lock")
"Global lock for changing logging configuration")

(defvar *hierarchy-watcher-heartbeat* 1
Expand All @@ -49,7 +49,7 @@ WATCHER-HOOK of each hierarchy")
;; Used for auto-reloading the modified files in
;; PROPERTY-CONFIGURATOR but can be used for other stuff.
(watch-tokens :initform nil :accessor watch-tokens)
(%lock :initform (make-lock))))
(%lock :initform (bt:make-recursive-lock "Log4CL hierarchy lock"))))

(defvar *hierarchies*
(make-array 1 :adjustable t :fill-pointer t
Expand Down
4 changes: 2 additions & 2 deletions src/hierarchy.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@
(aref *hierarchies* *hierarchy*))

(defmacro with-hierarchies-lock (&body body)
`(with-recursive-lock-held (*hierarchy-lock*)
`(bt:with-recursive-lock-held (*hierarchy-lock*)
,@body))

(defmacro with-hierarchy-lock ((&optional (hierarchy (current-hierarchy)))
&body body)
`(with-recursive-lock-held ((slot-value ,hierarchy '%lock))
`(bt:with-recursive-lock-held ((slot-value ,hierarchy '%lock))
,@body))


Expand Down
2 changes: 1 addition & 1 deletion src/impl-package.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@
(defpackage-form
`(defpackage #:log4cl-impl
(:nicknames #:log4cl)
(:use #:cl #:bordeaux-threads)
(:use #:cl)
(:export ,@new-exports))))
(when (and p2 removed-exports)
(unexport removed-exports p2))
Expand Down
2 changes: 1 addition & 1 deletion src/pattern-layout.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -1127,7 +1127,7 @@ strftime like PATTERN."))
(define-pattern-formatter (#\t)
"Output %t (thread name) pattern"
(declare (ignore logger log-level log-func))
(format-string (or (thread-name (current-thread)) "") stream fmt-info))
(format-string (or (bt:thread-name (bt:current-thread)) "") stream fmt-info))

(define-pattern-formatter (#\x)
(declare (ignore logger log-level log-func))
Expand Down

0 comments on commit b22002b

Please sign in to comment.