Concurrency

Get Help on scripting in POL with configurations, config setups, script trouble shooting, etc.

Moderator: POL Developer

Post Reply
xeon
Forum Regular
Posts: 338
Joined: Fri Oct 31, 2008 3:18 am

Concurrency

Post by xeon »

Just an informative thread,
how do you handle concurrency amongs scripts?
For example, suppose you have a script that modifies a prop somewhere. This prop could be modified by other scripts. How do you handle concurrency, I mean, there aren't atomic operations in EScript.
Set_critical() is really the only way?
RusseL
Forum Regular
Posts: 375
Joined: Fri Feb 20, 2009 8:30 pm

Re: Concurrency

Post by RusseL »

Set_priority()
8)
User avatar
Austin
Former Developer
Posts: 621
Joined: Wed Jan 25, 2006 2:30 am

Re: Concurrency

Post by Austin »

xeon wrote:Just an informative thread,
how do you handle concurrency amongs scripts?
For example, suppose you have a script that modifies a prop somewhere. This prop could be modified by other scripts. How do you handle concurrency, I mean, there aren't atomic operations in EScript.
Set_critical() is really the only way?
I think you want to look into the SendEvent() method and Wait_For_Event() module function.

You set a script pid somewhere - global, on an item, etc as a cprop.
SetObjProperty("#ControllerPidName", GetPid());

In other scripts you would do var process := GetProcess(GetObjProperty("#ControllerPidName"));

You can then do process.SendEvent(any escript var) and that data will be sent to the controller when it is at the var event := Wait_For_Event() line.


The timedScripts package is a good example of this:
https://svn.code.sf.net/p/mytharria/cod ... edScripts/
xeon
Forum Regular
Posts: 338
Joined: Fri Oct 31, 2008 3:18 am

Re: Concurrency

Post by xeon »

I imagined that Austin, and in fact I have many packages working like that.
Sometime anyway I feel the need for something easier. Setting up a script to handle centralized writing for, say, a CProp on a item, is really awkward.
No atomic operations in the future for Escript? :D
Turley
POL Developer
Posts: 670
Joined: Sun Feb 05, 2006 4:45 am

Re: Concurrency

Post by Turley »

every operation in escript is atomic. i think you mean something else :)
xeon
Forum Regular
Posts: 338
Joined: Fri Oct 31, 2008 3:18 am

Re: Concurrency

Post by xeon »

Yes, I mean this:

library:

Code: Select all

function dosomething(object)
var := GetObjProperty(object, prop);
var := var + 1;
SetObjProperty(object, prop, var);
endfunction
script which can be called multiple times in parallel, for example, a usescript on a item:

Code: Select all

...
dosomething(object)
...
This situation will lead to inconsistent results. It needs a "central script" which handle modifying this prop.
But this is just cumbersome, a dedicated script just to handle a prop on a item :)

What I would like is this https://en.wikipedia.org/wiki/Linearizability
Turley
POL Developer
Posts: 670
Joined: Sun Feb 05, 2006 4:45 am

Re: Concurrency

Post by Turley »

Ah k ;-)
Atomic read write operations are only supported for primitive operations like ++ and not for functions especially for cprops.
You can like in any other language wrap it with a critical section, but especially since escript has only a global mutex is a major slowdown.
The best choice ( which is also in real programming languages often the best choice) create a concurrent queue.
The escript variant is send_event() on a script object.
Of course for only one use case its overhead but why not write it in a more general way and let also other stuff gain from it.
Register the pid eg as a global property of a script which keeps listening for events. As the event struct you send a magic key which defines what should happen plus the object reference.
In this script you can then define the cprop +1 for any objects or source out non-critical stuff inside critical (eg most of the exported) functions which can noticeable speedup you shard.
As a small hint you should also log the size of the queue to see if it has to many producers. Then it could be worse it to split the script eg one for characters one for items depending what tasks the queue is used for.
Post Reply