blob: 9097a2af9028467f5479c6d0508aa6049c1b6a6b [file] [log] [blame]
Brad Fitzpatrick51947442016-03-01 22:57:46 +00001// Copyright 2009 The Go Authors. All rights reserved.
Devon H. O'Dellb3f538a2009-11-17 23:58:51 -08002// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
Devon H. O'Dellb0891062012-02-22 15:44:09 +11005#include <sys/types.h>
6#include <sys/signalvar.h>
Devon H. O'Dellb3f538a2009-11-17 23:58:51 -08007#include <pthread.h>
Devon H. O'Dellb0891062012-02-22 15:44:09 +11008#include <signal.h>
Ian Lance Taylorcc8bd892012-11-14 22:04:03 -08009#include <string.h>
Devon H. O'Dellb3f538a2009-11-17 23:58:51 -080010#include "libcgo.h"
Ian Lance Taylor7cba7792016-12-02 15:32:55 -080011#include "libcgo_unix.h"
Devon H. O'Dellb3f538a2009-11-17 23:58:51 -080012
13static void* threadentry(void*);
Russ Cox89f185f2014-06-26 11:54:39 -040014static void (*setg_gcc)(void*);
Devon H. O'Dellb3f538a2009-11-17 23:58:51 -080015
Russ Coxf8d49b52013-02-28 16:24:38 -050016void
Russ Cox89f185f2014-06-26 11:54:39 -040017x_cgo_init(G *g, void (*setg)(void*))
Devon H. O'Dellb3f538a2009-11-17 23:58:51 -080018{
Dmitriy Vyukovfbfed492011-11-09 23:11:48 +030019 pthread_attr_t attr;
20 size_t size;
21
Russ Cox89f185f2014-06-26 11:54:39 -040022 setg_gcc = setg;
Dmitriy Vyukovfbfed492011-11-09 23:11:48 +030023 pthread_attr_init(&attr);
24 pthread_attr_getstacksize(&attr, &size);
Russ Cox15b76ad2014-09-09 13:39:57 -040025 g->stacklo = (uintptr)&attr - size + 4096;
Dmitriy Vyukovfbfed492011-11-09 23:11:48 +030026 pthread_attr_destroy(&attr);
Devon H. O'Dellb3f538a2009-11-17 23:58:51 -080027}
28
Devon H. O'Dellb3f538a2009-11-17 23:58:51 -080029
30void
Russ Coxf8d49b52013-02-28 16:24:38 -050031_cgo_sys_thread_start(ThreadStart *ts)
Devon H. O'Dellb3f538a2009-11-17 23:58:51 -080032{
33 pthread_attr_t attr;
Devon H. O'Dellb0891062012-02-22 15:44:09 +110034 sigset_t ign, oset;
Devon H. O'Dellb3f538a2009-11-17 23:58:51 -080035 pthread_t p;
36 size_t size;
Albert Strasheima026d0f2011-06-28 12:04:50 -040037 int err;
Devon H. O'Dellb3f538a2009-11-17 23:58:51 -080038
Devon H. O'Dellb0891062012-02-22 15:44:09 +110039 SIGFILLSET(ign);
S.Çağlar Onur41183d02013-12-22 08:55:29 -080040 pthread_sigmask(SIG_SETMASK, &ign, &oset);
Devon H. O'Dellb0891062012-02-22 15:44:09 +110041
Devon H. O'Dellb3f538a2009-11-17 23:58:51 -080042 pthread_attr_init(&attr);
43 pthread_attr_getstacksize(&attr, &size);
Hiroshi Iokac4e0e812017-06-21 19:48:22 +090044 // Leave stacklo=0 and set stackhi=size; mstart will do the rest.
Russ Cox15b76ad2014-09-09 13:39:57 -040045 ts->g->stackhi = size;
Ian Lance Taylor7cba7792016-12-02 15:32:55 -080046 err = _cgo_try_pthread_create(&p, &attr, threadentry, ts);
Devon H. O'Dellb0891062012-02-22 15:44:09 +110047
S.Çağlar Onur41183d02013-12-22 08:55:29 -080048 pthread_sigmask(SIG_SETMASK, &oset, nil);
Devon H. O'Dellb0891062012-02-22 15:44:09 +110049
Albert Strasheima026d0f2011-06-28 12:04:50 -040050 if (err != 0) {
51 fprintf(stderr, "runtime/cgo: pthread_create failed: %s\n", strerror(err));
52 abort();
53 }
Devon H. O'Dellb3f538a2009-11-17 23:58:51 -080054}
55
56static void*
57threadentry(void *v)
58{
59 ThreadStart ts;
60
61 ts = *(ThreadStart*)v;
62 free(v);
63
Devon H. O'Dell60b1a172009-11-18 16:51:59 -080064 /*
Russ Cox6a70f9d2013-03-25 18:14:02 -040065 * Set specific keys.
Devon H. O'Dell60b1a172009-11-18 16:51:59 -080066 */
Russ Cox89f185f2014-06-26 11:54:39 -040067 setg_gcc((void*)ts.g);
Devon H. O'Dellb3f538a2009-11-17 23:58:51 -080068
69 crosscall_386(ts.fn);
70 return nil;
71}