| 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 |
//go:build debuglog |
| 6 |
|
| 7 |
package runtime |
| 8 |
|
| 9 |
const dlogEnabled = true |
| 10 |
|
| 11 |
// dlogger is the underlying implementation of the dlogger interface, selected |
| 12 |
// at build time. |
| 13 |
// |
| 14 |
// We use a type alias instead of struct embedding so that the dlogger type is |
| 15 |
// identical to the type returned by method chaining on the methods of this type. |
| 16 |
type dlogger = *dloggerImpl |
| 17 |
|
| 18 |
func dlog1() *dloggerImpl { |
| 19 |
return dlogImpl() |
| 20 |
} |
| 21 |
|
| 22 |
// dlogPerM is the per-M debug log data. This is embedded in the m |
| 23 |
// struct. |
| 24 |
type dlogPerM struct { |
| 25 |
dlogCache *dloggerImpl |
| 26 |
} |
| 27 |
|
| 28 |
// getCachedDlogger returns a cached dlogger if it can do so |
| 29 |
// efficiently, or nil otherwise. The returned dlogger will be owned. |
| 30 |
func getCachedDlogger() *dloggerImpl { |
| 31 |
mp := acquirem() |
| 32 |
// We don't return a cached dlogger if we're running on the |
| 33 |
// signal stack in case the signal arrived while in |
| 34 |
// get/putCachedDlogger. (Too bad we don't have non-atomic |
| 35 |
// exchange!) |
| 36 |
var l *dloggerImpl |
| 37 |
if getg() != mp.gsignal { |
| 38 |
l = mp.dlogCache |
| 39 |
mp.dlogCache = nil |
| 40 |
} |
| 41 |
releasem(mp) |
| 42 |
return l |
| 43 |
} |
| 44 |
|
| 45 |
// putCachedDlogger attempts to return l to the local cache. It |
| 46 |
// returns false if this fails. |
| 47 |
func putCachedDlogger(l *dloggerImpl) bool { |
| 48 |
mp := acquirem() |
| 49 |
if getg() != mp.gsignal && mp.dlogCache == nil { |
| 50 |
mp.dlogCache = l |
| 51 |
releasem(mp) |
| 52 |
return true |
| 53 |
} |
| 54 |
releasem(mp) |
| 55 |
return false |
| 56 |
} |