Source file src/runtime/os_openbsd_libc.go

     1  // Copyright 2020 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 openbsd && !mips64
     6  
     7  package runtime
     8  
     9  import (
    10  	"internal/abi"
    11  	"unsafe"
    12  )
    13  
    14  var failThreadCreate = []byte("runtime: failed to create new OS thread\n")
    15  
    16  // mstart_stub provides glue code to call mstart from pthread_create.
    17  func mstart_stub()
    18  
    19  // May run with m.p==nil, so write barriers are not allowed.
    20  //go:nowritebarrierrec
    21  func newosproc(mp *m) {
    22  	if false {
    23  		print("newosproc m=", mp, " g=", mp.g0, " id=", mp.id, " ostk=", &mp, "\n")
    24  	}
    25  
    26  	// Initialize an attribute object.
    27  	var attr pthreadattr
    28  	if err := pthread_attr_init(&attr); err != 0 {
    29  		write(2, unsafe.Pointer(&failThreadCreate[0]), int32(len(failThreadCreate)))
    30  		exit(1)
    31  	}
    32  
    33  	// Find out OS stack size for our own stack guard.
    34  	var stacksize uintptr
    35  	if pthread_attr_getstacksize(&attr, &stacksize) != 0 {
    36  		write(2, unsafe.Pointer(&failThreadCreate[0]), int32(len(failThreadCreate)))
    37  		exit(1)
    38  	}
    39  	mp.g0.stack.hi = stacksize // for mstart
    40  
    41  	// Tell the pthread library we won't join with this thread.
    42  	if pthread_attr_setdetachstate(&attr, _PTHREAD_CREATE_DETACHED) != 0 {
    43  		write(2, unsafe.Pointer(&failThreadCreate[0]), int32(len(failThreadCreate)))
    44  		exit(1)
    45  	}
    46  
    47  	// Finally, create the thread. It starts at mstart_stub, which does some low-level
    48  	// setup and then calls mstart.
    49  	var oset sigset
    50  	sigprocmask(_SIG_SETMASK, &sigset_all, &oset)
    51  	err := pthread_create(&attr, abi.FuncPCABI0(mstart_stub), unsafe.Pointer(mp))
    52  	sigprocmask(_SIG_SETMASK, &oset, nil)
    53  	if err != 0 {
    54  		write(2, unsafe.Pointer(&failThreadCreate[0]), int32(len(failThreadCreate)))
    55  		exit(1)
    56  	}
    57  
    58  	pthread_attr_destroy(&attr)
    59  }
    60  

View as plain text