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  //
    21  //go:nowritebarrierrec
    22  func newosproc(mp *m) {
    23  	if false {
    24  		print("newosproc m=", mp, " g=", mp.g0, " id=", mp.id, " ostk=", &mp, "\n")
    25  	}
    26  
    27  	// Initialize an attribute object.
    28  	var attr pthreadattr
    29  	if err := pthread_attr_init(&attr); err != 0 {
    30  		write(2, unsafe.Pointer(&failThreadCreate[0]), int32(len(failThreadCreate)))
    31  		exit(1)
    32  	}
    33  
    34  	// Find out OS stack size for our own stack guard.
    35  	var stacksize uintptr
    36  	if pthread_attr_getstacksize(&attr, &stacksize) != 0 {
    37  		write(2, unsafe.Pointer(&failThreadCreate[0]), int32(len(failThreadCreate)))
    38  		exit(1)
    39  	}
    40  	mp.g0.stack.hi = stacksize // for mstart
    41  
    42  	// Tell the pthread library we won't join with this thread.
    43  	if pthread_attr_setdetachstate(&attr, _PTHREAD_CREATE_DETACHED) != 0 {
    44  		write(2, unsafe.Pointer(&failThreadCreate[0]), int32(len(failThreadCreate)))
    45  		exit(1)
    46  	}
    47  
    48  	// Finally, create the thread. It starts at mstart_stub, which does some low-level
    49  	// setup and then calls mstart.
    50  	var oset sigset
    51  	sigprocmask(_SIG_SETMASK, &sigset_all, &oset)
    52  	err := pthread_create(&attr, abi.FuncPCABI0(mstart_stub), unsafe.Pointer(mp))
    53  	sigprocmask(_SIG_SETMASK, &oset, nil)
    54  	if err != 0 {
    55  		write(2, unsafe.Pointer(&failThreadCreate[0]), int32(len(failThreadCreate)))
    56  		exit(1)
    57  	}
    58  
    59  	pthread_attr_destroy(&attr)
    60  }
    61  

View as plain text