Black Lives Matter. Support the Equal Justice Initiative.

Text file src/runtime/cgo/gcc_linux_riscv64.c

Documentation: runtime/cgo

     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  #include <pthread.h>
     6  #include <string.h>
     7  #include <signal.h>
     8  #include "libcgo.h"
     9  #include "libcgo_unix.h"
    10  
    11  static void *threadentry(void*);
    12  
    13  void (*x_cgo_inittls)(void **tlsg, void **tlsbase);
    14  static void (*setg_gcc)(void*);
    15  
    16  void
    17  _cgo_sys_thread_start(ThreadStart *ts)
    18  {
    19  	pthread_attr_t attr;
    20  	sigset_t ign, oset;
    21  	pthread_t p;
    22  	size_t size;
    23  	int err;
    24  
    25  	sigfillset(&ign);
    26  	pthread_sigmask(SIG_SETMASK, &ign, &oset);
    27  
    28  	// Not sure why the memset is necessary here,
    29  	// but without it, we get a bogus stack size
    30  	// out of pthread_attr_getstacksize.  C'est la Linux.
    31  	memset(&attr, 0, sizeof attr);
    32  	pthread_attr_init(&attr);
    33  	size = 0;
    34  	pthread_attr_getstacksize(&attr, &size);
    35  	// Leave stacklo=0 and set stackhi=size; mstart will do the rest.
    36  	ts->g->stackhi = size;
    37  	err = _cgo_try_pthread_create(&p, &attr, threadentry, ts);
    38  
    39  	pthread_sigmask(SIG_SETMASK, &oset, nil);
    40  
    41  	if (err != 0) {
    42  		fatalf("pthread_create failed: %s", strerror(err));
    43  	}
    44  }
    45  
    46  extern void crosscall1(void (*fn)(void), void (*setg_gcc)(void*), void *g);
    47  static void*
    48  threadentry(void *v)
    49  {
    50  	ThreadStart ts;
    51  
    52  	ts = *(ThreadStart*)v;
    53  	free(v);
    54  
    55  	crosscall1(ts.fn, setg_gcc, (void*)ts.g);
    56  	return nil;
    57  }
    58  
    59  void
    60  x_cgo_init(G *g, void (*setg)(void*), void **tlsg, void **tlsbase)
    61  {
    62  	pthread_attr_t attr;
    63  	size_t size;
    64  
    65  	setg_gcc = setg;
    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  }
    75  

View as plain text