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