View topic - Lua thread safety..



All times are UTC + 1 hour


Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: Lua thread safety..
PostPosted: 12 Mar 2012, 01:03 
Journeywar Developer & Mapper
User avatar

Joined: 24 Jan 2006, 21:12
Location: There is no god - and reality is his prophetess
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?


Top
 Offline Profile  
 
 Post subject: Re: Lua thread safety..
PostPosted: 12 Mar 2012, 01:21 
Spring Developer
User avatar

Joined: 28 Jun 2007, 06:30
there are no threads only coroutines

-> no reason to worry about threading


Top
 Offline Profile  
 
 Post subject: Re: Lua thread safety..
PostPosted: 12 Mar 2012, 01:53 
Journeywar Developer & Mapper
User avatar

Joined: 24 Jan 2006, 21:12
Location: There is no god - and reality is his prophetess
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.

Top
 Offline Profile  
 
 Post subject: Re: Lua thread safety..
PostPosted: 12 Mar 2012, 01:59 
Spring Developer
User avatar

Joined: 28 Jun 2007, 06:30
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.


Top
 Offline Profile  
 
 Post subject: Re: Lua thread safety..
PostPosted: 12 Mar 2012, 02:11 
Journeywar Developer & Mapper
User avatar

Joined: 24 Jan 2006, 21:12
Location: There is no god - and reality is his prophetess
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.


Top
 Offline Profile  
 
 Post subject: Re: Lua thread safety..
PostPosted: 12 Mar 2012, 02:28 
Journeywar Developer & Mapper
User avatar

Joined: 24 Jan 2006, 21:12
Location: There is no god - and reality is his prophetess
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?


Top
 Offline Profile  
 
 Post subject: Re: Lua thread safety..
PostPosted: 15 Mar 2012, 15:53 
Moderator
User avatar

Joined: 22 Feb 2006, 01:02
Location: cheap kitchen
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


Top
 Offline Profile  
 
 Post subject: Re: Lua thread safety..
PostPosted: 15 Mar 2012, 18:45 
Journeywar Developer & Mapper
User avatar

Joined: 24 Jan 2006, 21:12
Location: There is no god - and reality is his prophetess
thx.. overread that. Happens way to often. So can a new Signal Mask break that heritage?


Top
 Offline Profile  
 
 Post subject: Re: Lua thread safety..
PostPosted: 18 Mar 2012, 19:20 
Spring Developer

Joined: 01 Jun 2005, 10:36
Location: The Netherlands
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:
local MY_UNIQUE_NAME = {}
...
Signal(MY_UNIQUE_NAME)
SetSignalMask(MY_UNIQUE_NAME)


Top
 Offline Profile  
 
 Post subject: Re: Lua thread safety..
PostPosted: 18 Mar 2012, 19:45 
Journeywar Developer & Mapper
User avatar

Joined: 24 Jan 2006, 21:12
Location: There is no god - and reality is his prophetess
Thanks Tobi.


Top
 Offline Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group

Site layout created by Roflcopter et al.