[WIP]: Tue 15 Sep 2020 04:54:27 PM EDT

This commit is contained in:
TJ DeVries
2020-09-15 16:54:27 -04:00
parent b8f17075d4
commit ee94a0db36
9 changed files with 209 additions and 31 deletions
+49
View File
@@ -0,0 +1,49 @@
-- Actually works & runs
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
-- libuv takes milliseconds as an integer,
-- while cqueues gives timeouts as a floating point number
-- use `math.ceil` as we'd rather wake up late than early
timer:set_repeat(math.ceil(timeout * 1000))
timer:again()
else
-- stop timer for now; it may be restarted later.
timer:stop()
end
end
local function onready()
-- Step the cqueues loop once (sleeping for max 0 seconds)
assert(cq:step(0))
reset_timer()
end
-- Need to call `start` on libuv timer now
-- to provide callback and so that `again` works
timer:start(0, 0, onready)
-- Ask libuv to watch the cqueue pollfd
uv.new_poll(cq:pollfd()):start(cq:events(), onready)
end
-- Adds a new function to the scheduler `cq`
-- The functions is an infinite loop that sleeps for 1 second and prints
cq:wrap(function()
while true do
cqueues.sleep(1)
print("HELLO FROM CQUEUES")
end
end)
-- Start a luv timer that fires every 1 second
uv.new_timer():start(1000, 1000, function()
print("HELLO FROM LUV")
end)
-- Run luv mainloop
uv.run()
+36
View File
@@ -0,0 +1,36 @@
local uv = vim.loop
-- local pipe_to_share = uv.new_pipe(false)
-- https://github.com/luvit/luv/blob/master/docs.md#uvwrite2stream-data-send_handle-callback
-- Requirements:
-- I only want to import the sorter ONCE (don't reload a million times)
-- I want to run a callback when we're done.
-- I want to be able to re-use sorters in the background
-- I don't want the thread to busy wait
-- I don't wnat a lot of overhead of sending tons of data back and forth between procs.
local pipe = uv.new_pipe(false)
local socket_name = '/tmp/sock.test_3'
pipe:bind(socket_name)
pipe:read_start(function(...)
print('we readin from this pipe')
print(...)
end)
local other_pipe = uv.pipe_open(pipe)
print(uv.pipe_getsockname(pipe))
pipe:listen(128, function()
local client = uv.new_pipe(false)
pipe:accept(client)
client:write("hello!\n")
client:close()
end)
other_pipe:write("other pipe!\n")
pipe:close()
+10
View File
@@ -0,0 +1,10 @@
local uv = vim.loop
local pipe = uv.new_pipe(false)
pipe:bind('/tmp/sock.test_2')
pipe:read_start(function(...)
print('we readin from this pipe')
print(...)
end)
pipe:write("hello??")
+39
View File
@@ -0,0 +1,39 @@
local uv = require('luv')
local step = 10
local hare_id = uv.new_thread(function(step,...)
local ffi = require'ffi'
local uv = require('luv')
local sleep
if ffi.os=='Windows' then
ffi.cdef "void Sleep(int ms);"
sleep = ffi.C.Sleep
else
ffi.cdef "unsigned int usleep(unsigned int seconds);"
sleep = ffi.C.usleep
end
while (step>0) do
step = step - 1
sleep(math.random(1000))
-- print("Hare ran another step")
end
-- print("Hare done running!")
end, step,true,'abcd','false')
local tortoise_id = uv.new_thread(function(step,...)
local uv = require('luv')
while (step>0) do
step = step - 1
uv.sleep(math.random(100))
-- this is just normal lua print, not neovim lua print
-- print("Tortoise ran another step")
end
-- print("Tortoise done running!")
end,step,'abcd','false')
print(hare_id == hare_id, uv.thread_equal(hare_id, hare_id))
print(tortoise_id == hare_id, uv.thread_equal(tortoise_id, hare_id))
print(uv.thread_join(hare_id))
print(uv.thread_join(tortoise_id))
+41 -12
View File
@@ -1,15 +1,44 @@
local uv = require('luv')
local uv = vim.loop
-- print(vim.inspect(uv))
ThreadsAvailable = {}
local work = vim.loop.new_work(function(path, prompt, line)
package.path = path
local had_to_load = false
local uv = require('luv')
if not fuzzy_sorter then
had_to_load = true
fuzzy_sorter = require('telescope.sorters').get_fuzzy_file()
end
-- return fuzzy_sorter:score(prompt, line), had_to_load, uv.hrtime(), tostring(uv.thread_self())
return tostring(uv.thread_self())
end, function(thread_string)
-- print(vim.inspect(vim.split(x, ';')))
ThreadsAvailable[thread_string] = true
end)
work:queue(package.path, "hello", "hello world")
work:queue(package.path, "hello", "hello world")
work:queue(package.path, "hello", "hello world")
work:queue(package.path, "hello", "hello world")
work:queue(package.path, "hello", "hello world")
work:queue(package.path, "hello", "hello world")
work:queue(package.path, "hello", "hello world")
work:queue(package.path, "hello", "hello world")
work:queue(package.path, "hello", "hello world")
work:queue(package.path, "hello", "hello world")
work:queue(package.path, "hello", "hello world")
work:queue(package.path, "hello", "hello world")
work:queue(package.path, "hello", "hello world")
work:queue(package.path, "hello", "hello world")
work:queue(package.path, "hello", "hello world")
work:queue(package.path, "hello", "hello world")
work:queue(package.path, "hello", "hello world")
work:queue(package.path, "hello", "hello world")
work:queue(package.path, "hello", "hello world")
work:queue(package.path, "hello", "hello world")
work:queue(package.path, "hello", "hello world")
local my_table = {}
local my_value = 1
local table_adder = uv.new_thread(function(tbl)
table.insert(tbl, "HELLO")
end, my_table)
uv.thread_join(table_adder)
-- print(vim.inspect(MY_TABLE))