blob: dd8f888290551a104637f4c4d31307fe05ed17e8 [file] [log] [blame]
Tobias Klauser33e39832019-10-04 10:11:29 +02001// 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 <errno.h>
7#include <sys/signalvar.h>
8#include <pthread.h>
9#include <signal.h>
10#include <string.h>
11#include "libcgo.h"
12#include "libcgo_unix.h"
13
14static void* threadentry(void*);
15static void (*setg_gcc)(void*);
16
17void
18x_cgo_init(G *g, void (*setg)(void*))
19{
20 pthread_attr_t attr;
21 size_t size;
22
23 setg_gcc = setg;
24 pthread_attr_init(&attr);
25 pthread_attr_getstacksize(&attr, &size);
26 g->stacklo = (uintptr)&attr - size + 4096;
27 pthread_attr_destroy(&attr);
28}
29
30void
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
56extern void crosscall1(void (*fn)(void), void (*setg_gcc)(void*), void *g);
57
58static void*
59threadentry(void *v)
60{
61 ThreadStart ts;
62
63 ts = *(ThreadStart*)v;
64 free(v);
65
66 crosscall1(ts.fn, setg_gcc, (void*)ts.g);
67 return nil;
68}