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"
|
|
|
|
|
|
|
|
Cgo *cgo; /* filled in by dynamic linker when Cgo is available */
|
2009-08-25 16:37:22 -06:00
|
|
|
int64 ncgocall;
|
2009-08-24 18:30:00 -06:00
|
|
|
|
|
|
|
void
|
|
|
|
cgocall(void (*fn)(void*), void *arg)
|
|
|
|
{
|
|
|
|
CgoWork w;
|
|
|
|
CgoServer *s;
|
|
|
|
|
2009-08-25 16:37:22 -06:00
|
|
|
ncgocall++;
|
|
|
|
|
2009-08-24 18:30:00 -06:00
|
|
|
if(cgo == nil)
|
|
|
|
throw("cgocall unavailable");
|
|
|
|
|
|
|
|
noteclear(&w.note);
|
|
|
|
w.next = nil;
|
|
|
|
w.fn = fn;
|
|
|
|
w.arg = arg;
|
|
|
|
lock(&cgo->lock);
|
|
|
|
if((s = cgo->idle) != nil) {
|
|
|
|
cgo->idle = s->next;
|
|
|
|
s->work = &w;
|
|
|
|
unlock(&cgo->lock);
|
|
|
|
notewakeup(&s->note);
|
|
|
|
} else {
|
|
|
|
if(cgo->whead == nil) {
|
|
|
|
cgo->whead = &w;
|
|
|
|
} else
|
|
|
|
cgo->wtail->next = &w;
|
|
|
|
cgo->wtail = &w;
|
|
|
|
unlock(&cgo->lock);
|
|
|
|
}
|
|
|
|
notesleep(&w.note);
|
|
|
|
}
|
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);
|
|
|
|
}
|
|
|
|
|