Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

PICO-8

A fantasy console for making, playing and sharing cute 32k cartridges. · By lexaloffle

Cartdata race condition

A topic by Autopawn created Mar 30, 2024 Views: 71
Viewing posts 1 to 1
(2 edits)

Hello, I would like to report a race condition regarding the cart data and cartridge restarts.

Consider the following code

value = 0
frame = 0
function _init()
 cartdata("race_condition")
 value = dget(0)
 dset(0,0) -- set value to 0 for next reload with ctrl+r
end
function _update()
 if btnp(5) then
  dset(0, (value+1)%10) -- increase value by 1
  run() -- reset cartridge
 end
 frame += 1
end
function _draw()
 cls()
 print("current value: "..value, value+1)
 print("frame: "..frame)
end

If you look at it, the expected behavior would be that:

  • If you press X, the stored value should increase by 1 (modulus 10) and then the cartridge should restart and load that value.
  • If you reset with Ctrl+R, no value is set so it should be reset to 0 because of the dset(0, 0) at the end of the init.

Notice however, that if you run the cartridge and press X, there are 3 possible outcomes:

  • Value gets reset to 0.
  • Value stays the same.
  • Value increases by 1 as intended, but this only seems to happen if the cartridge ran for more than 60 frames.

So my belief is that there is a race condition between the dset() calls of the different times the cartridge is reset, and they fight to write in the cartdata,