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 mips64 mips64le
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 void (*setg_gcc)(void*);
19 
20 void
_cgo_sys_thread_start(ThreadStart * ts)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 	// Not sure why the memset is necessary here,
33 	// but without it, we get a bogus stack size
34 	// out of pthread_attr_getstacksize.  C'est la Linux.
35 	memset(&attr, 0, sizeof attr);
36 	pthread_attr_init(&attr);
37 	size = 0;
38 	pthread_attr_getstacksize(&attr, &size);
39 	// Leave stacklo=0 and set stackhi=size; mstart will do the rest.
40 	ts->g->stackhi = size;
41 	err = _cgo_try_pthread_create(&p, &attr, threadentry, ts);
42 
43 	pthread_sigmask(SIG_SETMASK, &oset, nil);
44 
45 	if (err != 0) {
46 		fatalf("pthread_create failed: %s", strerror(err));
47 	}
48 }
49 
50 extern void crosscall1(void (*fn)(void), void (*setg_gcc)(void*), void *g);
51 static void*
threadentry(void * v)52 threadentry(void *v)
53 {
54 	ThreadStart ts;
55 
56 	ts = *(ThreadStart*)v;
57 	free(v);
58 
59 	crosscall1(ts.fn, setg_gcc, (void*)ts.g);
60 	return nil;
61 }
62 
63 void
x_cgo_init(G * g,void (* setg)(void *),void ** tlsg,void ** tlsbase)64 x_cgo_init(G *g, void (*setg)(void*), void **tlsg, void **tlsbase)
65 {
66 	pthread_attr_t attr;
67 	size_t size;
68 
69 	setg_gcc = setg;
70 	pthread_attr_init(&attr);
71 	pthread_attr_getstacksize(&attr, &size);
72 	g->stacklo = (uintptr)&attr - size + 4096;
73 	pthread_attr_destroy(&attr);
74 
75 	if (x_cgo_inittls) {
76 		x_cgo_inittls(tlsg, tlsbase);
77 	}
78 }
79