diff --git a/c/libuv/_demo/async/async.go b/c/libuv/_demo/async/async.go new file mode 100644 index 000000000..eb97ba0de --- /dev/null +++ b/c/libuv/_demo/async/async.go @@ -0,0 +1,41 @@ +package main + +import ( + "github.com/goplus/llgo/c" + "github.com/goplus/llgo/c/libuv" +) + +func ensure(b bool, msg string) { + if !b { + panic(msg) + } +} + +func main() { + loop := libuv.LoopNew() + defer loop.Close() + + a := &libuv.Async{} + r := loop.Async(a, func(a *libuv.Async) { + println("async callback") + a.Close(nil) // or loop.Stop() + }) + ensure(r == 0, "Async failed") + + go func() { + println("begin async task") + c.Usleep(100 * 1000) + println("send async event") + ensure(a.Send() == 0, "Send failed") + }() + + loop.Run(libuv.RUN_DEFAULT) + println("done") +} + +/*Expected Output: +begin async task +send async event +async callback +done +*/ diff --git a/c/libuv/async.go b/c/libuv/async.go new file mode 100644 index 000000000..f7f23ff7a --- /dev/null +++ b/c/libuv/async.go @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package libuv + +import ( + _ "unsafe" + + "github.com/goplus/llgo/c" +) + +// struct uv_async_t +type Async struct { + Handle + // On macOS arm64, sizeof uv_async_t is 128 bytes. + // Handle is 92 bytes, so we need 36 bytes to fill the gap. + // Maybe reserve more for future use. + Unused [36]byte +} + +// typedef void (*uv_async_cb)(uv_async_t* handle); +// llgo:type C +type AsyncCb func(*Async) + +// int uv_async_init(uv_loop_t*, uv_async_t* async, uv_async_cb async_cb); +// +// llgo:link (*Loop).Async C.uv_async_init +func (loop *Loop) Async(a *Async, cb AsyncCb) c.Int { + return 0 +} + +// int uv_async_send(uv_async_t* async); +// +// llgo:link (*Async).Send C.uv_async_send +func (a *Async) Send() c.Int { + return 0 +} diff --git a/c/libuv/libuv.go b/c/libuv/libuv.go index 2bf343a85..39c0f94b9 100644 --- a/c/libuv/libuv.go +++ b/c/libuv/libuv.go @@ -172,6 +172,11 @@ func (loop *Loop) Alive() c.Int { return 0 } +// void uv_stop(uv_loop_t *loop) +// +// llgo:link (*Loop).Stop C.uv_stop +func (loop *Loop) Stop() {} + // llgo:link (*Loop).Close C.uv_loop_close func (loop *Loop) Close() c.Int { return 0