Skip to content

Commit

Permalink
Merge pull request #673 from cpunion/libuv-async
Browse files Browse the repository at this point in the history
Add libuv async
  • Loading branch information
xushiwei authored Aug 7, 2024
2 parents f76fa87 + a44bb35 commit aa4f518
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
41 changes: 41 additions & 0 deletions c/libuv/_demo/async/async.go
Original file line number Diff line number Diff line change
@@ -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
*/
50 changes: 50 additions & 0 deletions c/libuv/async.go
Original file line number Diff line number Diff line change
@@ -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
}
5 changes: 5 additions & 0 deletions c/libuv/libuv.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit aa4f518

Please sign in to comment.