Skip to content

Instantly share code, notes, and snippets.

@cwshu
Last active July 13, 2017 19:06
Show Gist options
  • Save cwshu/f02f599a202721b28d442233984bd596 to your computer and use it in GitHub Desktop.
Save cwshu/f02f599a202721b28d442233984bd596 to your computer and use it in GitHub Desktop.

golang runtime implement libc pthread_create, mutex, malloc. it doesn't use libc API, use OS syscall instead.

  1. malloc
  • golang memory allocator: src/runtime/malloc.go
  • golang memory allocator os specific: src/runtime/mem_bsd.go
    • sysAlloc() use mmap() syscall instead of libc malloc()
  1. pthread_create
  1. mutex lock

low level implementation

lock() => futexsleep() => runtime.sys_umtx_op() => futexwakeup() => runtime.sys_umtx_op()
newm() => newosproc() => runtime.thr_new()
mheap.alloc() => mheap.alloc_m() => mheap.allocSpanLocked() => mheap.grow() => mheap.sysAlloc() => sysAlloc() => mmap()
// feels like mark-and-sweep
heapBits.initSpan() => newMarkBits() => newArena() => sysAlloc()
mspan.sweep() => newMarkBits()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment