...
Run Format

Text file src/runtime/cgo/gcc_linux_arm64.c

Documentation: runtime/cgo

     1	// Copyright 2015 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 <errno.h>
     7	#include <string.h>
     8	#include <signal.h>
     9	#include <stdlib.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	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		// Not sure why the memset is necessary here,
    31		// but without it, we get a bogus stack size
    32		// out of pthread_attr_getstacksize. C'est la Linux.
    33		memset(&attr, 0, sizeof attr);
    34		pthread_attr_init(&attr);
    35		size = 0;
    36		pthread_attr_getstacksize(&attr, &size);
    37		// Leave stacklo=0 and set stackhi=size; mstart will do the rest.
    38		ts->g->stackhi = size;
    39		err = _cgo_try_pthread_create(&p, &attr, threadentry, ts);
    40	
    41		pthread_sigmask(SIG_SETMASK, &oset, nil);
    42	
    43		if (err != 0) {
    44			fatalf("pthread_create failed: %s", strerror(err));
    45		}
    46	}
    47	
    48	extern void crosscall1(void (*fn)(void), void (*setg_gcc)(void*), void *g);
    49	static void*
    50	threadentry(void *v)
    51	{
    52		ThreadStart ts;
    53	
    54		ts = *(ThreadStart*)v;
    55		free(v);
    56	
    57		crosscall1(ts.fn, setg_gcc, (void*)ts.g);
    58		return nil;
    59	}
    60	
    61	void
    62	x_cgo_init(G *g, void (*setg)(void*), void **tlsg, void **tlsbase)
    63	{
    64		pthread_attr_t *attr;
    65		size_t size;
    66	
    67		/* The memory sanitizer distributed with versions of clang
    68		   before 3.8 has a bug: if you call mmap before malloc, mmap
    69		   may return an address that is later overwritten by the msan
    70		   library.  Avoid this problem by forcing a call to malloc
    71		   here, before we ever call malloc.
    72	
    73		   This is only required for the memory sanitizer, so it's
    74		   unfortunate that we always run it.  It should be possible
    75		   to remove this when we no longer care about versions of
    76		   clang before 3.8.  The test for this is
    77		   misc/cgo/testsanitizers.
    78	
    79		   GCC works hard to eliminate a seemingly unnecessary call to
    80		   malloc, so we actually use the memory we allocate.  */
    81	
    82		setg_gcc = setg;
    83		attr = (pthread_attr_t*)malloc(sizeof *attr);
    84		if (attr == NULL) {
    85			fatalf("malloc failed: %s", strerror(errno));
    86		}
    87		pthread_attr_init(attr);
    88		pthread_attr_getstacksize(attr, &size);
    89		g->stacklo = (uintptr)&size - size + 4096;
    90		pthread_attr_destroy(attr);
    91		free(attr);
    92	
    93		if (x_cgo_inittls) {
    94			x_cgo_inittls(tlsg, tlsbase);
    95		}
    96	}

View as plain text