Jump to content




use parallel api to run multiple programs on one screen


18 replies to this topic

#1 Jummit

  • Members
  • 306 posts
  • LocationJulfander Squad Studio

Posted 08 April 2018 - 06:54 PM

I tried to run two programs at the same time, on a split screen, but I cant work out how to use term.redirect well. It always redirects it to one window for both programs. Here is the code:
local width, height = term.getSize()
local halfwidth = math.floor(width/2)
local window1 = window.create(term.current(), 1, 1, halfwidth, height)
local window2 = window.create(term.current(), halfwidth, 1, halfwidth, height)
parallel.waitForAll(
  function()
    --# I kow that this is only called once
    term.redirect(window1)
    shell.run("worm")
  end,
  function()
    --# I kow that this is only called once
    term.redirect(window2)
    shell.run("paint image")
  end
)
How can I manage to redirect the terminal in time so the right program can use it?

#2 SquidDev

    Frickin' laser beams | Resident Necromancer

  • Members
  • 1,427 posts
  • LocationDoes anyone put something serious here?

Posted 08 April 2018 - 07:01 PM

If you write out the control flow of this program, it makes a little bit more sense what happens:
  • Redirect to window1, run worm, worm yields
  • Redirect to window2, run paint, paint yields
  • Wait for an event
  • Resume worm with this event, worm yields
  • Resume paint with this event, paint yields,
  • etc...
Can you see what went wrong? We never redirected back into window1 when resuming after the first event, meaning both programs are stuck on window2. Sadly this cannot be fixed with parallel - you'll need to write your own coroutine manager which handles redirecting the terminal.

I'd recommend having a look at how multishell handles this, as well as the implementation of the parallel API. CraftOS's code isn't always the cleanest, but it's often a good starting point on how to implement features like this.

#3 Jummit

  • Members
  • 306 posts
  • LocationJulfander Squad Studio

Posted 08 April 2018 - 07:07 PM

View PostSquidDev, on 08 April 2018 - 07:01 PM, said:

If you write out the control flow of this program, it makes a little bit more sense what happens:
  • Redirect to window1, run worm, worm yields
  • Redirect to window2, run paint, paint yields
  • Wait for an event
  • Resume worm with this event, worm yields
  • Resume paint with this event, paint yields,
  • etc...
Can you see what went wrong? We never redirected back into window1 when resuming after the first event, meaning both programs are stuck on window2.
Yeah, I thought that would happen.

View PostSquidDev, on 08 April 2018 - 07:01 PM, said:

Sadly this cannot be fixed with parallel - you'll need to write your own coroutine manager which handles redirecting the terminal.

I'd recommend having a look at how multishell handles this, as well as the implementation of the parallel API. CraftOS's code isn't always the cleanest, but it's often a good starting point on how to implement features like this.
I will do that, but I remembered from last time I looked at it that it looks complicated.

Thanks btw for the quick reply! It was very helpful.

#4 Jummit

  • Members
  • 306 posts
  • LocationJulfander Squad Studio

Posted 08 April 2018 - 07:23 PM

How would you/is the multishell api approaching this?

#5 SquidDev

    Frickin' laser beams | Resident Necromancer

  • Members
  • 1,427 posts
  • LocationDoes anyone put something serious here?

Posted 08 April 2018 - 07:26 PM

View PostJummit, on 08 April 2018 - 07:23 PM, said:

How would you/is the multishell api approaching this?
You're not meant to use it. You're meant to read it, understand it, and then implement something similar. If there's something specific you get stuck on, we can help, but otherwise we're just going to end up writing the whole program for you, which benefits nobody.

#6 Jummit

  • Members
  • 306 posts
  • LocationJulfander Squad Studio

Posted 08 April 2018 - 07:29 PM

View PostSquidDev, on 08 April 2018 - 07:26 PM, said:

View PostJummit, on 08 April 2018 - 07:23 PM, said:

How would you/is the multishell api approaching this?
You're not meant to use it. You're meant to read it, understand it, and then implement something similar. If there's something specific you get stuck on, we can help, but otherwise we're just going to end up writing the whole program for you, which benefits nobody.
I wanted to know the concept behind the multishell api, how to let programs run in their terminals, not how to use it

