Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Add examples for integrating luv with cqueues
- Loading branch information
1 parent
d0664a6
commit 6aadfa8
Showing
2 changed files
with
69 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,31 @@ | ||
| --[[ | ||
| Demonstrates using luv with a cqueues mainloop | ||
| ]] | ||
|
|
||
| local cqueues = require "cqueues" | ||
| local uv = require "luv" | ||
|
|
||
| local cq = cqueues.new() | ||
|
|
||
| cq:wrap(function() | ||
| while cqueues.poll({ | ||
| pollfd = uv.backend_fd(); | ||
| timeout = uv.backend_timeout() / 1000; | ||
| events = "r"; | ||
| }) do | ||
| uv.run("nowait") | ||
| end | ||
| end) | ||
|
|
||
| cq:wrap(function() | ||
| while true do | ||
| cqueues.sleep(1) | ||
| print("HELLO FROM CQUEUES") | ||
| end | ||
| end) | ||
|
|
||
| uv.timer_start(uv.new_timer(), 1000, 1000, function() | ||
| print("HELLO FROM LUV") | ||
| end) | ||
|
|
||
| assert(cq:loop()) |
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,38 @@ | ||
| --[[ | ||
| Demonstrates using cqueues with a luv mainloop | ||
| ]] | ||
|
|
||
| local cqueues = require "cqueues" | ||
| local uv = require "luv" | ||
|
|
||
| local cq = cqueues.new() | ||
|
|
||
| do | ||
| local timer = uv.new_timer() | ||
| local function reset_timer() | ||
| local timeout = cq:timeout() | ||
| if timeout then | ||
| uv.timer_set_repeat(timer, timeout * 1000) | ||
| uv.timer_again(timer) | ||
| end | ||
| end | ||
| local function onready() | ||
| assert(cq:step(0)) | ||
| reset_timer() | ||
| end | ||
| uv.timer_start(timer, 0, 0, onready) | ||
| uv.poll_start(uv.new_poll(cq:pollfd()), cq:events(), onready) | ||
| end | ||
|
|
||
| cq:wrap(function() | ||
| while true do | ||
| cqueues.sleep(1) | ||
| print("HELLO FROM CQUEUES") | ||
| end | ||
| end) | ||
|
|
||
| uv.timer_start(uv.new_timer(), 1000, 1000, function() | ||
| print("HELLO FROM LUV") | ||
| end) | ||
|
|
||
| uv.run() |