mutex in slog/handler.go?

152 views
Skip to first unread message

Jochen Voss

unread,
May 24, 2024, 9:25:06 AM5/24/24
to golang-nuts
Hello,

In the Go standard library, in the file log/slog/handler.go, I found the following code:

func (h *commonHandler) clone() *commonHandler {
// We can't use assignment because we can't copy the mutex.
return &commonHandler{
json:              h.json,
opts:              h.opts,
preformattedAttrs: slices.Clip(h.preformattedAttrs),
groupPrefix:       h.groupPrefix,
groups:            slices.Clip(h.groups),
nOpenGroups:       h.nOpenGroups,
w:                 h.w,
mu:                h.mu, // mutex shared among all clones of this handler
}
}

The first comment states that "we can't copy the mutex", but then the last line seems to copy the mutex anyway.  What is going on here?

Maybe this just an oversight from a time when every hander had its own mutex?  Or is there something subtle going on here?

Many thanks,
Jochen

Eli Lindsey

unread,
May 24, 2024, 10:19:43 AM5/24/24
to Jochen Voss, golang-nuts
Git blame may be helpful, and specifically commit 847d40d6998. It looks like code drifted from the comment.

-eli 

On May 24, 2024, at 9:25 AM, Jochen Voss <joche...@gmail.com> wrote:

Hello,
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/153e9898-f17e-44cc-ab2a-8570f916c0c3n%40googlegroups.com.

Harris, Andrew

unread,
May 24, 2024, 11:40:05 AM5/24/24
to Eli Lindsey, Jochen Voss, golang-nuts
The mutex field of the common handler struct became a pointer-to-mutex at some point IIRC - it is currently, so copying should be fine.

From: golan...@googlegroups.com <golan...@googlegroups.com> on behalf of Eli Lindsey <e...@siliconsprawl.com>
Sent: Friday, May 24, 2024 7:18:51 AM
To: Jochen Voss <joche...@gmail.com>
Cc: golang-nuts <golan...@googlegroups.com>
Subject: Re: [go-nuts] mutex in slog/handler.go?
 
Reply all
Reply to author
Forward
0 new messages