Home | History | Annotate | Download | only in cgo
      1 // Copyright 2009 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> // strerror
      8 #include <signal.h>
      9 #include <stdlib.h>
     10 #include "libcgo.h"
     11 #include "libcgo_unix.h"
     12 
     13 static void* threadentry(void*);
     14 static void (*setg_gcc)(void*);
     15 
     16 // These will be set in gcc_android_amd64.c for android-specific customization.
     17 void (*x_cgo_inittls)(void);
     18 void* (*x_cgo_threadentry)(void*);
     19 
     20 void
     21 x_cgo_init(G* g, void (*setg)(void*))
     22 {
     23 	pthread_attr_t *attr;
     24 	size_t size;
     25 
     26 	/* The memory sanitizer distributed with versions of clang
     27 	   before 3.8 has a bug: if you call mmap before malloc, mmap
     28 	   may return an address that is later overwritten by the msan
     29 	   library.  Avoid this problem by forcing a call to malloc
     30 	   here, before we ever call malloc.
     31 
     32 	   This is only required for the memory sanitizer, so it's
     33 	   unfortunate that we always run it.  It should be possible
     34 	   to remove this when we no longer care about versions of
     35 	   clang before 3.8.  The test for this is
     36 	   misc/cgo/testsanitizers.
     37 
     38 	   GCC works hard to eliminate a seemingly unnecessary call to
     39 	   malloc, so we actually use the memory we allocate.  */
     40 
     41 	setg_gcc = setg;
     42 	attr = (pthread_attr_t*)malloc(sizeof *attr);
     43 	if (attr == NULL) {
     44 		fatalf("malloc failed: %s", strerror(errno));
     45 	}
     46 	pthread_attr_init(attr);
     47 	pthread_attr_getstacksize(attr, &size);
     48 	g->stacklo = (uintptr)&size - size + 4096;
     49 	pthread_attr_destroy(attr);
     50 	free(attr);
     51 
     52 	if (x_cgo_inittls) {
     53 		x_cgo_inittls();
     54 	}
     55 }
     56 
     57 
     58 void
     59 _cgo_sys_thread_start(ThreadStart *ts)
     60 {
     61 	pthread_attr_t attr;
     62 	sigset_t ign, oset;
     63 	pthread_t p;
     64 	size_t size;
     65 	int err;
     66 
     67 	sigfillset(&ign);
     68 	pthread_sigmask(SIG_SETMASK, &ign, &oset);
     69 
     70 	pthread_attr_init(&attr);
     71 	pthread_attr_getstacksize(&attr, &size);
     72 	// Leave stacklo=0 and set stackhi=size; mstart will do the rest.
     73 	ts->g->stackhi = size;
     74 	err = _cgo_try_pthread_create(&p, &attr, threadentry, ts);
     75 
     76 	pthread_sigmask(SIG_SETMASK, &oset, nil);
     77 
     78 	if (err != 0) {
     79 		fatalf("pthread_create failed: %s", strerror(err));
     80 	}
     81 }
     82 
     83 static void*
     84 threadentry(void *v)
     85 {
     86 	if (x_cgo_threadentry) {
     87 		return x_cgo_threadentry(v);
     88 	}
     89 
     90 	ThreadStart ts;
     91 
     92 	ts = *(ThreadStart*)v;
     93 	_cgo_tsan_acquire();
     94 	free(v);
     95 	_cgo_tsan_release();
     96 
     97 	/*
     98 	 * Set specific keys.
     99 	 */
    100 	setg_gcc((void*)ts.g);
    101 
    102 	crosscall_amd64(ts.fn);
    103 	return nil;
    104 }
    105