diff options
| author | Ondřej Surý <ondrej@sury.org> | 2011-06-03 11:31:24 +0200 |
|---|---|---|
| committer | Ondřej Surý <ondrej@sury.org> | 2011-06-03 11:38:02 +0200 |
| commit | 13f4fcd5bf09c70942b6c85a2b919ffa1ca0c6a8 (patch) | |
| tree | 717e1ceeef3a60af29e7897c7629d1502b797838 /src/pkg/runtime/windows/thread.c | |
| parent | 6bf52070ef1028f7fcc98fad1e73795a7efd7ce7 (diff) | |
| download | golang-13f4fcd5bf09c70942b6c85a2b919ffa1ca0c6a8.tar.gz | |
Imported Upstream version 2011.06.02
Diffstat (limited to 'src/pkg/runtime/windows/thread.c')
| -rw-r--r-- | src/pkg/runtime/windows/thread.c | 40 |
1 files changed, 37 insertions, 3 deletions
diff --git a/src/pkg/runtime/windows/thread.c b/src/pkg/runtime/windows/thread.c index 2ce92dcfb..2ce8fae15 100644 --- a/src/pkg/runtime/windows/thread.c +++ b/src/pkg/runtime/windows/thread.c @@ -324,13 +324,31 @@ runtime·ctrlhandler1(uint32 type) return 0; } +// Will keep all callbacks in a linked list, so they don't get garbage collected. +typedef struct Callback Callback; +struct Callback { + Callback* link; + void* gobody; + byte asmbody; +}; + +typedef struct Callbacks Callbacks; +struct Callbacks { + Lock; + Callback* link; + int32 n; +}; + +static Callbacks cbs; + // Call back from windows dll into go. byte * runtime·compilecallback(Eface fn, bool cleanstack) { Func *f; int32 argsize, n; - byte *ret, *p; + byte *p; + Callback *c; if(fn.type->kind != KindFunc) runtime·panicstring("not a function"); @@ -348,7 +366,23 @@ runtime·compilecallback(Eface fn, bool cleanstack) if(cleanstack) n += 2; // ... argsize - ret = p = runtime·mal(n); + runtime·lock(&cbs); + for(c = cbs.link; c != nil; c = c->link) { + if(c->gobody == fn.data) { + runtime·unlock(&cbs); + return &c->asmbody; + } + } + if(cbs.n >= 20) + runtime·throw("too many callback functions"); + c = runtime·mal(sizeof *c + n); + c->gobody = fn.data; + c->link = cbs.link; + cbs.link = c; + cbs.n++; + runtime·unlock(&cbs); + + p = &c->asmbody; // MOVL fn, AX *p++ = 0xb8; @@ -376,7 +410,7 @@ runtime·compilecallback(Eface fn, bool cleanstack) } else *p = 0xc3; - return ret; + return &c->asmbody; } void |
