tinygo icon indicating copy to clipboard operation
tinygo copied to clipboard

internal/task: use a lock-free queue

Open niaow opened this issue 4 years ago • 9 comments
trafficstars

This changes task.Stack and task.Queue to use atomic compare-and-swap operations. With this change, Push can also be executed safely from multiple threads/cores. Additionally, the queue is actually LIFO now (not sure what I was thinking before).

Not entirely sure about this yet.

niaow avatar Nov 19 '21 02:11 niaow

Oops, i meant FIFO not LIFO. Also xtensa seems to still need work?

niaow avatar Nov 19 '21 03:11 niaow

Size impact statistics:

  • Arduino Uno Blinky (tasks): adds 172 bytes
  • Arduino Nano 33 Blinky: adds 84 bytes
  • Metro M4 Airlift Blinky: adds 40 bytes
  • ESP32-Mini32 Blinky: adds 40 bytes
  • Linux Channel Test: adds 76 bytes

niaow avatar Nov 19 '21 15:11 niaow

Out of curiosity: do you have plans to use the changes in this PR?

I am not intending to do anything specific right now, but this might be useful once we want to implement multicore support for something.

Any new features this will add?

Right now:

  1. The queue. . . is actually a queue instead of a weird mostly-stack
  2. On devices with support for atomics, this does not need any critical sections

The Push operation can also be safely called concurrently. . . but only if the tasks are pinned safely (which is currently an issue, but this is more of a future thing).

niaow avatar Nov 20 '21 04:11 niaow

This has been rebased now that #2307 is merged

niaow avatar Dec 28 '21 22:12 niaow

I still need to test this fully on AVR though

niaow avatar Dec 28 '21 23:12 niaow

Now that #2458 and #2459 are in, I consider this ready.

niaow avatar Jan 15 '22 15:01 niaow

Seems like this is perhaps not working correctly on the HiFive1b? I tried several times, and also ran some other test code successfully.

deadprogram avatar Apr 07 '22 16:04 deadprogram

@aykevl If this is correct, would you be willing to merge it? I'm going to try implementing threads again.

jclab-joseph avatar Jun 12 '24 23:06 jclab-joseph

I think we can close this now? I don't see what features it adds or what bugs it fixes, while it does increase binary size.

aykevl avatar Jan 20 '25 13:01 aykevl