...

Text file src/pkg/runtime/cgo/gcc_netbsd_arm64.c

     1	// Copyright 2019 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 <sys/types.h>
     6	#include <pthread.h>
     7	#include <signal.h>
     8	#include <string.h>
     9	#include "libcgo.h"
    10	#include "libcgo_unix.h"
    11	
    12	static void *threadentry(void*);
    13	
    14	static void (*setg_gcc)(void*);
    15	
    16	void
    17	x_cgo_init(G *g, void (*setg)(void*))
    18	{
    19		pthread_attr_t attr;
    20		size_t size;
    21	
    22		setg_gcc = setg;
    23		pthread_attr_init(&attr);
    24		pthread_attr_getstacksize(&attr, &size);
    25		g->stacklo = (uintptr)&attr - size + 4096;
    26		pthread_attr_destroy(&attr);
    27	}
    28	
    29	
    30	void
    31	_cgo_sys_thread_start(ThreadStart *ts)
    32	{
    33		pthread_attr_t attr;
    34		sigset_t ign, oset;
    35		pthread_t p;
    36		size_t size;
    37		int err;
    38	
    39		sigfillset(&ign);
    40		pthread_sigmask(SIG_SETMASK, &ign, &oset);
    41	
    42		pthread_attr_init(&attr);
    43		pthread_attr_getstacksize(&attr, &size);
    44		// Leave stacklo=0 and set stackhi=size; mstart will do the rest.
    45		ts->g->stackhi = size;
    46		err = _cgo_try_pthread_create(&p, &attr, threadentry, ts);
    47	
    48		pthread_sigmask(SIG_SETMASK, &oset, nil);
    49	
    50		if (err != 0) {
    51			fprintf(stderr, "runtime/cgo: pthread_create failed: %s\n", strerror(err));
    52			abort();
    53		}
    54	}
    55	
    56	static void*
    57	threadentry(void *v)
    58	{
    59		ThreadStart ts;
    60		stack_t ss;
    61	
    62		ts = *(ThreadStart*)v;
    63		free(v);
    64	
    65		// On NetBSD, a new thread inherits the signal stack of the
    66		// creating thread. That confuses minit, so we remove that
    67		// signal stack here before calling the regular mstart. It's
    68		// a bit baroque to remove a signal stack here only to add one
    69		// in minit, but it's a simple change that keeps NetBSD
    70		// working like other OS's. At this point all signals are
    71		// blocked, so there is no race.
    72		memset(&ss, 0, sizeof ss);
    73		ss.ss_flags = SS_DISABLE;
    74		sigaltstack(&ss, nil);
    75	
    76		crosscall1(ts.fn, setg_gcc, (void*)ts.g);
    77		return nil;
    78	}

View as plain text