#7 Purple

  • Members
  • 115 posts
  • LocationAlone in the dark, looking at the pretty lights with dreams of things I can not have.

Posted 08 April 2018 - 08:07 PM

Speaking of the API how does it handle function reentrancy? As in, can I trust my functions to be reentrent? Also does CC implement mutex locks and other synchronization tools?

#8 SquidDev

    Frickin' laser beams | Resident Necromancer

  • Members
  • 1,427 posts
  • LocationDoes anyone put something serious here?

Posted 08 April 2018 - 08:14 PM

View PostPurple, on 08 April 2018 - 08:07 PM, said:

Speaking of the API how does it handle function reentrancy? As in, can I trust my functions to be reentrent? Also does CC implement mutex locks and other synchronization tools?
Lua (and so CC) doesn't have preemptive multi-threading, but instead implements cooperative multithreading in the form of coroutines. This means only one thread* is ever running at once, and you have to explicitly change threads (through coroutine.yield). Consequently, you don't really need any fancy synchronisation schemes - you just need to be careful when and where you yield.

*It's worth noting that PUC Lua doesn't even use threads at all - a yield is little more than a jump into the parent coroutine's function. LuaJ does use threads, but that's an implementation detail one does not need to worry about.

For instance, consider the following Lua code:
local count = 0
parallel.waitForAll(function()
  while true do
	os.pullEvent("test") -- Yields here
	--# If Lua used preemptive multi-threading, this has the potential to be a race condition. However, + will not yield
	--# so it's fine.
	count = count + 1
  end
end, function()
  while true do
	sleep(math.random(1, 5)) --# Yields here
	count = count + 1

	--# Allows the above function to continue (due to test event). Note it won't actually start running until we're processing the
    --# "test" event, which may be several yields later.
	os.queueEvent("test")
  end
end)

Edited by SquidDev, 08 April 2018 - 08:21 PM.


#9 Jummit

  • Members
  • 306 posts
  • LocationJulfander Squad Studio

Posted 09 April 2018 - 07:15 AM

I know this isn't working in the slightest, but here is the code I have so far:
local width, height = term.getSize()
local halfwidth = math.floor(width/2)
local window1 = window.create(term.current(), 1, 1, halfwidth, height)
local window2 = window.create(term.current(), halfwidth, 1, halfwidth, height)
local worm = coroutine.create(function()
  shell.run("worm")
end)
local paint = coroutine.create(function()
  shell.run("paint test")
end)
while true do
  term.redirect(window1)
  coroutine.resume(worm)
  term.redirect(window2)
  coroutine.resume(paint)
end

I get the typical too long without yielding error. What am I doing wrong here?

#10 Jummit

  • Members
  • 306 posts
  • LocationJulfander Squad Studio

Posted 09 April 2018 - 08:29 AM

So, why is this not working? Isn't sleep calling corouting.yield?
local co1 = coroutine.create(function()
  for i = 1, 10 do
    print("toast"..i)
    sleep(0.5)
  end
end)

local co2 = coroutine.create(function()
  for i = 1, 10 do
    print("test"..i)
    sleep(0.5)
  end
end)

local resume = function(co)
  local status = coroutine.status(co)
  if status == "normal" or status == "suspended" then
    coroutine.resume(co)
    print("resuming coroutine with status "..status)
  else
    print("not resuming coroutine with status "..status)
  end
end

while true do
  resume(co1)
  resume(co2)
end

Edited by Jummit, 09 April 2018 - 08:35 AM.


#11 Lupus590

  • Members
  • 2,029 posts
  • LocationUK

Posted 09 April 2018 - 06:22 PM

Your coroutine manager doesn't yield, it needs to.

Edited by Lupus590, 09 April 2018 - 06:22 PM.


#12 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 09 April 2018 - 06:35 PM

[shameless promotion]
You should review this
[/shameless promotion]

#13 Jummit

  • Members
  • 306 posts
  • LocationJulfander Squad Studio

Posted 09 April 2018 - 06:45 PM

View PostLupus590, on 09 April 2018 - 06:22 PM, said:

