2009-08-24 18:30:00 -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 "runtime.h"
|
|
|
|
#include "cgocall.h"
|
|
|
|
|
2009-10-03 11:37:12 -06:00
|
|
|
void *initcgo; /* filled in by dynamic linker when Cgo is available */
|
2009-08-25 16:37:22 -06:00
|
|
|
int64 ncgocall;
|
2009-10-03 11:37:12 -06:00
|
|
|
void sys·entersyscall(void);
|
|
|
|
void sys·exitsyscall(void);
|
2009-08-24 18:30:00 -06:00
|
|
|
|
|
|
|
void
|
|
|
|
cgocall(void (*fn)(void*), void *arg)
|
|
|
|
{
|
2009-10-03 11:37:12 -06:00
|
|
|
if(initcgo == nil)
|
|
|
|
throw("cgocall unavailable");
|
2009-08-24 18:30:00 -06:00
|
|
|
|
2009-08-25 16:37:22 -06:00
|
|
|
ncgocall++;
|
|
|
|
|
2009-10-03 11:37:12 -06:00
|
|
|
/*
|
|
|
|
* Announce we are entering a system call
|
|
|
|
* so that the scheduler knows to create another
|
|
|
|
* M to run goroutines while we are in the
|
|
|
|
* foreign code.
|
|
|
|
*/
|
|
|
|
sys·entersyscall();
|
|
|
|
g->cgofn = fn;
|
|
|
|
g->cgoarg = arg;
|
|
|
|
g->status = Gcgocall;
|
|
|
|
gosched();
|
|
|
|
sys·exitsyscall();
|
|
|
|
return;
|
2009-08-24 18:30:00 -06:00
|
|
|
}
|
2009-08-25 16:37:22 -06:00
|
|
|
|
|
|
|
void
|
|
|
|
runtime·Cgocalls(int64 ret)
|
|
|
|
{
|
|
|
|
ret = ncgocall;
|
|
|
|
FLUSH(&ret);
|
|
|
|
}
|
|
|
|
|
2009-09-24 12:43:19 -06:00
|
|
|
void (*_cgo_malloc)(void*);
|
|
|
|
void (*_cgo_free)(void*);
|
|
|
|
|
|
|
|
void*
|
|
|
|
cmalloc(uintptr n)
|
|
|
|
{
|
|
|
|
struct a {
|
|
|
|
uint64 n;
|
|
|
|
void *ret;
|
|
|
|
} a;
|
|
|
|
|
|
|
|
a.n = n;
|
|
|
|
a.ret = nil;
|
|
|
|
cgocall(_cgo_malloc, &a);
|
|
|
|
return a.ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cfree(void *p)
|
|
|
|
{
|
|
|
|
cgocall(_cgo_free, p);
|
|
|
|
}
|
|
|
|
|