github.com/lovishpuri/go-40569/src@v0.0.0-20230519171745-f8623e7c56cf/runtime/cgo/gcc_linux_mips64x.c (about)

     1  // Copyright 2016 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 linux && (mips64 || mips64le)
     6  
     7  #include <pthread.h>
     8  #include <string.h>
     9  #include <signal.h>
    10  #include "libcgo.h"
    11  #include "libcgo_unix.h"
    12  
    13  static void *threadentry(void*);
    14  
    15  void (*x_cgo_inittls)(void **tlsg, void **tlsbase);
    16  static void (*setg_gcc)(void*);
    17  
    18  void
    19  _cgo_sys_thread_start(ThreadStart *ts)
    20  {
    21  	pthread_attr_t attr;
    22  	sigset_t ign, oset;
    23  	pthread_t p;
    24  	size_t size;
    25  	int err;
    26  
    27  	sigfillset(&ign);
    28  	pthread_sigmask(SIG_SETMASK, &ign, &oset);
    29  
    30  	pthread_attr_init(&attr);
    31  	pthread_attr_getstacksize(&attr, &size);
    32  	// Leave stacklo=0 and set stackhi=size; mstart will do the rest.
    33  	ts->g->stackhi = size;
    34  	err = _cgo_try_pthread_create(&p, &attr, threadentry, ts);
    35  
    36  	pthread_sigmask(SIG_SETMASK, &oset, nil);
    37  
    38  	if (err != 0) {
    39  		fatalf("pthread_create failed: %s", strerror(err));
    40  	}
    41  }
    42  
    43  extern void crosscall1(void (*fn)(void), void (*setg_gcc)(void*), void *g);
    44  static void*
    45  threadentry(void *v)
    46  {
    47  	ThreadStart ts;
    48  
    49  	ts = *(ThreadStart*)v;
    50  	free(v);
    51  
    52  	crosscall1(ts.fn, setg_gcc, (void*)ts.g);
    53  	return nil;
    54  }
    55  
    56  void
    57  x_cgo_init(G *g, void (*setg)(void*), void **tlsg, void **tlsbase)
    58  {
    59  	pthread_attr_t attr;
    60  	size_t size;
    61  
    62  	setg_gcc = setg;
    63  	pthread_attr_init(&attr);
    64  	pthread_attr_getstacksize(&attr, &size);
    65  	g->stacklo = (uintptr)&attr - size + 4096;
    66  	pthread_attr_destroy(&attr);
    67  
    68  	if (x_cgo_inittls) {
    69  		x_cgo_inittls(tlsg, tlsbase);
    70  	}
    71  }