Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
Xfel

Parallel

Question

Hi,

 

In computercraft, there is a library parallel which can run two or more functions interleaved using the coroutine system. It also distributes incoming events to all coroutines. Now I tried to build something similar with OpenComputers.

 

However, if one of my coroutines calls computer.pullSignal(), it does not return to the resume call I made; the call get's handled internally. The coroutine.resume call will only return if I explicitly use coroutine.yield. So, I can't to proper parallelity. I know I can have multiple separate event handlers with the event.listen system, but I can't use delay functions in those.

 

Any Ideas how I can solve this?

Link to post
Share on other sites

4 answers to this question

Recommended Posts

  • 0

Assuming you have control over the functions you call (which seems realistic, otherwise you might want to look into event listeners instead) you can probably just coroutine.yield instead of event.pull and do the actual event.pull in you parallel.run implementation?

Link to post
Share on other sites
  • 0

That's true, but the event.pull function is used in a lot of apis, eg. the term api or the internet api. And the term api is then used by central stuff like the IO library. During a term.read() call, any other incoming signals will be lost.

Link to post
Share on other sites
  • 0
  • Solution

You could register an event listener for the time your program runs, then, e.g. something along those lines:

local function listener(...)
  -- handle your events here
end
local result = table.pack(pcall(function()
  event.listen("signal_of_interest", listener)
  -- the rest of your program, including stuff like term.read()
end))
event.ignore("signal_of_interest", listener)
if result[1] then return table.unpack(2, result)
else error(result[2], 0)
This should probably be wrapped up into a custom library function or so, but it should illustrate how you might approach the problem in general.

Alternatively, someone could write a custom OS that behaves more like CC's OS, purely polling signals through coroutine.yields :)

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.