blob: 7a98c0de24bdfc7cf4fd94c6000f942c5f520f8d [file] [log] [blame]
Shenghou Ma2fdd60b2013-02-13 01:06:52 +08001// Copyright 2013 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
11static void *threadentry(void*);
12
Russ Cox89f185f2014-06-26 11:54:39 -040013static void (*setg_gcc)(void*);
Anthony Martin2b39e412013-02-26 09:51:33 -080014
15void
Russ Cox89f185f2014-06-26 11:54:39 -040016x_cgo_init(G *g, void (*setg)(void*))
Shenghou Ma2fdd60b2013-02-13 01:06:52 +080017{
18 pthread_attr_t attr;
19 size_t size;
Shenghou Ma2fdd60b2013-02-13 01:06:52 +080020
Russ Cox89f185f2014-06-26 11:54:39 -040021 setg_gcc = setg;
Shenghou Ma2fdd60b2013-02-13 01:06:52 +080022 pthread_attr_init(&attr);
23 pthread_attr_getstacksize(&attr, &size);
Russ Cox15b76ad2014-09-09 13:39:57 -040024 g->stacklo = (uintptr)&attr - size + 4096;
Shenghou Ma2fdd60b2013-02-13 01:06:52 +080025 pthread_attr_destroy(&attr);
26}
27
Shenghou Ma2fdd60b2013-02-13 01:06:52 +080028
29void
Russ Coxf8d49b52013-02-28 16:24:38 -050030_cgo_sys_thread_start(ThreadStart *ts)
Shenghou Ma2fdd60b2013-02-13 01:06:52 +080031{
32 pthread_attr_t attr;
33 sigset_t ign, oset;
34 pthread_t p;
35 size_t size;
36 int err;
37
38 sigfillset(&ign);
S.Çağlar Onur41183d02013-12-22 08:55:29 -080039 pthread_sigmask(SIG_SETMASK, &ign, &oset);
Shenghou Ma2fdd60b2013-02-13 01:06:52 +080040
41 pthread_attr_init(&attr);
42 pthread_attr_getstacksize(&attr, &size);
Russ Cox15b76ad2014-09-09 13:39:57 -040043 // Leave stacklo=0 and set stackhi=size; mstack will do the rest.
44 ts->g->stackhi = size;
Shenghou Ma2fdd60b2013-02-13 01:06:52 +080045 err = pthread_create(&p, &attr, threadentry, ts);
46
S.Çağlar Onur41183d02013-12-22 08:55:29 -080047 pthread_sigmask(SIG_SETMASK, &oset, nil);
Shenghou Ma2fdd60b2013-02-13 01:06:52 +080048
49 if (err != 0) {
50 fprintf(stderr, "runtime/cgo: pthread_create failed: %s\n", strerror(err));
51 abort();
52 }
53}
54
Russ Cox89f185f2014-06-26 11:54:39 -040055extern void crosscall_arm1(void (*fn)(void), void (*setg_gcc)(void*), void *g);
Shenghou Ma2fdd60b2013-02-13 01:06:52 +080056static void*
57threadentry(void *v)
58{
59 ThreadStart ts;
60
61 ts = *(ThreadStart*)v;
62 free(v);
63
Russ Cox89f185f2014-06-26 11:54:39 -040064 crosscall_arm1(ts.fn, setg_gcc, (void*)ts.g);
Shenghou Ma2fdd60b2013-02-13 01:06:52 +080065 return nil;
66}