Wednesday, February 21, 2018

Augh!

The bummer with doing under-the-hood things is that I can't really produce a cool screenshot of anything. However today I called an early defeat. Every attempt to keep the 1Ch thread from flipping tables was unsuccessful. I tried to use a few globals to pause the thread or control it but each would fail in unpredictable ways. It's mostly because each side is running blindly and autonomously from each other.

After watching access errors and deadlocks for about three hours I discovered I was just coding on accident to get a result and it wasn't getting anywhere. I sat and watched twitch streams for the rest of the day. I did it knowing full well that this was even less productive, but I wasn't glaring at broken code anymore. Frustration can be devilish sometimes.

I hope those reading don't mind, but I'm going to rubber duck this a bit.

The issue:
When BitBlit() is called from the thread, and something is being updated on a screen buffer, they will collide.

Why:
When using Allegro drawing commands, you must set a target on which the operation is preformed. While BitBlit is being ran in the thread, it is stealing the target away from the main thread, causing miswrites. At the same time, the target can be stolen by the main game while the thread has control.

How to fix:
The thread needs to signal the main code that it wants control of the target. The main code needs to signal to the thread that it currently has control and that it must wait.
If the main code wants to take the target, it will check if the thread requested it. It will then pause and signal the thread to go ahead, wait for the thread to flag a finish signal, and then continue.

Problem:
The thread can request, but if there is nothing being drawn by the main code, the request will go unanswered. I have no way to tell the thread it's OK to take the target unless the main code is about to take the target itself, or has finished with the target and can pause relinquish control

Try again:
Main code: "I'm going to need the target"
Main code: *check signals, is there a lock?*
                    yes  ---> pause and hawk the lock signal. When lock is clear set lock and go
                    when done clear lock

Thread:  "I need the target"
Thread: *check signals, is there a lock?*
               yes ---->pause and hawk the lock signal. When lock is clear, set lock and go
               when done clear lock.


I think that works? I don't think was was resetting the lock when the other half cleared it and before I took the target. It's 11:13 right now so I'll be looking at this with fresh eyes tomorrow.


No comments:

Post a Comment