-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #673 from cpunion/libuv-async
Add libuv async
- Loading branch information
Showing
3 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters