Skip to content

Commit 8d04821

Browse files
committed
runtime: prepare the leaking GC for concurrent operations
This uses the task.PMutex parallel-only-mutex type to make the leaking GC parallelism safe. The task.PMutex type is currently a no-op but will become a real mutex once we add true parallelism.
1 parent f751873 commit 8d04821

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/runtime/gc_leaking.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package runtime
77
// may be the only memory allocator possible.
88

99
import (
10+
"internal/task"
1011
"unsafe"
1112
)
1213

@@ -19,6 +20,9 @@ var gcTotalAlloc uint64
1920
// Total number of calls to alloc()
2021
var gcMallocs uint64
2122

23+
// Heap lock for parallel goroutines. No-op when single threaded.
24+
var gcLock task.PMutex
25+
2226
// Total number of objected freed; for leaking collector this stays 0
2327
const gcFrees = 0
2428

@@ -30,6 +34,7 @@ func alloc(size uintptr, layout unsafe.Pointer) unsafe.Pointer {
3034
// TODO: this can be optimized by not casting between pointers and ints so
3135
// much. And by using platform-native data types (e.g. *uint8 for 8-bit
3236
// systems).
37+
gcLock.Lock()
3338
size = align(size)
3439
addr := heapptr
3540
gcTotalAlloc += uint64(size)
@@ -43,6 +48,8 @@ func alloc(size uintptr, layout unsafe.Pointer) unsafe.Pointer {
4348
// Failed to make the heap bigger, so we must really be out of memory.
4449
runtimePanic("out of memory")
4550
}
51+
gcLock.Unlock()
52+
4653
pointer := unsafe.Pointer(addr)
4754
zero_new_alloc(pointer, size)
4855
return pointer
@@ -69,6 +76,8 @@ func free(ptr unsafe.Pointer) {
6976
// The returned memory statistics are up to date as of the
7077
// call to ReadMemStats. This would not do GC implicitly for you.
7178
func ReadMemStats(m *MemStats) {
79+
gcLock.Lock()
80+
7281
m.HeapIdle = 0
7382
m.HeapInuse = gcTotalAlloc
7483
m.HeapReleased = 0 // always 0, we don't currently release memory back to the OS.
@@ -82,6 +91,8 @@ func ReadMemStats(m *MemStats) {
8291
// no free -- current in use heap is the total allocated
8392
m.HeapAlloc = gcTotalAlloc
8493
m.Alloc = m.HeapAlloc
94+
95+
gcLock.Unlock()
8596
}
8697

8798
func GC() {

0 commit comments

Comments
 (0)