Your coroutine manager doesn't yield, it needs to.
I thought os.pullevent, sleep and some other CC functions where yielding? Also, with the new code that yields still does not work:
local co1 = coroutine.create(function()
  for i = 1, 10 do
	print("toast"..i)
	sleep(0.5)
	coroutine.yield()
  end
end)
local co2 = coroutine.create(function()
  for i = 1, 10 do
	print("test"..i)
	sleep(0.5)
	coroutine.yield()
  end
end)
local resume = function(co)
  local status = coroutine.status(co)
  if status == "normal" or status == "suspended" then
	coroutine.resume(co)
  end
end
while true do
  resume(co1)
  resume(co2)
end

Edited by Jummit, 09 April 2018 - 06:46 PM.


#14 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 09 April 2018 - 07:11 PM

They do yield, to the code that called coroutine.resume. Which in this case is your infinate loop that never yields. See the diagram I posted above.

#15 Jummit

  • Members
  • 306 posts
  • LocationJulfander Squad Studio

Posted 09 April 2018 - 07:23 PM

View PostKingofGamesYami, on 09 April 2018 - 06:35 PM, said:

[shameless promotion]
You should review this
[/shameless promotion]
Is this how cc uses coroutines to manage events or is this how coroutines work in cc?

#16 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 09 April 2018 - 07:54 PM

Both.

#17 Jummit

  • Members
  • 306 posts
  • LocationJulfander Squad Studio

Posted 10 April 2018 - 07:27 AM

I read this, this, this and looked at the coroutine functions in the devdocs lua wiki. Now I want to die.

I managed to get it to work:
local width, height = term.getSize()
local halfwidth = math.floor(width/2)
local window1 = window.create(term.current(), 1, 1, halfwidth, height)
local window2 = window.create(term.current(), halfwidth, 1, halfwidth, height)
local co1 = coroutine.create(function()
  os.run(_G, "rom/programs/fun/worm.lua")
end)
local co2 = coroutine.create(function()
  os.run(_G, "rom/programs/edit.lua", "test")
end)
while true do
  term.redirect(window1)
  coroutine.resume(co1, os.pullEventRaw())
  term.redirect(window2)
  coroutine.resume(co2, os.pullEventRaw())
end
But edit, paint and redirection always give the error 'loop in gettable'. What does this mean? The error comes from the shell api, but I don't know what happens in there.
EDIT: this explains everything
Edited code:
local width, height = term.getSize()
local halfwidth = math.floor(width/2)
local window1 = window.create(term.current(), 1, 1, halfwidth, height)
local window2 = window.create(term.current(), halfwidth, 1, halfwidth, height)

local co1 = coroutine.create(function()
  shell.run("rom/programs/fun/worm.lua")
end)

local co2 = coroutine.create(function()
  shell.run("rom/programs/edit.lua", "test")
end)

while true do
  local eventData = {os.pullEventRaw()}
  term.redirect(window1)
  coroutine.resume(co1, table.unpack(eventData))
  term.redirect(window2)
  coroutine.resume(co2, table.unpack(eventData))
end


Edited by Jummit, 10 April 2018 - 09:15 AM.


#18 Purple

  • Members
  • 115 posts
  • LocationAlone in the dark, looking at the pretty lights with dreams of things I can not have.

Posted 10 April 2018 - 10:20 AM

View PostSquidDev, on 08 April 2018 - 08:14 PM, said:

Lua (and so CC) doesn't have preemptive multi-threading, but instead implements cooperative multithreading in the form of coroutines. This means only one thread* is ever running at once, and you have to explicitly change threads (through coroutine.yield). Consequently, you don't really need any fancy synchronisation schemes - you just need to be careful when and where you yield.
Which is all well and good at times, but not always. Like say the simplest of all situations. You have a large loop that processes a large number of things so you want to break in the middle to give others a chance to do some work. But you don't want some other program touching your data.

I am of course operating from the perspective of writing each program individually and not wanting to care about what else they will be running along side with as I do so.

#19 Lupus590

  • Members
  • 2,029 posts
  • LocationUK

Posted 10 April 2018 - 06:46 PM

View PostPurple, on 10 April 2018 - 10:20 AM, said:

Like say the simplest of all situations. You have a large loop that processes a large number of things so you want to break in the middle to give others a chance to do some work. But you don't want some other program touching your data.

Use local variables?





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users