cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - You can change your system assigned username to something more personal in your community settings. X

ThingWorx Concurrency Extension

CarlesColl
18-Opal

ThingWorx Concurrency Extension

Hi all,

 

Starting from this post by @atondorf and my original believe (which once upon a time was true), I've been working on an extension to handle Mutex and Counter situations for now. Hope it helps anyone which faces this kind of problems which aren't easy to handle with ThingWorx out-of-the-box.

 

Here it's the public repository with the source code and the Extension ready to be used and fully documented:

https://github.com/carlescm/ThingWorxConcurrencyExtension

 

Here a copy of the Usage part on the README.md from GitHub:

Mutex

We implemented a mutex ThingShape with Java ReentrantLook with fair execution enabled which allows to synchronize and block thread execution. Blocking it's done at Thing's level, it means that each Thing which implements the wupMutexTS ThingShape has it's own ReentrantLook.

Mutex Usage Samples

Just add the wupMutexTS to the Thing or ThingTemplate to whom you want to add mutex blocking features.

On the Mutex Usage samples you will see the usage of copying the me.name in memory? The reason, it's that in between your locking a ThingRestart/ThingDesignSave may happen, and if garbage collector takes place "me" it's not valid anymore as the Thing had restarted and it's another instance in memory and you will get a "zombie" mutex blocking any execution on it!

In order to lock a piece of code in Javascript, and ensure that only one thread its entering on it at a time:

var meName = me.name;
me
.Lock_wupMutexTS(); try { // -- whatever code that needs to be mutex } finally { Things[meName].Unlock_wupMutexTS(); }

You can also tryLock a piece of code, in order to allow one thread and only one and discard the others. For instance it may be interesting if you have a timer which triggers once in a while and you don't want that two consecutive triggers are executed at the same time:

var meName = me.name;
if
(me.TryLock_wupMutexTS()===true) { try { // -- whatever code that needs to be mutex } finally { Things[meName].Unlock_wupMutexTS(); } } else { // -- The lock was already got and previous code its skipped }

You can create more than one mutex per thing, all wupMutexTS services has an optional "id" parameter, which allows to create a more quirurgic mutex. Each different passed "id" will create its own ReentrantLook. Sample with previous code but with a specific lock for one specific timer.

var meName = me.name;
if
(me.TryLock_wupMutexTS({ id: "timer1" })===true) { try { // -- whatever code that needs to be mutex } finally { Things[meName].Unlock_wupMutexTS({ id: "timer1" }); } } else { // -- The lock was already got and previous code it's skipped }

Counter

A thread safe "autoincrement" ThingShape. It provides a "counter" property and the corresponding services in order to increase (one by one) it's value.

Counter Usage Samples

To increase the counter value, no worries about having any amount of threads incrementing the property value, all will get it's own and unique value:

var newCounterValue = me.Increase_wupCounterTS();

You can reset or reset counter value with Set_wupCounterTS method:

me.Set_wupCounterTS({ value: 0 });

The counter it's stored and update on a persistent property named: counter_wupCounterTS

Concurrency Script Functions

List of script helper functions related with this concurrency extension. This services should go on a subsystem like entity, but subsystems on ThingWorx can't be created through extensions

GetTotalActiveLocks_wupMutexTS

Returns the total active locks, in the whole ThingWorx running system.

GetTotalActiveWaiting_wupMutexTS

Returns the total active threads which are waiting on a lock, in the whole ThingWorx running system.

GetTotalThingsLocksUsage_wupMutexTS

Returns the total number of mutex created on Things (ReentranLocks), in the whole ThingWorx running system since last start.

 

Best Regards,

Carles Coll

1 REPLY 1
slangley
23-Emerald II
(To:CarlesColl)

Hi @CarlesColl.

 

Thank you for your contributions to the community!  We are very appreciative of your level of participation.

 

Regards.

 

--Sharon

Announcements


Top Tags