Skip to content

Commit aa4f518

Browse files
authored
Merge pull request #673 from cpunion/libuv-async
Add libuv async
2 parents f76fa87 + a44bb35 commit aa4f518

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

c/libuv/_demo/async/async.go

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package main
2+
3+
import (
4+
"github.com/goplus/llgo/c"
5+
"github.com/goplus/llgo/c/libuv"
6+
)
7+
8+
func ensure(b bool, msg string) {
9+
if !b {
10+
panic(msg)
11+
}
12+
}
13+
14+
func main() {
15+
loop := libuv.LoopNew()
16+
defer loop.Close()
17+
18+
a := &libuv.Async{}
19+
r := loop.Async(a, func(a *libuv.Async) {
20+
println("async callback")
21+
a.Close(nil) // or loop.Stop()
22+
})
23+
ensure(r == 0, "Async failed")
24+
25+
go func() {
26+
println("begin async task")
27+
c.Usleep(100 * 1000)
28+
println("send async event")
29+
ensure(a.Send() == 0, "Send failed")
30+
}()
31+
32+
loop.Run(libuv.RUN_DEFAULT)
33+
println("done")
34+
}
35+
36+
/*Expected Output:
37+
begin async task
38+
send async event
39+
async callback
40+
done
41+
*/

c/libuv/async.go

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package libuv
18+
19+
import (
20+
_ "unsafe"
21+
22+
"github.com/goplus/llgo/c"
23+
)
24+
25+
// struct uv_async_t
26+
type Async struct {
27+
Handle
28+
// On macOS arm64, sizeof uv_async_t is 128 bytes.
29+
// Handle is 92 bytes, so we need 36 bytes to fill the gap.
30+
// Maybe reserve more for future use.
31+
Unused [36]byte
32+
}
33+
34+
// typedef void (*uv_async_cb)(uv_async_t* handle);
35+
// llgo:type C
36+
type AsyncCb func(*Async)
37+
38+
// int uv_async_init(uv_loop_t*, uv_async_t* async, uv_async_cb async_cb);
39+
//
40+
// llgo:link (*Loop).Async C.uv_async_init
41+
func (loop *Loop) Async(a *Async, cb AsyncCb) c.Int {
42+
return 0
43+
}
44+
45+
// int uv_async_send(uv_async_t* async);
46+
//
47+
// llgo:link (*Async).Send C.uv_async_send
48+
func (a *Async) Send() c.Int {
49+
return 0
50+
}

c/libuv/libuv.go

+5
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,11 @@ func (loop *Loop) Alive() c.Int {
172172
return 0
173173
}
174174

175+
// void uv_stop(uv_loop_t *loop)
176+
//
177+
// llgo:link (*Loop).Stop C.uv_stop
178+
func (loop *Loop) Stop() {}
179+
175180
// llgo:link (*Loop).Close C.uv_loop_close
176181
func (loop *Loop) Close() c.Int {
177182
return 0

0 commit comments

Comments
 (0)