2012-06-04 09:43:04 -06:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <signal.h>
|
2012-11-14 23:04:03 -07:00
|
|
|
#include <string.h>
|
2012-06-04 09:43:04 -06:00
|
|
|
#include "libcgo.h"
|
|
|
|
|
|
|
|
static void* threadentry(void*);
|
2013-03-25 16:14:02 -06:00
|
|
|
static void (*setmg_gcc)(void*, void*);
|
2012-06-04 09:43:04 -06:00
|
|
|
|
2013-02-28 14:24:38 -07:00
|
|
|
void
|
2013-03-25 16:14:02 -06:00
|
|
|
x_cgo_init(G *g, void (*setmg)(void*, void*))
|
2012-06-04 09:43:04 -06:00
|
|
|
{
|
|
|
|
pthread_attr_t attr;
|
|
|
|
size_t size;
|
|
|
|
|
2013-03-25 16:14:02 -06:00
|
|
|
setmg_gcc = setmg;
|
2012-06-04 09:43:04 -06:00
|
|
|
pthread_attr_init(&attr);
|
|
|
|
pthread_attr_getstacksize(&attr, &size);
|
|
|
|
g->stackguard = (uintptr)&attr - size + 4096;
|
|
|
|
pthread_attr_destroy(&attr);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2013-02-28 14:24:38 -07:00
|
|
|
_cgo_sys_thread_start(ThreadStart *ts)
|
2012-06-04 09:43:04 -06:00
|
|
|
{
|
|
|
|
pthread_attr_t attr;
|
|
|
|
sigset_t ign, oset;
|
|
|
|
pthread_t p;
|
|
|
|
size_t size;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
sigfillset(&ign);
|
2013-12-22 09:55:29 -07:00
|
|
|
pthread_sigmask(SIG_SETMASK, &ign, &oset);
|
2012-06-04 09:43:04 -06:00
|
|
|
|
|
|
|
pthread_attr_init(&attr);
|
|
|
|
pthread_attr_getstacksize(&attr, &size);
|
|
|
|
ts->g->stackguard = size;
|
|
|
|
err = pthread_create(&p, &attr, threadentry, ts);
|
|
|
|
|
2013-12-22 09:55:29 -07:00
|
|
|
pthread_sigmask(SIG_SETMASK, &oset, nil);
|
2012-06-04 09:43:04 -06:00
|
|
|
|
|
|
|
if (err != 0) {
|
|
|
|
fprintf(stderr, "runtime/cgo: pthread_create failed: %s\n", strerror(err));
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void*
|
|
|
|
threadentry(void *v)
|
|
|
|
{
|
|
|
|
ThreadStart ts;
|
|
|
|
|
|
|
|
ts = *(ThreadStart*)v;
|
|
|
|
free(v);
|
|
|
|
|
|
|
|
ts.g->stackbase = (uintptr)&ts;
|
|
|
|
|
|
|
|
/*
|
2013-02-28 14:24:38 -07:00
|
|
|
* _cgo_sys_thread_start set stackguard to stack size;
|
2012-06-04 09:43:04 -06:00
|
|
|
* change to actual guard pointer.
|
|
|
|
*/
|
|
|
|
ts.g->stackguard = (uintptr)&ts - ts.g->stackguard + 4096;
|
|
|
|
|
|
|
|
/*
|
2013-03-25 16:14:02 -06:00
|
|
|
* Set specific keys.
|
2012-06-04 09:43:04 -06:00
|
|
|
*/
|
2013-03-25 16:14:02 -06:00
|
|
|
setmg_gcc((void*)ts.m, (void*)ts.g);
|
2012-06-04 09:43:04 -06:00
|
|
|
|
|
|
|
crosscall_386(ts.fn);
|
|
|
|
return nil;
|
|
|
|
}
|