1
0
mirror of https://github.com/golang/go synced 2024-11-20 06:04:52 -07:00
go/src/pkg/runtime/cgocall.c

73 lines
1.1 KiB
C
Raw Normal View History

// 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 */
int64 ncgocall;
void
cgocall(void (*fn)(void*), void *arg)
{
CgoWork w;
CgoServer *s;
ncgocall++;
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);
}
void
runtime·Cgocalls(int64 ret)
{
ret = ncgocall;
FLUSH(&ret);
}
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);
}