ThingWorx Concurrency Extension
Hi all,
Starting from this post by
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.
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

