1// Copyright 2019 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5// +build debuglog
6
7package runtime
8
9const dlogEnabled = true
10
11// dlogPerM is the per-M debug log data. This is embedded in the m
12// struct.
13type dlogPerM struct {
14	dlogCache *dlogger
15}
16
17// getCachedDlogger returns a cached dlogger if it can do so
18// efficiently, or nil otherwise. The returned dlogger will be owned.
19func getCachedDlogger() *dlogger {
20	mp := acquirem()
21	// We don't return a cached dlogger if we're running on the
22	// signal stack in case the signal arrived while in
23	// get/putCachedDlogger. (Too bad we don't have non-atomic
24	// exchange!)
25	var l *dlogger
26	if getg() != mp.gsignal {
27		l = mp.dlogCache
28		mp.dlogCache = nil
29	}
30	releasem(mp)
31	return l
32}
33
34// putCachedDlogger attempts to return l to the local cache. It
35// returns false if this fails.
36func putCachedDlogger(l *dlogger) bool {
37	mp := acquirem()
38	if getg() != mp.gsignal && mp.dlogCache == nil {
39		mp.dlogCache = l
40		releasem(mp)
41		return true
42	}
43	releasem(mp)
44	return false
45}
46