Lua thread safety..

Lua thread safety..

Discuss Lua based Spring scripts (LuaUI widgets, mission scripts, gaia scripts, mod-rules scripts, scripted keybindings, etc...)

Moderator: Moderators

Post Reply
User avatar
PicassoCT
Journeywar Developer & Mapper
Posts: 10454
Joined: 24 Jan 2006, 21:12

Lua thread safety..

Post by PicassoCT »

does it exist? I mean, if two threads try to acces at the same variable, what happens? I mean.. you have a lot of threads in a game.. a silent colission in the background, that can happen if various threads use the same var.. so .. do you have to built your own monitors? Lock them up?
Would something like this

function threadSafety(globalBoolNr,value2ChangeInto,boolLockedNR)
.....while(true).do
..........ifboolLocks[boolLockedNR]==true.then
..........Sleep(5)
..........else
..........boolLocks[boolLockedNR]=true
..........globalBooleans[globalBoolNr]=value2ChangeInto

..........boolLocks[boolLockedNR]=false
..........return
..........end

.....end

end



be sufficient?
User avatar
jK
Spring Developer
Posts: 2299
Joined: 28 Jun 2007, 07:30

Re: Lua thread safety..

Post by jK »

there are no threads only coroutines

-> no reason to worry about threading
User avatar
PicassoCT
Journeywar Developer & Mapper
Posts: 10454
Joined: 24 Jan 2006, 21:12

Re: Lua thread safety..

Post by PicassoCT »

So its basically a timeshared simulation of threads, cutting off at random? Same thing windows uses to schedule processes with a one Core?


Its just, it would be so nice if this would solve.. i mean. the thread just vannishes. A freaking OS-Loop.


while(true) do
Spring.Echo(Alive)

end

Ends.. No Error Message. Nothing. Just Silence. And the other threads going mad because the sensory data provided by the loop is amiss.
Last edited by PicassoCT on 12 Mar 2012, 02:00, edited 1 time in total.
User avatar
jK
Spring Developer
Posts: 2299
Joined: 28 Jun 2007, 07:30

Re: Lua thread safety..

Post by jK »

not really random, it cuts off at `yield()`which is automatically called by sleep()

so never ever make a loop w/o a sleep in its body.
User avatar
PicassoCT
Journeywar Developer & Mapper
Posts: 10454
Joined: 24 Jan 2006, 21:12

Re: Lua thread safety..

Post by PicassoCT »

will check all the whiles for missing Sleep(S)

still this is .. strange, so even when there is the feared "harddrive-wait for instructions after cache and memory-miss" it will not pause the thread and idle until the data arrives to make it to the next sleep? Sounds inefficient.. but well, im not an expert.
User avatar
PicassoCT
Journeywar Developer & Mapper
Posts: 10454
Joined: 24 Jan 2006, 21:12

Re: Lua thread safety..

Post by PicassoCT »

while(weareatit) do
Spring.Echo("Wasntit")
end

All the whiles get there rest..


Could a Signal fired off end a thread that doesent have a SetSignalMask?
I mean, what happens if i Signal(256) and there is no corresponding signal Mask?
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: Lua thread safety..

Post by knorke »

PicassoCT wrote:Could a Signal fired off end a thread that doesent have a SetSignalMask?
:arrow:
"SetSignalMask assigns a mask to the currently running thread (any new threads started by this one will inherit the signal mask)"
http://springrts.com/wiki/Animation-LuaCallouts#Threads
User avatar
PicassoCT
Journeywar Developer & Mapper
Posts: 10454
Joined: 24 Jan 2006, 21:12

Re: Lua thread safety..

Post by PicassoCT »

thx.. overread that. Happens way to often. So can a new Signal Mask break that heritage?
Tobi
Spring Developer
Posts: 4598
Joined: 01 Jun 2005, 11:36

Re: Lua thread safety..

Post by Tobi »

Yes.

SetSignalMask(name) = give the current thread a name
Signal(name) = kill all threads with the given name

So when you call SetSignalMask with a different name than the name inherited from the "parent" thread, the heritage is broken.

If name is a number there is a special case that all threads with a name for which the bitwise-and with the signal is non-zero are killed. So after SetSignalMask(3), both Signal(1) and Signal(2) will kill this thread. Similarly, Signal(3) will kill all threads with a numeric signal mask with either one or both of the least significant bits set.

You can exploit the fact that every new empty table is a unique object to create unique symbolic thread names:

Code: Select all

local MY_UNIQUE_NAME = {}
...
Signal(MY_UNIQUE_NAME)
SetSignalMask(MY_UNIQUE_NAME)
User avatar
PicassoCT
Journeywar Developer & Mapper
Posts: 10454
Joined: 24 Jan 2006, 21:12

Re: Lua thread safety..

Post by PicassoCT »

Thanks Tobi.
Post Reply

Return to “Lua Scripts”